update
This commit is contained in:
parent
b4899db5b3
commit
077e212e94
@ -25,11 +25,11 @@ func main() {
|
|||||||
//g.UseDB(app.GORM)
|
//g.UseDB(app.GORM)
|
||||||
|
|
||||||
g.ApplyBasic(
|
g.ApplyBasic(
|
||||||
entity.User{},
|
entity.Api{},
|
||||||
)
|
)
|
||||||
|
|
||||||
// Generate Type Safe API with Dynamic SQL defined on Querier interface for `model.User` and `model.Company`
|
// Generate Type Safe API with Dynamic SQL defined on Querier interface for `model.Api` and `model.Company`
|
||||||
//g.ApplyInterface(func(Querier) {}, model.User{}, model.Company{})
|
//g.ApplyInterface(func(Querier) {}, model.Api{}, model.Company{})
|
||||||
|
|
||||||
g.Execute()
|
g.Execute()
|
||||||
}
|
}
|
||||||
|
@ -3,9 +3,8 @@ package orm
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"gorm.io/driver/postgres"
|
"gorm.io/driver/postgres"
|
||||||
"leafdev.top/Leaf/api-p
|
|
||||||
"leafdev.top/Leaf/api-platform/internal/base/conf"
|
"leafdev.top/Leaf/api-platform/internal/base/conf"
|
||||||
"gorm.io/driver/postgres"
|
"leafdev.top/Leaf/api-platform/internal/base/logger"
|
||||||
|
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
"moul.io/zapgorm2"
|
"moul.io/zapgorm2"
|
||||||
|
@ -23,7 +23,7 @@ func newUser(db *gorm.DB, opts ...gen.DOOption) user {
|
|||||||
_user := user{}
|
_user := user{}
|
||||||
|
|
||||||
_user.userDo.UseDB(db, opts...)
|
_user.userDo.UseDB(db, opts...)
|
||||||
_user.userDo.UseModel(&entity.User{})
|
_user.userDo.UseModel(&entity.Api{})
|
||||||
|
|
||||||
tableName := _user.userDo.TableName()
|
tableName := _user.userDo.TableName()
|
||||||
_user.ALL = field.NewAsterisk(tableName)
|
_user.ALL = field.NewAsterisk(tableName)
|
||||||
@ -129,17 +129,17 @@ type IUserDo interface {
|
|||||||
Count() (count int64, err error)
|
Count() (count int64, err error)
|
||||||
Scopes(funcs ...func(gen.Dao) gen.Dao) IUserDo
|
Scopes(funcs ...func(gen.Dao) gen.Dao) IUserDo
|
||||||
Unscoped() IUserDo
|
Unscoped() IUserDo
|
||||||
Create(values ...*entity.User) error
|
Create(values ...*entity.Api) error
|
||||||
CreateInBatches(values []*entity.User, batchSize int) error
|
CreateInBatches(values []*entity.Api, batchSize int) error
|
||||||
Save(values ...*entity.User) error
|
Save(values ...*entity.Api) error
|
||||||
First() (*entity.User, error)
|
First() (*entity.Api, error)
|
||||||
Take() (*entity.User, error)
|
Take() (*entity.Api, error)
|
||||||
Last() (*entity.User, error)
|
Last() (*entity.Api, error)
|
||||||
Find() ([]*entity.User, error)
|
Find() ([]*entity.Api, error)
|
||||||
FindInBatch(batchSize int, fc func(tx gen.Dao, batch int) error) (results []*entity.User, err error)
|
FindInBatch(batchSize int, fc func(tx gen.Dao, batch int) error) (results []*entity.Api, err error)
|
||||||
FindInBatches(result *[]*entity.User, batchSize int, fc func(tx gen.Dao, batch int) error) error
|
FindInBatches(result *[]*entity.Api, batchSize int, fc func(tx gen.Dao, batch int) error) error
|
||||||
Pluck(column field.Expr, dest interface{}) error
|
Pluck(column field.Expr, dest interface{}) error
|
||||||
Delete(...*entity.User) (info gen.ResultInfo, err error)
|
Delete(...*entity.Api) (info gen.ResultInfo, err error)
|
||||||
Update(column field.Expr, value interface{}) (info gen.ResultInfo, err error)
|
Update(column field.Expr, value interface{}) (info gen.ResultInfo, err error)
|
||||||
UpdateSimple(columns ...field.AssignExpr) (info gen.ResultInfo, err error)
|
UpdateSimple(columns ...field.AssignExpr) (info gen.ResultInfo, err error)
|
||||||
Updates(value interface{}) (info gen.ResultInfo, err error)
|
Updates(value interface{}) (info gen.ResultInfo, err error)
|
||||||
@ -151,9 +151,9 @@ type IUserDo interface {
|
|||||||
Assign(attrs ...field.AssignExpr) IUserDo
|
Assign(attrs ...field.AssignExpr) IUserDo
|
||||||
Joins(fields ...field.RelationField) IUserDo
|
Joins(fields ...field.RelationField) IUserDo
|
||||||
Preload(fields ...field.RelationField) IUserDo
|
Preload(fields ...field.RelationField) IUserDo
|
||||||
FirstOrInit() (*entity.User, error)
|
FirstOrInit() (*entity.Api, error)
|
||||||
FirstOrCreate() (*entity.User, error)
|
FirstOrCreate() (*entity.Api, error)
|
||||||
FindByPage(offset int, limit int) (result []*entity.User, count int64, err error)
|
FindByPage(offset int, limit int) (result []*entity.Api, count int64, err error)
|
||||||
ScanByPage(result interface{}, offset int, limit int) (count int64, err error)
|
ScanByPage(result interface{}, offset int, limit int) (count int64, err error)
|
||||||
Scan(result interface{}) (err error)
|
Scan(result interface{}) (err error)
|
||||||
Returning(value interface{}, columns ...string) IUserDo
|
Returning(value interface{}, columns ...string) IUserDo
|
||||||
@ -253,57 +253,57 @@ func (u userDo) Unscoped() IUserDo {
|
|||||||
return u.withDO(u.DO.Unscoped())
|
return u.withDO(u.DO.Unscoped())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u userDo) Create(values ...*entity.User) error {
|
func (u userDo) Create(values ...*entity.Api) error {
|
||||||
if len(values) == 0 {
|
if len(values) == 0 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return u.DO.Create(values)
|
return u.DO.Create(values)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u userDo) CreateInBatches(values []*entity.User, batchSize int) error {
|
func (u userDo) CreateInBatches(values []*entity.Api, batchSize int) error {
|
||||||
return u.DO.CreateInBatches(values, batchSize)
|
return u.DO.CreateInBatches(values, batchSize)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Save : !!! underlying implementation is different with GORM
|
// Save : !!! underlying implementation is different with GORM
|
||||||
// The method is equivalent to executing the statement: db.Clauses(clause.OnConflict{UpdateAll: true}).Create(values)
|
// The method is equivalent to executing the statement: db.Clauses(clause.OnConflict{UpdateAll: true}).Create(values)
|
||||||
func (u userDo) Save(values ...*entity.User) error {
|
func (u userDo) Save(values ...*entity.Api) error {
|
||||||
if len(values) == 0 {
|
if len(values) == 0 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return u.DO.Save(values)
|
return u.DO.Save(values)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u userDo) First() (*entity.User, error) {
|
func (u userDo) First() (*entity.Api, error) {
|
||||||
if result, err := u.DO.First(); err != nil {
|
if result, err := u.DO.First(); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
} else {
|
} else {
|
||||||
return result.(*entity.User), nil
|
return result.(*entity.Api), nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u userDo) Take() (*entity.User, error) {
|
func (u userDo) Take() (*entity.Api, error) {
|
||||||
if result, err := u.DO.Take(); err != nil {
|
if result, err := u.DO.Take(); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
} else {
|
} else {
|
||||||
return result.(*entity.User), nil
|
return result.(*entity.Api), nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u userDo) Last() (*entity.User, error) {
|
func (u userDo) Last() (*entity.Api, error) {
|
||||||
if result, err := u.DO.Last(); err != nil {
|
if result, err := u.DO.Last(); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
} else {
|
} else {
|
||||||
return result.(*entity.User), nil
|
return result.(*entity.Api), nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u userDo) Find() ([]*entity.User, error) {
|
func (u userDo) Find() ([]*entity.Api, error) {
|
||||||
result, err := u.DO.Find()
|
result, err := u.DO.Find()
|
||||||
return result.([]*entity.User), err
|
return result.([]*entity.Api), err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u userDo) FindInBatch(batchSize int, fc func(tx gen.Dao, batch int) error) (results []*entity.User, err error) {
|
func (u userDo) FindInBatch(batchSize int, fc func(tx gen.Dao, batch int) error) (results []*entity.Api, err error) {
|
||||||
buf := make([]*entity.User, 0, batchSize)
|
buf := make([]*entity.Api, 0, batchSize)
|
||||||
err = u.DO.FindInBatches(&buf, batchSize, func(tx gen.Dao, batch int) error {
|
err = u.DO.FindInBatches(&buf, batchSize, func(tx gen.Dao, batch int) error {
|
||||||
defer func() { results = append(results, buf...) }()
|
defer func() { results = append(results, buf...) }()
|
||||||
return fc(tx, batch)
|
return fc(tx, batch)
|
||||||
@ -311,7 +311,7 @@ func (u userDo) FindInBatch(batchSize int, fc func(tx gen.Dao, batch int) error)
|
|||||||
return results, err
|
return results, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u userDo) FindInBatches(result *[]*entity.User, batchSize int, fc func(tx gen.Dao, batch int) error) error {
|
func (u userDo) FindInBatches(result *[]*entity.Api, batchSize int, fc func(tx gen.Dao, batch int) error) error {
|
||||||
return u.DO.FindInBatches(result, batchSize, fc)
|
return u.DO.FindInBatches(result, batchSize, fc)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -337,23 +337,23 @@ func (u userDo) Preload(fields ...field.RelationField) IUserDo {
|
|||||||
return &u
|
return &u
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u userDo) FirstOrInit() (*entity.User, error) {
|
func (u userDo) FirstOrInit() (*entity.Api, error) {
|
||||||
if result, err := u.DO.FirstOrInit(); err != nil {
|
if result, err := u.DO.FirstOrInit(); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
} else {
|
} else {
|
||||||
return result.(*entity.User), nil
|
return result.(*entity.Api), nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u userDo) FirstOrCreate() (*entity.User, error) {
|
func (u userDo) FirstOrCreate() (*entity.Api, error) {
|
||||||
if result, err := u.DO.FirstOrCreate(); err != nil {
|
if result, err := u.DO.FirstOrCreate(); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
} else {
|
} else {
|
||||||
return result.(*entity.User), nil
|
return result.(*entity.Api), nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u userDo) FindByPage(offset int, limit int) (result []*entity.User, count int64, err error) {
|
func (u userDo) FindByPage(offset int, limit int) (result []*entity.Api, count int64, err error) {
|
||||||
result, err = u.Offset(offset).Limit(limit).Find()
|
result, err = u.Offset(offset).Limit(limit).Find()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
@ -382,7 +382,7 @@ func (u userDo) Scan(result interface{}) (err error) {
|
|||||||
return u.DO.Scan(result)
|
return u.DO.Scan(result)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u userDo) Delete(models ...*entity.User) (result gen.ResultInfo, err error) {
|
func (u userDo) Delete(models ...*entity.Api) (result gen.ResultInfo, err error) {
|
||||||
return u.DO.Delete(models)
|
return u.DO.Delete(models)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
36
internal/entity/Api.go
Normal file
36
internal/entity/Api.go
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
package entity
|
||||||
|
|
||||||
|
import (
|
||||||
|
"gorm.io/gorm"
|
||||||
|
"leafdev.top/Leaf/api-platform/internal/schema"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Api struct {
|
||||||
|
Model
|
||||||
|
Name string `json:"name"`
|
||||||
|
Description string `json:"description"`
|
||||||
|
BackendUrl string `json:"backend_url"`
|
||||||
|
AuthToken string `json:"auth_token"`
|
||||||
|
OwnershipVerifiedAt *time.Time `json:"ownership_verified_at"`
|
||||||
|
OwnershipVerificationToken string `json:"ownership_verification_token"`
|
||||||
|
UserId schema.UserId `json:"user_id"`
|
||||||
|
Status string `json:"status"`
|
||||||
|
DeletedAt gorm.DeletedAt `json:"deleted_at"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (u *Api) TableName() string {
|
||||||
|
return "apis"
|
||||||
|
}
|
||||||
|
|
||||||
|
type ApiDocument struct {
|
||||||
|
Model
|
||||||
|
ApiId schema.EntityId `json:"api_id"`
|
||||||
|
Api *Api `json:"api"`
|
||||||
|
OpenAPIUrl string `json:"openapi_url" gorm:"column:openapi_url"`
|
||||||
|
Content string `json:"content"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (u *ApiDocument) TableName() string {
|
||||||
|
return "api_documents"
|
||||||
|
}
|
31
internal/entity/Order.go
Normal file
31
internal/entity/Order.go
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
package entity
|
||||||
|
|
||||||
|
import (
|
||||||
|
"leafdev.top/Leaf/api-platform/internal/schema"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Order struct {
|
||||||
|
Model
|
||||||
|
UserId schema.UserId `json:"user_id"`
|
||||||
|
PromoCodeId *schema.EntityId `json:"promo_code_id"`
|
||||||
|
PromoCode *PromoCode `json:"promo_code"`
|
||||||
|
Price float64 `json:"price"`
|
||||||
|
OrderDate time.Time `json:"order_date"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (u *Order) TableName() string {
|
||||||
|
return "orders"
|
||||||
|
}
|
||||||
|
|
||||||
|
type OrderItem struct {
|
||||||
|
Model
|
||||||
|
OrderId schema.EntityId `json:"order_id"`
|
||||||
|
PackageId schema.EntityId `json:"package_id"`
|
||||||
|
Order *Order
|
||||||
|
Package *Package
|
||||||
|
}
|
||||||
|
|
||||||
|
func (u *OrderItem) TableName() string {
|
||||||
|
return "order_items"
|
||||||
|
}
|
42
internal/entity/Package.go
Normal file
42
internal/entity/Package.go
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
package entity
|
||||||
|
|
||||||
|
import (
|
||||||
|
"gorm.io/gorm"
|
||||||
|
"leafdev.top/Leaf/api-platform/internal/schema"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Package struct {
|
||||||
|
Model
|
||||||
|
Name string `json:"name"`
|
||||||
|
Description string `json:"description"`
|
||||||
|
ApiId schema.EntityId `json:"api_id"`
|
||||||
|
Api *Api `json:"api"`
|
||||||
|
Price float64 `json:"price"`
|
||||||
|
IsHidden bool `json:"is_hidden"`
|
||||||
|
IsPublished bool `json:"is_published"`
|
||||||
|
CallLimit int64 `json:"call_limit"`
|
||||||
|
Duration int64 `json:"duration"`
|
||||||
|
IsTrial bool `json:"is_trial"`
|
||||||
|
PurchaseLimit int64 `json:"purchase_limit"`
|
||||||
|
DeletedAt *gorm.DeletedAt `json:"deleted_at"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (u *Package) TableName() string {
|
||||||
|
return "packages"
|
||||||
|
}
|
||||||
|
|
||||||
|
type UserPackage struct {
|
||||||
|
Model
|
||||||
|
UserId schema.UserId `json:"user_id"`
|
||||||
|
PackageId schema.EntityId `json:"package_id"`
|
||||||
|
Package *Package `json:"api_package"`
|
||||||
|
CallsLeft int64 `json:"calls_left"`
|
||||||
|
IsActive bool `json:"is_active"`
|
||||||
|
ExpirationAt *time.Time `json:"expiration_at"`
|
||||||
|
PurchaseAt time.Time `json:"purchase_at"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (u *UserPackage) TableName() string {
|
||||||
|
return "user_packages"
|
||||||
|
}
|
16
internal/entity/PromoCode.go
Normal file
16
internal/entity/PromoCode.go
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
package entity
|
||||||
|
|
||||||
|
import (
|
||||||
|
"gorm.io/gorm"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type PromoCode struct {
|
||||||
|
Model
|
||||||
|
Code string `json:"code"`
|
||||||
|
DiscountType string `json:"discount_type"`
|
||||||
|
DiscountValue float64 `json:"discount_value"`
|
||||||
|
ExpirationDate *time.Time `json:"expiration_date"`
|
||||||
|
IsActive bool `json:"is_active"`
|
||||||
|
DeletedAt *gorm.DeletedAt `json:"deleted_at"`
|
||||||
|
}
|
@ -1,10 +0,0 @@
|
|||||||
package entity
|
|
||||||
|
|
||||||
type User struct {
|
|
||||||
Model
|
|
||||||
Name string `json:"name"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func (u *User) TableName() string {
|
|
||||||
return "user"
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user