public async Task <BoardEnvelope> Handle(Command request, CancellationToken cancellationToken) { if (await _context.Boards.AnyAsync(x => x.Name == request.Board.Name, cancellationToken)) { throw new RestException(HttpStatusCode.BadRequest, new { Name = Constants.IN_USE }); } var owner = await _context.Users.SingleOrDefaultAsync(t => t.Email.Equals(_currentUserAccessor.GetCurrentUserEmail()), cancellationToken); if (owner == null) { throw new RestException(HttpStatusCode.NotFound, new { User = Constants.NOT_FOUND }); } var board = new Board() { Name = request.Board.Name, OwnerId = owner.Id }; await _context.Boards.AddAsync(board, cancellationToken); await _context.UserBoards.AddAsync(new UserBoard() { BoardId = board.Id, UserId = owner.Id }, cancellationToken); await _context.SaveChangesAsync(cancellationToken); return(new BoardEnvelope(board)); }
public async Task <ColumnEnvelope> Handle(Command request, CancellationToken cancellationToken) { var owner = await _context.Users.SingleOrDefaultAsync( t => t.Email.Equals(_currentUserAccessor.GetCurrentUserEmail()), cancellationToken); if (owner == null) { throw new RestException(HttpStatusCode.BadRequest, new { User = Constants.NOT_FOUND }); } var board = await _context.Boards .Include(t => t.Columns) .SingleOrDefaultAsync(t => t.Id == request.Column.BoardId, cancellationToken); if (board == null) { throw new RestException(HttpStatusCode.BadRequest, new { Board = Constants.NOT_FOUND }); } var column = new Column() { BoardId = request.Column.BoardId, Color = request.Column.Color, Header = request.Column.Header, OwnerId = owner.Id, OrderNum = board.Columns.GetNextOrderNum(), }; await _context.Columns.AddAsync(column, cancellationToken); await _context.SaveChangesAsync(cancellationToken); return(new ColumnEnvelope(column)); }
public async Task <UserEnvelope> Handle(Command message, CancellationToken cancellationToken) { var currentUserEmail = _currentUserAccessor.GetCurrentUserEmail(); var user = await _context.Users.SingleOrDefaultAsync(x => x.Email == currentUserEmail, cancellationToken); if (user == null) { throw new RestException(HttpStatusCode.NotFound, new { User = Constants.NOT_FOUND }); } user.Name = message.User.Name ?? user.Name; user.Email = message.User.Email ?? user.Email; user.Biography = message.User.Biography ?? user.Biography; if (!string.IsNullOrWhiteSpace(message.User.Password)) { var salt = Guid.NewGuid().ToByteArray(); user.Hash = _passwordHasher.Hash(message.User.Password, salt); user.Salt = salt; } await _context.SaveChangesAsync(cancellationToken); return(new UserEnvelope(_mapper.Map <User, PublicUser>(user))); }
public async Task <Unit> Handle(Command request, CancellationToken cancellationToken) { var board = await _context.Boards .Include(t => t.Owner) .SingleOrDefaultAsync(t => t.Id == request.BoardId, cancellationToken); if (board == null) { throw new RestException(HttpStatusCode.NotFound, new { Board = Constants.NOT_FOUND }); } if (!board.Owner.Email.Equals(_currentUserAccessor.GetCurrentUserEmail())) { throw new RestException(HttpStatusCode.BadRequest, new { User = Constants.NO_OWNER }); } _context.Boards.Remove(board); await _context.SaveChangesAsync(cancellationToken); return(Unit.Value); }
public async Task <BoardsEnvelope> Handle(Query request, CancellationToken cancellationToken) { var currentUserEmail = _currentUserAccessor.GetCurrentUserEmail(); var queryable = _context.Boards .Include(t => t.Owner) .AsNoTracking(); queryable = queryable.Where(t => t.Owner.Email.Equals(currentUserEmail)); if (!string.IsNullOrEmpty(request.Name)) { queryable = queryable.Where(t => t.Name.Contains(request.Name)); } var listOfBoards = await queryable.ToListAsync(cancellationToken); return(new BoardsEnvelope() { BoardsCount = listOfBoards.Count, Boards = listOfBoards.Select(t => _mapper.Map <Board, SmallBoardModel>(t)).ToList() }); }
public async Task <TaskEnvelope> Handle(Command request, CancellationToken cancellationToken) { var owner = await _context.Users.SingleOrDefaultAsync(t => t.Email.Equals(_userAccessor.GetCurrentUserEmail()), cancellationToken); if (owner == null) { throw new RestException(HttpStatusCode.BadRequest, new { User = Constants.NOT_FOUND }); } var column = await _context.Columns .Include(t => t.Tasks) .SingleOrDefaultAsync(t => t.Id == request.Task.ColumnId, cancellationToken); if (column == null) { throw new RestException(HttpStatusCode.BadRequest, new { Column = Constants.NOT_FOUND }); } var task = new Task() { ColumnId = column.Id, Header = request.Task.Header, Description = request.Task.Description, OrderNum = column.Tasks.GetNextOrderNum(), OwnerId = owner.Id, }; await _context.Tasks.AddAsync(task, cancellationToken); await _context.UserTasks.AddAsync(new UserTask() { TaskId = task.Id, UserId = owner.Id }, cancellationToken); await _context.SaveChangesAsync(cancellationToken); return(new TaskEnvelope(task)); }
public async Task <UserEnvelope> GetCurrent() { var currentUserEmail = _currentUserAccessor.GetCurrentUserEmail(); return(await _mediator.Send(new Details.Query(currentUserEmail))); }