79 lines
1.8 KiB
Go
79 lines
1.8 KiB
Go
|
|
package handler
|
||
|
|
|
||
|
|
import (
|
||
|
|
"github.com/gin-gonic/gin"
|
||
|
|
|
||
|
|
"with_you/internal/pkg/response"
|
||
|
|
"with_you/internal/repository"
|
||
|
|
"with_you/internal/service"
|
||
|
|
)
|
||
|
|
|
||
|
|
type ExamHandler struct {
|
||
|
|
examSyncService service.ExamSyncService
|
||
|
|
examRepo repository.ExamRepository
|
||
|
|
}
|
||
|
|
|
||
|
|
func NewExamHandler(examSyncService service.ExamSyncService, examRepo repository.ExamRepository) *ExamHandler {
|
||
|
|
return &ExamHandler{
|
||
|
|
examSyncService: examSyncService,
|
||
|
|
examRepo: examRepo,
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
type syncExamsRequest struct {
|
||
|
|
Username string `json:"username" binding:"required"`
|
||
|
|
Password string `json:"password" binding:"required"`
|
||
|
|
Semester string `json:"semester"`
|
||
|
|
}
|
||
|
|
|
||
|
|
func (h *ExamHandler) SyncExams(c *gin.Context) {
|
||
|
|
userID := c.GetString("user_id")
|
||
|
|
if userID == "" {
|
||
|
|
response.Unauthorized(c, "")
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
var req syncExamsRequest
|
||
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
||
|
|
response.BadRequest(c, "invalid request: "+err.Error())
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
result, err := h.examSyncService.SyncUserExams(c.Request.Context(), userID, &service.SyncExamsRequest{
|
||
|
|
Username: req.Username,
|
||
|
|
Password: req.Password,
|
||
|
|
Semester: req.Semester,
|
||
|
|
})
|
||
|
|
if err != nil {
|
||
|
|
response.HandleError(c, err, "failed to sync exams")
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
if !result.Success {
|
||
|
|
response.Success(c, result)
|
||
|
|
return
|
||
|
|
}
|
||
|
|
response.SuccessWithMessage(c, result.Message, result)
|
||
|
|
}
|
||
|
|
|
||
|
|
func (h *ExamHandler) ListExams(c *gin.Context) {
|
||
|
|
userID := c.GetString("user_id")
|
||
|
|
if userID == "" {
|
||
|
|
response.Unauthorized(c, "")
|
||
|
|
return
|
||
|
|
}
|
||
|
|
semester := c.Query("semester")
|
||
|
|
if semester == "" {
|
||
|
|
response.BadRequest(c, "semester is required")
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
exams, err := h.examRepo.ListByUserAndSemester(userID, semester)
|
||
|
|
if err != nil {
|
||
|
|
response.HandleError(c, err, "failed to list exams")
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
response.Success(c, gin.H{"exams": exams})
|
||
|
|
}
|