Files
backend/internal/handler/schedule_handler.go
lafay 67a5660952
All checks were successful
Build Backend / build (push) Successful in 3m54s
Build Backend / build-docker (push) Successful in 1m9s
refactor: unify code formatting and improve push/search implementations
- Remove BOM from all Go source files
- Standardize import ordering and whitespace across handlers and services
- Replace PostgreSQL full-text search with ILIKE pattern matching in post and user repositories
- Enhance JPush client with TLS transport, rate limit monitoring, and simplified API
- Refactor PushService to include userID in device management methods
- Add MaxDevicesPerUser limit and extract helper functions for push payload construction
2026-04-28 14:53:04 +08:00

184 lines
4.6 KiB
Go

package handler
import (
"strconv"
"github.com/gin-gonic/gin"
"with_you/internal/pkg/response"
"with_you/internal/service"
)
type ScheduleHandler struct {
scheduleService service.ScheduleService
scheduleSyncService service.ScheduleSyncService
}
func NewScheduleHandler(scheduleService service.ScheduleService, scheduleSyncService service.ScheduleSyncService) *ScheduleHandler {
return &ScheduleHandler{
scheduleService: scheduleService,
scheduleSyncService: scheduleSyncService,
}
}
type createScheduleCourseRequest struct {
Name string `json:"name" binding:"required"`
Teacher string `json:"teacher"`
Location string `json:"location"`
DayOfWeek int `json:"day_of_week" binding:"required"`
StartSection int `json:"start_section" binding:"required"`
EndSection int `json:"end_section" binding:"required"`
Weeks []int `json:"weeks" binding:"required,min=1"`
Color string `json:"color"`
}
type updateScheduleCourseRequest = createScheduleCourseRequest
func (h *ScheduleHandler) ListCourses(c *gin.Context) {
userID := c.GetString("user_id")
if userID == "" {
response.Unauthorized(c, "")
return
}
week := 0
if rawWeek := c.Query("week"); rawWeek != "" {
parsed, err := strconv.Atoi(rawWeek)
if err != nil {
response.BadRequest(c, "invalid week")
return
}
week = parsed
}
list, err := h.scheduleService.ListCourses(userID, week)
if err != nil {
response.HandleError(c, err, "failed to list schedule courses")
return
}
response.Success(c, gin.H{"list": list})
}
func (h *ScheduleHandler) CreateCourse(c *gin.Context) {
userID := c.GetString("user_id")
if userID == "" {
response.Unauthorized(c, "")
return
}
var req createScheduleCourseRequest
if err := c.ShouldBindJSON(&req); err != nil {
response.BadRequest(c, err.Error())
return
}
created, err := h.scheduleService.CreateCourse(userID, service.CreateScheduleCourseInput{
Name: req.Name,
Teacher: req.Teacher,
Location: req.Location,
DayOfWeek: req.DayOfWeek,
StartSection: req.StartSection,
EndSection: req.EndSection,
Weeks: req.Weeks,
Color: req.Color,
})
if err != nil {
response.HandleError(c, err, "failed to create schedule course")
return
}
response.SuccessWithMessage(c, "course created", gin.H{"course": created})
}
func (h *ScheduleHandler) UpdateCourse(c *gin.Context) {
userID := c.GetString("user_id")
if userID == "" {
response.Unauthorized(c, "")
return
}
courseID := c.Param("id")
if courseID == "" {
response.BadRequest(c, "invalid course id")
return
}
var req updateScheduleCourseRequest
if err := c.ShouldBindJSON(&req); err != nil {
response.BadRequest(c, err.Error())
return
}
updated, err := h.scheduleService.UpdateCourse(userID, courseID, service.CreateScheduleCourseInput{
Name: req.Name,
Teacher: req.Teacher,
Location: req.Location,
DayOfWeek: req.DayOfWeek,
StartSection: req.StartSection,
EndSection: req.EndSection,
Weeks: req.Weeks,
Color: req.Color,
})
if err != nil {
response.HandleError(c, err, "failed to update schedule course")
return
}
response.SuccessWithMessage(c, "course updated", gin.H{"course": updated})
}
func (h *ScheduleHandler) DeleteCourse(c *gin.Context) {
userID := c.GetString("user_id")
if userID == "" {
response.Unauthorized(c, "")
return
}
courseID := c.Param("id")
if courseID == "" {
response.BadRequest(c, "invalid course id")
return
}
if err := h.scheduleService.DeleteCourse(userID, courseID); err != nil {
response.HandleError(c, err, "failed to delete schedule course")
return
}
response.SuccessWithMessage(c, "course deleted", nil)
}
// syncScheduleRequest 同步课表请求
type syncScheduleRequest struct {
Username string `json:"username" binding:"required"`
Password string `json:"password" binding:"required"`
Semester string `json:"semester"`
}
// SyncSchedule 同步课表接口
func (h *ScheduleHandler) SyncSchedule(c *gin.Context) {
userID := c.GetString("user_id")
if userID == "" {
response.Unauthorized(c, "")
return
}
var req syncScheduleRequest
if err := c.ShouldBindJSON(&req); err != nil {
response.BadRequest(c, "invalid request: "+err.Error())
return
}
result, err := h.scheduleSyncService.SyncUserSchedule(c.Request.Context(), userID, &service.SyncScheduleRequest{
Username: req.Username,
Password: req.Password,
Semester: req.Semester,
})
if err != nil {
response.HandleError(c, err, "failed to sync schedule")
return
}
if !result.Success {
response.Success(c, result)
return
}
response.SuccessWithMessage(c, result.Message, result)
}