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

‌
‌
‌
‌

Service Layer

The Service layer carries business logic and is the only place that touches the database directly. It's called by the API layer and never touches gin.Context — anything request-related is parsed by the API layer and passed in.

INFO

Single-table CRUD doesn't need this layer. The generic Actions in common/actions already cover it, and a module only needs three files — model, dto, router. See Standard Module Development.

This page is for logic that outgrows single-table CRUD — cross-table transactions, calls to external services, complex validation.

import

go
package service
import (
"errors"
"github.com/go-admin-team/go-admin-core/sdk/service"
"gorm.io/gorm"
"go-admin/app/admin/models"
"go-admin/app/admin/service/dto"
cDto "go-admin/common/dto"
)

Defining the Business Struct

go
type SysPost struct {
service.Service
}

GetList

GetList handles the business logic for the paginated list endpoint, using the related dto and models functions.

Example:

go
// GetPage fetches the SysPost list
func (e *SysPost) GetPage(c *dto.SysPostPageReq, list *[]models.SysPost, count *int64) error {
var err error
var data models.SysPost
err = e.Orm.Model(&data).
Scopes(
cDto.MakeCondition(c.GetNeedSearch()),
cDto.Paginate(c.GetPageSize(), c.GetPageIndex()),
).
Find(list).Limit(-1).Offset(-1).
Count(count).Error
if err != nil {
e.Log.Errorf("db error:%s \r", err)
return err
}
return nil
}

Get

Get handles the business logic for fetching a single record by ID, using the related dto and models functions.

Example:

go
// Get fetches a SysPost object
func (e *SysPost) Get(d *dto.SysPostGetReq, model *models.SysPost) error {
var err error
var data models.SysPost
db := e.Orm.Model(&data).
First(model, d.GetId())
err = db.Error
if err != nil && errors.Is(err, gorm.ErrRecordNotFound) {
err = errors.New("查看对象不存在或无权查看")
e.Log.Errorf("db error:%s", err)
return err
}
if db.Error != nil {
e.Log.Errorf("db error:%s", err)
return err
}
return nil
}

Post

Post handles the business logic for creating a single record, using the related dto and models functions.

Example:

go
// Insert creates a SysPost object
func (e *SysPost) Insert(c *dto.SysPostInsertReq) error {
var err error
var data models.SysPost
c.Generate(&data)
err = e.Orm.Create(&data).Error
if err != nil {
e.Log.Errorf("db error:%s", err)
return err
}
return nil
}

Put

Put handles the business logic for updating a single record, using the related dto and models functions.

Example:

go
// Update modifies a SysPost object
func (e *SysPost) Update(c *dto.SysPostUpdateReq) error {
var err error
var model = models.SysPost{}
e.Orm.First(&model, c.GetId())
c.Generate(&model)
db := e.Orm.Save(&model)
if db.Error != nil {
e.Log.Errorf("db error:%s", err)
return err
}
if db.RowsAffected == 0 {
return errors.New("无权更新该数据")
}
return nil
}

Delete

Delete handles the business logic for deleting a single record, using the related dto and models functions.

Example:

go
// Remove deletes a SysPost
func (e *SysPost) Remove(d *dto.SysPostDeleteReq) error {
var err error
var data models.SysPost
db := e.Orm.Model(&data).Delete(&data, d.GetId())
if db.Error != nil {
err = db.Error
e.Log.Errorf("Delete error: %s", err)
return err
}
if db.RowsAffected == 0 {
err = errors.New("无权删除该数据")
return err
}
return nil
}

WARNING

Where to get help:

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