public async Task <List <TodoResult> > Handle(AllTodosQuery request, CancellationToken cancellationToken) { var isUserAllowedReq = new IsUserAllowedRequest { Permission = "READ_TODO", UserId = request.UserId?.ToString() ?? string.Empty }; var isUserAllowedRes = permissionsClient.Execute <IsUserAllowedRequest, IsUserAllowedResponse>(c => c.IsUserAllowed, isUserAllowedReq); if (isUserAllowedRes.IsAllowed == false) { throw new InvalidOperationException("User is not allowed"); } var todos = await context .Todos .ProjectTo <TodoResult>(mapper.ConfigurationProvider) .ToListAsync(cancellationToken); return(todos); }
public async Task <long> Handle(CreateTodoCommand request, CancellationToken cancellationToken) { var isUserAllowedReq = new IsUserAllowedRequest { Permission = "CREATE_TODO", UserId = request.UserId.ToString() }; var isUserAllowedRes = permissionsClient.Execute <IsUserAllowedRequest, IsUserAllowedResponse>(c => c.IsUserAllowed, isUserAllowedReq); if (isUserAllowedRes.IsAllowed == false) { throw new InvalidOperationException("User is not allowed"); } var todo = this.mapper.Map <CreateTodoCommand, Todo>(request); context.Todos.Add(todo); await context.SaveChangesAsync(cancellationToken); var sendEmailReq = new SendEmailRequest { Body = new SendEmailRequest.Types.Body { Text = $"Body: {request.Name}" }, Subject = new SendEmailRequest.Types.Subject { Text = $"Subj: {request.Name} - {request.IsComplete}" }, BodyType = "html" }; sendEmailReq.Recipients.Add(new SendEmailRequest.Types.Recipient { Email = $"{request.UserId}@example.com", Name = request.UserId.ToString() }); notificationClient.Execute(c => c.SendEmail, sendEmailReq); return(todo.Id); }
public async Task <Unit> Handle(DeleteTodoCommand request, CancellationToken cancellationToken) { var isUserAllowedReq = new IsUserAllowedRequest { Permission = "DELETE_TODO", UserId = request.UserId.ToString() }; var isUserAllowedRes = permissionsClient.Execute <IsUserAllowedRequest, IsUserAllowedResponse>(c => c.IsUserAllowed, isUserAllowedReq); if (isUserAllowedRes.IsAllowed == false) { throw new InvalidOperationException("User is not allowed"); } var todo = await context.Todos.SingleOrDefaultAsync(x => x.Id == request.Id, cancellationToken); if (todo == null) { throw new EntityNotFoundException <Todo>(request.Id); } context.Todos.Remove(todo); await context.SaveChangesAsync(cancellationToken); var sendEmailReq = new SendPushRequest { Message = new SendPushRequest.Types.Message { Text = $"Todo {request.Id} has been deleted" } }; sendEmailReq.Recipients.Add(new SendPushRequest.Types.Recipient { Name = request.UserId.ToString() }); notificationClient.Execute(c => c.SendPush, sendEmailReq); return(Unit.Value); }
public async Task <TodoResult> Handle(TodoDetailsQuery request, CancellationToken cancellationToken) { var isUserAllowedReq = new IsUserAllowedRequest { Permission = "READ_TODO", UserId = request.UserId?.ToString() ?? string.Empty }; var isUserAllowedRes = permissionsClient.Execute <IsUserAllowedRequest, IsUserAllowedResponse>(c => c.IsUserAllowed, isUserAllowedReq); if (isUserAllowedRes.IsAllowed == false) { throw new InvalidOperationException("User is not allowed"); } var todo = await context .Todos .ProjectTo <TodoResult>(mapper.ConfigurationProvider) .FirstOrDefaultAsync(s => s.Id == request.Id, cancellationToken); ////if (todo == null) ////{ //// throw new EntityNotFoundException<Todo>(request.Id); ////} return(todo); }