diff --git a/hack/gorm-gen/gorm.go b/hack/gorm-gen/gorm.go index 6f4191d..5076ea9 100644 --- a/hack/gorm-gen/gorm.go +++ b/hack/gorm-gen/gorm.go @@ -25,11 +25,11 @@ func main() { //g.UseDB(app.GORM) g.ApplyBasic( - entity.User{}, + entity.Api{}, ) - // Generate Type Safe API with Dynamic SQL defined on Querier interface for `model.User` and `model.Company` - //g.ApplyInterface(func(Querier) {}, model.User{}, model.Company{}) + // Generate Type Safe API with Dynamic SQL defined on Querier interface for `model.Api` and `model.Company` + //g.ApplyInterface(func(Querier) {}, model.Api{}, model.Company{}) g.Execute() } diff --git a/internal/base/orm/provide.go b/internal/base/orm/provide.go index 2baeb7a..c745c93 100644 --- a/internal/base/orm/provide.go +++ b/internal/base/orm/provide.go @@ -3,9 +3,8 @@ package orm import ( "fmt" "gorm.io/driver/postgres" - "leafdev.top/Leaf/api-p "leafdev.top/Leaf/api-platform/internal/base/conf" - "gorm.io/driver/postgres" + "leafdev.top/Leaf/api-platform/internal/base/logger" "gorm.io/gorm" "moul.io/zapgorm2" diff --git a/internal/dao/users.gen.go b/internal/dao/users.gen.go index 32cb89d..6ae6b95 100644 --- a/internal/dao/users.gen.go +++ b/internal/dao/users.gen.go @@ -23,7 +23,7 @@ func newUser(db *gorm.DB, opts ...gen.DOOption) user { _user := user{} _user.userDo.UseDB(db, opts...) - _user.userDo.UseModel(&entity.User{}) + _user.userDo.UseModel(&entity.Api{}) tableName := _user.userDo.TableName() _user.ALL = field.NewAsterisk(tableName) @@ -129,17 +129,17 @@ type IUserDo interface { Count() (count int64, err error) Scopes(funcs ...func(gen.Dao) gen.Dao) IUserDo Unscoped() IUserDo - Create(values ...*entity.User) error - CreateInBatches(values []*entity.User, batchSize int) error - Save(values ...*entity.User) error - First() (*entity.User, error) - Take() (*entity.User, error) - Last() (*entity.User, error) - Find() ([]*entity.User, error) - FindInBatch(batchSize int, fc func(tx gen.Dao, batch int) error) (results []*entity.User, err error) - FindInBatches(result *[]*entity.User, batchSize int, fc func(tx gen.Dao, batch int) error) error + Create(values ...*entity.Api) error + CreateInBatches(values []*entity.Api, batchSize int) error + Save(values ...*entity.Api) error + First() (*entity.Api, error) + Take() (*entity.Api, error) + Last() (*entity.Api, error) + Find() ([]*entity.Api, error) + FindInBatch(batchSize int, fc func(tx gen.Dao, batch int) error) (results []*entity.Api, err error) + FindInBatches(result *[]*entity.Api, batchSize int, fc func(tx gen.Dao, batch int) error) 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) UpdateSimple(columns ...field.AssignExpr) (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 Joins(fields ...field.RelationField) IUserDo Preload(fields ...field.RelationField) IUserDo - FirstOrInit() (*entity.User, error) - FirstOrCreate() (*entity.User, error) - FindByPage(offset int, limit int) (result []*entity.User, count int64, err error) + FirstOrInit() (*entity.Api, error) + FirstOrCreate() (*entity.Api, error) + FindByPage(offset int, limit int) (result []*entity.Api, count int64, err error) ScanByPage(result interface{}, offset int, limit int) (count int64, err error) Scan(result interface{}) (err error) Returning(value interface{}, columns ...string) IUserDo @@ -253,57 +253,57 @@ func (u userDo) Unscoped() IUserDo { 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 { return nil } 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) } // Save : !!! underlying implementation is different with GORM // 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 { return nil } 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 { return nil, err } 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 { return nil, err } 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 { return nil, err } 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() - 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) { - buf := make([]*entity.User, 0, batchSize) +func (u userDo) FindInBatch(batchSize int, fc func(tx gen.Dao, batch int) error) (results []*entity.Api, err error) { + buf := make([]*entity.Api, 0, batchSize) err = u.DO.FindInBatches(&buf, batchSize, func(tx gen.Dao, batch int) error { defer func() { results = append(results, buf...) }() 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 } -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) } @@ -337,23 +337,23 @@ func (u userDo) Preload(fields ...field.RelationField) IUserDo { return &u } -func (u userDo) FirstOrInit() (*entity.User, error) { +func (u userDo) FirstOrInit() (*entity.Api, error) { if result, err := u.DO.FirstOrInit(); err != nil { return nil, err } 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 { return nil, err } 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() if err != nil { return @@ -382,7 +382,7 @@ func (u userDo) Scan(result interface{}) (err error) { 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) } diff --git a/internal/entity/Api.go b/internal/entity/Api.go new file mode 100644 index 0000000..8212bd4 --- /dev/null +++ b/internal/entity/Api.go @@ -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" +} diff --git a/internal/entity/Order.go b/internal/entity/Order.go new file mode 100644 index 0000000..5b8a8e0 --- /dev/null +++ b/internal/entity/Order.go @@ -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" +} diff --git a/internal/entity/Package.go b/internal/entity/Package.go new file mode 100644 index 0000000..4437034 --- /dev/null +++ b/internal/entity/Package.go @@ -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" +} diff --git a/internal/entity/PromoCode.go b/internal/entity/PromoCode.go new file mode 100644 index 0000000..3f1964b --- /dev/null +++ b/internal/entity/PromoCode.go @@ -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"` +} diff --git a/internal/entity/User.go b/internal/entity/User.go deleted file mode 100644 index c7c65ae..0000000 --- a/internal/entity/User.go +++ /dev/null @@ -1,10 +0,0 @@ -package entity - -type User struct { - Model - Name string `json:"name"` -} - -func (u *User) TableName() string { - return "user" -}