public IActionResult Create(ContentEditViewModel content)
 {
     if (ModelState.IsValid)
     {
         using (_contentEditorRepository)
         {
             _contentEditorRepository.Create(content);
         }
         return RedirectToAction(nameof(Index));
     }
     return View(content);
 }
        public ContentEditViewModel Create(ContentEditViewModel contentEditViewModel)
        {
            GuardNotDisposed();

            var content = _dataDbContext.Contents.Add(new Content
            {
                InternalCaption = contentEditViewModel.InternalCaption
            });

            _dataDbContext.SaveChanges();

            var entity = content.Entity;

            return new ContentEditViewModel(entity);
        }
        public IActionResult Edit(ContentEditViewModel content)
        {
            if (ModelState.IsValid)
            {
                using(_contentEditorRepository)
                {
                    _contentEditorRepository.Update(content);
                }

                return RedirectToAction(nameof(Edit), new { id = content.Id });
            }
            return View(content);
        }
        public ContentEditViewModel Read(int contentId)
        {
            GuardNotDisposed();

            var content = _dataDbContext.Contents.Include(c => c.Translations).FirstOrDefault(x => x.Id == contentId);
            if (content != null)
            {
                var vm = new ContentEditViewModel(content);

                // Build missing translations

                var missingTranslations = from x in Enum.GetValues(typeof(LanguageDefinition)).Cast<LanguageDefinition>()
                                                 where !vm.Translations.Select(t => t.Version).Contains(x)
                                                 select new TranslationEditViewModel(new Translation()) { ContentId = vm.Id, Version = x };

                vm.Translations = vm.Translations.Concat(missingTranslations).ToList();

                return vm;
            }
            else
            {
                return null;
            }
        }
        public ContentEditViewModel Update(ContentEditViewModel contentEditViewModel)
        {
            GuardNotDisposed();

            var content = _dataDbContext.Contents.Include(c => c.Translations)
                                        .Single(x => x.Id == contentEditViewModel.Id);

            var updateTime = DateTime.UtcNow;

            content.InternalCaption = contentEditViewModel.InternalCaption;

            var newTranslations = from x in contentEditViewModel.Translations
                                    where x.Id == default(int)
                                    let translation = (new Translation()).UpdateFromViewModel(x)
                                   select translation;

            var updatedOldData = from x in contentEditViewModel.Translations
                                  where x.Id != default(int)
                                  select x;

            var translationsAndChanges = from translation in content.Translations
                                         join changes in updatedOldData on translation.Id equals changes.Id
                                         select new { translation, changes };

            // Update existing translations
            foreach(var translationAndChange in translationsAndChanges)
            {
                translationAndChange.translation.UpdateFromViewModel(translationAndChange.changes);
                translationAndChange.translation.UpdatedAt = updateTime;
            }

            // Add new translations
            foreach(var newTranslation in newTranslations)
            {
                newTranslation.UpdatedAt = updateTime;
                content.Translations.Add(newTranslation);
            }

            _dataDbContext.Contents.Update(content);
            _dataDbContext.SaveChanges();

            return contentEditViewModel;
        }