コード例 #1
0
        public void Execute(PieceOfArtDto request)
        {
            validator.ValidateAndThrow(request);


            var guid      = Guid.NewGuid();
            var extension = Path.GetExtension(request.Picture.FileName);

            var newFileName = guid + extension;

            var path = Path.Combine("wwwroot", "images", newFileName);

            using (var fileStream = new FileStream(path, FileMode.Create))
            {
                request.Picture.CopyTo(fileStream);
            }



            var post = mapper.Map <PieceOfArt>(request);

            post.Picture = newFileName;
            context.PieceOfArts.Add(post);
            context.SaveChanges();
        }
        public void Execute(PieceOfArtDto request)
        {
            validator.ValidateAndThrow(request);

            var findPost = context.PieceOfArts.Find(request.Id);

            if (findPost == null)
            {
                throw new EntityNotFoundException(request.Id, typeof(PieceOfArt));
            }

            if (actor.Id != findPost.UserId)
            {
                throw new UnAuthorizedAccessUserException(actor, Name);
            }

            var post = context.PieceOfArts.Where(x => x.Id == request.Id).First();

            var newFileName = "";

            if (request.Picture != null)
            {
                var guid      = Guid.NewGuid();
                var extension = Path.GetExtension(request.Picture.FileName);

                newFileName = guid + extension;

                var path = Path.Combine("wwwroot", "images", newFileName);

                using (var fileStream = new FileStream(path, FileMode.Create))
                {
                    request.Picture.CopyTo(fileStream);
                }
            }
            else
            {
                newFileName = post.Picture;
            }

            mapper.Map(request, post);
            post.Picture = newFileName;
            context.SaveChanges();
        }