public Lecture CreateLecture(CreateLectureDTO lectureDTO) { var lecture = new Lecture() { TeacherId = lectureDTO.TeacherId, StudentClassId = lectureDTO.StudentClassId, Title = lectureDTO.Title, Start = lectureDTO.Start, End = lectureDTO.End, Status = LectureStatus.Ahead, Subject = (Subject)Enum.Parse(typeof(Subject), lectureDTO.SubjectName) }; return(lecture); }
public void View_AddLectureClick(object sender, AddNewLectureEventArgs e) { var startDate = this.dateParser.GetDate(e.LectureDTO.Date, e.LectureDTO.Start); var endDate = this.dateParser.GetDate(e.LectureDTO.Date, e.LectureDTO.End); if (startDate < DateTime.Now) { this.View.Model.ErrorMessage = "Cannot add lecture in the past!"; this.View.Model.IsSuccess = false; return; } if (startDate > endDate) { this.View.Model.ErrorMessage = "Lecture start time cannot be after end time!"; this.View.Model.IsSuccess = false; return; } var teacher = this.teacherService.FindById(e.LectureDTO.TeacherId); var studentClass = this.studentClassService.FindById(e.LectureDTO.StudentClassId); var hasLecture = teacher.Lectures.Any(l => l.Status == Data.Enums.LectureStatus.Ahead && startDate < l.End && l.Start < endDate); if (hasLecture) { this.View.Model.ErrorMessage = "This Teacher already has lecture in this time period!"; this.View.Model.IsSuccess = false; return; } var lectureDTO = new CreateLectureDTO() { StudentClassId = e.LectureDTO.StudentClassId, Title = e.LectureDTO.Title, SubjectName = teacher.Subject.ToString(), TeacherId = e.LectureDTO.TeacherId, Start = startDate, End = endDate }; try { e.LectureDTO.SubjectName = teacher.Subject.ToString(); var lecture = this.lectureFactory.CreateLecture(lectureDTO); teacher.Lectures.Add(lecture); studentClass.Lectures.Add(lecture); } catch (ArgumentException ex) { this.View.Model.ErrorMessage = ex.Message; this.View.Model.IsSuccess = false; return; } try { this.teacherService.Save(); } catch (Exception ex) { this.View.Model.ErrorMessage = ex.Message; this.View.Model.IsSuccess = false; return; } this.View.Model.IsSuccess = true; }