public async Task <IActionResult> Post([FromBody] ElectiveGroupVM model) { using (var ctx = new RypDbContext()) { using (var tran = ctx.Database.BeginTransaction()) { var electiveGroup = new ElectiveGroup(); electiveGroup.Name = model.Name; electiveGroup.Shifr = model.Shifr; electiveGroup.Credits = model.Credits; electiveGroup.Pr = model.Pr; electiveGroup.UserId = model.UserId; electiveGroup.Date = DateTime.Now.ToString(); electiveGroup.TypeId = model.Type.Id; ctx.ElectiveGroups.Add(electiveGroup); ctx.SaveChanges(); foreach (var item in model.Subjects) { var t = new SubjectElectiveGroup { ElectiveGroupId = electiveGroup.Id, SubjectId = item.Id }; electiveGroup.SubjectElectiveGroups.Add(t); } await ctx.SaveChangesAsync(); tran.Commit(); } } return(Ok()); }
public IActionResult Put(int id, [FromBody] ElectiveGroupVM model) { using (var context = new RypDbContext()) { var electiveGroup = context.ElectiveGroups.FirstOrDefault(x => x.Id == id); var subjects = context.SubjectElectiveGroups.Where(x => x.ElectiveGroupId == electiveGroup.Id).ToList(); electiveGroup.SubjectElectiveGroups = subjects; electiveGroup.Name = model.Name; electiveGroup.Shifr = model.Shifr; electiveGroup.Credits = model.Credits; electiveGroup.Pr = model.Pr; electiveGroup.UpdateDate = DateTime.Now.ToString(); electiveGroup.TypeId = model.Type.Id; for (int i = electiveGroup.SubjectElectiveGroups.Count; i > 0; i--) { context.SubjectElectiveGroups.Remove(electiveGroup.SubjectElectiveGroups[i - 1]); } context.SaveChanges(); electiveGroup.SubjectElectiveGroups = new List <SubjectElectiveGroup>(); for (int i = 0; i < model.Subjects.Count; i++) { electiveGroup.SubjectElectiveGroups.Add(new SubjectElectiveGroup { ElectiveGroupId = electiveGroup.Id, SubjectId = model.Subjects[i].Id }); } context.SaveChanges(); } return(Ok()); }