Skip to content

Commit

Permalink
refactor: refactored total_courses offered by adding a grouped by cla…
Browse files Browse the repository at this point in the history
…use on facility and adding a sum() function
  • Loading branch information
corypride committed Jan 21, 2025
1 parent 684c26a commit e81aec4
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions backend/src/database/activity.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,15 +181,18 @@ func (db *DB) GetAdminDashboardInfo(facilityID uint) (models.AdminDashboardJoin,
func (db *DB) GetTotalCoursesOffered(facilityID *uint) (int, error) {
var totalCourses int
subQry := db.Table("courses c").
Select("COUNT(DISTINCT c.id) as total_courses_offered").
Joins("INNER JOIN user_enrollments ue on c.id = ue.course_id").
Joins("INNER JOIN users u on ue.user_id = u.id")

Select("COUNT(DISTINCT c.id) AS facility_course_count, f.id AS facility_id").
Joins("INNER JOIN user_enrollments ue ON c.id = ue.course_id").
Joins("INNER JOIN users u ON ue.user_id = u.id").
Joins("INNER JOIN facilities f ON u.facility_id = f.id").
Group("f.id")
if facilityID != nil {
subQry = subQry.Where("u.facility_id = ?", facilityID)
}

err := subQry.Find(&totalCourses).Error
err := db.Table("(?) AS sub", subQry).
Select("COALESCE(SUM(sub.facility_course_count), 0)").
Scan(&totalCourses).Error
if err != nil {
return 0, NewDBError(err, "error getting total courses offered")
}
Expand Down

0 comments on commit e81aec4

Please sign in to comment.