Skip to content

Commit

Permalink
feat: page query api add order condition
Browse files Browse the repository at this point in the history
  • Loading branch information
xvrzhao committed Dec 6, 2024
1 parent 344f614 commit e5c98ad
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 2 deletions.
2 changes: 1 addition & 1 deletion cmd/gencode/templates/svc.go.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func ({{.lowerCamelModel}}Svc) Page(offset, limit int) ({{.lowerCamelModel}}s []
}

{{.lowerCamelModel}}s = make([]model.{{.upperCamelModel}}, 0, limit)
if err := db.Client.Limit(limit).Offset(offset).Preload(clause.Associations).Find(&{{.lowerCamelModel}}s).Error; err != nil {
if err := db.Client.Order("created_at desc").Limit(limit).Offset(offset).Preload(clause.Associations).Find(&{{.lowerCamelModel}}s).Error; err != nil {
return nil, 0, err
}
return {{.lowerCamelModel}}s, total, nil
Expand Down
27 changes: 27 additions & 0 deletions db/migration/README-CN.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
建表注意事项:

1. 除了多对多关联表之外,其他所有表都应具备 `created_at``updated_at``deleted_at` 字段,建表语句模版:

```sql
create table table_name (
id serial primary key not null,

-- other columns...

created_at timestamptz not null default now(),
updated_at timestamptz not null default now(),
deleted_at timestamptz null default null
);
```

2. 多对多关联表只需具备两个关联 ID 字段,不应该包含 `created_at``updated_at``deleted_at` 字段。同时,关联表只需建表,无需定义 Model,需要进行多对多关联的 Model 只需要声明 `gorm:"many2many:xxx"` 标签即可,具体使用请查阅 GORM 文档。以下是用户角色关联表的例子:

```sql
create table users_roles (
user_id int not null,
role_id int not null
);
create unique index users_roles_user_id_role_id on users_roles(user_id, role_id);
```

3. Go 结构体字段默认取零值,为了方便处理请求参数验证,尽量不要使用零值作为数据表字段的值。例如,使用 `int` 类型的 `1 2` 代替 `bool` 类型的 `true false`,因为收到 `false` 无法判知是否为用户漏传。
27 changes: 27 additions & 0 deletions db/migration/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
Notes on table creation:

1. Except for many-to-many association tables, all other tables should have `created_at`, `updated_at` and `deleted_at` fields. Table creation statement template:

```sql
create table table_name (
id serial primary key not null,

-- other columns...

created_at timestamptz not null default now(),
updated_at timestamptz not null default now(),
deleted_at timestamptz null default null
);
```

2. Many-to-many association tables only need two association ID fields and should not contain `created_at`, `updated_at` and `deleted_at` fields. And association tables don't need not to define models. The model that need many-to-many associations only need to declare the `gorm:"many2many:xxx"` tag. For specific usage, please refer to the GORM documentation. The following is an example of a user-role association table:
```sql
create table users_roles (
user_id int not null,
role_id int not null
);
create unique index users_roles_user_id_role_id on users_roles(user_id, role_id);
```
3. Go structure fields take zero values ​​by default. To facilitate request parameter validation, try not to use zero values ​​as the values ​​of data table fields. For example, use `1 2` of type `int` instead of `true false` of type `bool`, because if `false` is received, it is impossible to determine whether it is a user miss.
2 changes: 1 addition & 1 deletion handler/user_svc.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (userSvc) Page(offset, limit int) (users []model.User, total int64, err err
}

users = make([]model.User, 0, limit)
if err := db.Client.Limit(limit).Offset(offset).Find(&users).Error; err != nil {
if err := db.Client.Order("created_at desc").Limit(limit).Offset(offset).Find(&users).Error; err != nil {
return nil, 0, err
}
return users, total, nil
Expand Down

0 comments on commit e5c98ad

Please sign in to comment.