feat: update api curd

v3
veypi 2 months ago
parent 272f054e1b
commit 14931a51e5

@ -1,11 +1,11 @@
package app package app
import ( import (
"github.com/google/uuid"
"github.com/veypi/OneBD/rest" "github.com/veypi/OneBD/rest"
M "oa/models"
"oa/cfg" "oa/cfg"
M "oa/models"
"strings" "strings"
"github.com/google/uuid"
) )
func useApp(r rest.Router) { func useApp(r rest.Router) {
@ -23,7 +23,6 @@ func appDelete(x *rest.X) (any, error) {
} }
data := &M.App{} data := &M.App{}
err = cfg.DB().Where("id = ?", opts.ID).Delete(data).Error err = cfg.DB().Where("id = ?", opts.ID).Delete(data).Error
return data, err return data, err
@ -36,7 +35,6 @@ func appGet(x *rest.X) (any, error) {
} }
data := &M.App{} data := &M.App{}
err = cfg.DB().Where("id = ?", opts.ID).First(data).Error err = cfg.DB().Where("id = ?", opts.ID).First(data).Error
return data, err return data, err
@ -49,10 +47,9 @@ func appList(x *rest.X) (any, error) {
} }
data := make([]*M.App, 0, 10) data := make([]*M.App, 0, 10)
query := cfg.DB() query := cfg.DB()
if opts.Name != nil { if opts.Name != nil {
query = query.Where("name LIKE ?", opts.Name) query = query.Where("name LIKE ?", opts.Name)
} }
err = query.Find(&data).Error err = query.Find(&data).Error
@ -66,7 +63,6 @@ func appPatch(x *rest.X) (any, error) {
} }
data := &M.App{} data := &M.App{}
err = cfg.DB().Where("id = ?", opts.ID).First(data).Error err = cfg.DB().Where("id = ?", opts.ID).First(data).Error
if err != nil { if err != nil {
return nil, err return nil, err
@ -99,7 +95,6 @@ func appPost(x *rest.X) (any, error) {
} }
data := &M.App{} data := &M.App{}
data.ID = strings.ReplaceAll(uuid.New().String(), "-", "") data.ID = strings.ReplaceAll(uuid.New().String(), "-", "")
data.Name = opts.Name data.Name = opts.Name
data.Icon = opts.Icon data.Icon = opts.Icon

@ -1,11 +1,11 @@
package app package app
import ( import (
"github.com/google/uuid"
"github.com/veypi/OneBD/rest" "github.com/veypi/OneBD/rest"
M "oa/models"
"oa/cfg" "oa/cfg"
M "oa/models"
"strings" "strings"
"github.com/google/uuid"
) )
func useAppUser(r rest.Router) { func useAppUser(r rest.Router) {
@ -23,7 +23,6 @@ func appUserPost(x *rest.X) (any, error) {
} }
data := &M.AppUser{} data := &M.AppUser{}
data.ID = strings.ReplaceAll(uuid.New().String(), "-", "") data.ID = strings.ReplaceAll(uuid.New().String(), "-", "")
data.AppID = opts.AppID data.AppID = opts.AppID
data.UserID = opts.UserID data.UserID = opts.UserID
@ -40,7 +39,6 @@ func appUserDelete(x *rest.X) (any, error) {
} }
data := &M.AppUser{} data := &M.AppUser{}
err = cfg.DB().Where("id = ?", opts.ID).Delete(data).Error err = cfg.DB().Where("id = ?", opts.ID).Delete(data).Error
return data, err return data, err
@ -53,7 +51,6 @@ func appUserGet(x *rest.X) (any, error) {
} }
data := &M.AppUser{} data := &M.AppUser{}
err = cfg.DB().Where("id = ?", opts.ID).First(data).Error err = cfg.DB().Where("id = ?", opts.ID).First(data).Error
return data, err return data, err
@ -66,16 +63,15 @@ func appUserList(x *rest.X) (any, error) {
} }
data := make([]*M.AppUser, 0, 10) data := make([]*M.AppUser, 0, 10)
query := cfg.DB() query := cfg.DB()
if opts.AppID != nil { if opts.AppID != nil {
query = query.Where("app_id LIKE ?", opts.AppID) query = query.Where("app_id LIKE ?", opts.AppID)
} }
if opts.UserID != nil { if opts.UserID != nil {
query = query.Where("user_id LIKE ?", opts.UserID) query = query.Where("user_id LIKE ?", opts.UserID)
} }
if opts.Status != nil { if opts.Status != nil {
query = query.Where("status LIKE ?", opts.Status) query = query.Where("status LIKE ?", opts.Status)
} }
err = query.Find(&data).Error err = query.Find(&data).Error
@ -89,7 +85,6 @@ func appUserPatch(x *rest.X) (any, error) {
} }
data := &M.AppUser{} data := &M.AppUser{}
err = cfg.DB().Where("id = ?", opts.ID).First(data).Error err = cfg.DB().Where("id = ?", opts.ID).First(data).Error
if err != nil { if err != nil {
return nil, err return nil, err

@ -34,8 +34,8 @@ func resourceDelete(x *rest.X) (any, error) {
return nil, err return nil, err
} }
data := &M.Resource{ data := &M.Resource{
AppID: opts.AppID, AppID: opts.AppID,
Name: opts.Name, Name: opts.Name,
} }
err = cfg.DB().Delete(data).Error err = cfg.DB().Delete(data).Error

@ -12,6 +12,7 @@ import (
"oa/api/access" "oa/api/access"
"oa/api/app" "oa/api/app"
"oa/api/role" "oa/api/role"
"oa/api/token"
"oa/api/user" "oa/api/user"
) )
@ -20,4 +21,5 @@ func Use(r rest.Router) {
app.Use(r.SubRouter("app")) app.Use(r.SubRouter("app"))
role.Use(r.SubRouter("role")) role.Use(r.SubRouter("role"))
user.Use(r.SubRouter("user")) user.Use(r.SubRouter("user"))
token.Use(r.SubRouter("token"))
} }

@ -1,11 +1,11 @@
package role package role
import ( import (
"github.com/google/uuid"
"github.com/veypi/OneBD/rest" "github.com/veypi/OneBD/rest"
M "oa/models"
"oa/cfg" "oa/cfg"
M "oa/models"
"strings" "strings"
"github.com/google/uuid"
) )
func useRole(r rest.Router) { func useRole(r rest.Router) {

@ -0,0 +1,16 @@
//
// Copyright (C) 2024 veypi <i@veypi.com>
// 2024-09-24 22:37:12
// Distributed under terms of the MIT license.
//
// Auto generated by OneBD. DO NOT EDIT
package token
import (
"github.com/veypi/OneBD/rest"
)
func Use(r rest.Router) {
useToken(r)
}

@ -0,0 +1,113 @@
package token
import (
"oa/cfg"
M "oa/models"
"strings"
"github.com/google/uuid"
"github.com/veypi/OneBD/rest"
)
func useToken(r rest.Router) {
r.Get("/salt/:id", tokenSalt)
r.Post("/", tokenPost)
r.Get("/:token_id", tokenGet)
r.Patch("/:token_id", tokenPatch)
r.Delete("/:token_id", tokenDelete)
r.Get("/", tokenList)
}
func tokenSalt(x *rest.X) (any, error) {
opts := &M.TokenSalt{}
err := x.Parse(opts)
if err != nil {
return nil, err
}
data := &M.User{}
err = cfg.DB().Where("id = ?", opts.ID).First(data).Error
return data.Salt, err
}
func tokenGet(x *rest.X) (any, error) {
opts := &M.TokenGet{}
err := x.Parse(opts)
if err != nil {
return nil, err
}
data := &M.Token{}
err = cfg.DB().Where("id = ?", opts.ID).First(data).Error
return data, err
}
func tokenPatch(x *rest.X) (any, error) {
opts := &M.TokenPatch{}
err := x.Parse(opts)
if err != nil {
return nil, err
}
data := &M.Token{}
err = cfg.DB().Where("id = ?", opts.ID).First(data).Error
if err != nil {
return nil, err
}
optsMap := make(map[string]interface{})
if opts.ExpiredAt != nil {
optsMap["expired_at"] = opts.ExpiredAt
}
if opts.OverPerm != nil {
optsMap["over_perm"] = opts.OverPerm
}
err = cfg.DB().Model(data).Updates(optsMap).Error
return data, err
}
func tokenDelete(x *rest.X) (any, error) {
opts := &M.TokenDelete{}
err := x.Parse(opts)
if err != nil {
return nil, err
}
data := &M.Token{}
err = cfg.DB().Where("id = ?", opts.ID).Delete(data).Error
return data, err
}
func tokenPost(x *rest.X) (any, error) {
opts := &M.TokenPost{}
err := x.Parse(opts)
if err != nil {
return nil, err
}
data := &M.Token{}
data.ID = strings.ReplaceAll(uuid.New().String(), "-", "")
data.UserID = opts.UserID
data.AppID = opts.AppID
if opts.ExpiredAt != nil {
data.ExpiredAt = *opts.ExpiredAt
}
if opts.OverPerm != nil {
data.OverPerm = *opts.OverPerm
}
err = cfg.DB().Create(data).Error
return data, err
}
func tokenList(x *rest.X) (any, error) {
opts := &M.TokenList{}
err := x.Parse(opts)
if err != nil {
return nil, err
}
data := make([]*M.Token, 0, 10)
query := cfg.DB()
query = query.Where("user_id = ?", opts.UserID)
query = query.Where("app_id = ?", opts.AppID)
err = query.Find(&data).Error
return data, err
}

@ -1,10 +1,14 @@
package user package user
import ( import (
"fmt"
"math/rand"
"oa/cfg" "oa/cfg"
"oa/errs"
"oa/libs/auth" "oa/libs/auth"
M "oa/models" M "oa/models"
"strings" "strings"
"time"
"github.com/google/uuid" "github.com/google/uuid"
"github.com/veypi/OneBD/rest" "github.com/veypi/OneBD/rest"
@ -15,8 +19,9 @@ func useUser(r rest.Router) {
r.Get("/:user_id", auth.Check("user", "user_id", auth.DoRead), userGet) r.Get("/:user_id", auth.Check("user", "user_id", auth.DoRead), userGet)
r.Get("/", auth.Check("user", "", auth.DoRead), userList) r.Get("/", auth.Check("user", "", auth.DoRead), userList)
r.Patch("/:user_id", auth.Check("user", "user_id", auth.DoUpdate), userPatch) r.Patch("/:user_id", auth.Check("user", "user_id", auth.DoUpdate), userPatch)
r.Post("/", auth.Check("user", "", auth.DoCreate), userPost) r.Post("/", userPost)
} }
func userDelete(x *rest.X) (any, error) { func userDelete(x *rest.X) (any, error) {
opts := &M.UserDelete{} opts := &M.UserDelete{}
err := x.Parse(opts) err := x.Parse(opts)
@ -114,11 +119,18 @@ func userPost(x *rest.X) (any, error) {
data.ID = strings.ReplaceAll(uuid.New().String(), "-", "") data.ID = strings.ReplaceAll(uuid.New().String(), "-", "")
data.Username = opts.Username data.Username = opts.Username
data.Salt = opts.Salt
data.Code = opts.Code
if data.Username == "" || len(data.Salt) != 32 || len(data.Code) != 256 {
return nil, errs.ArgsInvalid
}
if opts.Nickname != nil { if opts.Nickname != nil {
data.Nickname = *opts.Nickname data.Nickname = *opts.Nickname
} }
if opts.Icon != nil { if opts.Icon != nil {
data.Icon = *opts.Icon data.Icon = *opts.Icon
} else {
data.Icon = fmt.Sprintf("/media/icon/%d.jpg", rand.New(rand.NewSource(time.Now().UnixNano())).Intn(230))
} }
if opts.Email != nil { if opts.Email != nil {
data.Email = *opts.Email data.Email = *opts.Email
@ -126,7 +138,10 @@ func userPost(x *rest.X) (any, error) {
if opts.Phone != nil { if opts.Phone != nil {
data.Phone = *opts.Phone data.Phone = *opts.Phone
} }
data.Status = 1
err = cfg.DB().Create(data).Error err = cfg.DB().Create(data).Error
if err != nil {
return data, err return nil, err
}
return data, nil
} }

@ -7,6 +7,7 @@ replace github.com/veypi/OneBD => ../../../workspace/OneBD/
replace github.com/veypi/utils => ../../../workspace/OceanCurrent/utils/ replace github.com/veypi/utils => ../../../workspace/OceanCurrent/utils/
require ( require (
github.com/go-sql-driver/mysql v1.7.0
github.com/golang-jwt/jwt/v5 v5.2.1 github.com/golang-jwt/jwt/v5 v5.2.1
github.com/google/uuid v1.6.0 github.com/google/uuid v1.6.0
github.com/veypi/OneBD v0.0.0-00010101000000-000000000000 github.com/veypi/OneBD v0.0.0-00010101000000-000000000000
@ -16,7 +17,6 @@ require (
) )
require ( require (
github.com/go-sql-driver/mysql v1.7.0 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect github.com/jinzhu/now v1.1.5 // indirect
github.com/rs/zerolog v1.17.2 // indirect github.com/rs/zerolog v1.17.2 // indirect

Loading…
Cancel
Save