Skip to content

Commit

Permalink
[Add] methods CanCreateLiveCourse, CanCreateOnlineCourse and GetLiveC…
Browse files Browse the repository at this point in the history
…ourses to CourseController class
  • Loading branch information
darinkaloncar committed Mar 31, 2024
1 parent d6a1c84 commit 8fe037c
Showing 1 changed file with 140 additions and 0 deletions.
140 changes: 140 additions & 0 deletions LangLang/Core/Controller/CourseController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,19 @@ public Dictionary<int, Course> GetAllCourses()
return _courses.GetAllCourses();
}

public Dictionary<int, Course> GetLiveCourses()
{
Dictionary<int, Course> courses = _courses.GetAllCourses();
foreach (Course course in courses.Values)
{
if (course.Online)
{
courses.Remove(course.Id);
}
}
return courses;
}

public void Add(Course course)
{
_courses.AddCourse(course);
Expand All @@ -34,8 +47,135 @@ public void Subscribe(IObserver observer)
{
_courses.Subscribe(observer);
}
// Method returns true if a new online course can be added
// or false if there are time overlaps and the course cannot be created
// The parameter exams is a list of ExamSlots of the same tutor who wants to create a new course
public bool CanCreateOnlineCourse(Course course, List<ExamSlot> exams)
{
// Get the time and the date of the beginning and of the end of the new couse
TimeSpan courseTime = course.StartDateTime.TimeOfDay;
DateTime courseStartDate = course.StartDateTime.Date;
DateTime courseEndDate = GetCourseEnd(course);

// Go through each course with the same tutor and check if there is time overlapping
foreach (Course cour in GetCoursesByTutor(course.TutorId).Values)
{
// Check if the courses are overlapping
DateTime courStartDate = cour.StartDateTime.Date;
DateTime courEndDate = GetCourseEnd(cour);
if (courStartDate <= courseEndDate && courEndDate >= courseStartDate)
{
foreach (DayOfWeek day in cour.Days)
{
if (course.Days.Contains(day))
{
TimeSpan courTime = cour.StartDateTime.TimeOfDay;
TimeSpan difference = courseTime - courTime;
if (difference.Duration().TotalMinutes < 90)
{
return false;
}
}
}
}
}

// Go through all exams of the same tutor
foreach (ExamSlot exam in exams)
{
DateTime examDate = exam.ExamDateTime.Date;
if (examDate >= courseStartDate && examDate <= courseEndDate && course.Days.Contains(examDate.DayOfWeek))
{
// Check if there are time overlaps
TimeSpan examTime = exam.ExamDateTime.TimeOfDay;
TimeSpan difference = courseTime - examTime;
if ((examTime < courseTime && difference.Duration().TotalMinutes < 240) || (examTime > courseTime && difference.Duration().TotalMinutes < 90))
{
return false;
}
}
}
return true;
}

// Method returns true if a new live course can be added
// or false if there are time overlaps and the course cannot be created
public bool CanCreateLiveCourse(Course course, List<ExamSlot> exams)
{
// Get the time and the date of the beginning and of the end of the new couse
TimeSpan courseTime = course.StartDateTime.TimeOfDay;
DateTime courseStartDate = course.StartDateTime.Date;
DateTime courseEndDate = GetCourseEnd(course);

bool firstClassroomTaken = false;
bool secondClassromTaken = false;
// Go through each course with the same tutor and check if there is time overlapping
foreach (Course cour in GetLiveCourses().Values)
{
// Check if the courses are overlapping
DateTime courStartDate = cour.StartDateTime.Date;
DateTime courEndDate = GetCourseEnd(cour);
if (courStartDate <= courseEndDate && courEndDate >= courseStartDate)
{
foreach (DayOfWeek day in cour.Days)
{
if (course.Days.Contains(day))
{
TimeSpan courTime = cour.StartDateTime.TimeOfDay;
TimeSpan difference = courseTime - courTime;
if (difference.Duration().TotalMinutes < 90)
{
if(firstClassroomTaken && secondClassromTaken)
{
return false;
}
else if(firstClassroomTaken)
{
secondClassromTaken = true;
}
else
{
firstClassroomTaken = true;
}
}
}
}
}
}

// Go through all exams of the same tutor
foreach (ExamSlot exam in exams)
{
DateTime examDate = exam.ExamDateTime.Date;
if (examDate >= courseStartDate && examDate <= courseEndDate && course.Days.Contains(examDate.DayOfWeek))
{
// Check if there are time overlaps
TimeSpan examTime = exam.ExamDateTime.TimeOfDay;
TimeSpan difference = courseTime - examTime;
if ((examTime < courseTime && difference.Duration().TotalMinutes < 240) || (examTime > courseTime && difference.Duration().TotalMinutes < 90))
{
if (firstClassroomTaken && secondClassromTaken)
{
return false;
}
else if (firstClassroomTaken)
{
secondClassromTaken = true;
}
else
{
firstClassroomTaken = true;
}
}
}
}

if (firstClassroomTaken && secondClassromTaken)
{
return false;
}
return true;
}
// Method checks if the course is valid for updating or canceling
public bool IsCourseValid(int courseId)
{
Expand Down

0 comments on commit 8fe037c

Please sign in to comment.