コード例 #1
0
        public async Task <IActionResult> AddLanguage(long movieId, int languageId)
        {
            var language = await db.Languages
                           .Where(x => x.Id == languageId)
                           .SingleOrDefaultAsync();

            if (language == null)
            {
                return(NotFound(new ResWrapper {
                    args = new { movieId, languageId }, Status = "NotFound:Language"
                }));
            }

            var movie = await db.Movies
                        .Where(x => x.Id == movieId)
                        .SingleOrDefaultAsync();

            if (movie == null)
            {
                return(NotFound(new ResWrapper {
                    args = new { movieId, languageId }, Status = "NotFound:Movie"
                }));
            }

            var movieLanguage = await db.MovieLanguages
                                .Where(x => x.MovieId == movieId && x.LanguageId == languageId)
                                .SingleOrDefaultAsync();

            if (movieLanguage != null)
            {
                return(Ok(new ResWrapper {
                    args = new { movieId, languageId }, Status = "OK", Data = new { movie, language }
                }));
            }

            movieLanguage = new MovieLanguage {
                Movie = movie, Language = language, LanguageId = languageId, MovieId = movieId
            };
            await db.AddAsync(movieLanguage);

            await db.SaveChangesAsync();

            return(Ok(new ResWrapper {
                args = new { movieId, languageId }, Status = "OK", Data = new { movie, language }
            }));
        }