public Guid CreateGame(GamePostDto dto) { var creator = Session.Get <PlayerModel>(dto.CreatorId); if (creator == null) { return(Guid.Empty); } var course = Session.Get <CourseModel>(dto.CourseId); if (course == null) { return(Guid.Empty); } var gatheringCard = new CardModel { IsGatheringCard = true }; var game = dto.ToEntity(creator, course, gatheringCard); using (var transaction = Session.BeginTransaction()) { Session.Save(game); transaction.Commit(); } return(game.Id); }
public object Put(int id, [FromBody] GamePostDto model) { if (ModelState.IsValid) { try { Game game = _context.Game.SingleOrDefault(m => m.Id == id); if (game == null) { return(NotFound()); } model.UpdateGame(game); _context.Update(game); _context.SaveChanges(); return(StatusCode(200, game)); } catch (Exception exception) { return(BadRequest(new { exception = exception.InnerException.Message })); } } else { return(BadRequest(ModelState)); } }
/* 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 Game CreateMapper(GamePostDto dto, Guid idUser) { var game = new Game { IdUser = idUser, Name = dto.Name }; return(game); }
public IHttpActionResult CreateGame(GamePostDto dto) { var response = _gameRepository.CreateGame(dto); if (response == Guid.Empty) { return(BadRequest()); } return(Ok(response)); }
public async Task <IActionResult> Create(GamePostDto dto) { var validationResult = await _app.Create(dto, User.Identity.Name); if (!validationResult.IsValid) { return(BadRequest(validationResult.Errors.Select(x => x.ErrorMessage).ToList())); } return(NoContent()); }
public static GameModel ToEntity(this GamePostDto dto, PlayerModel creator, CourseModel course, CardModel card) { var game = new GameModel { Secretary = creator, Course = course, State = GameState.Pending, Cards = new List <CardModel> { card } }; return(game); }
public async Task <ValidationResult> Create(GamePostDto dto, string username) { var user = _accountApplicationService.GetByUsername(username); var game = CreateMapper(dto, user.Id); var validationResult = await _gameValidation.CreateValidation.ValidateAsync(game); if (!validationResult.IsValid) { return(validationResult); } _uow.GameRepository.Add(game); await _uow.CommitAsync(); return(validationResult); }
public object Post([FromBody] GamePostDto model) { if (ModelState.IsValid) { try { Game game = model.GetGame(); _context.Add(game); _context.SaveChanges(); return(StatusCode(201, game)); } catch (Exception exception) { return(BadRequest(new { exception = exception.InnerException.Message })); } } else { return(BadRequest(ModelState)); } }