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 async Task <ResponseBase> BatchInsertEntitiesAsync <TEntity>(IEnumerable <TranslatedRequest> requests, Type type, string idPropName) where TEntity : BaseTranslateModel { var response = new ResponseBase(); EntityEntry <TEntity> insertedContributor = null; EntityEntry <Langtext> insertedLangText = null; foreach (var request in requests) { var langId = _context.Lang.FirstOrDefault(l => l.LangCode == request.LangCode.GetDescription())?.LangId; string suffix = string.Empty; if (!SuffixToPropertyMap.TryGetValue(idPropName, out suffix)) { response.ErrorMessage = $"BatchInsertOrUpdateEntitiesAsync:: Provided argument idPropName -> ${idPropName} is invalid"; } //RECORD CREATION RULES var langTextCode = $"{request.Name.ToUpperInvariant().Replace(" ", "_")}_{suffix}"; if (_context.GetDbSet <TEntity>().FirstOrDefault(c => c.LangTextCode == langTextCode) != null) { throw new Exception($"ERROR: entity of type {type} with that id already exists."); } //Add to TEntity Table if not exists if (_context.ChangeTracker.Entries <TEntity>().FirstOrDefault(m => m.Entity.LangTextCode == langTextCode) == null) { var instantiatedObject = Activator.CreateInstance(type, langTextCode) as TEntity; insertedContributor = await _context.GetDbSet <TEntity>().AddAsync(instantiatedObject); } var entityId = insertedContributor.Member(idPropName).CurrentValue; //Add to Langtext based on request var lngTextEntry = new Langtext { TextCode = langTextCode, TextName = request.Name, TextTitle = request.Title, TextDescription = request.Description, LangId = langId.Value, }; lngTextEntry.GetType().GetProperty(idPropName).SetValue(lngTextEntry, (int?)entityId); insertedLangText = await _context.Langtext.AddAsync(lngTextEntry); } response.RowsAffected = await _context.SaveChangesAsync(); return(await Task.FromResult(response)); }