public void Execute(AddPost request)
        {
            _validator.ValidateAndThrow(request);

            var potDto = Context.Posts.Find(request);

            if (potDto != null)
            {
                try
                {
                    potDto.Name        = request.Name;
                    potDto.UserId      = request.UserId;
                    potDto.IsDeleted   = false;
                    potDto.Description = request.Description;
                    potDto.ModifidedAt = DateTime.Now;
                    Context.SaveChanges();
                }
                catch (Exception)
                {
                    throw new Exception();
                }
            }
            else
            {
                throw new EntityNotFoundException(potDto.Id, typeof(AddPost));
            }
        }
Пример #2
0
        public void Execute(PostDto request)
        {
            var id = request.Id;

            var post = _context.Posts.Find(id);

            if (post == null)
            {
                throw new EntityNotFoundException(id, typeof(PostDto));
            }

            _validator.ValidateAndThrow(request);

            _mapper.Map(request, post);

            _context.SaveChanges();
        }
Пример #3
0
        public void Execute(EditPostDto request)
        {
            var post = _context.Posts.Include(p => p.Photo).FirstOrDefault(p => p.Id == request.Id);

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

            if (post.CreatedBy != _actor.Id && _actor.RoleType != RoleType.Administrator && _actor.RoleType != RoleType.Moderator)
            {
                throw new NotAllowedException(UseCase.getUseCase(this.Id), _actor, $"You can only edit posts created by you.");
            }

            _validator.ValidateAndThrow(request);

            _mapper.Map <EditPostDto, Post>(request, post);

            _context.SaveChanges(_actor.Id);
        }