public async Task <IActionResult> EditBlock([FromBody] UpdateBlockModel updateBlockModel) { var _user = await _userService.GetUserByEmailAsync(updateBlockModel.User.Email); bool isValidGUID = Guid.TryParse(_user.Student.Id.ToString(), out Guid guid); if (!isValidGUID) { return(ErrorResponse($"Student id: {_user.Student.Id.ToString()} is not valid GUID.")); } var student = await _studentService.FindByIdAsync(guid); if (student == null) { return(ErrorResponse($"Student with id: {updateBlockModel.User.Student.Id} does not exist.")); } if (student.Timetable == null) { return(ErrorResponse($"Timetable for student with id: {student.Id} does not exist.")); } Course newCourse = await _courseService.FindByCodeAsync(updateBlockModel.TimetableBlock.CourseCode); if (newCourse == null) { return(ErrorResponse($"New course: {updateBlockModel.TimetableBlock.CourseName} does not exist.")); } Block newBlock = TimetableBlock.ConvertToBlock(updateBlockModel.TimetableBlock, newCourse.Id); if (student.Timetable.UpdateBlock(newBlock)) { student.Timetable.UpdateColorOfBlocksWithSameCourseId(newBlock); await _studentService.UpdateStudentAsync(student); } else { return(ErrorResponse($"Block {newBlock.ToString()} is not updated")); } return(Ok(newBlock)); }
public async Task <IActionResult> AddNewBlock([FromBody] AddNewBlockModel newBlockModel) { var _user = await _userService.GetUserByEmailAsync(newBlockModel.User.Email); var student = await _studentService.FindByIdAsync(_user.Student.Id); if (student == null) { return(ErrorResponse($"Student with id: {_user.Student.Id} does not exist.")); } if (student.Timetable == null) { return(ErrorResponse($"Timetable for student with id: {student.Id} does not exist.")); } TimetableBlock timetableBlock = newBlockModel.TimetableBlock; Course course = await _courseService.GetOrAddNotExistsCourse(timetableBlock.CourseCode, timetableBlock.CourseName); if (course == null) { return(ErrorResponse($"Course: {timetableBlock.CourseName} does not exist.")); } Block block = TimetableBlock.ConvertToBlock(timetableBlock, course.Id); if (student.Timetable.IsSubjectPresentInTimetable(block)) { return(ErrorResponse($"Course: {timetableBlock.CourseName} is already present.")); } student.Timetable.AddNewBlock(block); student.Timetable.UpdateColorOfBlocksWithSameCourseId(block); await _studentService.UpdateStudentAsync(student); //return block with new id return(Ok(newBlockModel.TimetableBlock)); }