feat: update api curd

v3
veypi 2 months ago
parent 272f054e1b
commit 14931a51e5

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

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

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

@ -1,11 +1,11 @@
package role
import (
"github.com/google/uuid"
"github.com/veypi/OneBD/rest"
M "oa/models"
"oa/cfg"
M "oa/models"
"strings"
"github.com/google/uuid"
)
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
import (
"fmt"
"math/rand"
"oa/cfg"
"oa/errs"
"oa/libs/auth"
M "oa/models"
"strings"
"time"
"github.com/google/uuid"
"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("/", auth.Check("user", "", auth.DoRead), userList)
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) {
opts := &M.UserDelete{}
err := x.Parse(opts)
@ -114,11 +119,18 @@ func userPost(x *rest.X) (any, error) {
data.ID = strings.ReplaceAll(uuid.New().String(), "-", "")
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 {
data.Nickname = *opts.Nickname
}
if opts.Icon != nil {
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 {
data.Email = *opts.Email
@ -126,7 +138,10 @@ func userPost(x *rest.X) (any, error) {
if opts.Phone != nil {
data.Phone = *opts.Phone
}
data.Status = 1
err = cfg.DB().Create(data).Error
return data, err
if err != nil {
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/
require (
github.com/go-sql-driver/mysql v1.7.0
github.com/golang-jwt/jwt/v5 v5.2.1
github.com/google/uuid v1.6.0
github.com/veypi/OneBD v0.0.0-00010101000000-000000000000
@ -16,7 +17,6 @@ require (
)
require (
github.com/go-sql-driver/mysql v1.7.0 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/rs/zerolog v1.17.2 // indirect

Loading…
Cancel
Save