public async Task <FriendDto> Handle(CreateFriendCommand request, CancellationToken cancellationToken) { var user = await _userManager.FindByEmailAsync("*****@*****.**"); var adress = new Address(request.Number, request.Street, request.Neighborhood, request.City); var friend = new Friend(request.Name, request.Email, request.Phone, adress, user); var photoUploadResult = _photoAccesor.AddPhoto(request.File); friend.AddPhoto(photoUploadResult); _entityValidator.Validate(new Entity[] { friend, adress }); if (_notification.HasNotifications) { _photoAccesor.DeletePhoto(photoUploadResult?.PublicId); return(null); } _context.Friends.Add(friend); await _context.Commit(); return(_mapper.Map <FriendDto>(friend)); }
public async Task <LoanDto> Handle(CreateLoanCommand request, CancellationToken cancellationToken) { var friend = await _context.Friends.FindAsync(request.FriendId); if (friend == null) { _notification.AddNotification("Friend", "Friend not found"); return(null); } var game = await _context.Games.FindAsync(request.GameId); if (game == null) { _notification.AddNotification("Game", "Game not found"); return(null); } bool isGameBorrowed = _context.Loans.Any(x => x.GameId == request.GameId && x.EndDate == null); if (isGameBorrowed) { _notification.AddNotification("Game", "This game is borrowed. You must get back before to borrow it."); return(null); } var loan = new Loan(friend, game); game.AddLoan(loan); friend.AddLoan(loan); _entityValidator.Validate(new Entity[] { loan }); if (_notification.HasNotifications) { return(null); } await _context.Commit(); return(_mapper.Map <LoanDto>(loan)); }
public async Task <GameDto> Handle(CreateGameCommand request, CancellationToken cancellationToken) { var user = await _userManager.FindByEmailAsync("*****@*****.**"); var game = new Game(request.Name, request.Gender, user); var photoUploadResult = _photoAccesor.AddPhoto(request.File); game.AddPhoto(photoUploadResult); _entityValidator.Validate(new Entity[] { game }); if (_notification.HasNotifications) { _photoAccesor.DeletePhoto(photoUploadResult?.PublicId); return(null); } _context.Games.Add(game); await _context.Commit(); return(_mapper.Map <GameDto>(game)); }