[agent-go] 开始使用GO重构Agent部分
This commit is contained in:
91
server-go/service/ServerAPI.go
Normal file
91
server-go/service/ServerAPI.go
Normal file
@@ -0,0 +1,91 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
"server-go/entity"
|
||||
)
|
||||
|
||||
type ServerAPI struct {
|
||||
}
|
||||
|
||||
var ServerAPIService = new(ServerAPI)
|
||||
|
||||
var db = entity.CONGIG.DB
|
||||
|
||||
func (s *ServerAPI) QueryAPI(c *gin.Context) {
|
||||
|
||||
fmt.Println("ServerAPI -> QueryAPI")
|
||||
}
|
||||
|
||||
func (s *ServerAPI) CreateAPI(c *gin.Context) {
|
||||
|
||||
var serverInfo entity.ServerInfo
|
||||
// get the body
|
||||
err := c.ShouldBindJSON(&serverInfo)
|
||||
if err != nil {
|
||||
fmt.Println("server info error! pls check !")
|
||||
}
|
||||
|
||||
fmt.Sprintf("server info is => %s", serverInfo)
|
||||
|
||||
create := db.Create(&serverInfo)
|
||||
//user.ID // 返回插入数据的主键
|
||||
//result.Error // 返回 error
|
||||
//result.RowsAffected // 返回插入记录的条数
|
||||
|
||||
fmt.Sprintf(
|
||||
"inter id is %s , resul error is %s, RowsAffected is %d ", serverInfo.ID, create.Error, create.RowsAffected)
|
||||
}
|
||||
|
||||
func (s *ServerAPI) FakeAPI(c *gin.Context) {
|
||||
|
||||
c.JSON(
|
||||
http.StatusOK,
|
||||
new(entity.ServerInfo))
|
||||
}
|
||||
|
||||
// UpdateAPI 根据前端传递的参数id进行更新
|
||||
func (s *ServerAPI) UpdateAPI(c *gin.Context) {
|
||||
|
||||
var jsonServerInfo entity.ServerInfo
|
||||
|
||||
err := c.ShouldBindJSON(&jsonServerInfo)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
id := jsonServerInfo.ID
|
||||
|
||||
// 查询要更新的记录
|
||||
var dbProduct entity.ServerInfo
|
||||
if err := db.First(&dbProduct, id).Error; err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "Record not found"})
|
||||
return
|
||||
}
|
||||
|
||||
// 绑定模型并更新属性
|
||||
db.Model(&dbProduct).Updates(jsonServerInfo)
|
||||
|
||||
// 保存更改并提交事务
|
||||
if err := db.Save(&dbProduct).Error; err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to update record"})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, dbProduct)
|
||||
|
||||
}
|
||||
|
||||
// DeleteAPI 跟俊前端传递的id进行删除
|
||||
func (s *ServerAPI) DeleteAPI(c *gin.Context) {
|
||||
|
||||
// 获取前端传递的值
|
||||
id := c.Param("id")
|
||||
|
||||
// 根据ID删除 是否是软删除
|
||||
db.Delete(&entity.ServerInfo{}, id)
|
||||
|
||||
// 软删除
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user