The Go module name has been changed from `carrot_bbs` to `with_you`, which requires all import paths to be updated throughout the codebase and by external consumers. BREAKING CHANGE: Module name changed from carrot_bbs to with_you. All imports and dependencies must be updated from carrot_bbs/* to with_you/*.
184 lines
4.6 KiB
Go
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)
|
|
}
|