public async Task <IActionResult> Edit(int id, [Bind("Id,SocietyName,AgeLimit,Sum,Lessons")] SocietyBindingModel society) { if (id != society.Id) { return(NotFound()); } if (ModelState.IsValid) { try { society.ClientId = Program.Client.Id; _societyLogic.CreateOrUpdate(society); } catch (DbUpdateConcurrencyException) { if (!SocietyExists(id)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } ViewData["LessonId"] = new MultiSelectList(_lessonLogic.Read(null), "Id", "LessonName"); return(View(society)); }
public void Update(SocietyBindingModel model) { using (var context = new SchoolDataBase()) { using (var transaction = context.Database.BeginTransaction()) { try { var society = context.Societies.FirstOrDefault(rec => rec.Id == model.Id); if (society == null) { throw new Exception("Кружок не найден"); } CreateModel(model, society, context); context.SaveChanges(); transaction.Commit(); } catch { transaction.Rollback(); throw; } } } }
public List <SocietyViewModel> GetFilteredList(SocietyBindingModel model) { if (model == null) { return(null); } using (var context = new SchoolDataBase()) { return(context.Societies .Include(rec => rec.SocietyLessons) .ThenInclude(rec => rec.Lesson) .Include(rec => rec.Client) .ThenInclude(rec => rec.User) .Include(rec => rec.SocietyCosts) .ThenInclude(rec => rec.Cost) .Where(rec => model.DateFrom.HasValue && model.DateTo.HasValue && rec.ClientId == model.ClientId && rec.SocietyCosts.Any(rec => rec.Cost.CostDate.Date >= model.DateFrom.Value.Date && rec.Cost.CostDate.Date <= model.DateTo.Value.Date) || !model.DateFrom.HasValue && !model.DateTo.HasValue && rec.ClientId == model.ClientId && model.SelectedSocieties != null && model.SelectedSocieties.Contains(rec.Id) || !model.DateFrom.HasValue && !model.DateTo.HasValue && model.SelectedSocieties == null && rec.ClientId == model.ClientId) .Select(CreateViewModel) .ToList()); } }
public async Task <IActionResult> Create([Bind("SocietyName,AgeLimit,Sum,Lessons")] SocietyBindingModel society) { if (ModelState.IsValid) { society.ClientId = Program.Client.Id; _societyLogic.CreateOrUpdate(society); return(RedirectToAction(nameof(Index))); } ViewData["LessonId"] = new MultiSelectList(_lessonLogic.Read(null), "Id", "LessonName"); return(View(society)); }
public void Delete(SocietyBindingModel model) { var society = _societyStorage.GetElement(new SocietyBindingModel { Id = model.Id }); if (society == null) { throw new Exception("Кружок не найден"); } _societyStorage.Delete(model); }
public List <SocietyViewModel> Read(SocietyBindingModel model) { if (model == null) { return(_societyStorage.GetFullList()); } if (model.Id.HasValue) { return(new List <SocietyViewModel> { _societyStorage.GetElement(model) }); } return(_societyStorage.GetFilteredList(model)); }
public void Delete(SocietyBindingModel model) { using (var context = new SchoolDataBase()) { var society = context.Societies.FirstOrDefault(rec => rec.Id == model.Id); if (society == null) { throw new Exception("Кружок не найден"); } context.Societies.Remove(society); context.SaveChanges(); } }
private Society CreateModel(SocietyBindingModel model, Society society, SchoolDataBase context) { society.SocietyName = model.SocietyName; society.AgeLimit = model.AgeLimit; society.Sum = model.Sum; society.ClientId = model.ClientId; if (society.Id == 0) { society.DateCreate = DateTime.Now; context.Societies.Add(society); context.SaveChanges(); } if (model.Id.HasValue) { var lessons = context.SocietyLessons .Where(rec => rec.SocietyId == model.Id.Value) .ToList(); context.SocietyLessons.RemoveRange(lessons .Where(rec => !model.Lessons.Contains(rec.LessonId)) .ToList()); context.SaveChanges(); foreach (var updateLesson in lessons) { model.Lessons.Remove(updateLesson.LessonId); } } foreach (var lesson in model.Lessons) { context.SocietyLessons.Add(new SocietyLesson { SocietyId = society.Id, LessonId = lesson }); context.SaveChanges(); } return(society); }
public void CreateOrUpdate(SocietyBindingModel model) { var society = _societyStorage.GetElement(new SocietyBindingModel { SocietyName = model.SocietyName }); if (society != null && society.Id != model.Id) { throw new Exception("Уже есть кружок с таким названием"); } if (model.Id.HasValue) { _societyStorage.Update(model); } else { _societyStorage.Insert(model); } }
public void Insert(SocietyBindingModel model) { using (var context = new SchoolDataBase()) { using (var transaction = context.Database.BeginTransaction()) { try { CreateModel(model, new Society(), context); context.SaveChanges(); transaction.Commit(); } catch { transaction.Rollback(); throw; } } } }
public SocietyViewModel GetElement(SocietyBindingModel model) { if (model == null) { return(null); } using (var context = new SchoolDataBase()) { var society = context.Societies .Include(rec => rec.SocietyLessons) .ThenInclude(rec => rec.Lesson) .Include(rec => rec.Client) .ThenInclude(rec => rec.User) .Include(rec => rec.SocietyCosts) .ThenInclude(rec => rec.Cost) .FirstOrDefault(rec => rec.SocietyName == model.SocietyName || rec.Id == model.Id); return(society != null?CreateViewModel(society) : null); } }