:refactor: 更新机器人模型,添加用户ID字段并优化数据库查询逻辑
All checks were successful
BuildImage / build-image (push) Successful in 2m8s
All checks were successful
BuildImage / build-image (push) Successful in 2m8s
This commit is contained in:
parent
c8427c851d
commit
3be5e65a88
@ -83,6 +83,7 @@ func CreateRobot(c *fiber.Ctx) error {
|
|||||||
|
|
||||||
// 创建机器人数据库记录
|
// 创建机器人数据库记录
|
||||||
robot := model.Robot{
|
robot := model.Robot{
|
||||||
|
UserId: c.Locals("userId").(string),
|
||||||
ContainerID: containerID,
|
ContainerID: containerID,
|
||||||
ContainerHost: containerHost, // 使用新的字段名
|
ContainerHost: containerHost, // 使用新的字段名
|
||||||
Nickname: robotName,
|
Nickname: robotName,
|
||||||
@ -111,8 +112,8 @@ func ShowRobot(c *fiber.Ctx) error {
|
|||||||
var robot model.Robot
|
var robot model.Robot
|
||||||
db := model.GetDB()
|
db := model.GetDB()
|
||||||
|
|
||||||
if err := db.First(&robot, id).Error; err != nil {
|
if err = db.First(&robot, id).Error; err != nil {
|
||||||
if err == gorm.ErrRecordNotFound {
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
return fiber.NewError(fiber.StatusNotFound, "机器人不存在")
|
return fiber.NewError(fiber.StatusNotFound, "机器人不存在")
|
||||||
}
|
}
|
||||||
return fiber.NewError(fiber.StatusInternalServerError, "查询数据库失败")
|
return fiber.NewError(fiber.StatusInternalServerError, "查询数据库失败")
|
||||||
@ -141,7 +142,7 @@ func DeleteRobot(c *fiber.Ctx) error {
|
|||||||
var robot model.Robot
|
var robot model.Robot
|
||||||
db := model.GetDB()
|
db := model.GetDB()
|
||||||
|
|
||||||
if err = db.First(&robot, id).Error; err != nil {
|
if err = db.Where("user_id = ?", c.Locals("userId")).First(&robot, id).Error; err != nil {
|
||||||
// 针对API请求返回JSON
|
// 针对API请求返回JSON
|
||||||
if strings.HasPrefix(c.Get("Accept"), "application/json") || c.Method() == "DELETE" {
|
if strings.HasPrefix(c.Get("Accept"), "application/json") || c.Method() == "DELETE" {
|
||||||
return c.Status(fiber.StatusNotFound).JSON(fiber.Map{
|
return c.Status(fiber.StatusNotFound).JSON(fiber.Map{
|
||||||
@ -210,7 +211,7 @@ func RobotLogin(c *fiber.Ctx) error {
|
|||||||
|
|
||||||
var robot model.Robot
|
var robot model.Robot
|
||||||
db := model.GetDB()
|
db := model.GetDB()
|
||||||
if err = db.First(&robot, id).Error; err != nil {
|
if err = db.Where("user_id = ?", c.Locals("userId")).First(&robot, id).Error; err != nil {
|
||||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
return fiber.NewError(fiber.StatusNotFound, "机器人不存在")
|
return fiber.NewError(fiber.StatusNotFound, "机器人不存在")
|
||||||
}
|
}
|
||||||
@ -296,7 +297,7 @@ func RobotLogout(c *fiber.Ctx) error {
|
|||||||
var robot model.Robot
|
var robot model.Robot
|
||||||
db := model.GetDB()
|
db := model.GetDB()
|
||||||
|
|
||||||
if err = db.First(&robot, id).Error; err != nil {
|
if err = db.Where("user_id = ?", c.Locals("userId")).First(&robot, id).Error; err != nil {
|
||||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
return fiber.NewError(fiber.StatusNotFound, "机器人不存在")
|
return fiber.NewError(fiber.StatusNotFound, "机器人不存在")
|
||||||
}
|
}
|
||||||
|
@ -19,6 +19,7 @@ const (
|
|||||||
// Robot 表示微信机器人实例
|
// Robot 表示微信机器人实例
|
||||||
type Robot struct {
|
type Robot struct {
|
||||||
BaseModel
|
BaseModel
|
||||||
|
UserId string `gorm:"column:user_id;index:deleted,unique;" json:"userId"` // 用户ID
|
||||||
ContainerID string `gorm:"column:container_id;index:deleted,unique,length:64" json:"container_id"`
|
ContainerID string `gorm:"column:container_id;index:deleted,unique,length:64" json:"container_id"`
|
||||||
ShortContainerID string `gorm:"column:short_container_id;index" json:"short_container_id"` // 容器ID的前12位
|
ShortContainerID string `gorm:"column:short_container_id;index" json:"short_container_id"` // 容器ID的前12位
|
||||||
ContainerHost string `gorm:"column:container_host" json:"container_host"` // 容器访问地址,格式为ip:port
|
ContainerHost string `gorm:"column:container_host" json:"container_host"` // 容器访问地址,格式为ip:port
|
||||||
|
Loading…
x
Reference in New Issue
Block a user