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

classconnect-grupo3 / courses-service / 15687239309

16 Jun 2025 05:07PM UTC coverage: 88.681% (-0.1%) from 88.829%
15687239309

push

github

rovifran
fixed tests after refactor of delete urls

3173 of 3578 relevant lines covered (88.68%)

1.0 hits per line

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

82.7
/src/controller/enrollment_controller.go
1
package controller
2

3
import (
4
        "courses-service/src/ai"
5
        "courses-service/src/model"
6
        "courses-service/src/schemas"
7
        "courses-service/src/service"
8
        "log/slog"
9
        "net/http"
10
        "slices"
11

12
        "github.com/gin-gonic/gin"
13
)
14

15
type EnrollmentController struct {
16
        enrollmentService service.EnrollmentServiceInterface
17
        aiClient          *ai.AiClient
18
}
19

20
func NewEnrollmentController(enrollmentService service.EnrollmentServiceInterface, aiClient *ai.AiClient) *EnrollmentController {
1✔
21
        return &EnrollmentController{enrollmentService: enrollmentService, aiClient: aiClient}
1✔
22
}
1✔
23

24
// @Summary Enroll a student in a course
25
// @Description Enroll a student in a course
26
// @Tags enrollments
27
// @Accept json
28
// @Produce json
29
// @Param id path string true "Course ID"
30
// @Param enrollmentRequest body schemas.EnrollStudentRequest true "Enrollment request"
31
// @Router /courses/{id}/enroll [post]
32
func (c *EnrollmentController) EnrollStudent(ctx *gin.Context) {
1✔
33
        slog.Debug("Enrolling student", "studentId", ctx.Param("studentId"), "courseId", ctx.Param("id"))
1✔
34
        courseID := ctx.Param("id")
1✔
35

1✔
36
        if courseID == "" {
2✔
37
                slog.Error("Invalid course ID", "courseId", courseID)
1✔
38
                ctx.JSON(http.StatusBadRequest, gin.H{"error": "Invalid course ID"})
1✔
39
                return
1✔
40
        }
1✔
41

42
        var enrollmentRequest schemas.EnrollStudentRequest
1✔
43
        if err := ctx.ShouldBindJSON(&enrollmentRequest); err != nil {
2✔
44
                slog.Error("Error binding enrollment request", "error", err)
1✔
45
                ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
1✔
46
                return
1✔
47
        }
1✔
48

49
        err := c.enrollmentService.EnrollStudent(enrollmentRequest.StudentID, courseID)
1✔
50
        if err != nil {
2✔
51
                slog.Error("Error enrolling student", "error", err)
1✔
52
                ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
1✔
53
                return
1✔
54
        }
1✔
55

56
        slog.Debug("Student enrolled in course", "studentId", enrollmentRequest.StudentID, "courseId", courseID)
1✔
57
        ctx.JSON(http.StatusCreated, gin.H{"message": "Student successfully enrolled in course"})
1✔
58
}
59

60
// @Summary Unenroll a student from a course
61
// @Description Unenroll a student from a course
62
// @Tags enrollments
63
// @Accept json
64
// @Produce json
65
// @Param id path string true "Course ID"
66
// @Param studentId query string true "Student ID"
67
// @Success 200 {object} schemas.UnenrollStudentResponse
68
// @Router /courses/{id}/unenroll [delete]
69
func (c *EnrollmentController) UnenrollStudent(ctx *gin.Context) {
1✔
70
        slog.Debug("Unenrolling student", "studentId", ctx.Param("studentId"), "courseId", ctx.Param("id"))
1✔
71
        courseID := ctx.Param("id")
1✔
72

1✔
73
        if courseID == "" {
2✔
74
                slog.Error("Invalid student ID or course ID")
1✔
75
                ctx.JSON(http.StatusBadRequest, gin.H{"error": "Invalid student ID or course ID"})
1✔
76
                return
1✔
77
        }
1✔
78

79
        studentId := ctx.Query("studentId")
1✔
80
        if studentId == "" {
2✔
81
                slog.Error("Student ID is required")
1✔
82
                ctx.JSON(http.StatusBadRequest, gin.H{"error": "Student ID is required"})
1✔
83
                return
1✔
84
        }
1✔
85

86
        err := c.enrollmentService.UnenrollStudent(studentId, courseID)
1✔
87
        if err != nil {
2✔
88
                slog.Error("Error unenrolling student", "error", err)
1✔
89
                ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
1✔
90
                return
1✔
91
        }
1✔
92

93
        slog.Debug("Student unenrolled from course", "studentId", studentId, "courseId", courseID)
1✔
94
        ctx.JSON(http.StatusOK, gin.H{"message": "Student successfully unenrolled from course"})
1✔
95
}
96

97
// @Summary Get enrollments by course ID
98
// @Description Get enrollments by course ID
99
// @Tags enrollments
100
// @Accept json
101
// @Produce json
102
// @Param id path string true "Course ID"
103
// @Success 200 {array} model.Enrollment
104
// @Router /courses/{id}/enrollments [get]
105
func (c *EnrollmentController) GetEnrollmentsByCourseId(ctx *gin.Context) {
1✔
106
        slog.Debug("Getting enrollments by course ID", "courseId", ctx.Param("id"))
1✔
107
        courseID := ctx.Param("id")
1✔
108

1✔
109
        enrollments, err := c.enrollmentService.GetEnrollmentsByCourseId(courseID)
1✔
110
        if err != nil {
2✔
111
                slog.Error("Error getting enrollments by course ID", "error", err)
1✔
112
                ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
1✔
113
                return
1✔
114
        }
1✔
115

116
        ctx.JSON(http.StatusOK, enrollments)
1✔
117
}
118

119
// @Summary Set a course as favourite
120
// @Description Set a course as favourite
121
// @Tags enrollments
122
// @Accept json
123
// @Produce json
124
// @Param id path string true "Course ID"
125
// @Param favouriteCourseRequest body schemas.SetFavouriteCourseRequest true "Favourite course request"
126
// @Success 200 {object} schemas.SetFavouriteCourseResponse
127
// @Router /courses/{id}/favourite [post]
128
func (c *EnrollmentController) SetFavouriteCourse(ctx *gin.Context) {
1✔
129
        slog.Debug("Setting favourite course", "courseId", ctx.Param("id"))
1✔
130
        courseID := ctx.Param("id")
1✔
131

1✔
132
        if courseID == "" {
2✔
133
                slog.Error("Invalid course ID")
1✔
134
                ctx.JSON(http.StatusBadRequest, gin.H{"error": "Invalid course ID"})
1✔
135
                return
1✔
136
        }
1✔
137

138
        var favouriteCourseRequest schemas.SetFavouriteCourseRequest
1✔
139
        if err := ctx.ShouldBindJSON(&favouriteCourseRequest); err != nil {
2✔
140
                slog.Error("Error binding favourite course request", "error", err)
1✔
141
                ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
1✔
142
                return
1✔
143
        }
1✔
144

145
        err := c.enrollmentService.SetFavouriteCourse(favouriteCourseRequest.StudentID, courseID)
1✔
146
        if err != nil {
2✔
147
                slog.Error("Error setting favourite course", "error", err)
1✔
148
                ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
1✔
149
                return
1✔
150
        }
1✔
151

152
        slog.Debug("Favourite course set", "studentId", favouriteCourseRequest.StudentID, "courseId", courseID)
1✔
153
        ctx.JSON(http.StatusOK, gin.H{"message": "Favourite course set"})
1✔
154
}
155

156
// @Summary Unset a course as favourite
157
// @Description Unset a course as favourite
158
// @Tags enrollments
159
// @Accept json
160
// @Produce json
161
// @Param id path string true "Course ID"
162
// @Param studentId query string true "Student ID"
163
// @Success 200 {object} schemas.UnsetFavouriteCourseResponse
164
// @Router /courses/{id}/favourite [delete]
165
func (c *EnrollmentController) UnsetFavouriteCourse(ctx *gin.Context) {
1✔
166
        slog.Debug("Unsetting favourite course", "courseId", ctx.Param("id"))
1✔
167
        courseID := ctx.Param("id")
1✔
168

1✔
169
        if courseID == "" {
2✔
170
                slog.Error("Invalid course ID")
1✔
171
                ctx.JSON(http.StatusBadRequest, gin.H{"error": "Invalid course ID"})
1✔
172
                return
1✔
173
        }
1✔
174

175
        studentId := ctx.Query("studentId")
1✔
176
        if studentId == "" {
2✔
177
                slog.Error("Student ID is required")
1✔
178
                ctx.JSON(http.StatusBadRequest, gin.H{"error": "Student ID is required"})
1✔
179
                return
1✔
180
        }
1✔
181
        err := c.enrollmentService.UnsetFavouriteCourse(studentId, courseID)
1✔
182
        if err != nil {
2✔
183
                slog.Error("Error unsetting favourite course", "error", err)
1✔
184
                ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
1✔
185
                return
1✔
186
        }
1✔
187

188
        slog.Debug("Favourite course unset", "studentId", studentId, "courseId", courseID)
1✔
189
        ctx.JSON(http.StatusOK, gin.H{"message": "Favourite course unset"})
1✔
190
}
191

192
// @Summary Create a feedback for a course
193
// @Description Create a feedback for a course
194
// @Tags enrollments
195
// @Accept json
196
// @Produce json
197
// @Param id path string true "Course ID"
198
// @Param feedbackRequest body schemas.CreateStudentFeedbackRequest true "Feedback request"
199
// @Success 200 {object} model.StudentFeedback
200
// @Router /courses/{id}/student-feedback [post]
201
func (c *EnrollmentController) CreateFeedback(ctx *gin.Context) {
1✔
202
        slog.Debug("Creating feedback", "courseId", ctx.Param("id"))
1✔
203
        courseID := ctx.Param("id")
1✔
204

1✔
205
        if courseID == "" {
2✔
206
                slog.Error("Invalid course ID")
1✔
207
                ctx.JSON(http.StatusBadRequest, gin.H{"error": "Invalid course ID"})
1✔
208
                return
1✔
209
        }
1✔
210

211
        var feedbackRequest schemas.CreateStudentFeedbackRequest
1✔
212
        if err := ctx.ShouldBindJSON(&feedbackRequest); err != nil {
2✔
213
                slog.Error("Error binding feedback request", "error", err)
1✔
214
                ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
1✔
215
                return
1✔
216
        }
1✔
217

218
        if !slices.Contains(model.FeedbackTypes, feedbackRequest.FeedbackType) {
2✔
219
                slog.Error("Invalid feedback type")
1✔
220
                ctx.JSON(http.StatusBadRequest, gin.H{"error": "Invalid feedback type"})
1✔
221
                return
1✔
222
        }
1✔
223

224
        err := c.enrollmentService.CreateStudentFeedback(feedbackRequest)
1✔
225
        if err != nil {
2✔
226
                slog.Error("Error creating feedback", "error", err)
1✔
227
                ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
1✔
228
                return
1✔
229
        }
1✔
230

231
        slog.Debug("Feedback created", "studentId", feedbackRequest.StudentUUID, "teacherId", feedbackRequest.TeacherUUID)
1✔
232
        ctx.JSON(http.StatusOK, gin.H{"message": "Feedback created"})
1✔
233
}
234

235
// @Summary Get feedback by student ID
236
// @Description Get feedback by student ID
237
// @Tags enrollments
238
// @Accept json
239
// @Produce json
240
// @Param id path string true "Student ID"
241
// @Param getFeedbackByStudentIdRequest body schemas.GetFeedbackByStudentIdRequest true "Get feedback by student ID request"
242
// @Success 200 {array} model.StudentFeedback
243
// @Router /feedback/student/{id} [put]
244
func (c *EnrollmentController) GetFeedbackByStudentId(ctx *gin.Context) {
1✔
245
        slog.Debug("Getting feedback by student ID", "studentId", ctx.Param("id"))
1✔
246
        studentID := ctx.Param("id")
1✔
247

1✔
248
        if studentID == "" {
1✔
249
                slog.Error("Invalid student ID")
×
250
                ctx.JSON(http.StatusBadRequest, gin.H{"error": "Invalid student ID"})
×
251
                return
×
252
        }
×
253

254
        var getFeedbackByStudentIdRequest schemas.GetFeedbackByStudentIdRequest
1✔
255
        if err := ctx.ShouldBindJSON(&getFeedbackByStudentIdRequest); err != nil {
2✔
256
                slog.Error("Error binding get feedback by student ID request", "error", err)
1✔
257
                ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
1✔
258
                return
1✔
259
        }
1✔
260

261
        feedback, err := c.enrollmentService.GetFeedbackByStudentId(studentID, getFeedbackByStudentIdRequest)
1✔
262
        if err != nil {
2✔
263
                slog.Error("Error getting feedback by student ID", "error", err)
1✔
264
                ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
1✔
265
                return
1✔
266
        }
1✔
267

268
        slog.Debug("Feedback retrieved", "studentId", studentID)
1✔
269
        ctx.JSON(http.StatusOK, feedback)
1✔
270
}
271

272
// @Summary Get student feedback summary
273
// @Description Get student feedback summary by student ID
274
// @Tags enrollments
275
// @Accept json
276
// @Produce json
277
// @Param id path string true "Student ID"
278
// @Success 200 {string} string "Student feedback summary"
279
// @Router /feedback/student/{id}/summary [get]
280
func (c *EnrollmentController) GetStudentFeedbackSummary(ctx *gin.Context) {
×
281
        slog.Debug("Getting student feedback summary", "studentId", ctx.Param("id"))
×
282
        studentID := ctx.Param("id")
×
283

×
284
        if studentID == "" {
×
285
                slog.Error("Invalid student ID")
×
286
                ctx.JSON(http.StatusBadRequest, gin.H{"error": "Invalid student ID"})
×
287
                return
×
288
        }
×
289

290
        feedbacks, err := c.enrollmentService.GetFeedbackByStudentId(studentID, schemas.GetFeedbackByStudentIdRequest{})
×
291
        if err != nil {
×
292
                slog.Error("Error getting student feedback", "error", err)
×
293
                ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
×
294
                return
×
295
        }
×
296

297
        if len(feedbacks) == 0 {
×
298
                slog.Error("No feedbacks found")
×
299
                ctx.JSON(http.StatusNotFound, gin.H{"error": "No feedbacks found"})
×
300
                return
×
301
        }
×
302

303
        summary, err := c.aiClient.SummarizeStudentFeedbacks(feedbacks)
×
304
        if err != nil {
×
305
                slog.Error("Error summarizing student feedback", "error", err)
×
306
                ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
×
307
                return
×
308
        }
×
309

310
        slog.Debug("Student feedback summary retrieved", "summary", summary)
×
311
        ctx.JSON(http.StatusOK, summary)
×
312
}
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