From b8d4a8fff9d47546a89a1edf5c872519505d9a39 Mon Sep 17 00:00:00 2001 From: Natasa Kasikovic~ Date: Tue, 28 May 2024 00:29:17 +0200 Subject: [PATCH] [refactor] Modify GetAvailableExams method so it doesnt return exams of lower levels if an exam if a higher level has been passed --- .../BusinessLogic/UseCases/ExamSlotService.cs | 62 +++++++++---------- 1 file changed, 30 insertions(+), 32 deletions(-) diff --git a/LangLang/BusinessLogic/UseCases/ExamSlotService.cs b/LangLang/BusinessLogic/UseCases/ExamSlotService.cs index bb36a70..8f0d459 100644 --- a/LangLang/BusinessLogic/UseCases/ExamSlotService.cs +++ b/LangLang/BusinessLogic/UseCases/ExamSlotService.cs @@ -258,51 +258,49 @@ public List SearchByStudent(Student student, DateTime examDate, string } - // returns a list of exams that are available for student application public List GetAvailableExams(Student student) { - - if (student == null) return null; List availableExams = new(); var enrollmentReqService = new EnrollmentRequestService(); - List studentRequests = enrollmentReqService.GetByStudent(student); + var resultService = new ExamResultService(); + var applicationService = new ExamApplicationService(); + var examService = new ExamSlotService(); + var courseService = new CourseService(); - foreach (ExamSlot exam in GetAll()) - { - //don't include filled exams and exams that passed or are less then a month away - if (!IsAvailable(exam)) continue; - //don't include exams for which student has already applied - ExamApplicationService appService = new(); - bool hasAlreadyApplied = appService.HasApplied(student, exam); - if (hasAlreadyApplied) continue; + List studentRequests = enrollmentReqService.GetByStudent(student); + List studentResults = resultService.GetByStudent(student); - foreach (EnrollmentRequest enrollmentRequest in studentRequests) - { - CourseService courseService = new(); - Course course = courseService.Get(enrollmentRequest.CourseId); - if (HasStudentAttendedCourse(course, enrollmentRequest, exam)) - { - availableExams.Add(exam); - } - } - } + var exams = GetAll().Where(exam => IsAvailable(exam)) // exclude exams that have been filled, passed, or are less than a month away. + .Where(exam => applicationService.HasApplied(student, exam)) //exclude exams for which student has already applied + .Where(exam => !HasPassedLowerLevel(studentResults, exam)) + .Where(exam => HasAttendedRequiredCourse(studentRequests, exam)).ToList(); + return exams; + } - return availableExams; + private bool HasPassedLowerLevel(List results, ExamSlot exam) + { + var examService = new ExamSlotService(); + return results.Any(result => result.Outcome == ExamOutcome.Passed && + exam.Language == examService.Get(result.ExamSlotId).Language && + exam.Level < examService.Get(result.ExamSlotId).Level); } - private bool HasStudentAttendedCourse(Course course, EnrollmentRequest enrollmentRequest, ExamSlot examSlot) + private bool HasAttendedRequiredCourse(List requests, ExamSlot exam) { - if (course.Language == examSlot.Language && course.Level == examSlot.Level) - { - if (enrollmentRequest.Status == Status.Accepted && course.IsCompleted()) - { - return true; - } - } - return false; + var courseService = new CourseService(); + return requests.Select(request => courseService.Get(request.CourseId)) + .Any(course => HasStudentAttendedCourse(course, exam)); } + + private bool HasStudentAttendedCourse(Course course, ExamSlot examSlot) + { + return course.Language == examSlot.Language && + course.Level == examSlot.Level && + course.IsCompleted(); + } + public List GetExamsHeldInLastYear() { return GetAll().Where(exam => exam.IsHeldInLastYear()).ToList();