public async Task <IActionResult> SetStudentTimetableFromPersonalNumber([FromBody] StudentModel body) { _logger.LogInformation($"Setting timetable for student: {body.Email}."); User user = await _userService.GetUserByEmailAsync(body.Email); if (user == null) { _logger.LogError($"User with email: {body.Email} does not exist."); return(ErrorResponse($"User with email: {body.Email} does not exist.")); } var timetable = await _schoolScheduleProxy.GetByPersonalNumber(body.PersonalNumber); if (timetable == null) { return(ErrorResponse($"Student with number: {body.PersonalNumber} does not exist.")); } FRITeam.Swapify.SwapifyBase.Entities.Timetable studentTimetable = await ConverterApiToDomain.ConvertTimetableForPersonalNumberAsync(timetable, _courseService); Student student = user.Student; if (student == null) { student = new Student { PersonalNumber = body.PersonalNumber, UserId = user.Id }; await _studentService.AddAsync(student); user.Student = student; await _studentService.UpdateStudentTimetableAsync(student, studentTimetable); await _userService.UpdateUserAsync(user); return(Ok(student.Timetable)); } student.PersonalNumber = body.PersonalNumber; await _studentService.UpdateStudentTimetableAsync(student, studentTimetable); var requests = await _blockChangesService.FindWaitingStudentRequests(student.Id); foreach (var item in requests) { await _blockChangesService.CancelExchangeRequest(item); } await _userService.UpdateUserAsync(user); return(Ok(student.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.")); } }