public IActionResult Put(int id, [FromBody] RypVM model) { using (var context = new RypDbContext()) { using (var tran = context.Database.BeginTransaction()) { var ryp = context.Ryps.FirstOrDefault(x => x.Id == id); ryp.Semesters = context.Semesters.Where(x => x.RypId == ryp.Id).ToList(); ryp.UpdateDate = DateTime.Now.ToString(); for (int i = ryp.Semesters.Count; i > 0; i--) { ryp.Semesters[i - 1].SemesterSubjects = context.SemesterSubjects.Where(x => x.SemesterId == ryp.Semesters[i - 1].Id).ToList(); ryp.Semesters[i - 1].SemesterElectiveGroups = context.SemesterElectiveGroups.Where(x => x.SemesterId == ryp.Semesters[i - 1].Id).ToList(); for (int j = ryp.Semesters[i - 1].SemesterSubjects.Count; j > 0; j--) { context.SemesterSubjects.Remove(ryp.Semesters[i - 1].SemesterSubjects[j - 1]); } for (int j = ryp.Semesters[i - 1].SemesterElectiveGroups.Count; j > 0; j--) { context.SemesterElectiveGroups.Remove(ryp.Semesters[i - 1].SemesterElectiveGroups[j - 1]); } context.Semesters.Remove(ryp.Semesters[i - 1]); } context.SaveChanges(); ryp.Semesters = new List <Semester>(); for (int i = 0; i < 8; i++) { var semester = new Semester(); semester.RypId = ryp.Id; context.Semesters.Add(semester); context.SaveChanges(); foreach (var subject in model.Semesters[i].Subjects) { var t = new SemesterSubject { SubjectId = subject.Id, SemesterId = semester.Id }; context.SemesterSubjects.Add(t); semester.SemesterSubjects.Add(t); } foreach (var electiveGroup in model.Semesters[i].Electives) { var t = new SemesterElectiveGroup { ElectiveGroupId = electiveGroup.Id, SemesterId = semester.Id }; context.SemesterElectiveGroups.Add(t); semester.SemesterElectiveGroups.Add(t); } ryp.Semesters.Add(semester); } context.SaveChanges(); tran.Commit(); } } return(Ok()); }
public IActionResult Post([FromBody] RypVM model) { using (var ctx = new RypDbContext()) { using (var tran = ctx.Database.BeginTransaction()) { var ryp = new Ryp(); ryp.Name = model.Name; ryp.Year = model.Year; ryp.UserId = model.UserId; ryp.SpecialtyId = model.Specialty.Id; ryp.Date = DateTime.Now.ToString(); ryp.Prototype = model.Prototype; ctx.Ryps.Add(ryp); ctx.SaveChanges(); for (int i = 0; i < 8; i++) { var semester = new Semester(); semester.RypId = ryp.Id; ctx.Semesters.Add(semester); ctx.SaveChanges(); foreach (var subject in model.Semesters[i].Subjects) { var t = new SemesterSubject { SubjectId = subject.Id, SemesterId = semester.Id }; ctx.SemesterSubjects.Add(t); semester.SemesterSubjects.Add(t); } foreach (var electiveGroup in model.Semesters[i].Electives) { var t = new SemesterElectiveGroup { ElectiveGroupId = electiveGroup.Id, SemesterId = semester.Id }; ctx.SemesterElectiveGroups.Add(t); semester.SemesterElectiveGroups.Add(t); } ryp.Semesters.Add(semester); } ctx.SaveChanges(); tran.Commit(); } } return(Ok()); }