feat: sample data

This commit is contained in:
2025-03-08 20:06:06 +07:00
parent b4ea46140d
commit 9ea91f3b40
13 changed files with 396 additions and 73 deletions

View File

@@ -28,6 +28,24 @@ func newAuthor(db *gorm.DB, opts ...gen.DOOption) author {
_author.ALL = field.NewAsterisk(tableName)
_author.Id = field.NewUint(tableName, "id")
_author.Name = field.NewString(tableName, "name")
_author.Posts = authorHasManyPosts{
db: db.Session(&gorm.Session{}),
RelationField: field.NewRelation("Posts", "models.Post"),
Author: struct {
field.RelationField
Posts struct {
field.RelationField
}
}{
RelationField: field.NewRelation("Posts.Author", "models.Author"),
Posts: struct {
field.RelationField
}{
RelationField: field.NewRelation("Posts.Author.Posts", "models.Post"),
},
},
}
_author.fillFieldMap()
@@ -37,9 +55,10 @@ func newAuthor(db *gorm.DB, opts ...gen.DOOption) author {
type author struct {
authorDo
ALL field.Asterisk
Id field.Uint
Name field.String
ALL field.Asterisk
Id field.Uint
Name field.String
Posts authorHasManyPosts
fieldMap map[string]field.Expr
}
@@ -74,9 +93,10 @@ func (a *author) GetFieldByName(fieldName string) (field.OrderExpr, bool) {
}
func (a *author) fillFieldMap() {
a.fieldMap = make(map[string]field.Expr, 2)
a.fieldMap = make(map[string]field.Expr, 3)
a.fieldMap["id"] = a.Id
a.fieldMap["name"] = a.Name
}
func (a author) clone(db *gorm.DB) author {
@@ -89,6 +109,84 @@ func (a author) replaceDB(db *gorm.DB) author {
return a
}
type authorHasManyPosts struct {
db *gorm.DB
field.RelationField
Author struct {
field.RelationField
Posts struct {
field.RelationField
}
}
}
func (a authorHasManyPosts) Where(conds ...field.Expr) *authorHasManyPosts {
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 authorHasManyPosts) WithContext(ctx context.Context) *authorHasManyPosts {
a.db = a.db.WithContext(ctx)
return &a
}
func (a authorHasManyPosts) Session(session *gorm.Session) *authorHasManyPosts {
a.db = a.db.Session(session)
return &a
}
func (a authorHasManyPosts) Model(m *models.Author) *authorHasManyPostsTx {
return &authorHasManyPostsTx{a.db.Model(m).Association(a.Name())}
}
type authorHasManyPostsTx struct{ tx *gorm.Association }
func (a authorHasManyPostsTx) Find() (result []*models.Post, err error) {
return result, a.tx.Find(&result)
}
func (a authorHasManyPostsTx) Append(values ...*models.Post) (err error) {
targetValues := make([]interface{}, len(values))
for i, v := range values {
targetValues[i] = v
}
return a.tx.Append(targetValues...)
}
func (a authorHasManyPostsTx) Replace(values ...*models.Post) (err error) {
targetValues := make([]interface{}, len(values))
for i, v := range values {
targetValues[i] = v
}
return a.tx.Replace(targetValues...)
}
func (a authorHasManyPostsTx) Delete(values ...*models.Post) (err error) {
targetValues := make([]interface{}, len(values))
for i, v := range values {
targetValues[i] = v
}
return a.tx.Delete(targetValues...)
}
func (a authorHasManyPostsTx) Clear() error {
return a.tx.Clear()
}
func (a authorHasManyPostsTx) Count() int64 {
return a.tx.Count()
}
type authorDo struct{ gen.DO }
type IAuthorDo interface {

View File

@@ -28,7 +28,27 @@ func newPost(db *gorm.DB, opts ...gen.DOOption) post {
_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.Author = postBelongsToAuthor{
db: db.Session(&gorm.Session{}),
RelationField: field.NewRelation("Author", "models.Author"),
Posts: struct {
field.RelationField
Author struct {
field.RelationField
}
}{
RelationField: field.NewRelation("Author.Posts", "models.Post"),
Author: struct {
field.RelationField
}{
RelationField: field.NewRelation("Author.Posts.Author", "models.Author"),
},
},
}
_post.fillFieldMap()
@@ -41,7 +61,10 @@ type post struct {
ALL field.Asterisk
Id field.Uint
Text field.String
Deadline field.Int64
CreatedAt field.Int64
AuthorId field.Uint
Author postBelongsToAuthor
fieldMap map[string]field.Expr
}
@@ -60,7 +83,9 @@ 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.fillFieldMap()
@@ -77,10 +102,13 @@ func (p *post) GetFieldByName(fieldName string) (field.OrderExpr, bool) {
}
func (p *post) fillFieldMap() {
p.fieldMap = make(map[string]field.Expr, 3)
p.fieldMap = make(map[string]field.Expr, 6)
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
}
func (p post) clone(db *gorm.DB) post {
@@ -93,6 +121,84 @@ func (p post) replaceDB(db *gorm.DB) post {
return p
}
type postBelongsToAuthor struct {
db *gorm.DB
field.RelationField
Posts struct {
field.RelationField
Author 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 postDo struct{ gen.DO }
type IPostDo interface {

View File

@@ -7,10 +7,16 @@ var Entities = []any{
type Post struct {
Id uint `gorm:"primaryKey" ui:"hidden"`
Text string `displayName:"Текст" ui:"label=Текст"`
Deadline int64 `ui:"label=Дедлайн"`
CreatedAt int64 `gorm:"autoCreateTime" ui:"hidden"`
AuthorId uint
Author Author `ui:"label=Автор, data=Author, field=[Name]"`
}
type Author struct {
Id uint `gorm:"primaryKey" ui:"hidden"`
Name string `ui:"label=Имя"`
Id uint `gorm:"primaryKey" ui:"hidden"`
Name string `ui:"label=Имя"`
Posts []Post `ui:"label=Посты, data=Post, field=[Text]"`
}
// TODO: correct processing the semicolon (get attention to quotes)

View File

@@ -3,11 +3,12 @@ package services
import (
"app/internal/dialogs"
"fmt"
"time"
)
func InsertDefaultData() {
insertPosts()
insertAuthors()
insertPosts()
}
func InsertDefaultEntityData[T any](service Service[T], entities []T) {
@@ -22,16 +23,22 @@ func InsertDefaultEntityData[T any](service Service[T], entities []T) {
func insertPosts() {
InsertDefaultEntityData(&PostService{}, []Post{
{
Id: 1,
Text: "Жителям Кузбасса запретили болеть.",
Id: 1,
Text: "Жителям Кузбасса запретили болеть.",
Deadline: time.Now().Unix(),
AuthorId: 1,
},
{
Id: 2,
Text: "⚡️⚡️⚡️Дома будут летать.",
Id: 2,
Deadline: time.Now().Add(time.Hour * 24 * 5).Unix(),
Text: "⚡️⚡️⚡️Дома будут летать.",
AuthorId: 2,
},
{
Id: 3,
Text: "В Кузбассе начали строить дома выше, чтобы жители были ближе к богу и солнцу.",
Id: 3,
Deadline: time.Now().Add(time.Hour * 24 * 6).Unix(),
Text: "В Кузбассе начали строить дома выше, чтобы жители были ближе к богу и солнцу.",
AuthorId: 3,
},
})
}
@@ -42,5 +49,13 @@ func insertAuthors() {
Id: 1,
Name: "ИА Кузбасс",
},
{
Id: 2,
Name: "ASTRA",
},
{
Id: 3,
Name: "ЧТД",
},
})
}