示例#1
0
        public async Task <IActionResult> CreateMovieAsync(
            [FromBody, SwaggerParameter(Description = "Movie to create", Required = true)] MovieToCreateDto movieToCreate,
            [FromHeader(Name = "Accept"), SwaggerParameter(Description = "media type to request betwen json or json+hateoas")] string mediaType)
        {
            var movieToAdd = Mapper.Map <Movie>(movieToCreate);

            movieToAdd.Id = Guid.NewGuid();

            _movieRepository.AddMovie(movieToAdd);

            if (!await _movieRepository.SaveChangesAsync())
            {
                _logger.LogError("Saving changes to database while creating a movie failed");
            }

            // if any genre is added to movie, create many-to-many relationship for each genre
            if (movieToCreate.GenreIds != null && movieToCreate.GenreIds.Count > 0)
            {
                for (int i = 0; i < movieToCreate.GenreIds.Count; i++)
                {
                    // create moviegenre object with genre id and movie id from movieToCreate object
                    MovieGenre movieGenreToAdd = new MovieGenre {
                        MovieId = movieToAdd.Id, GenreId = movieToCreate.GenreIds[i]
                    };
                    movieGenreToAdd.Id = Guid.NewGuid();
                    _movieGenreRepository.AddMovieGenre(movieGenreToAdd);
                }
                // save changes to database after all many-to-many relationships has been created
                if (!await _movieGenreRepository.SaveChangesAsync())
                {
                    _logger.LogError("Saving changes to database while creating a moviegenre failed");
                }
            }

            var foundMovie = await _movieRepository.GetMovieAsync(movieToAdd.Id);

            var movieDto = Mapper.Map <MovieDto>(foundMovie);

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

                var linkedMovie = movieDto.ShapeData(null) as IDictionary <string, object>;

                linkedMovie.Add("links", links);

                return(CreatedAtRoute("GetMovie", new { movieId = movieDto.Id }, linkedMovie));
            }
            else
            {
                return(CreatedAtRoute("GetMovie", new { movieId = movieDto.Id }, movieDto));
            }
        }
示例#2
0
        /// <summary>
        /// Handling save request and response
        /// </summary>
        /// <param name="request">Messaging.MovieGenres.SaveMovieGenreResponse</param>
        /// <returns>Messaging.MovieGenres.SaveMovieGenreRequest</returns>
        SaveMovieGenreResponse IMovieGenreService.SaveMovieGenre(SaveMovieGenreRequest request)
        {
            var response = new SaveMovieGenreResponse()
            {
                Request       = request,
                ResponseToken = Guid.NewGuid()
            };

            try
            {
                _repository.AddMovieGenre(request.MovieId, request.GenreId);

                response.Success = true;
            }
            catch (Exception ex)
            {
                response.Message = ex.Message;
                response.Success = false;
            }
            return(response);
        }