public async Task <IActionResult> GetCourseTimetable(string courseId, string studentId) { bool isValidCourseGUID = Guid.TryParse(courseId, out Guid courseGuid); if (!isValidCourseGUID) { return(ErrorResponse($"Course id: {courseId} is not valid GUID.")); } bool isValidStudentGUID = Guid.TryParse(studentId, out Guid studentGuid); var _student = await _studentService.FindByIdAsync(studentGuid); if (!isValidStudentGUID) { return(ErrorResponse($"Student id: {studentGuid} is not valid GUID.")); } if (_student == null) { return(ErrorResponse($"Student with id: {studentId} does not exist.")); } var _course = await _courseService.FindCourseTimetableFromProxy(courseGuid); if (_course == null) { return(ErrorResponse($"Course with id: {courseId} does not exist.")); } var timetable = new Timetable(); var Blocks = new List <TimetableBlock>(); foreach (var block in _course.Timetable.AllBlocks) { TimetableBlock timetableBlock = new TimetableBlock { Id = block.BlockId.ToString(), Day = block.Day.GetHashCode(), StartBlock = block.StartHour - 6, EndBlock = block.StartHour - 6 + block.Duration, CourseId = _course.Id.ToString(), CourseName = _course.CourseName, CourseCode = _course.CourseCode ?? "", Room = block.Room, Teacher = block.Teacher, Type = (TimetableBlockType)block.BlockType }; if (!_student.Timetable.ContainsBlock(block)) { Blocks.Add(timetableBlock); } } timetable.Blocks = Blocks; return(Ok(timetable)); }
public async Task <IActionResult> GetStudentTimetable(string userEmail) { try { _logger.LogInformation($"[API getStudentTimetable] Setting timetable for test student"); User user = await _userService.GetUserByEmailAsync(userEmail); if (user?.Student == null) { _logger.Log(LogLevel.Information, $"Student for user: {userEmail} does not exist."); return(Ok(new Timetable())); } string studentId = user.Student.Id.ToString(); if (!Guid.TryParse(studentId, out Guid guid)) { _logger.Log(LogLevel.Error, $"Student id: {studentId} is not valid GUID."); return(ErrorResponse($"Student id: {studentId} is not valid GUID.")); } var student = await _studentService.FindByIdAsync(guid); if (student == null) { _logger.Log(LogLevel.Error, $"Student with id: {studentId} does not exist."); return(ErrorResponse($"Student with id: {studentId} does not exist.")); } if (student.Timetable == null) { _logger.Log(LogLevel.Error, $"Timetable for student with id: {studentId} does not exist."); return(Ok(new Timetable())); } if (student.Timetable.IsOutDated() && !string.IsNullOrEmpty(student.PersonalNumber)) { var scheduleTimetable = await _schoolScheduleProxy.GetByPersonalNumber(student.PersonalNumber); if (scheduleTimetable == null) { return(ErrorResponse($"Student with number: {student.PersonalNumber} does not exist.")); } await _studentService.UpdateStudentTimetableAsync(student, await ConverterApiToDomain.ConvertTimetableForPersonalNumberAsync(scheduleTimetable, _courseService) ); await _userService.UpdateUserAsync(user); var requests = await _blockChangesService.FindWaitingStudentRequests(student.Id); foreach (var item in requests) { await _blockChangesService.CancelExchangeRequest(item); } } var timetable = new Timetable(); var Blocks = new List <TimetableBlock>(); foreach (var block in student.Timetable.AllBlocks) { TimetableBlock timetableBlock = new TimetableBlock(); Course course = await _courseService.FindByIdAsync(block.CourseId); timetableBlock.Id = block.BlockId.ToString(); timetableBlock.Day = block.Day.GetHashCode(); timetableBlock.StartBlock = block.StartHour - 6; timetableBlock.EndBlock = timetableBlock.StartBlock + block.Duration; timetableBlock.CourseId = course.Id.ToString(); timetableBlock.CourseName = course.CourseName; timetableBlock.CourseCode = course.CourseCode; timetableBlock.Room = block.Room; timetableBlock.Teacher = block.Teacher; timetableBlock.Type = (TimetableBlockType)block.BlockType; timetableBlock.BlockColor = block.BlockColor; Blocks.Add(timetableBlock); } timetable.Blocks = Blocks; return(Ok(timetable)); } catch (Exception e) { _logger.Log(LogLevel.Error, $"When processing user: {userEmail} exception was invoked: {e}"); return(ErrorResponse($"Student: {userEmail} produced exception.")); } }