saw-go/handler/session.go

102 lines
3.3 KiB
Go
Raw Permalink Normal View History

2025-03-21 16:20:24 +08:00
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 {
2025-04-03 19:16:14 +08:00
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"`
SessionType int `json:"session_type" form:"session_type"`
2025-03-21 16:20:24 +08:00
}
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)), proto.SessionTypeUserWithModelGeneration, req.Name)
2025-03-21 16:20:24 +08:00
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" {
2025-04-03 19:16:14 +08:00
err2, session := service.FindSessionByID(req.ID, userID, req.SessionType)
2025-03-21 16:20:24 +08:00
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" {
2025-04-03 19:16:14 +08:00
sessions := service.FindSessionByUserID(userID, req.SessionType)
2025-03-21 16:20:24 +08:00
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 {
// 修改会话
2025-03-23 13:17:03 +08:00
err2 := service.UpdateSessionByID(req.ID, userID, req.Name, nil)
2025-03-21 16:20:24 +08:00
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"})
}
}