public async Task <IActionResult> UpdateSeatByIdAsync( [FromRoute, SwaggerParameter(Description = "Hall id of seat to update", Required = true)] Guid hallId, [FromRoute, SwaggerParameter(Description = "Id of seat to update", Required = true)] Guid seatId, [FromBody, SwaggerParameter(Description = "Seat to update", Required = true)] SeatToUpdateDto seatToUpdate, [FromHeader(Name = "Accept"), SwaggerParameter(Description = "media type to request betwen json or json+hateoas")] string mediaType) { if (!await _seatRepository.HallExists(hallId)) { return(NotFound()); } var seatFromDb = await _seatRepository.GetSeatAsync(seatId); // upserting if movie does not already exist if (seatFromDb == null) { var seatEntity = Mapper.Map <Seat>(seatToUpdate); seatEntity.Id = seatId; _seatRepository.AddSeat(hallId, seatEntity); if (!await _seatRepository.SaveChangesAsync()) { _logger.LogError($"Upserting seat: {seatId} failed on save"); } var seatToReturn = Mapper.Map <SeatDto>(seatEntity); if (mediaType == "application/vnd.biob.json+hateoas") { var links = CreateLinksForSeats(seatToReturn.Id, null); var linkedSeat = seatToReturn.ShapeData(null) as IDictionary <string, object>; linkedSeat.Add("links", links); return(CreatedAtRoute("GetSeat", new { hallId, seatId = seatToReturn.Id }, linkedSeat)); } else { return(CreatedAtRoute("GetSeat", new { hallId, seatId = seatToReturn.Id }, seatToReturn)); } } Mapper.Map(seatToUpdate, seatFromDb); _seatRepository.UpdateSeat(seatFromDb); if (!await _seatRepository.SaveChangesAsync()) { _logger.LogError($"Updating seat: {seatId} failed on save"); } return(NoContent()); }
public async Task <IActionResult> PartiuallyUpdateSeatByIdAsync( [FromRoute, SwaggerParameter(Description = "ID of seat to update", Required = true)] Guid hallId, [FromRoute, SwaggerParameter(Description = "ID of hall to update seat", Required = true)] Guid seatId, [FromBody, SwaggerParameter(Description = "Jsonpatch operation document to update", Required = true)] JsonPatchDocument <SeatToUpdateDto> patchDoc, [FromHeader(Name = "Accept"), SwaggerParameter(Description = "media type to request betwen json or json+hateoas")] string mediaType) { if (!await _seatRepository.HallExists(hallId)) { return(NotFound()); } if (patchDoc == null) { return(BadRequest()); } var seatFromDb = await _seatRepository.GetSeatAsync(seatId); // upserting if movie does not already exist if (seatFromDb == null) { var seatToCreate = new SeatToUpdateDto(); patchDoc.ApplyTo(seatToCreate, ModelState); if (!ModelState.IsValid) { new ProccessingEntityObjectResultErrors(ModelState); } var seatToAddToDb = Mapper.Map <Seat>(seatToCreate); seatToAddToDb.Id = seatId; _seatRepository.AddSeat(hallId, seatToAddToDb); if (!await _seatRepository.SaveChangesAsync()) { _logger.LogError($"Upserting seat: {seatId} failed on save"); } var seatToReturn = Mapper.Map <SeatDto>(seatToAddToDb); if (mediaType == "application/vnd.biob.json+hateoas") { var links = CreateLinksForSeats(seatToReturn.Id, null); var linkedSeat = seatToReturn.ShapeData(null) as IDictionary <string, object>; linkedSeat.Add("links", links); return(CreatedAtRoute("GetSeat", new { hallId, seatId = seatToReturn.Id }, linkedSeat)); } else { return(CreatedAtRoute("GetSeat", new { hallId, seatId = seatToReturn.Id }, seatToReturn)); } } var seatToPatch = Mapper.Map <SeatToUpdateDto>(seatFromDb); patchDoc.ApplyTo(seatToPatch, ModelState); if (!ModelState.IsValid) { new ProccessingEntityObjectResultErrors(ModelState); } Mapper.Map(seatToPatch, seatFromDb); _seatRepository.UpdateSeat(seatFromDb); if (!await _seatRepository.SaveChangesAsync()) { _logger.LogError($"Partially updating seat: {seatId} failed on save"); } return(NoContent()); }