public async Task <ActionResult <GameDetailDto> > AddGame(GameForCreationDto gameForCreationDto) { var game = _mapper.Map <Game>(gameForCreationDto); _context.Games.Add(game); await _context.SaveChangesAsync(); var gameToReturn = _mapper.Map <GameDetailDto>(game); return(CreatedAtAction("GetGame", new { id = gameToReturn.Id }, gameToReturn)); }
public async Task <IActionResult> UpdateGame(int id, GameForCreationDto gameForCreationDto) { var gameFromRepo = await _repo.GetGame(id); _mapper.Map(gameForCreationDto, gameFromRepo); if (await _repo.SaveAll()) { return(NoContent()); } throw new Exception($"Updating game {id} failed on save"); }
public ActionResult <GameDto> CreateGameForTournament(int tournamnetId, GameForCreationDto game) { if (!gameService.Exists(tournamnetId)) { return(NotFound()); } var gameEntity = mapper.Map <Game>(game); gameService.Post(gameEntity); gameService.SaveChanges(); var gameToReturn = mapper.Map <GameDto>(gameEntity); return(gameToReturn); }
public async Task <IActionResult> CreateGameForReview(int reviewId, [FromBody] GameForCreationDto game) { if (game == null) { return(BadRequest()); } if (game.Developer == game.Title && game.Publisher == game.Title) { ModelState.AddModelError(nameof(GameForCreationDto), "The title should not have developer or publisher in it"); } if (!ModelState.IsValid) { return(new UnprocessableEntityObjectResult(ModelState)); } if (!await _reviewRepository.ReviewExists(reviewId)) { return(NotFound()); } var gameEntity = Mapper.Map <Game>(game); await _reviewRepository.AddGame(reviewId, gameEntity); if (!await _reviewRepository.Save()) { throw new Exception($"Creating a game for review {reviewId} failed on save."); } var gameToReturn = Mapper.Map <GameDto>(gameEntity); return(CreatedAtRoute("GetReviewedGames", new { reviewId = reviewId, id = gameToReturn.Id }, CreateLinksForGame(gameToReturn))); }
public async Task <IActionResult> AddGameForUser(int userId, [FromForm] GameForCreationDto gameForCreationDto) { if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } // get the current user User userFromRepo = await _repo.GetUser(userId); var file = gameForCreationDto.File; var uploadResult = new ImageUploadResult(); // config the upload params and get the upload result from cloudinary if (file.Length > 0) { // the logic below is the cloudinary api logic, see // https://cloudinary.com/documentation/dotnet_integration#installation // using block would clean up the variable inside when this block // is over using (var stream = file.OpenReadStream()) { var uploadParams = new ImageUploadParams() { File = new FileDescription(file.Name, stream), Transformation = new Transformation().Width(500).Height(500). Crop("fill").Gravity("face") }; uploadResult = _cloudinary.Upload(uploadParams); } } // create the gameForCreationDto object gameForCreationDto.Url = uploadResult.Uri.ToString(); gameForCreationDto.PublicId = uploadResult.PublicId; var game = _mapper.Map <Game>(gameForCreationDto); // Any method, determine whether any element in games sequence satisfy // the condition inside, for this case is whether there is an Game whose // IsMain property is true // if no such element Any method would return false, so that we know // there is not main game yet if (!userFromRepo.Games.Any(u => u.IsMain)) { game.IsMain = true; } // add that game into current user userFromRepo.Games.Add(game); // save changes if (await _repo.SaveAll()) { // usually the post request should return a 201 code, and that // code will contain the url that you can use to get the newly // created object(in this case is the newly created game), and // also contain the newly created object that will be returned var gameToReturn = _mapper.Map <GameForReturnDto>(game); return(CreatedAtRoute("GetGame", new { id = game.Id }, gameToReturn)); } return(BadRequest("Could not add the game")); }