data curd
This commit is contained in:
parent
b50bd2c776
commit
a36de499c3
1
API.md
1
API.md
|
|
@ -101,7 +101,6 @@ type DataModel struct {
|
||||||
Comment string
|
Comment string
|
||||||
Doc string
|
Doc string
|
||||||
UserName string
|
UserName string
|
||||||
Password string
|
|
||||||
AccessInfo string
|
AccessInfo string
|
||||||
IsPublished bool
|
IsPublished bool
|
||||||
}
|
}
|
||||||
|
|
|
||||||
2
main.go
2
main.go
|
|
@ -80,7 +80,7 @@ func main() {
|
||||||
r.DELETE("/image", controller.DeleteImage)
|
r.DELETE("/image", controller.DeleteImage)
|
||||||
r.GET("/images", controller.GetImages)
|
r.GET("/images", controller.GetImages)
|
||||||
|
|
||||||
//r.POST("/data", controller.Add[model.DataModel])
|
controller.AddDataController(r)
|
||||||
|
|
||||||
err := r.Run(":8000")
|
err := r.Run(":8000")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
||||||
|
|
@ -1,70 +1,43 @@
|
||||||
package controller
|
package controller
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"errors"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"k8s-manager/src/model"
|
"k8s-manager/src/model"
|
||||||
"k8s-manager/src/utils"
|
"k8s-manager/src/utils"
|
||||||
"net/http"
|
"net/http"
|
||||||
"reflect"
|
|
||||||
"strconv"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type BaseController[T model.DataModel, PT model.ModalInterface[T]] struct {
|
type BaseController[T model.ModalType, PT model.ModalInterface[T]] struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (BaseController[T, PT]) Add(c *gin.Context) {
|
func (BaseController[T, PT]) Add(t *T) (utils.OKType, error) {
|
||||||
data := PT(new(T))
|
PT(t).Create()
|
||||||
fmt.Println(reflect.TypeOf(data))
|
return utils.OK, nil
|
||||||
if utils.GinErrorCheck(c, c.BindJSON(&data)) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
username := c.MustGet("username").(string)
|
|
||||||
data.SetUserName(username)
|
|
||||||
data.Create()
|
|
||||||
utils.GinOk(c, "ok")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (BaseController[T, PT]) Delete(c *gin.Context) {
|
func (BaseController[T, PT]) Delete(ID int) (utils.OKType, error) {
|
||||||
id := c.Query("ID")
|
|
||||||
if id == "" {
|
|
||||||
utils.GinError(c, http.StatusBadRequest, "no id")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
username := c.MustGet("username").(string)
|
|
||||||
idi, err := strconv.Atoi(id)
|
|
||||||
if utils.GinErrorCheck(c, err) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
data := PT(new(T))
|
data := PT(new(T))
|
||||||
data.SetID(uint(idi))
|
data.SetID(uint(ID))
|
||||||
data.Fill()
|
|
||||||
if data.GetUserName() != username {
|
|
||||||
utils.GinError(c, http.StatusUnauthorized, "username error")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
data.Delete()
|
data.Delete()
|
||||||
|
return utils.OK, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (BaseController[T, PT]) Update(c *gin.Context) {
|
func (BaseController[T, PT]) Update(username string, t *T) (utils.OKType, error) {
|
||||||
data := PT(new(T))
|
data := PT(t)
|
||||||
if utils.GinErrorCheck(c, c.BindJSON(data)) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
username := c.MustGet("username").(string)
|
|
||||||
oldData := PT(new(T))
|
oldData := PT(new(T))
|
||||||
oldData.SetID(data.GetID())
|
oldData.SetID(data.GetID())
|
||||||
oldData.Fill()
|
oldData.Fill()
|
||||||
if oldData.GetUserName() != username {
|
if oldData.GetUserName() != username {
|
||||||
utils.GinError(c, http.StatusUnauthorized, "username error")
|
return nil, errors.New("wrong username")
|
||||||
return
|
|
||||||
}
|
}
|
||||||
data.Save()
|
data.Save()
|
||||||
|
return utils.OK, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (BaseController[T, PT]) Gets(c *gin.Context) {
|
func (BaseController[T, PT]) Gets(c *gin.Context) {
|
||||||
username := c.MustGet("username").(string)
|
username := c.MustGet("username").(string)
|
||||||
var ans []T
|
var ans []any
|
||||||
query := map[string]interface{}{}
|
query := map[string]interface{}{}
|
||||||
if c.Query("UserName") != "" {
|
if c.Query("UserName") != "" {
|
||||||
if username != c.Query("UserName") {
|
if username != c.Query("UserName") {
|
||||||
|
|
@ -72,11 +45,21 @@ func (BaseController[T, PT]) Gets(c *gin.Context) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
query["UserName"] = username
|
query["UserName"] = username
|
||||||
} else if c.Query("IsPublic") != "" {
|
} else if c.Query("IsPublic") == "1" {
|
||||||
query["IsPublic"] = true
|
query["IsPublic"] = true
|
||||||
} else {
|
} else {
|
||||||
utils.GinError(c, http.StatusBadRequest, "param error.")
|
utils.GinError(c, http.StatusBadRequest, "param error.")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
ans = PT(new(T)).Gets(query)
|
||||||
c.JSON(http.StatusOK, gin.H{"data": ans})
|
c.JSON(http.StatusOK, gin.H{"data": ans})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//func AddCRUDRoute[T model.ModalType](r *gin.Engine, path string) {
|
||||||
|
// t := BaseController[T, *T]{}
|
||||||
|
// typeName := reflect.TypeOf((*T)(nil)).Name()
|
||||||
|
// utils.RouteWrapper(r, "POST", path, t.Add, []string{typeName})
|
||||||
|
// utils.RouteWrapper(r, "PUT", path, t.Update, []string{"username", typeName})
|
||||||
|
// utils.RouteWrapper(r, "DELETE", path, t.Delete, []string{"ID"})
|
||||||
|
// r.Handle("GET", path, t.Gets)
|
||||||
|
//}
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,56 @@
|
||||||
package controller
|
package controller
|
||||||
|
|
||||||
import "github.com/gin-gonic/gin"
|
import (
|
||||||
|
"errors"
|
||||||
func AddData(c *gin.Context) {
|
"github.com/gin-gonic/gin"
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
|
"k8s-manager/src/model"
|
||||||
|
"k8s-manager/src/utils"
|
||||||
|
)
|
||||||
|
|
||||||
|
func AddData(username string, data *model.DataModel) (utils.OKType, error) {
|
||||||
|
data.UserName = username
|
||||||
|
data.Save()
|
||||||
|
return utils.OK, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func DeleteData(username string, ID uint) (utils.OKType, error) {
|
||||||
|
oldData := model.DataModel{}
|
||||||
|
oldData.SetID(ID)
|
||||||
|
oldData.Fill()
|
||||||
|
if username != oldData.GetUserName() {
|
||||||
|
return nil, errors.New("wrong username")
|
||||||
|
}
|
||||||
|
oldData.Delete()
|
||||||
|
return utils.OK, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func UpdateData(username string, data *model.DataModel) (utils.OKType, error) {
|
||||||
|
oldData := model.DataModel{}
|
||||||
|
oldData.SetID(data.ID)
|
||||||
|
oldData.Fill()
|
||||||
|
if username != oldData.GetUserName() {
|
||||||
|
return nil, errors.New("wrong username")
|
||||||
|
}
|
||||||
|
data.Save()
|
||||||
|
return utils.OK, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetData(UserName string, IsPublish bool) (gin.H, error) {
|
||||||
|
data := model.DataModel{}
|
||||||
|
data.UserName = UserName
|
||||||
|
data.IsPublished = IsPublish
|
||||||
|
log.Info(UserName, IsPublish)
|
||||||
|
if UserName == "" && !IsPublish {
|
||||||
|
return gin.H{"data": []string{}}, nil
|
||||||
|
}
|
||||||
|
ans := data.Gets()
|
||||||
|
return gin.H{"data": ans}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func AddDataController(r *gin.Engine) {
|
||||||
|
utils.RouteWrapper(r, "POST", "/data", AddData, []string{"username", "1"})
|
||||||
|
utils.RouteWrapper(r, "DELETE", "/data", DeleteData, []string{"username", "ID"})
|
||||||
|
utils.RouteWrapper(r, "PUT", "/data", UpdateData, []string{"username", "1"})
|
||||||
|
utils.RouteWrapper(r, "GET", "/data", GetData, []string{"UserName", "IsPublished"})
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -17,8 +17,7 @@ type ModalInterface[T ModalType] interface {
|
||||||
Fill()
|
Fill()
|
||||||
Delete()
|
Delete()
|
||||||
Save()
|
Save()
|
||||||
Gets(map[string]interface{}) []T
|
Gets(map[string]interface{}) []any
|
||||||
//Gets() []T
|
|
||||||
|
|
||||||
GetUserName() string
|
GetUserName() string
|
||||||
SetUserName(string)
|
SetUserName(string)
|
||||||
|
|
@ -54,19 +53,20 @@ func (b *BaseModel[T]) Delete() {
|
||||||
db.Delete(p)
|
db.Delete(p)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *BaseModel[T]) Gets(query map[string]interface{}) []T {
|
func (b *BaseModel[T]) Gets() []T {
|
||||||
db := k8s_manager.DB()
|
db := k8s_manager.DB()
|
||||||
ans := make([]T, 0)
|
ans := make([]T, 0)
|
||||||
db.Where(query).Find(&ans)
|
p := (*T)(unsafe.Pointer(b))
|
||||||
|
db.Where(p).Find(&ans)
|
||||||
return ans
|
return ans
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *BaseModel[T]) Get(query map[string]interface{}) *T {
|
//func (b *BaseModel[T]) Get(query map[string]interface{}) *T {
|
||||||
db := k8s_manager.DB()
|
// db := k8s_manager.DB()
|
||||||
p := (*T)(unsafe.Pointer(b))
|
// p := (*T)(unsafe.Pointer(b))
|
||||||
db.Where(query).First(p)
|
// db.Where(query).First(p)
|
||||||
return p
|
// return p
|
||||||
}
|
//}
|
||||||
|
|
||||||
func (b *BaseModel[T]) SetUserName(name string) {
|
func (b *BaseModel[T]) SetUserName(name string) {
|
||||||
p := (*T)(unsafe.Pointer(b))
|
p := (*T)(unsafe.Pointer(b))
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,6 @@ type DataModel struct {
|
||||||
Comment string
|
Comment string
|
||||||
Doc string
|
Doc string
|
||||||
UserName string
|
UserName string
|
||||||
Password string
|
|
||||||
IsPublished bool
|
IsPublished bool
|
||||||
AccessInfo string
|
AccessInfo string
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -38,13 +38,17 @@ func GinErrorCheck(c *gin.Context, err error) bool {
|
||||||
func setValue(data string, value reflect.Value) error {
|
func setValue(data string, value reflect.Value) error {
|
||||||
switch value.Kind() {
|
switch value.Kind() {
|
||||||
case reflect.Bool:
|
case reflect.Bool:
|
||||||
break
|
v, err := strconv.ParseBool(data)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
value.SetBool(v)
|
||||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
||||||
v, err := strconv.ParseInt(data, 10, 64)
|
v, err := strconv.ParseInt(data, 10, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
value.SetInt(int64(v))
|
value.SetInt(v)
|
||||||
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
||||||
v, err := strconv.ParseUint(data, 10, 64)
|
v, err := strconv.ParseUint(data, 10, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -73,6 +77,10 @@ func setValue(data string, value reflect.Value) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type OKType = map[string]any
|
||||||
|
|
||||||
|
var OK = OKType{"ok": true}
|
||||||
|
|
||||||
func routeWrapperHandler(handler any, names []string) func(ctx *gin.Context) {
|
func routeWrapperHandler(handler any, names []string) func(ctx *gin.Context) {
|
||||||
return func(c *gin.Context) {
|
return func(c *gin.Context) {
|
||||||
handlerFunc := reflect.ValueOf(handler)
|
handlerFunc := reflect.ValueOf(handler)
|
||||||
|
|
@ -92,7 +100,8 @@ func routeWrapperHandler(handler any, names []string) func(ctx *gin.Context) {
|
||||||
}
|
}
|
||||||
v, ok := c.Get(names[i])
|
v, ok := c.Get(names[i])
|
||||||
if ok {
|
if ok {
|
||||||
paraValue.Set(reflect.ValueOf(v))
|
t := reflect.ValueOf(v)
|
||||||
|
paraValue.Elem().Set(t)
|
||||||
} else {
|
} else {
|
||||||
vs := c.Param(names[i])
|
vs := c.Param(names[i])
|
||||||
if vs == "" {
|
if vs == "" {
|
||||||
|
|
@ -132,6 +141,6 @@ func routeWrapperHandler(handler any, names []string) func(ctx *gin.Context) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func RouteWrapper(r *gin.Engine, method string, route string, handler any, names []string) {
|
func RouteWrapper(r *gin.Engine, method string, path string, handler any, names []string) {
|
||||||
r.Handle(method, route, routeWrapperHandler(handler, names))
|
r.Handle(method, path, routeWrapperHandler(handler, names))
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue