public (bool success, int?createdovieId) CreateMovie(TranslatableRequest request) { EntityEntry <Movie> createdMovie; try { var movieLangText = $"{request.Title.ToUpperInvariant().Replace(" ", "_")}_MOVIE"; createdMovie = _context.Movie.Add(new Movie { LangTextCode = movieLangText }); var translations = request.Translations.Select(tr => { var langId = _context.Lang.FirstOrDefault(l => l.LangCode == tr.LangCode)?.LangId; if (!langId.HasValue) { throw new ArgumentException($"Invalid LangCode {tr.LangCode}"); } return(new Langtext { TextCode = movieLangText, LangId = langId.Value, TextName = request.Title, TextTitle = tr.Title, TextDescription = tr.Description, MovieId = createdMovie.Entity.MovieId }); } ).ToList(); var defaultEnglishRecord = new Langtext { TextCode = movieLangText, LangId = _context.Lang.First(l => l.LangCode == LanguageEnum.en_US.GetDescription()).LangId, TextName = request.Title, TextTitle = request.Title, TextDescription = request.Description, MovieId = createdMovie.Entity.MovieId }; translations.Add(defaultEnglishRecord); _context.AddRange(translations); _context.SaveChanges(); } catch (Exception) { return(false, null); } return(true, createdMovie.Entity.MovieId); }
public IActionResult Create(TranslatableRequest request) { var response = _service.CreateMovie(request); if (!response.success) { return(BadRequest()); } return(Created(new Uri($"movies/{response.createdovieId}", UriKind.Relative), response.createdovieId)); }