Esempio n. 1
0
        public async Task AssingTimetableToStudent()
        {
            var           options             = GetProxyOptions();
            var           schoolScheduleProxy = new SchoolScheduleProxy(options);
            var           schoolCourseProxy   = new SchoolCourseProxy(options);
            CourseService serviceCourse       = new(_loggerMockCourse.Object, _database, schoolScheduleProxy, schoolCourseProxy);

            var timetable = await schoolScheduleProxy.GetByPersonalNumber("559841");

            Student student = new()
            {
                PersonalNumber = "559841",
                Timetable      = await ConverterApiToDomain.ConvertTimetableForPersonalNumberAsync(timetable, serviceCourse)
            };

            var newBlock      = new Block();
            var countShouldBe = student.Timetable.AllBlocks.Count;

            student.Timetable.AddNewBlock(newBlock);

            student.Timetable.AllBlocks.Count().Should().Be(countShouldBe + 1);

            var newBlockSt = new Block();

            countShouldBe = student.Timetable.AllBlocks.Count;
            student.Timetable.AddNewBlock(newBlockSt);

            student.Timetable.AllBlocks.Count().Should().Be(countShouldBe + 1);
        }
Esempio n. 2
0
        public async Task <IActionResult> InitializeTimetableForTestUsers([FromBody] StudentModel body)
        {
            _logger.LogInformation($"[TEST] Setting test timetable for test student: {body.Email}.");
            User user = await _userService.GetUserByEmailAsync(body.Email);

            if (user == null)
            {
                _logger.LogError($"[TEST] Test user with email: {body.Email} does not exist.");
                return(ErrorResponse($"User with email: {body.Email} does not exist."));
            }
            var timetable = _schoolScheduleProxy.GetFromJsonFile("TestTimetable.json");

            if (timetable == null)
            {
                return(ErrorResponse("Loading of test timetable failed."));
            }

            FRITeam.Swapify.SwapifyBase.Entities.Timetable studentTimetable = await ConverterApiToDomain.ConvertTimetableForPersonalNumberAsync(timetable, _courseService);

            Student student = user.Student;

            if (student == null)
            {
                student = new Student
                {
                    PersonalNumber = "000000",
                    UserId         = user.Id
                };
                await _studentService.AddAsync(student);

                user.Student = student;

                await _studentService.UpdateStudentTimetableAsync(student, studentTimetable);

                await _userService.UpdateUserAsync(user);

                return(Ok(student.Timetable));
            }
            if (student.PersonalNumber != null && student.PersonalNumber.Equals("000000"))
            {
                return(Ok(student.Timetable));
            }
            else
            {
                student.PersonalNumber = "000000";
                await _studentService.UpdateStudentTimetableAsync(student, studentTimetable);

                await _userService.UpdateUserAsync(user);

                return(Ok(student.Timetable));
            }
        }
Esempio n. 3
0
        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));
        }
Esempio n. 4
0
        public async Task <Course> FindCourseTimetableFromProxy(Course course)
        {
            List <ScheduleTimetableResult> schedules = new();
            int years = course.StudyType.Contains("inž") ? 2 : 3;

            for (int i = 1; i <= years; i++)
            {
                try
                {
                    ScheduleTimetableResult schedule = await _scheduleProxy.GetBySubjectCode(course.CourseCode, i + "", course.StudyType);

                    if (schedule != null)
                    {
                        schedules.Add(schedule);
                    }
                } catch (Exception ex)
                {
                    _logger.LogWarning($"Error while searching timetable of course {course.CourseName}({course.CourseCode}): {ex.Message}");
                }
                if (schedules == null)
                {
                    _logger.LogError($"Unable to load schedule for subject {course.CourseCode}. Schedule proxy returned null");
                    return(null);
                }
            }
            course.Timetable = new Timetable(Semester.GetSemester());
            schedules.ForEach(async(schedule) => {
                Timetable t = await ConverterApiToDomain.ConvertTimetableForCourseAsync(schedule, this);
                if (t != null)
                {
                    foreach (Block b in t.AllBlocks)
                    {
                        if (!course.Timetable.ContainsBlock(b))
                        {
                            course.Timetable.AddNewBlock(b);
                        }
                    }
                }
            });
            course.LastUpdateOfTimetable = DateTime.Now;
            await UpdateAsync(course);

            return(course);
        }
Esempio n. 5
0
        public async Task AddStudentTest()
        {
            var            options             = GetProxyOptions();
            var            schoolScheduleProxy = new SchoolScheduleProxy(options);
            var            schoolCourseProxy   = new SchoolCourseProxy(options);
            CourseService  serviceCourse       = new(_loggerMockCourse.Object, _database, schoolScheduleProxy, schoolCourseProxy);
            StudentService stSer = new(_database);

            var timetable = await schoolScheduleProxy.GetByPersonalNumber("559841");

            Student st = new()
            {
                PersonalNumber = "559841",
                Timetable      = await ConverterApiToDomain.ConvertTimetableForPersonalNumberAsync(timetable, serviceCourse)
            };
            Course cr = new() { CourseName = "DISS", Id = Guid.NewGuid() };
            Block  bl = new()
            {
                BlockType = BlockType.Lecture,
                CourseId  = cr.Id,
                StartHour = 16,
                Duration  = 2,
                Day       = Day.Thursday
            };

            st.Timetable.AddNewBlock(bl);

            await stSer.AddAsync(st);

            st = await stSer.FindByIdAsync(st.Id);

            st.Id.Should().NotBeEmpty();
            st.PersonalNumber.Should().Be("559841");
            st.Timetable.AllBlocks.Last().Day.Should().Be(Day.Thursday);
            st.Timetable.AllBlocks.Last().Duration.Should().Be(2);
            st.Timetable.AllBlocks.Last().StartHour.Should().Be(16);
            st.Timetable.AllBlocks.Last().BlockType.Should().Be(BlockType.Lecture);
        }

        [Fact]
Esempio n. 6
0
        public void ConvertTest_MergeSameBlocksWithDifferentTeacher()
        {
            var blocks = new List <Block>()
            {
                new Block()
                {
                    Day       = Day.Monday,
                    StartHour = 7,
                    Duration  = 2,
                    Room      = "room200",
                    Teacher   = "teacher1",
                    BlockType = BlockType.Laboratory,
                    CourseId  = Guid.Empty
                },
                new Block()
                {
                    Day       = Day.Monday,
                    StartHour = 8,
                    Duration  = 2,
                    Room      = "room200",
                    Teacher   = "teacher1",
                    BlockType = BlockType.Laboratory,
                    CourseId  = Guid.Empty
                },
                new Block()
                {
                    Day       = Day.Tuesday,
                    StartHour = 7,
                    Duration  = 2,
                    Room      = "room200",
                    Teacher   = "teacher1",
                    BlockType = BlockType.Laboratory,
                    CourseId  = Guid.Empty
                },
                new Block()
                {
                    Day       = Day.Tuesday,
                    StartHour = 7,
                    Duration  = 2,
                    Room      = "room200",
                    Teacher   = "teacher2",
                    BlockType = BlockType.Laboratory,
                    CourseId  = Guid.Empty
                },
                new Block()
                {
                    Day       = Day.Wednesday,
                    StartHour = 9,
                    Duration  = 2,
                    Room      = "room200",
                    Teacher   = "teacher1",
                    BlockType = BlockType.Laboratory,
                    CourseId  = Guid.Empty
                },
                new Block()
                {
                    Day       = Day.Wednesday,
                    StartHour = 9,
                    Duration  = 2,
                    Room      = "room201",
                    Teacher   = "teacher1",
                    BlockType = BlockType.Laboratory,
                    CourseId  = Guid.Empty
                }
            };

            var timetable = ConverterApiToDomain.MergeSameBlocksWithDifferentTeacher(blocks);

            var mergedBlocks = new List <Block>()
            {
                new Block()
                {
                    Day       = Day.Monday,
                    StartHour = 7,
                    Duration  = 2,
                    Room      = "room200",
                    Teacher   = "teacher1",
                    BlockType = BlockType.Laboratory,
                    CourseId  = Guid.Empty
                },
                new Block()
                {
                    Day       = Day.Monday,
                    StartHour = 8,
                    Duration  = 2,
                    Room      = "room200",
                    Teacher   = "teacher1",
                    BlockType = BlockType.Laboratory,
                    CourseId  = Guid.Empty
                },
                new Block()
                {
                    Day       = Day.Tuesday,
                    StartHour = 7,
                    Duration  = 2,
                    Room      = "room200",
                    Teacher   = "teacher1,teacher2",
                    BlockType = BlockType.Laboratory,
                    CourseId  = Guid.Empty
                },
                new Block()
                {
                    Day       = Day.Wednesday,
                    StartHour = 9,
                    Duration  = 2,
                    Room      = "room200",
                    Teacher   = "teacher1",
                    BlockType = BlockType.Laboratory,
                    CourseId  = Guid.Empty
                },
                new Block()
                {
                    Day       = Day.Wednesday,
                    StartHour = 9,
                    Duration  = 2,
                    Room      = "room201",
                    Teacher   = "teacher1",
                    BlockType = BlockType.Laboratory,
                    CourseId  = Guid.Empty
                }
            };

            timetable.AllBlocks.Should().BeEquivalentTo(mergedBlocks);
        }
Esempio n. 7
0
        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."));
            }
        }