| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- package main
- import (
- "git.sxidc.com/go-framework/baize"
- "git.sxidc.com/go-framework/baize/application"
- "git.sxidc.com/go-framework/baize/binding"
- "git.sxidc.com/go-framework/baize/binding/request"
- "git.sxidc.com/go-framework/baize/convenient/entity"
- "git.sxidc.com/go-framework/baize/domain"
- "git.sxidc.com/go-framework/baize/infrastructure"
- "git.sxidc.com/go-framework/baize/infrastructure/database/operations"
- DEATH "github.com/vrecan/death"
- "syscall"
- "time"
- )
- // curl -X POST -H "Content-Type: application/json" -d '{"name":"test", "studentNum": 10}' "http://localhost:10100/test/v1/class/create"
- // curl -X PUT -H "Content-Type: application/json" -d '{"id":"1a8d5cf5c4574430903e7cfcf2f13e4f", "name":"test-new"}' "http://localhost:10100/test/v1/class/update"
- // curl -X GET "http://localhost:10100/test/v1/class/query?name=test-new&pageNo=1&pageSize=1"
- // curl -X GET "http://localhost:10100/test/v1/class/get?id=1a8d5cf5c4574430903e7cfcf2f13e4f"
- // curl -X DELETE "http://localhost:10100/test/v1/class/1a8d5cf5c4574430903e7cfcf2f13e4f/delete"
- type CreateClassJsonBody struct {
- Name string `json:"name" binding:"required" assign:"toField:Name"`
- StudentNum int `json:"studentNum" binding:"required" assign:"toField:StudentNum"`
- }
- type DeleteClassPathParams struct {
- request.IDPath
- }
- type UpdateClassJsonBody struct {
- request.IDJsonBody
- Name string `json:"name" assign:"toField:Name"`
- StudentNum int `json:"studentNum" assign:"toField:StudentNum"`
- }
- type QueryClassesQueryParams struct {
- Name string `form:"name" assign:"toField:Name"`
- StudentNum int `form:"studentNum" assign:"toField:StudentNum"`
- request.BaseQuery
- }
- type GetClassQueryParams struct {
- request.IDQuery
- }
- type Class struct {
- domain.BaseEntity
- Name string `sqlmapping:"column:name"`
- StudentNum int `sqlmapping:"column:student_num"`
- CreatedTime time.Time
- LastUpdatedTime *time.Time
- }
- func (class *Class) DomainCNName() string {
- return "班级"
- }
- type InfoIDField struct {
- ID string `json:"id" sqlresult:"column:id"`
- }
- type ClassInfo struct {
- *InfoIDField
- Name string `json:"name" sqlresult:"column:name"`
- StudentNum int `json:"studentNum" sqlresult:"column:student_num"`
- CreatedTime string `sqlresult:"parseTime:'2006-01-02 15:04:05'"`
- LastUpdatedTime string `sqlresult:"parseTime:'2006-01-02 15:04:05'"`
- }
- const (
- tableName = "test.classes"
- )
- func main() {
- app := baize.NewApplication(application.Config{
- ApiConfig: application.ApiConfig{
- UrlPrefix: "test",
- Port: "10100",
- },
- InfrastructureConfig: application.InfrastructureConfig{
- Database: infrastructure.DatabaseConfig{
- Operations: &operations.Config{
- UserName: "test",
- Password: "123456",
- Address: "localhost",
- Port: "30432",
- Database: "test",
- MaxConnections: 40,
- MaxIdleConnections: 10,
- },
- },
- },
- })
- defer func() {
- baize.DestroyApplication(app)
- }()
- dbOperations := app.Infrastructure().DBOperations()
- err := dbOperations.AutoMigrate(operations.Table{
- TableName: tableName,
- Columns: []operations.TableColumn{
- {
- Name: "id",
- Type: "varchar(32)",
- Comment: "id",
- PrimaryKey: true,
- },
- {
- Name: "name",
- Type: "varchar(128)",
- Comment: "名称",
- NotNull: true,
- Index: true,
- },
- {
- Name: "student_num",
- Type: "integer",
- Comment: "学生数量",
- NotNull: true,
- Index: true,
- },
- {
- Name: "created_time",
- Type: "timestamp with time zone",
- Comment: "创建时间",
- NotNull: true,
- Index: true,
- },
- {
- Name: "last_updated_time",
- Type: "timestamp with time zone",
- Comment: "更新时间",
- NotNull: true,
- Index: true,
- },
- },
- })
- if err != nil {
- panic(err)
- }
- app.Api().
- PrefixRouter().
- RegisterVersionedRouter("v1")
- entity.BindSimple[ClassInfo](app.Binder("v1"), &entity.Simple[ClassInfo]{
- Entity: &Class{},
- TableName: tableName,
- DBExecutorType: binding.DBExecutorOperations,
- DomainPath: "/class",
- CreateJsonBody: &CreateClassJsonBody{},
- DeleteQueryParams: &DeleteClassPathParams{},
- UpdateJsonBody: &UpdateClassJsonBody{},
- QueryParams: &QueryClassesQueryParams{},
- GetByIDQueryParams: &GetClassQueryParams{},
- }, entity.WithCreateTx[ClassInfo]())
- go func() {
- err := app.Start()
- if err != nil {
- panic(err)
- }
- }()
- defer func() {
- err := app.Finish()
- if err != nil {
- panic(err)
- }
- }()
- death := DEATH.NewDeath(syscall.SIGINT, syscall.SIGTERM)
- _ = death.WaitForDeath()
- }
|