feat: new models, sorting fix

This commit is contained in:
2025-03-15 21:12:15 +07:00
parent db7076182e
commit 2b8df155d3
103 changed files with 16214 additions and 4048 deletions

View File

@@ -1,713 +0,0 @@
// Code generated by gorm.io/gen. DO NOT EDIT.
// Code generated by gorm.io/gen. DO NOT EDIT.
// Code generated by gorm.io/gen. DO NOT EDIT.
package dal
import (
"app/internal/models"
"context"
"gorm.io/gorm"
"gorm.io/gorm/clause"
"gorm.io/gorm/schema"
"gorm.io/gen"
"gorm.io/gen/field"
"gorm.io/plugin/dbresolver"
)
func newPost(db *gorm.DB, opts ...gen.DOOption) post {
_post := post{}
_post.postDo.UseDB(db, opts...)
_post.postDo.UseModel(&models.Post{})
tableName := _post.postDo.TableName()
_post.ALL = field.NewAsterisk(tableName)
_post.Id = field.NewUint(tableName, "id")
_post.Text = field.NewString(tableName, "text")
_post.Deadline = field.NewInt64(tableName, "deadline")
_post.CreatedAt = field.NewInt64(tableName, "created_at")
_post.AuthorId = field.NewUint(tableName, "author_id")
_post.PostTypeId = field.NewUint(tableName, "post_type_id")
_post.Author = postBelongsToAuthor{
db: db.Session(&gorm.Session{}),
RelationField: field.NewRelation("Author", "models.Author"),
Posts: struct {
field.RelationField
Author struct {
field.RelationField
}
PostType struct {
field.RelationField
}
Comments struct {
field.RelationField
Author struct {
field.RelationField
}
Posts struct {
field.RelationField
}
}
}{
RelationField: field.NewRelation("Author.Posts", "models.Post"),
Author: struct {
field.RelationField
}{
RelationField: field.NewRelation("Author.Posts.Author", "models.Author"),
},
PostType: struct {
field.RelationField
}{
RelationField: field.NewRelation("Author.Posts.PostType", "models.PostType"),
},
Comments: struct {
field.RelationField
Author struct {
field.RelationField
}
Posts struct {
field.RelationField
}
}{
RelationField: field.NewRelation("Author.Posts.Comments", "models.Comment"),
Author: struct {
field.RelationField
}{
RelationField: field.NewRelation("Author.Posts.Comments.Author", "models.Author"),
},
Posts: struct {
field.RelationField
}{
RelationField: field.NewRelation("Author.Posts.Comments.Posts", "models.Post"),
},
},
},
Comments: struct {
field.RelationField
}{
RelationField: field.NewRelation("Author.Comments", "models.Comment"),
},
}
_post.PostType = postBelongsToPostType{
db: db.Session(&gorm.Session{}),
RelationField: field.NewRelation("PostType", "models.PostType"),
}
_post.Comments = postManyToManyComments{
db: db.Session(&gorm.Session{}),
RelationField: field.NewRelation("Comments", "models.Comment"),
}
_post.fillFieldMap()
return _post
}
type post struct {
postDo
ALL field.Asterisk
Id field.Uint
Text field.String
Deadline field.Int64
CreatedAt field.Int64
AuthorId field.Uint
PostTypeId field.Uint
Author postBelongsToAuthor
PostType postBelongsToPostType
Comments postManyToManyComments
fieldMap map[string]field.Expr
}
func (p post) Table(newTableName string) *post {
p.postDo.UseTable(newTableName)
return p.updateTableName(newTableName)
}
func (p post) As(alias string) *post {
p.postDo.DO = *(p.postDo.As(alias).(*gen.DO))
return p.updateTableName(alias)
}
func (p *post) updateTableName(table string) *post {
p.ALL = field.NewAsterisk(table)
p.Id = field.NewUint(table, "id")
p.Text = field.NewString(table, "text")
p.Deadline = field.NewInt64(table, "deadline")
p.CreatedAt = field.NewInt64(table, "created_at")
p.AuthorId = field.NewUint(table, "author_id")
p.PostTypeId = field.NewUint(table, "post_type_id")
p.fillFieldMap()
return p
}
func (p *post) GetFieldByName(fieldName string) (field.OrderExpr, bool) {
_f, ok := p.fieldMap[fieldName]
if !ok || _f == nil {
return nil, false
}
_oe, ok := _f.(field.OrderExpr)
return _oe, ok
}
func (p *post) fillFieldMap() {
p.fieldMap = make(map[string]field.Expr, 9)
p.fieldMap["id"] = p.Id
p.fieldMap["text"] = p.Text
p.fieldMap["deadline"] = p.Deadline
p.fieldMap["created_at"] = p.CreatedAt
p.fieldMap["author_id"] = p.AuthorId
p.fieldMap["post_type_id"] = p.PostTypeId
}
func (p post) clone(db *gorm.DB) post {
p.postDo.ReplaceConnPool(db.Statement.ConnPool)
return p
}
func (p post) replaceDB(db *gorm.DB) post {
p.postDo.ReplaceDB(db)
return p
}
type postBelongsToAuthor struct {
db *gorm.DB
field.RelationField
Posts struct {
field.RelationField
Author struct {
field.RelationField
}
PostType struct {
field.RelationField
}
Comments struct {
field.RelationField
Author struct {
field.RelationField
}
Posts struct {
field.RelationField
}
}
}
Comments struct {
field.RelationField
}
}
func (a postBelongsToAuthor) Where(conds ...field.Expr) *postBelongsToAuthor {
if len(conds) == 0 {
return &a
}
exprs := make([]clause.Expression, 0, len(conds))
for _, cond := range conds {
exprs = append(exprs, cond.BeCond().(clause.Expression))
}
a.db = a.db.Clauses(clause.Where{Exprs: exprs})
return &a
}
func (a postBelongsToAuthor) WithContext(ctx context.Context) *postBelongsToAuthor {
a.db = a.db.WithContext(ctx)
return &a
}
func (a postBelongsToAuthor) Session(session *gorm.Session) *postBelongsToAuthor {
a.db = a.db.Session(session)
return &a
}
func (a postBelongsToAuthor) Model(m *models.Post) *postBelongsToAuthorTx {
return &postBelongsToAuthorTx{a.db.Model(m).Association(a.Name())}
}
type postBelongsToAuthorTx struct{ tx *gorm.Association }
func (a postBelongsToAuthorTx) Find() (result *models.Author, err error) {
return result, a.tx.Find(&result)
}
func (a postBelongsToAuthorTx) Append(values ...*models.Author) (err error) {
targetValues := make([]interface{}, len(values))
for i, v := range values {
targetValues[i] = v
}
return a.tx.Append(targetValues...)
}
func (a postBelongsToAuthorTx) Replace(values ...*models.Author) (err error) {
targetValues := make([]interface{}, len(values))
for i, v := range values {
targetValues[i] = v
}
return a.tx.Replace(targetValues...)
}
func (a postBelongsToAuthorTx) Delete(values ...*models.Author) (err error) {
targetValues := make([]interface{}, len(values))
for i, v := range values {
targetValues[i] = v
}
return a.tx.Delete(targetValues...)
}
func (a postBelongsToAuthorTx) Clear() error {
return a.tx.Clear()
}
func (a postBelongsToAuthorTx) Count() int64 {
return a.tx.Count()
}
type postBelongsToPostType struct {
db *gorm.DB
field.RelationField
}
func (a postBelongsToPostType) Where(conds ...field.Expr) *postBelongsToPostType {
if len(conds) == 0 {
return &a
}
exprs := make([]clause.Expression, 0, len(conds))
for _, cond := range conds {
exprs = append(exprs, cond.BeCond().(clause.Expression))
}
a.db = a.db.Clauses(clause.Where{Exprs: exprs})
return &a
}
func (a postBelongsToPostType) WithContext(ctx context.Context) *postBelongsToPostType {
a.db = a.db.WithContext(ctx)
return &a
}
func (a postBelongsToPostType) Session(session *gorm.Session) *postBelongsToPostType {
a.db = a.db.Session(session)
return &a
}
func (a postBelongsToPostType) Model(m *models.Post) *postBelongsToPostTypeTx {
return &postBelongsToPostTypeTx{a.db.Model(m).Association(a.Name())}
}
type postBelongsToPostTypeTx struct{ tx *gorm.Association }
func (a postBelongsToPostTypeTx) Find() (result *models.PostType, err error) {
return result, a.tx.Find(&result)
}
func (a postBelongsToPostTypeTx) Append(values ...*models.PostType) (err error) {
targetValues := make([]interface{}, len(values))
for i, v := range values {
targetValues[i] = v
}
return a.tx.Append(targetValues...)
}
func (a postBelongsToPostTypeTx) Replace(values ...*models.PostType) (err error) {
targetValues := make([]interface{}, len(values))
for i, v := range values {
targetValues[i] = v
}
return a.tx.Replace(targetValues...)
}
func (a postBelongsToPostTypeTx) Delete(values ...*models.PostType) (err error) {
targetValues := make([]interface{}, len(values))
for i, v := range values {
targetValues[i] = v
}
return a.tx.Delete(targetValues...)
}
func (a postBelongsToPostTypeTx) Clear() error {
return a.tx.Clear()
}
func (a postBelongsToPostTypeTx) Count() int64 {
return a.tx.Count()
}
type postManyToManyComments struct {
db *gorm.DB
field.RelationField
}
func (a postManyToManyComments) Where(conds ...field.Expr) *postManyToManyComments {
if len(conds) == 0 {
return &a
}
exprs := make([]clause.Expression, 0, len(conds))
for _, cond := range conds {
exprs = append(exprs, cond.BeCond().(clause.Expression))
}
a.db = a.db.Clauses(clause.Where{Exprs: exprs})
return &a
}
func (a postManyToManyComments) WithContext(ctx context.Context) *postManyToManyComments {
a.db = a.db.WithContext(ctx)
return &a
}
func (a postManyToManyComments) Session(session *gorm.Session) *postManyToManyComments {
a.db = a.db.Session(session)
return &a
}
func (a postManyToManyComments) Model(m *models.Post) *postManyToManyCommentsTx {
return &postManyToManyCommentsTx{a.db.Model(m).Association(a.Name())}
}
type postManyToManyCommentsTx struct{ tx *gorm.Association }
func (a postManyToManyCommentsTx) Find() (result []*models.Comment, err error) {
return result, a.tx.Find(&result)
}
func (a postManyToManyCommentsTx) Append(values ...*models.Comment) (err error) {
targetValues := make([]interface{}, len(values))
for i, v := range values {
targetValues[i] = v
}
return a.tx.Append(targetValues...)
}
func (a postManyToManyCommentsTx) Replace(values ...*models.Comment) (err error) {
targetValues := make([]interface{}, len(values))
for i, v := range values {
targetValues[i] = v
}
return a.tx.Replace(targetValues...)
}
func (a postManyToManyCommentsTx) Delete(values ...*models.Comment) (err error) {
targetValues := make([]interface{}, len(values))
for i, v := range values {
targetValues[i] = v
}
return a.tx.Delete(targetValues...)
}
func (a postManyToManyCommentsTx) Clear() error {
return a.tx.Clear()
}
func (a postManyToManyCommentsTx) Count() int64 {
return a.tx.Count()
}
type postDo struct{ gen.DO }
type IPostDo interface {
gen.SubQuery
Debug() IPostDo
WithContext(ctx context.Context) IPostDo
WithResult(fc func(tx gen.Dao)) gen.ResultInfo
ReplaceDB(db *gorm.DB)
ReadDB() IPostDo
WriteDB() IPostDo
As(alias string) gen.Dao
Session(config *gorm.Session) IPostDo
Columns(cols ...field.Expr) gen.Columns
Clauses(conds ...clause.Expression) IPostDo
Not(conds ...gen.Condition) IPostDo
Or(conds ...gen.Condition) IPostDo
Select(conds ...field.Expr) IPostDo
Where(conds ...gen.Condition) IPostDo
Order(conds ...field.Expr) IPostDo
Distinct(cols ...field.Expr) IPostDo
Omit(cols ...field.Expr) IPostDo
Join(table schema.Tabler, on ...field.Expr) IPostDo
LeftJoin(table schema.Tabler, on ...field.Expr) IPostDo
RightJoin(table schema.Tabler, on ...field.Expr) IPostDo
Group(cols ...field.Expr) IPostDo
Having(conds ...gen.Condition) IPostDo
Limit(limit int) IPostDo
Offset(offset int) IPostDo
Count() (count int64, err error)
Scopes(funcs ...func(gen.Dao) gen.Dao) IPostDo
Unscoped() IPostDo
Create(values ...*models.Post) error
CreateInBatches(values []*models.Post, batchSize int) error
Save(values ...*models.Post) error
First() (*models.Post, error)
Take() (*models.Post, error)
Last() (*models.Post, error)
Find() ([]*models.Post, error)
FindInBatch(batchSize int, fc func(tx gen.Dao, batch int) error) (results []*models.Post, err error)
FindInBatches(result *[]*models.Post, batchSize int, fc func(tx gen.Dao, batch int) error) error
Pluck(column field.Expr, dest interface{}) error
Delete(...*models.Post) (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)
UpdateColumn(column field.Expr, value interface{}) (info gen.ResultInfo, err error)
UpdateColumnSimple(columns ...field.AssignExpr) (info gen.ResultInfo, err error)
UpdateColumns(value interface{}) (info gen.ResultInfo, err error)
UpdateFrom(q gen.SubQuery) gen.Dao
Attrs(attrs ...field.AssignExpr) IPostDo
Assign(attrs ...field.AssignExpr) IPostDo
Joins(fields ...field.RelationField) IPostDo
Preload(fields ...field.RelationField) IPostDo
FirstOrInit() (*models.Post, error)
FirstOrCreate() (*models.Post, error)
FindByPage(offset int, limit int) (result []*models.Post, 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) IPostDo
UnderlyingDB() *gorm.DB
schema.Tabler
}
func (p postDo) Debug() IPostDo {
return p.withDO(p.DO.Debug())
}
func (p postDo) WithContext(ctx context.Context) IPostDo {
return p.withDO(p.DO.WithContext(ctx))
}
func (p postDo) ReadDB() IPostDo {
return p.Clauses(dbresolver.Read)
}
func (p postDo) WriteDB() IPostDo {
return p.Clauses(dbresolver.Write)
}
func (p postDo) Session(config *gorm.Session) IPostDo {
return p.withDO(p.DO.Session(config))
}
func (p postDo) Clauses(conds ...clause.Expression) IPostDo {
return p.withDO(p.DO.Clauses(conds...))
}
func (p postDo) Returning(value interface{}, columns ...string) IPostDo {
return p.withDO(p.DO.Returning(value, columns...))
}
func (p postDo) Not(conds ...gen.Condition) IPostDo {
return p.withDO(p.DO.Not(conds...))
}
func (p postDo) Or(conds ...gen.Condition) IPostDo {
return p.withDO(p.DO.Or(conds...))
}
func (p postDo) Select(conds ...field.Expr) IPostDo {
return p.withDO(p.DO.Select(conds...))
}
func (p postDo) Where(conds ...gen.Condition) IPostDo {
return p.withDO(p.DO.Where(conds...))
}
func (p postDo) Order(conds ...field.Expr) IPostDo {
return p.withDO(p.DO.Order(conds...))
}
func (p postDo) Distinct(cols ...field.Expr) IPostDo {
return p.withDO(p.DO.Distinct(cols...))
}
func (p postDo) Omit(cols ...field.Expr) IPostDo {
return p.withDO(p.DO.Omit(cols...))
}
func (p postDo) Join(table schema.Tabler, on ...field.Expr) IPostDo {
return p.withDO(p.DO.Join(table, on...))
}
func (p postDo) LeftJoin(table schema.Tabler, on ...field.Expr) IPostDo {
return p.withDO(p.DO.LeftJoin(table, on...))
}
func (p postDo) RightJoin(table schema.Tabler, on ...field.Expr) IPostDo {
return p.withDO(p.DO.RightJoin(table, on...))
}
func (p postDo) Group(cols ...field.Expr) IPostDo {
return p.withDO(p.DO.Group(cols...))
}
func (p postDo) Having(conds ...gen.Condition) IPostDo {
return p.withDO(p.DO.Having(conds...))
}
func (p postDo) Limit(limit int) IPostDo {
return p.withDO(p.DO.Limit(limit))
}
func (p postDo) Offset(offset int) IPostDo {
return p.withDO(p.DO.Offset(offset))
}
func (p postDo) Scopes(funcs ...func(gen.Dao) gen.Dao) IPostDo {
return p.withDO(p.DO.Scopes(funcs...))
}
func (p postDo) Unscoped() IPostDo {
return p.withDO(p.DO.Unscoped())
}
func (p postDo) Create(values ...*models.Post) error {
if len(values) == 0 {
return nil
}
return p.DO.Create(values)
}
func (p postDo) CreateInBatches(values []*models.Post, batchSize int) error {
return p.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 (p postDo) Save(values ...*models.Post) error {
if len(values) == 0 {
return nil
}
return p.DO.Save(values)
}
func (p postDo) First() (*models.Post, error) {
if result, err := p.DO.First(); err != nil {
return nil, err
} else {
return result.(*models.Post), nil
}
}
func (p postDo) Take() (*models.Post, error) {
if result, err := p.DO.Take(); err != nil {
return nil, err
} else {
return result.(*models.Post), nil
}
}
func (p postDo) Last() (*models.Post, error) {
if result, err := p.DO.Last(); err != nil {
return nil, err
} else {
return result.(*models.Post), nil
}
}
func (p postDo) Find() ([]*models.Post, error) {
result, err := p.DO.Find()
return result.([]*models.Post), err
}
func (p postDo) FindInBatch(batchSize int, fc func(tx gen.Dao, batch int) error) (results []*models.Post, err error) {
buf := make([]*models.Post, 0, batchSize)
err = p.DO.FindInBatches(&buf, batchSize, func(tx gen.Dao, batch int) error {
defer func() { results = append(results, buf...) }()
return fc(tx, batch)
})
return results, err
}
func (p postDo) FindInBatches(result *[]*models.Post, batchSize int, fc func(tx gen.Dao, batch int) error) error {
return p.DO.FindInBatches(result, batchSize, fc)
}
func (p postDo) Attrs(attrs ...field.AssignExpr) IPostDo {
return p.withDO(p.DO.Attrs(attrs...))
}
func (p postDo) Assign(attrs ...field.AssignExpr) IPostDo {
return p.withDO(p.DO.Assign(attrs...))
}
func (p postDo) Joins(fields ...field.RelationField) IPostDo {
for _, _f := range fields {
p = *p.withDO(p.DO.Joins(_f))
}
return &p
}
func (p postDo) Preload(fields ...field.RelationField) IPostDo {
for _, _f := range fields {
p = *p.withDO(p.DO.Preload(_f))
}
return &p
}
func (p postDo) FirstOrInit() (*models.Post, error) {
if result, err := p.DO.FirstOrInit(); err != nil {
return nil, err
} else {
return result.(*models.Post), nil
}
}
func (p postDo) FirstOrCreate() (*models.Post, error) {
if result, err := p.DO.FirstOrCreate(); err != nil {
return nil, err
} else {
return result.(*models.Post), nil
}
}
func (p postDo) FindByPage(offset int, limit int) (result []*models.Post, count int64, err error) {
result, err = p.Offset(offset).Limit(limit).Find()
if err != nil {
return
}
if size := len(result); 0 < limit && 0 < size && size < limit {
count = int64(size + offset)
return
}
count, err = p.Offset(-1).Limit(-1).Count()
return
}
func (p postDo) ScanByPage(result interface{}, offset int, limit int) (count int64, err error) {
count, err = p.Count()
if err != nil {
return
}
err = p.Offset(offset).Limit(limit).Scan(result)
return
}
func (p postDo) Scan(result interface{}) (err error) {
return p.DO.Scan(result)
}
func (p postDo) Delete(models ...*models.Post) (result gen.ResultInfo, err error) {
return p.DO.Delete(models)
}
func (p *postDo) withDO(do gen.Dao) *postDo {
p.DO = *do.(*gen.DO)
return p
}