diff --git a/dao/im.go b/dao/im.go index 663e4f9..e2b51fa 100644 --- a/dao/im.go +++ b/dao/im.go @@ -65,7 +65,7 @@ func UpdateSessionByID(id int, userId int, name string) error { } // 删除会话 -func DeleteSessionByID(id int, userId int) error { +func DeleteSessionByID(id int) error { var res *gorm.DB if proto.Config.SERVER_SQL_LOG { res = DB.Debug().Delete(&Session{}, id) diff --git a/dao/model.go b/dao/model.go index ef680e7..21b9af9 100644 --- a/dao/model.go +++ b/dao/model.go @@ -7,15 +7,16 @@ import ( type Model struct { gorm.Model - Type string `gorm:"column:type"` //模型类型 - UserID uint `gorm:"column:user_id"` //用户id - Url string `gorm:"column:url"` //模型地址 - Parameter string `gorm:"column:parameter"` //模型参数,存储json + Type string `gorm:"column:type"` //模型类型 + UserID uint `gorm:"column:user_id"` //用户id + Url string `gorm:"column:url"` //模型地址 + Parameter string `gorm:"column:parameter"` //模型参数,存储json + Description string `gorm:"column:description"` //模型描述 } // 创建模型 -func CreateModel(userID uint, modelType, url, parameter string) (error, uint) { - model := Model{UserID: userID, Type: modelType, Url: url, Parameter: parameter} +func CreateModel(userID uint, modelType, url, parameter, description string) (error, uint) { + model := Model{UserID: userID, Type: modelType, Url: url, Parameter: parameter, Description: description} var res *gorm.DB if proto.Config.SERVER_SQL_LOG { res = DB.Debug().Create(&model) @@ -59,7 +60,7 @@ func DeleteModelByID(id int) error { } // 根据id更新模型 -func UpdateModelByID(id int, userID uint, modelType, url, parameter string) error { +func UpdateModelByID(id int, userID uint, modelType, url, parameter, description string) error { model := Model{UserID: userID, Type: modelType, Url: url, Parameter: parameter} var res *gorm.DB if proto.Config.SERVER_SQL_LOG { diff --git a/handler/model.go b/handler/model.go index 8b5156f..547cf43 100644 --- a/handler/model.go +++ b/handler/model.go @@ -11,16 +11,17 @@ func SetUpModelGroup(router *gin.Engine) { modelGroup := router.Group("/model") modelGroup.POST("/create", CreateModel) modelGroup.POST("/find", FindModel) - modelGroup.DELETE("/delete", DeleteModel) - modelGroup.PUT("/update", UpdateModel) + 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"` + 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) { @@ -29,7 +30,7 @@ func CreateModel(c *gin.Context) { var req Model if err := c.ShouldBind(&req); err == nil { // 创建模型信息 - err2, mid := service.CreateModel(uint(userID), req.Type, req.Url, req.Parameter) + 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 { @@ -68,7 +69,7 @@ func UpdateModel(c *gin.Context) { var req Model if err := c.ShouldBind(&req); err == nil { // 更新模型信息 - err2 := service.UpdateModelByID(req.ID, uint(userID), req.Type, req.Url, req.Parameter) + 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 { diff --git a/handler/session.go b/handler/session.go new file mode 100644 index 0000000..769fe8d --- /dev/null +++ b/handler/session.go @@ -0,0 +1,100 @@ +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"}) + } +} diff --git a/proto/status.go b/proto/status.go index 8af508b..4ac4c90 100644 --- a/proto/status.go +++ b/proto/status.go @@ -78,4 +78,9 @@ const ( ModelUpdateFailed = 111 // 更新模型失败 ModelDeleteFailed = 112 // 删除模型失败 ModelSearchFailed = 113 // 获取模型失败 + + SessionSearchFailed = 120 // 获取会话失败 + SessionCreateFailed = 121 // 创建会话失败 + SessionDeleteFailed = 122 // 删除会话失败 + SessionUpdateFailed = 123 // 更新会话失败 ) diff --git a/service/imService.go b/service/imService.go index bebb99e..1015ada 100644 --- a/service/imService.go +++ b/service/imService.go @@ -16,6 +16,24 @@ func FindSessionByUserID(userID int) []dao.Session { sessions := dao.FindSessionByUserID(userID) return sessions } +func FindSessionByID(id, userID int) (error, []dao.Session) { + session := dao.FindSessionByID(id) + var res []dao.Session + if session.UserID != userID { + return errors.New("session not belong to user"), res + } + res = append(res, session) + return nil, res +} + +func DeleteSessionByID(id, userID int) error { + session := dao.FindSessionByID(id) + if session.UserID != userID { + return errors.New("session not belong to user") + } + err := dao.DeleteSessionByID(id) + return err +} // 更新会话名字 func UpdateSessionByID(id int, userId int, name string) error { diff --git a/service/modelService.go b/service/modelService.go index 72af7bf..b52966e 100644 --- a/service/modelService.go +++ b/service/modelService.go @@ -6,7 +6,7 @@ import ( ) // 创建模型信息 -func CreateModel(userID uint, modelType, url, parameter string) (error, uint) { +func CreateModel(userID uint, modelType, url, parameter, description string) (error, uint) { //查看用户是否有权限创建模型 user := GetUserByIDWithCache(int(userID)) if user.ID == 0 { @@ -15,7 +15,7 @@ func CreateModel(userID uint, modelType, url, parameter string) (error, uint) { if user.Role != "admin" { return errors.New("user not admin,no permission"), 0 } - return dao.CreateModel(userID, modelType, url, parameter) + return dao.CreateModel(userID, modelType, url, parameter, description) } // 根据id查找模型 @@ -43,7 +43,7 @@ func DeleteModelByID(id, userID int) error { } // 更新模型信息 -func UpdateModelByID(id int, userID uint, modelType, url, parameter string) error { +func UpdateModelByID(id int, userID uint, modelType, url, parameter, description string) error { - return dao.UpdateModelByID(id, userID, modelType, url, parameter) + return dao.UpdateModelByID(id, userID, modelType, url, parameter, description) }