• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

classconnect-grupo3 / courses-service / 15914402693

26 Jun 2025 11:12PM UTC coverage: 81.843% (-5.9%) from 87.722%
15914402693

Pull #49

github

web-flow
Merge pull request #48 from classconnect-grupo3/notification-messages-and-testing

Notification messages and testing
Pull Request #49: Final merge (thank you for the ride)

1542 of 2153 new or added lines in 24 files covered. (71.62%)

16 existing lines in 5 files now uncovered.

4548 of 5557 relevant lines covered (81.84%)

0.9 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

50.41
/src/controller/statistics_controller.go
1
package controller
2

3
import (
4
        "courses-service/src/service"
5
        "net/http"
6
        "time"
7

8
        "github.com/gin-gonic/gin"
9
)
10

11
// StatisticsController handles requests for statistics data
12
type StatisticsController struct {
13
        statisticsService service.StatisticsServiceInterface
14
}
15

16
// NewStatisticsController creates a new statistics controller
17
func NewStatisticsController(statisticsService service.StatisticsServiceInterface) *StatisticsController {
1✔
18
        return &StatisticsController{
1✔
19
                statisticsService: statisticsService,
1✔
20
        }
1✔
21
}
1✔
22

23
// GetCourseStatistics returns statistics for a specific course
24
func (c *StatisticsController) GetCourseStatistics(ctx *gin.Context) {
1✔
25
        courseID := ctx.Param("courseId")
1✔
26
        if courseID == "" {
1✔
NEW
27
                ctx.JSON(http.StatusBadRequest, gin.H{"error": "course_id is required"})
×
NEW
28
                return
×
NEW
29
        }
×
30

31
        // Parse time range from query parameters (optional)
32
        fromStr := ctx.Query("from")
1✔
33
        toStr := ctx.Query("to")
1✔
34

1✔
35
        var from, to time.Time
1✔
36
        var err error
1✔
37

1✔
38
        if fromStr != "" {
1✔
NEW
39
                from, err = time.Parse("2006-01-02", fromStr)
×
NEW
40
                if err != nil {
×
NEW
41
                        ctx.JSON(http.StatusBadRequest, gin.H{"error": "invalid from date format, use YYYY-MM-DD"})
×
NEW
42
                        return
×
NEW
43
                }
×
44
        }
45

46
        if toStr != "" {
1✔
NEW
47
                to, err = time.Parse("2006-01-02", toStr)
×
NEW
48
                if err != nil {
×
NEW
49
                        ctx.JSON(http.StatusBadRequest, gin.H{"error": "invalid to date format, use YYYY-MM-DD"})
×
NEW
50
                        return
×
NEW
51
                }
×
52
        }
53

54
        data, _, err := c.statisticsService.ExportCourseStatsCSV(ctx, courseID, from, to)
1✔
55
        if err != nil {
1✔
NEW
56
                ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
×
NEW
57
                return
×
NEW
58
        }
×
59

60
        ctx.JSON(http.StatusOK, gin.H{"csv": string(data)})
1✔
61
}
62

63
// Returns statistics for a specific student
64
func (c *StatisticsController) GetStudentStatistics(ctx *gin.Context) {
1✔
65
        studentID := ctx.Param("studentId")
1✔
66
        if studentID == "" {
1✔
NEW
67
                ctx.JSON(http.StatusBadRequest, gin.H{"error": "student_id is required"})
×
NEW
68
                return
×
NEW
69
        }
×
70

71
        courseID := ctx.Query("course_id")
1✔
72
        if courseID == "" {
1✔
NEW
73
                ctx.JSON(http.StatusBadRequest, gin.H{"error": "course_id is required"})
×
NEW
74
                return
×
NEW
75
        }
×
76

77
        // Parse time range from query parameters (optional)
78
        fromStr := ctx.Query("from")
1✔
79
        toStr := ctx.Query("to")
1✔
80

1✔
81
        var from, to time.Time
1✔
82
        var err error
1✔
83

1✔
84
        if fromStr != "" {
1✔
NEW
85
                from, err = time.Parse("2006-01-02", fromStr)
×
NEW
86
                if err != nil {
×
NEW
87
                        ctx.JSON(http.StatusBadRequest, gin.H{"error": "invalid from date format, use YYYY-MM-DD"})
×
NEW
88
                        return
×
NEW
89
                }
×
90
        }
91

92
        if toStr != "" {
1✔
NEW
93
                to, err = time.Parse("2006-01-02", toStr)
×
NEW
94
                if err != nil {
×
NEW
95
                        ctx.JSON(http.StatusBadRequest, gin.H{"error": "invalid to date format, use YYYY-MM-DD"})
×
NEW
96
                        return
×
NEW
97
                }
×
98
        }
99

100
        data, _, err := c.statisticsService.ExportStudentStatsCSV(ctx, studentID, courseID, from, to)
1✔
101
        if err != nil {
1✔
NEW
102
                ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
×
NEW
103
                return
×
NEW
104
        }
×
105

106
        ctx.JSON(http.StatusOK, gin.H{"csv": string(data)})
1✔
107
}
108

109
// GetTeacherCoursesStatistics returns aggregated statistics for all courses of a teacher
110
func (c *StatisticsController) GetTeacherCoursesStatistics(ctx *gin.Context) {
1✔
111
        teacherID := ctx.Param("teacherId")
1✔
112
        if teacherID == "" {
1✔
NEW
113
                ctx.JSON(http.StatusBadRequest, gin.H{"error": "teacher_id is required"})
×
NEW
114
                return
×
NEW
115
        }
×
116

117
        fromStr := ctx.Query("from")
1✔
118
        toStr := ctx.Query("to")
1✔
119

1✔
120
        var from, to time.Time
1✔
121
        var err error
1✔
122

1✔
123
        if fromStr != "" {
1✔
NEW
124
                from, err = time.Parse("2006-01-02", fromStr)
×
NEW
125
                if err != nil {
×
NEW
126
                        ctx.JSON(http.StatusBadRequest, gin.H{"error": "invalid from date format, use YYYY-MM-DD"})
×
NEW
127
                        return
×
NEW
128
                }
×
129
        }
130

131
        if toStr != "" {
1✔
NEW
132
                to, err = time.Parse("2006-01-02", toStr)
×
NEW
133
                if err != nil {
×
NEW
134
                        ctx.JSON(http.StatusBadRequest, gin.H{"error": "invalid to date format, use YYYY-MM-DD"})
×
NEW
135
                        return
×
NEW
136
                }
×
137
        }
138

139
        data, _, err := c.statisticsService.ExportTeacherCoursesStatsCSV(ctx, teacherID, from, to)
1✔
140
        if err != nil {
1✔
NEW
141
                ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
×
NEW
142
                return
×
NEW
143
        }
×
144

145
        ctx.JSON(http.StatusOK, gin.H{"csv": string(data)})
1✔
146
}
147

148
// GetBackofficeStatistics returns general system statistics for backoffice
149
func (c *StatisticsController) GetBackofficeStatistics(ctx *gin.Context) {
1✔
150
        data, err := c.statisticsService.GetBackofficeStatistics(ctx)
1✔
151
        if err != nil {
1✔
NEW
152
                ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
×
NEW
153
                return
×
NEW
154
        }
×
155

156
        ctx.JSON(http.StatusOK, data)
1✔
157
}
158

159
// GetBackofficeCoursesStats returns detailed course statistics for backoffice
160
func (c *StatisticsController) GetBackofficeCoursesStats(ctx *gin.Context) {
1✔
161
        data, err := c.statisticsService.GetBackofficeCoursesStats(ctx)
1✔
162
        if err != nil {
1✔
NEW
163
                ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
×
NEW
164
                return
×
NEW
165
        }
×
166

167
        ctx.JSON(http.StatusOK, data)
1✔
168
}
169

170
// GetBackofficeAssignmentsStats returns detailed assignment statistics for backoffice
171
func (c *StatisticsController) GetBackofficeAssignmentsStats(ctx *gin.Context) {
1✔
172
        data, err := c.statisticsService.GetBackofficeAssignmentsStats(ctx)
1✔
173
        if err != nil {
1✔
NEW
174
                ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
×
NEW
175
                return
×
NEW
176
        }
×
177

178
        ctx.JSON(http.StatusOK, data)
1✔
179
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc