/* O mapeamento entre entidade e DTO pode ser automatizado por bibliotecas como AutoMapper, * porém, já ouvi várias opiniões positivas e negativas sobre o uso do AutoMapper. */ private BorrowedGame LendMapper(BorrowedGamePostDto dto, Guid idUser) { var borrowedGame = new BorrowedGame { IdUser = idUser, IdGame = dto.IdGame, IdFriend = dto.IdFriend }; return(borrowedGame); }
public async Task <IActionResult> Lend(BorrowedGamePostDto dto) { var validationResult = await _app.Lend(dto, User.Identity.Name); if (!validationResult.IsValid) { return(BadRequest(validationResult.Errors.Select(x => x.ErrorMessage).ToList())); } return(NoContent()); }
public async Task <ValidationResult> Lend(BorrowedGamePostDto dto, string username) { var user = _accountApplicationService.GetByUsername(username); var borrowedGame = LendMapper(dto, user.Id); var validationResult = await _borrowedGameValidation.LendValidation.ValidateAsync(borrowedGame); if (!validationResult.IsValid) { return(validationResult); } using var transaction = _uow.Database.BeginTransaction(); _uow.BorrowedGameRepository.Add(borrowedGame); await _uow.CommitAsync(); await _gameApplicationService.UpdateBorrowed(dto.IdGame, true); await transaction.CommitAsync(); return(validationResult); }