saw-go/handler/func_model.go

106 lines
3.5 KiB
Go

package handler
import (
"StuAcaWorksAI/proto"
"StuAcaWorksAI/service"
"github.com/gin-gonic/gin"
"net/http"
)
func SetUpFuncModelGroup(router *gin.Engine) {
funcGroup := router.Group("/func")
funcGroup.POST("/create", CreateFuncModel)
funcGroup.POST("/find", FindFuncModel)
funcGroup.POST("/delete", DeleteFuncModel)
funcGroup.POST("/update", UpdateFuncModel)
}
type FuncModelReq struct {
ID int `json:"id" form:"id"`
ModelID int `json:"model_id" form:"model_id"`
Function string `json:"function" form:"function"`
Name string `json:"name" form:"name"`
Info string `json:"info" form:"info"`
}
func CreateFuncModel(c *gin.Context) {
id, _ := c.Get("id")
userID := int(id.(float64))
var req FuncModelReq
if err := c.ShouldBind(&req); err == nil {
// 创建功能模型
err2, mid := service.CreateFuncModel(int(uint(userID)), req.ModelID, req.Name, req.Info, req.Function)
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.FuncModelCreateFailed, "message": "failed"})
}
} else {
c.JSON(http.StatusOK, gin.H{"error": err.Error(), "code": proto.ParameterError, "message": "failed"})
}
}
func FindFuncModel(c *gin.Context) {
id, _ := c.Get("id")
userID := int(id.(float64))
var req FuncModelReq
if err := c.ShouldBind(&req); err == nil {
// 查找功能模型
if req.ID != 0 {
funcModels, err2 := service.FindFuncModelByID(req.ID, userID)
if err2 != nil {
c.JSON(http.StatusOK, gin.H{"error": err2.Error(), "code": proto.FuncModelSearchFailed, "message": "failed"})
return
}
c.JSON(http.StatusOK, gin.H{"code": proto.SuccessCode, "message": "success", "data": funcModels})
} else if req.ModelID != 0 {
funcModels, err3 := service.FindFuncModelByUserID(userID)
if err3 != nil {
c.JSON(http.StatusOK, gin.H{"error": err3.Error(), "code": proto.FuncModelSearchFailed, "message": "failed"})
return
}
c.JSON(http.StatusOK, gin.H{"code": proto.SuccessCode, "message": "success", "data": funcModels})
} else {
c.JSON(http.StatusOK, gin.H{"error": "type error", "code": proto.FuncModelSearchFailed, "message": "failed"})
}
} else {
c.JSON(http.StatusOK, gin.H{"error": err.Error(), "code": proto.ParameterError, "message": "failed"})
}
}
// 修改
func UpdateFuncModel(c *gin.Context) {
id, _ := c.Get("id")
userID := int(id.(float64))
var req FuncModelReq
if err := c.ShouldBind(&req); err == nil {
// 修改功能模型
err2 := service.UpdateFuncModelByID(req.ID, uint(userID), uint(req.ModelID), req.Name, req.Info, req.Function)
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.FuncModelUpdateFailed, "message": "failed"})
}
} else {
c.JSON(http.StatusOK, gin.H{"error": err.Error(), "code": proto.ParameterError, "message": "failed"})
}
}
// 删除
func DeleteFuncModel(c *gin.Context) {
id, _ := c.Get("id")
userID := int(id.(float64))
var req FuncModelReq
if err := c.ShouldBind(&req); err == nil {
// 删除功能模型
err2 := service.DeleteFuncModelByID(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.FuncModelDeleteFailed, "message": "failed"})
}
} else {
c.JSON(http.StatusOK, gin.H{"error": err.Error(), "code": proto.ParameterError, "message": "failed"})
}
}