package handler import ( "StuAcaWorksAI/proto" "StuAcaWorksAI/service" "github.com/gin-gonic/gin" "net/http" ) func SetUpSessionGroup(router *gin.Engine) { sessionGroup := router.Group("/session") sessionGroup.POST("/create", CreateSession) sessionGroup.POST("/find", FindSession) sessionGroup.POST("/delete", DeleteSession) sessionGroup.POST("/update", UpdateSession) } type Session struct { ID int `json:"id" form:"id"` UserID int `json:"user_id" form:"user_id"` Name string `json:"name" form:"name"` Type string `json:"type" form:"type"` } func CreateSession(c *gin.Context) { id, _ := c.Get("id") userID := int(id.(float64)) var req Session if err := c.ShouldBind(&req); err == nil { // 创建会话 err2, mid := service.CreateSession(int(uint(userID)), req.Name) if err2 == nil { c.JSON(http.StatusOK, gin.H{"code": proto.SuccessCode, "message": "success", "data": mid}) } else { c.JSON(http.StatusOK, gin.H{"error": err2.Error(), "code": proto.SessionCreateFailed, "message": "failed"}) } } else { c.JSON(http.StatusOK, gin.H{"error": err.Error(), "code": proto.ParameterError, "message": "failed"}) } } func FindSession(c *gin.Context) { id, _ := c.Get("id") userID := int(id.(float64)) var req Session if err := c.ShouldBind(&req); err == nil { // 查找会话 if req.Type == "ID" { err2, session := service.FindSessionByID(req.ID, userID) if err2 != nil { c.JSON(http.StatusOK, gin.H{"error": err2.Error(), "code": proto.SessionSearchFailed, "message": "failed"}) return } c.JSON(http.StatusOK, gin.H{"code": proto.SuccessCode, "message": "success", "data": session}) } else if req.Type == "UserID" { sessions := service.FindSessionByUserID(userID) c.JSON(http.StatusOK, gin.H{"code": proto.SuccessCode, "message": "success", "data": sessions}) } else { c.JSON(http.StatusOK, gin.H{"error": "type error", "code": proto.SessionSearchFailed, "message": "failed"}) } } else { c.JSON(http.StatusOK, gin.H{"error": err.Error(), "code": proto.ParameterError, "message": "failed"}) } } // 修改 func UpdateSession(c *gin.Context) { id, _ := c.Get("id") userID := int(id.(float64)) var req Session if err := c.ShouldBind(&req); err == nil { // 修改会话 err2 := service.UpdateSessionByID(req.ID, userID, req.Name) if err2 == nil { c.JSON(http.StatusOK, gin.H{"code": proto.SuccessCode, "message": "success"}) } else { c.JSON(http.StatusOK, gin.H{"error": err2.Error(), "code": proto.SessionUpdateFailed, "message": "failed"}) } } else { c.JSON(http.StatusOK, gin.H{"error": err.Error(), "code": proto.ParameterError, "message": "failed"}) } } // 删除 func DeleteSession(c *gin.Context) { id, _ := c.Get("id") userID := int(id.(float64)) var req Session if err := c.ShouldBind(&req); err == nil { // 删除会话 err2 := service.DeleteSessionByID(req.ID, userID) if err2 == nil { c.JSON(http.StatusOK, gin.H{"code": proto.SuccessCode, "message": "success"}) } else { c.JSON(http.StatusOK, gin.H{"error": err2.Error(), "code": proto.SessionDeleteFailed, "message": "failed"}) } } else { c.JSON(http.StatusOK, gin.H{"error": err.Error(), "code": proto.ParameterError, "message": "failed"}) } }