public async Task <Unit> Handle(CreateTagCommand request, CancellationToken cancellationToken) { var entity = new Tag(request.Name); _context.Tags.Add(entity); await _context.SaveChangesAsync(cancellationToken); return(Unit.Value); }
public async Task <PostDetailDto> Handle(CreatePostCommand request, CancellationToken cancellationToken) { var entity = new Post(request.Title, request.Description, request.Content, request.CategoryId, request.Tags, request.Published); _context.Posts.Add(entity); await _context.SaveChangesAsync(cancellationToken); return(_mapper.Map <PostDetailDto>(entity)); }
public async Task <Unit> Handle(UpdateCategoryCommand request, CancellationToken cancellationToken) { var entity = await _context.Categories .FirstOrDefaultAsync(i => i.Id == request.Id, cancellationToken); if (entity == null) { throw new NotFoundException(nameof(Category), nameof(Category.Id), request.Id); } entity.Update(request.Name, request.UpdateSlug); await _context.SaveChangesAsync(cancellationToken); return(Unit.Value); }
public async Task <Unit> Handle(DeleteTagCommand request, CancellationToken cancellationToken) { var entity = await _context.Tags .FirstOrDefaultAsync(i => i.Id == request.Id, cancellationToken); if (entity == null) { throw new NotFoundException(nameof(Tag), nameof(Tag.Id), request.Id); } _context.Tags.Remove(entity); await _context.SaveChangesAsync(cancellationToken); return(Unit.Value); }
public async Task <Unit> Handle(UpdatePostCommand request, CancellationToken cancellationToken) { var entity = await _context.Posts .Include(x => x.PostTags) .FirstOrDefaultAsync(i => i.Id == request.Id, cancellationToken); if (entity == null) { throw new NotFoundException(nameof(Post), nameof(Post.Id), request.Id); } entity.Update(request.Title, request.Description, request.Content, request.CategoryId, request.Tags, request.Published, request.UpdateSlug); await _context.SaveChangesAsync(cancellationToken); return(Unit.Value); }
public async Task <Unit> Handle(DeleteCategoryCommand request, CancellationToken cancellationToken) { var entity = await _context.Categories .FirstOrDefaultAsync(i => i.Id == request.Id, cancellationToken); if (entity == null) { throw new NotFoundException(nameof(Category), nameof(Category.Id), request.Id); } var hasPosts = _context.Posts.Any(p => p.CategoryId == entity.Id); if (hasPosts) { throw new DeleteFailureException(nameof(Category), nameof(Category.Name), entity.Name, "There are existing posts associated with this category."); } _context.Categories.Remove(entity); await _context.SaveChangesAsync(cancellationToken); return(Unit.Value); }