Skip to content

Commit

Permalink
[Update] service and model class for Course
Browse files Browse the repository at this point in the history
  • Loading branch information
darinkaloncar committed May 27, 2024
1 parent 9df3a0a commit 86928dc
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 37 deletions.
27 changes: 2 additions & 25 deletions LangLang/BusinessLogic/UseCases/CourseService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public void Delete(int id)
}
public void DeleteByTutor(Tutor tutor)
{
foreach (Course course in GetByTutor(tutor))
foreach (Course course in GetByTutor(tutor.Id))
{
if (course.StartDateTime > DateTime.Now)
{
Expand Down Expand Up @@ -180,20 +180,11 @@ public List<Course> GetAll()
{
return _courses.GetAll();
}
public DateTime GetEnd(Course course)
{
return course.TimeSlots[course.TimeSlots.Count - 1].GetEnd();
}

public bool IsActive(Course course)
{
if (course.StartDateTime <= DateTime.Now && GetEnd(course) >= DateTime.Now) return true;
return false;
}
public int NumActiveCourses(Tutor tutor)
{
int active = 0;
List<Course> coursesByTutor = GetByTutor(tutor);
List<Course> coursesByTutor = GetByTutor(tutor.Id);
foreach (Course course in coursesByTutor)
{
if (course.IsActive())
Expand All @@ -203,20 +194,6 @@ public int NumActiveCourses(Tutor tutor)
}
return active;
}
public List<Course> GetByTutor(Tutor tutor)
{
List<Course> coursesByTutor = new List<Course>();

foreach (Course course in GetAll())
{
if (course.TutorId == tutor.Id)
{
coursesByTutor.Add(course);
}
}

return coursesByTutor;
}

public List<Course> GetByTutor(int tutorId)
{
Expand Down
2 changes: 1 addition & 1 deletion LangLang/BusinessLogic/UseCases/TutorRatingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public double GetAverageRating(Tutor tutor)
{
CourseService courseService = new();
List<double> ratings = new();
foreach (Course course in courseService.GetByTutor(tutor))
foreach (Course course in courseService.GetByTutor(tutor.Id))
{
ratings.Add(GetAverageTutorRating(course));
}
Expand Down
17 changes: 15 additions & 2 deletions LangLang/Domain/Models/Course.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,8 @@ public int DaysUntilStart()

public bool IsHeldInLastYear()
{
var endDate = TimeSlots[^1].GetEnd();
DateTime oneYearAgo = DateTime.Now.AddYears(-1);
return endDate > oneYearAgo && endDate <= DateTime.Now;
return GetEnd() > oneYearAgo && GetEnd() <= DateTime.Now;
}

public override string ToString()
Expand All @@ -130,5 +129,19 @@ public string ToPdfString()
{
return Id + " " + " " + Language + " " + Level;
}

public DateTime GetEnd()
{
return TimeSlots[TimeSlots.Count - 1].GetEnd();
}
public bool IsActive()
{
if (StartDateTime <= DateTime.Now && GetEnd() >= DateTime.Now) return true;
return false;
}
public bool CanChange()
{
return StartDateTime >= DateTime.Now.AddDays(Constants.COURSE_MODIFY_PERIOD); ;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public void Update()
{
Courses.Clear();
CourseService courseService = new();
foreach (Course course in courseService.GetByTutor(LoggedIn))
foreach (Course course in courseService.GetByTutor(LoggedIn.Id))
{
Courses.Add(new CourseViewModel(course));
}
Expand Down
13 changes: 5 additions & 8 deletions LangLang/WPF/Views/TutorView/Tabs/Courses.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ private void CourseSearchWindowBtn_Click(object sender, RoutedEventArgs e)

private void CourseUpdateWindowBtn_Click(object sender, RoutedEventArgs e)
{
CourseService courseService = new();
if (courseService.CanChange(CoursesViewModel.SelectedCourse.Id))
if (CoursesViewModel.SelectedCourse.ToCourse().CanChange())
{
CourseUpdateWindow updateWindow = new(this, CoursesViewModel.SelectedCourse.ToCourse());
updateWindow.Show();
Expand All @@ -81,9 +80,9 @@ private void CourseUpdateWindowBtn_Click(object sender, RoutedEventArgs e)

private void CourseDeleteBtn_Click(object sender, RoutedEventArgs e)
{
CourseService courseService = new();
if (courseService.CanChange(CoursesViewModel.SelectedCourse.Id))
if (CoursesViewModel.SelectedCourse.ToCourse().CanChange())
{
CourseService courseService = new();
courseService.Delete(CoursesViewModel.SelectedCourse.Id);
CoursesViewModel.Update();
MessageBox.Show("The course has been successfully deleted.", "Information", MessageBoxButton.OK, MessageBoxImage.Information);
Expand All @@ -96,8 +95,7 @@ private void CourseDeleteBtn_Click(object sender, RoutedEventArgs e)

private void EnterGradeBtn_Click(object sender, RoutedEventArgs e)
{
CourseService courseService = new();
if (courseService.GetEnd(CoursesViewModel.SelectedCourse.ToCourse()) < DateTime.Now)
if (CoursesViewModel.SelectedCourse.ToCourse().GetEnd() < DateTime.Now)
{
EnterGradesWindow gradesWindow = new(CoursesViewModel.SelectedCourse);
gradesWindow.Show();
Expand All @@ -109,8 +107,7 @@ private void EnterGradeBtn_Click(object sender, RoutedEventArgs e)
}
private void DurationOfCourseBtn_Click(object sender, RoutedEventArgs e)
{
CourseService courseService = new();
if (courseService.IsActive(CoursesViewModel.SelectedCourse.ToCourse()))
if (CoursesViewModel.SelectedCourse.ToCourse().IsActive())
{
DurationOfCourseWindow courseWindow = new(CoursesViewModel.SelectedCourse);
courseWindow.Show();
Expand Down

0 comments on commit 86928dc

Please sign in to comment.