100 lines
3.2 KiB
Go
100 lines
3.2 KiB
Go
package handler
|
|
|
|
import (
|
|
"StuAcaWorksAI/proto"
|
|
"StuAcaWorksAI/service"
|
|
"github.com/gin-gonic/gin"
|
|
"net/http"
|
|
)
|
|
|
|
func SetUpModelGroup(router *gin.Engine) {
|
|
modelGroup := router.Group("/model")
|
|
modelGroup.POST("/create", CreateModel)
|
|
modelGroup.POST("/find", FindModel)
|
|
modelGroup.POST("/delete", DeleteModel)
|
|
modelGroup.POST("/update", UpdateModel)
|
|
}
|
|
|
|
type Model struct {
|
|
ID int `json:"id" form:"id"`
|
|
UserID int `json:"user_id" form:"user_id"`
|
|
Type string `json:"type" form:"type"`
|
|
Url string `json:"url" form:"url"`
|
|
Parameter string `json:"parameter" form:"parameter"`
|
|
Description string `json:"description" form:"description"`
|
|
}
|
|
|
|
func CreateModel(c *gin.Context) {
|
|
id, _ := c.Get("id")
|
|
userID := int(id.(float64))
|
|
var req Model
|
|
if err := c.ShouldBind(&req); err == nil {
|
|
// 创建模型信息
|
|
err2, mid := service.CreateModel(uint(userID), req.Type, req.Url, req.Parameter, req.Description)
|
|
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.ModelCreateFailed, "message": "failed"})
|
|
}
|
|
} else {
|
|
c.JSON(http.StatusOK, gin.H{"error": err.Error(), "code": proto.ParameterError, "message": "failed"})
|
|
}
|
|
}
|
|
|
|
func FindModel(c *gin.Context) {
|
|
id, _ := c.Get("id")
|
|
userID := int(id.(float64))
|
|
var req Model
|
|
if err := c.ShouldBind(&req); err == nil {
|
|
if req.Type == "ID" {
|
|
// 根据id查找模型
|
|
model := service.FindModelByID(req.ID, userID)
|
|
c.JSON(http.StatusOK, gin.H{"code": proto.SuccessCode, "message": "success", "data": model})
|
|
} else if req.Type == "UserID" {
|
|
// 根据用户id查找模型
|
|
models := service.FindModelByUserID(userID)
|
|
c.JSON(http.StatusOK, gin.H{"code": proto.SuccessCode, "message": "success", "data": models})
|
|
} else {
|
|
c.JSON(http.StatusOK, gin.H{"error": "type error", "code": proto.ModelSearchFailed, "message": "failed"})
|
|
}
|
|
} else {
|
|
c.JSON(http.StatusOK, gin.H{"error": err.Error(), "code": proto.ParameterError, "message": "failed"})
|
|
}
|
|
}
|
|
|
|
// 修改
|
|
func UpdateModel(c *gin.Context) {
|
|
id, _ := c.Get("id")
|
|
userID := int(id.(float64))
|
|
var req Model
|
|
if err := c.ShouldBind(&req); err == nil {
|
|
// 更新模型信息
|
|
err2 := service.UpdateModelByID(req.ID, uint(userID), req.Type, req.Url, req.Parameter, req.Description)
|
|
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.ModelUpdateFailed, "message": "failed"})
|
|
}
|
|
} else {
|
|
c.JSON(http.StatusOK, gin.H{"error": err.Error(), "code": proto.ParameterError, "message": "failed"})
|
|
}
|
|
}
|
|
|
|
// 删除
|
|
func DeleteModel(c *gin.Context) {
|
|
id, _ := c.Get("id")
|
|
userID := int(id.(float64))
|
|
var req Model
|
|
if err := c.ShouldBind(&req); err == nil {
|
|
// 删除模型
|
|
err2 := service.DeleteModelByID(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.ModelDeleteFailed, "message": "failed"})
|
|
}
|
|
} else {
|
|
c.JSON(http.StatusOK, gin.H{"error": err.Error(), "code": proto.ParameterError, "message": "failed"})
|
|
}
|
|
}
|