public async Task <ClassEditRequestModel> GetEditModel(int classId) { var @class = await this.data.Classes.Include(c => c.Subject).FirstOrDefaultAsync(c => c.Id == classId); if (@class == null) { throw new Exception("This class doesn't exist"); } var subjects = await this.data.Subjects.Include(s => s.Class).Where(s => s.SchoolId == @class.SchoolId).ToListAsync(); var classSubjectEditModel = new ClassSubjectEditModel(); var classSubjects = new List <ClassSubjectEditModel>(); foreach (var subject in subjects) { classSubjectEditModel = new ClassSubjectEditModel { Id = subject.Id, Name = subject.Name, SchoolId = subject.SchoolId }; classSubjectEditModel.IsForClass = subject.Class.Contains(@class); classSubjects.Add(classSubjectEditModel); } var model = new ClassEditRequestModel { Id = @class.Id, Name = @class.Name, Group = @class.Group, AllSubjects = classSubjects }; return(model); }
public async Task Edit(ClassEditRequestModel model) { if (model == null) { throw new Exception("Model cannot be null"); } var @class = await this.data.Classes.Include(s => s.Subject).FirstOrDefaultAsync(c => c.Id == model.Id); if (@class == null) { throw new Exception("This class doesn't exist"); } @class.Name = model.Name; @class.Group = model.Group; var subjects = await this.data.Subjects.Where(s => s.SchoolId == @class.SchoolId && model.SubjectIds.Contains(s.Id)).ToListAsync(); @class.Subject = subjects; await this.data.SaveChangesAsync(); }
public async Task <IActionResult> Edit(ClassEditRequestModel model) { await this.classService.Edit(model); return(await Index()); }