go-admingo-admin
  • Guide
  • Development
    • Advanced
    • Commands
  • Advanced
  • Help
  • GitHub
  • Changelog
⌘ K
Standard Practice
Standard Module Development
Backend Basics
Backend Directory Structure
Backend Config File
Starting the Backend
Frontend Basics
Frontend Directory Structure
Frontend Config File
Starting the Frontend
Development Patterns
Actions Pattern
Hand-Written Pattern
First API Endpoint
Layered Development
API Layer
Service Layer
DTO Definitions
Model Definitions
Router Registration
Multi-Environment Config
Database Table Conventions
Data Permissions
Response Format
Code Generation
Pre-Generation Setup
Generating Business Code
One-Click Menu Generation
Binding APIs to the Menu
Configuring Role Permissions
Verifying the Feature
Generating Code with an LLM
Advanced Capabilities
Runtime Core API
Authentication & Authorization
Logging
Request Tracing
Cache
Queue
File Upload
Rate Limiting
Scheduled Jobs
Air Hot Reload
Swagger Docs
Code Generation Tool
Last updated:
Open-source MIT Licensed | Copyright © 2020-present
Powered by go-admin-team

TABLE OF CONTENTS

‌
‌
‌
‌

DTO Definitions

A DTO (Data Transfer Object) describes the data a request carries: fields, validation rules, and the search conditions for a list query. A request is received and validated by its DTO before moving further into the code.

go-admin has three DTO shapes: Search for list queries, Control for create/update, and ById for by-ID operations.

INFO

Every module needs this layer, whether it uses the Actions Pattern or the Hand-Written Pattern.

Import

go
package dto
import (
"go-admin/app/admin/models"
common "go-admin/common/models"
"go-admin/common/dto"
)

The Search Struct

First declare the properties a search request accepts, then list them out one by one.

MethodDescription
GetNeedSearchImplements the GORM scope

Example:

go
// SysPostPageReq is used for list/search requests
type SysPostPageReq struct {
dto.Pagination `search:"-"`
PostId int `form:"postId" search:"type:exact;column:post_id;table:sys_post" comment:"id"` // id
PostName string `form:"postName" search:"type:contains;column:post_name;table:sys_post" comment:"名称"` // name
PostCode string `form:"postCode" search:"type:contains;column:post_code;table:sys_post" comment:"编码"` // code
Sort int `form:"sort" search:"type:exact;column:sort;table:sys_post" comment:"排序"` // sort order
Status int `form:"status" search:"type:exact;column:status;table:sys_post" comment:"状态"` // status
Remark string `form:"remark" search:"type:exact;column:remark;table:sys_post" comment:"备注"` // remark
}
func (m *SysPostPageReq) GetNeedSearch() interface{} {
return *m
}

struct Tags

Covering just the search tag's own content:

TagDescription
typeoperation type
columnthe database column name
tablethe database table name

type Values

typeMeaningQuery example
exact/iexactequalsstatus=1
gltnot equalspostName<>1
contains/icontaninscontainsname=n
gt/gtegreater than / greater than or equalage=18
lt/lteless than / less than or equalage=18
startswith/istartswithstarts withcontent=hell
endswith/iendswithends withcontent=world
inin-list querystatus=0&status=1
isnullis-null querystartTime=1
ordersort ordersort=asc/sort=desc
joinjoin-

Example:

go
type ApplicationQuery struct {
Id string `search:"type:icontains;column:id;table:receipt" form:"id"`
Domain string `search:"type:icontains;column:domain;table:receipt" form:"domain"`
Version string `search:"type:exact;column:version;table:receipt" form:"version"`
Status []int `search:"type:in;column:status;table:receipt" form:"status"`
Start time.Time `search:"type:gte;column:created_at;table:receipt" form:"start"`
End time.Time `search:"type:lte;column:created_at;table:receipt" form:"end"`
TestJoin `search:"type:left;on:id:receipt_id;table:receipt_goods;join:receipts"`
ApplicationOrder
}
type ApplicationOrder struct {
IdOrder string `search:"type:order;column:id;table:receipt" form"id_order"`
}
type TestJoin struct {
PaymentAccount string `search:"type:icontains;column:payment_account;table:receipts" form:"payment_account"`
}

The Insert Struct

MethodDescription
Generatemaps into the model
GetIdreturns the record's id
go
// SysPostInsertReq is used for create requests
type SysPostInsertReq struct {
PostId int `uri:"id" comment:"id"`
PostName string `form:"postName" comment:"名称"`
PostCode string `form:"postCode" comment:"编码"`
Sort int `form:"sort" comment:"排序"`
Status int `form:"status" comment:"状态"`
Remark string `form:"remark" comment:"备注"`
common.ControlBy
}
func (s *SysPostInsertReq) Generate(model *models.SysPost) {
model.PostName = s.PostName
model.PostCode = s.PostCode
model.Sort = s.Sort
model.Status = s.Status
model.Remark = s.Remark
if s.ControlBy.UpdateBy != 0 {
model.UpdateBy = s.UpdateBy
}
if s.ControlBy.CreateBy != 0 {
model.CreateBy = s.CreateBy
}
}
// GetId returns the record's ID
func (s *SysPostInsertReq) GetId() interface{} {
return s.PostId
}

The Update Struct

MethodDescription
Generatemaps into the model
GetIdreturns the record's id
go
// SysPostUpdateReq is used for update requests
type SysPostUpdateReq struct {
PostId int `uri:"id" comment:"id"`
PostName string `form:"postName" comment:"名称"`
PostCode string `form:"postCode" comment:"编码"`
Sort int `form:"sort" comment:"排序"`
Status int `form:"status" comment:"状态"`
Remark string `form:"remark" comment:"备注"`
common.ControlBy
}
func (s *SysPostUpdateReq) Generate(model *models.SysPost) {
model.PostId = s.PostId
model.PostName = s.PostName
model.PostCode = s.PostCode
model.Sort = s.Sort
model.Status = s.Status
model.Remark = s.Remark
if s.ControlBy.UpdateBy != 0 {
model.UpdateBy = s.UpdateBy
}
if s.ControlBy.CreateBy != 0 {
model.CreateBy = s.CreateBy
}
}
func (s *SysPostUpdateReq) GetId() interface{} {
return s.PostId
}

The Get Struct

MethodDescription
GetIdreturns the record's id
go
// SysPostGetReq is used to fetch a single record
type SysPostGetReq struct {
Id int `uri:"id"`
}
func (s *SysPostGetReq) GetId() interface{} {
return s.Id
}

The Delete Struct

MethodDescription
Generatemaps into the model
GetIdreturns the record's id
go
// SysPostDeleteReq is used for delete requests
type SysPostDeleteReq struct {
Ids []int `json:"ids"`
common.ControlBy
}
func (s *SysPostDeleteReq) Generate(model *models.SysPost) {
if s.ControlBy.UpdateBy != 0 {
model.UpdateBy = s.UpdateBy
}
if s.ControlBy.CreateBy != 0 {
model.CreateBy = s.CreateBy
}
}
func (s *SysPostDeleteReq) GetId() interface{} {
return s.Ids
}

WARNING

Where to get help:

If anything in this guide is unclear, please open an issue.