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

classconnect-grupo3 / courses-service / 15887176432

25 Jun 2025 09:04PM UTC coverage: 76.163% (-10.4%) from 86.577%
15887176432

Pull #45

github

rafaortegano
Format
Pull Request #45: Endpoints with info for backoffice

12 of 629 new or added lines in 8 files covered. (1.91%)

34 existing lines in 2 files now uncovered.

3895 of 5114 relevant lines covered (76.16%)

0.85 hits per line

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

40.5
/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✔
27
                ctx.JSON(http.StatusBadRequest, gin.H{"error": "course_id is required"})
×
28
                return
×
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✔
39
                from, err = time.Parse("2006-01-02", fromStr)
×
40
                if err != nil {
×
41
                        ctx.JSON(http.StatusBadRequest, gin.H{"error": "invalid from date format, use YYYY-MM-DD"})
×
42
                        return
×
43
                }
×
44
        }
45

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

54
        data, _, err := c.statisticsService.ExportCourseStatsCSV(ctx, courseID, from, to)
1✔
55
        if err != nil {
1✔
56
                ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
×
57
                return
×
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✔
67
                ctx.JSON(http.StatusBadRequest, gin.H{"error": "student_id is required"})
×
68
                return
×
69
        }
×
70

71
        courseID := ctx.Query("course_id")
1✔
72
        if courseID == "" {
1✔
73
                ctx.JSON(http.StatusBadRequest, gin.H{"error": "course_id is required"})
×
74
                return
×
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✔
85
                from, err = time.Parse("2006-01-02", fromStr)
×
86
                if err != nil {
×
87
                        ctx.JSON(http.StatusBadRequest, gin.H{"error": "invalid from date format, use YYYY-MM-DD"})
×
88
                        return
×
89
                }
×
90
        }
91

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

100
        data, _, err := c.statisticsService.ExportStudentStatsCSV(ctx, studentID, courseID, from, to)
1✔
101
        if err != nil {
1✔
102
                ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
×
103
                return
×
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✔
113
                ctx.JSON(http.StatusBadRequest, gin.H{"error": "teacher_id is required"})
×
114
                return
×
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✔
124
                from, err = time.Parse("2006-01-02", fromStr)
×
125
                if err != nil {
×
126
                        ctx.JSON(http.StatusBadRequest, gin.H{"error": "invalid from date format, use YYYY-MM-DD"})
×
127
                        return
×
128
                }
×
129
        }
130

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

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

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

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

NEW
156
        ctx.JSON(http.StatusOK, data)
×
157
}
158

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

NEW
167
        ctx.JSON(http.StatusOK, data)
×
168
}
169

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

NEW
178
        ctx.JSON(http.StatusOK, data)
×
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