public static TrainingModel CreateTraining(TrainingModel model) { using (var ctx = new DFAppEntities()) { DBModel.Trainings existing = ctx.Trainings .FirstOrDefault(x => x.TrainingDate == model.TrainingDate && x.TrainingLocationID == model.TrainingLocationID && x.TrainingDanceGroupID == model.TrainingDanceGroupID && x.StartTime == model.StartTime ); if (existing != null) { throw new Exception("Trening sa zadatim datumom, lokacijom, grupom i početnim vremenom već postoji."); } DBModel.Trainings t = new DBModel.Trainings { TrainingDate = model.TrainingDate, TrainingLocationID = model.TrainingLocationID, TrainingDanceGroupID = model.TrainingDanceGroupID, WeekDay = model.TrainingDate.ToString("dddd", new CultureInfo("sr-Latn-RS")), StartTime = model.StartTime, EndTime = model.EndTime, TrainerUserID = model.TrainerUserID, Note = model.Note }; // training member presence registrations var groupMembers = ctx.DanceGroupMembers .Include(tbl => tbl.Members) .Where(x => x.DanceGroupID == model.TrainingDanceGroupID && x.Members.IsActive); foreach (var groupMember in groupMembers) { TrainingMemberPresenceRegistrations r = new TrainingMemberPresenceRegistrations { TrainingID = t.TrainingID, MemberID = groupMember.MemberID, IsPresent = true, AbsenceJustified = true }; ctx.TrainingMemberPresenceRegistrations.Add(r); } ctx.Trainings.Add(t); ctx.SaveChanges(); model.WeekDay = t.WeekDay; model.TrainingID = t.TrainingID; return(model); } }
public static void EditTraining(TrainingModel model) { using (var ctx = new DFAppEntities()) { DBModel.Trainings existing = ctx.Trainings.FirstOrDefault(x => x.TrainingID == model.TrainingID); if (existing != null) { existing.Note = model.Note; ctx.SaveChanges(); } } }
public static void DeleteTraining(int id) { using (var ctx = new DFAppEntities()) { DBModel.Trainings t = ctx.Trainings.FirstOrDefault(x => x.TrainingID == id); if (t != null) { ctx.Entry(t).Collection(x => x.TrainingMemberPresenceRegistrations).Load(); for (int i = t.TrainingMemberPresenceRegistrations.Count() - 1; i >= 0; i--) { ctx.TrainingMemberPresenceRegistrations.Remove(t.TrainingMemberPresenceRegistrations.ElementAt(i)); } ctx.Trainings.Remove(t); ctx.SaveChanges(); } } }