public async Task <Unit> Handle(Command request, CancellationToken cancellationToken) { Domain.Forum.Thread thread = await _context.Threads.Where(x => x.Id == request.ThreadId).SingleOrDefaultAsync(); thread.Views++; if (await _context.SaveChangesAsync() > 0) { return(Unit.Value); } else { throw new Exception("Problem saving changes"); } }
public async Task <Unit> Handle(Command request, CancellationToken cancellationToken) { Domain.Forum.Thread threadToDelete = await _context.Threads.SingleOrDefaultAsync(x => x.Id == request.Id); if (threadToDelete == null) { throw new RestException(HttpStatusCode.BadRequest, "Invalid thread."); } ClaimsPrincipal claimsPrincipal = _httpContextAccessor.HttpContext.User; User user = await _signInManager.ValidateSecurityStampAsync(claimsPrincipal); if (user == null) { throw new RestException(HttpStatusCode.Unauthorized); } bool isAdmin = claimsPrincipal.IsInRole("Administrator"); if (isAdmin == false || threadToDelete.AuthorFK != user.Id) { throw new RestException(HttpStatusCode.Unauthorized); } _context.Remove(threadToDelete); if (await _context.SaveChangesAsync() > 0) { return(Unit.Value); } else { throw new Exception("Problem saving changes"); } }