public SuPageSectionModel UpdatePageSection(SuPageSectionModel suPageSectionChanges) { var changedPageSection = context.DbPageSection.Attach(suPageSectionChanges); changedPageSection.State = Microsoft.EntityFrameworkCore.EntityState.Modified; context.SaveChanges(); return(suPageSectionChanges); }
public SuPageSectionModel AddPageSection(SuPageSectionModel suPageSection) { context.DbPageSection.Add(suPageSection); context.SaveChanges(); return(suPageSection); }
public async Task <IActionResult> Create(PageSectionAndStatusViewModel NewLevel) { if (ModelState.IsValid) { var CurrentUser = await _userManager.GetUserAsync(User); var TestForNull = ( from l in _pageSection.GetAllPageSections() where l.Id == NewLevel.SuObject.ObjectId select l.Sequence).Count(); int MaxLevelSequence; if (TestForNull == 0) { MaxLevelSequence = 1; } else { MaxLevelSequence = ( from c in _pageSection.GetAllPageSections() where c.PageId == NewLevel.SuObject.ObjectId select c.Sequence).Max(); MaxLevelSequence++; } if (NewLevel.SuObject.Sequence != MaxLevelSequence) { //Here need to update other levels to make sequence correct //https://stackoverflow.com/questions/812630/how-can-i-reorder-rows-in-sql-database List <IdAndSequence> ExistingLevels = (from c in _pageSection.GetAllPageSections() where c.PageId == NewLevel.SuObject.ObjectId && c.Sequence >= NewLevel.SuObject.Sequence select new IdAndSequence { Id = c.Id , Sequence = c.Sequence }).ToList(); int x = 0; while (x < ExistingLevels.Count()) { SuPageSectionModel u = new SuPageSectionModel(); u = _pageSection.GetPageSection(ExistingLevels[x].Id); u.Sequence = ++ExistingLevels[x].Sequence; _pageSection.UpdatePageSection(u); x++; } } var PageSection = new SuPageSectionModel { Sequence = NewLevel.SuObject.Sequence, PageId = NewLevel.SuObject.ObjectId, PageSectionTypeId = NewLevel.SuObject.Type, ShowSectionTitleName = NewLevel.SuObject.ShowSectionTitle, ShowSectionTitleDescription = NewLevel.SuObject.ShowSectionDescription, ShowContentTypeTitle = NewLevel.SuObject.ShowContentTypeTitle, ShowContentTypeDescription = NewLevel.SuObject.ShowContentTypeTitleDescription, OneTwoColumns = NewLevel.SuObject.OneTwoColumns }; if (NewLevel.SuObject.ContentTypeId != 0) { PageSection.ContentTypeId = NewLevel.SuObject.ContentTypeId; } PageSection.SortById = NewLevel.SuObject.SortById; PageSection.MaxContent = NewLevel.SuObject.MaxContent; PageSection.HasPaging = NewLevel.SuObject.HasPaging; var NewPageSection = _pageSection.AddPageSection(PageSection); var PageSectionLanguage = new SuPageSectionLanguageModel { Name = NewLevel.SuObject.Name, Description = NewLevel.SuObject.Description, TitleName = NewLevel.SuObject.Title, TitleDescription = NewLevel.SuObject.TitleDescription, MouseOver = NewLevel.SuObject.MouseOver, PageSectionId = NewPageSection.Id, LanguageId = CurrentUser.DefaultLanguageId }; _pageSectionLanguage.AddPageSectionLanguage(PageSectionLanguage); } return(RedirectToAction("Index", new { Id = NewLevel.SuObject.ObjectId.ToString() })); }