public static bool AddStudentInSubscription(StudentInSubscriptionDTO studentInSubscriptionDTO) { try { using (Gymnastics_Studio_DataEntities GSDE = new Gymnastics_Studio_DataEntities()) { StudentInSubscription studentInSubscription = StudentInSubscriptionDTO.ConvertFromDTO(studentInSubscriptionDTO); Subscription subscription = GSDE.Subscriptions.Where(x => x.Id == studentInSubscription.SubscribtionId).FirstOrDefault(); if (studentInSubscription != default) { Convert.ToDateTime(studentInSubscription.StartDate); studentInSubscription.FinishDate = studentInSubscription.StartDate; studentInSubscription.FinishDate = Convert.ToDateTime(studentInSubscription.FinishDate).AddDays(7 * (double)subscription.WeeksNum); } var lastStudentInSubscriptions = GSDE.StudentInSubscriptions.Where(x => x.StartDate == studentInSubscription.StartDate).FirstOrDefault(); if (lastStudentInSubscriptions != default) { studentInSubscription.Id = lastStudentInSubscriptions.Id; return(EditStudentInSubscription(StudentInSubscriptionDTO.ConvertToDTO(studentInSubscription))); } GSDE.StudentInSubscriptions.Add(studentInSubscription); GSDE.SaveChanges(); return(true); } } catch (Exception e) { throw e; } }
public static bool CreateStudentInSubscription(StudentInSubscriptionDTO studentInSubscriptionDTO) { try { using (Gymnastics_Studio_DataEntities GSDE = new Gymnastics_Studio_DataEntities()) { DateTime StartDate = Convert.ToDateTime(studentInSubscriptionDTO.StartDate); Subscription Subscription = new Subscription(); Subscription = GSDE.Subscriptions.Where(x => x.Id == studentInSubscriptionDTO.SubscribtionId).FirstOrDefault(); DateTime FinishDate = Convert.ToDateTime(studentInSubscriptionDTO.StartDate).AddDays((int)Subscription.WeeksNum * 7); if (GSDE.StudentInSubscriptions.Where(x => x.StudentId == studentInSubscriptionDTO.StudentId && ((x.StartDate <= StartDate && x.FinishDate > StartDate) || (x.StartDate > StartDate && x.StartDate < FinishDate))) .FirstOrDefault() != default) { return(false); } studentInSubscriptionDTO.StartDate = StartDate.ToString(); studentInSubscriptionDTO.FinishDate = FinishDate.ToString(); if (Subscription.WeeksNum % 4 == 0) { studentInSubscriptionDTO.Stop = false; } else { studentInSubscriptionDTO.Stop = true; } GSDE.StudentInSubscriptions.Add(StudentInSubscriptionDTO.ConvertFromDTO(studentInSubscriptionDTO)); GSDE.SaveChanges(); } return(true); } catch (Exception e) { throw (e); } }