Beispiel #1
0
        public async Task <IActionResult> UpdateShowtimeAsync(
            [FromRoute, SwaggerParameter(Description = "Movie id of showtime to update", Required = true)] Guid movieId,
            [FromRoute, SwaggerParameter(Description = "Id of showtime to update", Required = true)] Guid showtimeId,
            [FromBody, SwaggerParameter(Description = "Showtime to update", Required = true)] ShowtimeToUpdateDto showtimeToUpdate,
            [FromHeader(Name = "Accept"), SwaggerParameter(Description = "media type to request betwen json or json+hateoas")] string mediaType)
        {
            if (!await _showtimeRepository.MovieExists(movieId))
            {
                return(NotFound());
            }

            if (showtimeToUpdate == null)
            {
                return(BadRequest());
            }

            var showtimeFromDb = await _showtimeRepository.GetShowtimeAsync(showtimeId, movieId);

            if (showtimeFromDb == null)
            {
                var showtimeEntity = Mapper.Map <Showtime>(showtimeToUpdate);
                showtimeEntity.Id = showtimeId;
                _showtimeRepository.AddShowtime(movieId, showtimeEntity);

                if (!await _showtimeRepository.SaveChangesAsync())
                {
                    _logger.LogError($"Upserting showtime: {showtimeId} failed on save");
                }

                var showtimeToReturn = Mapper.Map <ShowtimeDto>(showtimeEntity);

                if (mediaType == "application/vnd.biob.json+hateoas")
                {
                    var links = CreateLinksForShowtimes(showtimeToReturn.Id, null);

                    var linkedShowtime = showtimeToReturn.ShapeData(null) as IDictionary <string, object>;

                    linkedShowtime.Add("links", links);

                    return(CreatedAtRoute("GetShowtime", new { movieId, showtimeId = showtimeToReturn.Id }, linkedShowtime));
                }
                else
                {
                    return(CreatedAtRoute("GetShowtime", new { movieId, showtimeId = showtimeToReturn.Id }, showtimeToReturn));
                }
            }

            Mapper.Map(showtimeToUpdate, showtimeFromDb);

            _showtimeRepository.UpdateShowtime(showtimeFromDb);

            if (!await _showtimeRepository.SaveChangesAsync())
            {
                _logger.LogError($"Updating showtime: {showtimeId} failed on save");
            }

            return(NoContent());
        }
        public IActionResult Put(int id, [FromBody] ShowtimeRequest showtimeRequest)
        {
            if (showtimeRepository.GetShowtimeById(id) == null)
            {
                return(NotFound());
            }

            if (showtimeRequest == null)
            {
                return(StatusCode(400, ModelState));
            }

            var statusCode = ValidateShowtime(showtimeRequest);

            if (!ModelState.IsValid)
            {
                return(StatusCode(statusCode.StatusCode));
            }

            var showtime = showtimeRepository.UpdateShowtime(id, showtimeRequest);

            if (showtime == null)
            {
                var error = new Error()
                {
                    message = "Showtime went oopsie when updating"
                };
                return(StatusCode(400, error));
            }
            return(Ok(new ShowtimeDTO(showtime)));
        }