补充泛型函数
This commit is contained in:
parent
665fde6609
commit
4d5b597380
@ -67,3 +67,14 @@ func (c *BookController) CreateBook(ctx context.Context, in *models.BookSpec) (*
|
||||
|
||||
return bookInstance, nil
|
||||
}
|
||||
|
||||
func (c *BookController) UpdateBook() {
|
||||
// update(obj)
|
||||
// config.DB().Updates()
|
||||
}
|
||||
|
||||
func (c *BookController) update(ctx context.Context, obj models.Book) error {
|
||||
// obj.UpdateTime = now()
|
||||
// config.DB().Updates()
|
||||
return nil
|
||||
}
|
||||
|
@ -9,6 +9,16 @@ type BookSet struct {
|
||||
Items []*Book `json:"items"`
|
||||
}
|
||||
|
||||
type BookSpec struct {
|
||||
// type 用于要使用gorm 来自动创建和更新表的时候 才需要定义
|
||||
Title string `json:"title" gorm:"column:title;type:varchar(200)" validate:"required"`
|
||||
Author string `json:"author" gorm:"column:author;type:varchar(200);index" validate:"required"`
|
||||
Price float64 `json:"price" gorm:"column:price" validate:"required"`
|
||||
// bool false
|
||||
// nil 是零值, false
|
||||
IsSale *bool `json:"is_sale" gorm:"column:is_sale"`
|
||||
}
|
||||
|
||||
type Book struct {
|
||||
// 对象Id
|
||||
Id uint `json:"id" gorm:"primaryKey;column:id"`
|
||||
@ -20,16 +30,6 @@ func (b *Book) String() string {
|
||||
return pretty.ToJSON(b)
|
||||
}
|
||||
|
||||
type BookSpec struct {
|
||||
// type 用于要使用gorm 来自动创建和更新表的时候 才需要定义
|
||||
Title string `json:"title" gorm:"column:title;type:varchar(200)" validate:"required"`
|
||||
Author string `json:"author" gorm:"column:author;type:varchar(200);index" validate:"required"`
|
||||
Price float64 `json:"price" gorm:"column:price" validate:"required"`
|
||||
// bool false
|
||||
// nil 是零值, false
|
||||
IsSale *bool `json:"is_sale" gorm:"column:is_sale"`
|
||||
}
|
||||
|
||||
// books
|
||||
func (b *Book) TableName() string {
|
||||
return "books"
|
||||
|
@ -10,6 +10,7 @@ import (
|
||||
// 2. 正常直接返回数据, Restful接口 怎么知道这些请求是成功还是失败喃? 通过HTTP判断 2xx
|
||||
// 如果后面 所有的返回数据 要进过特殊处理,都在这个函数内进行扩展,方便维护,比如 数据脱敏
|
||||
func OK(ctx *gin.Context, data any) {
|
||||
// v, ok := data.(Densener)
|
||||
ctx.JSON(200, data)
|
||||
ctx.Abort()
|
||||
}
|
||||
|
@ -1 +1,19 @@
|
||||
# 业务分区架构(基于mcube)
|
||||
|
||||
mcube 与 Ioc
|
||||
|
||||

|
||||
|
||||
更新部分:
|
||||
1. 单元测试 支持通过环境变量注入,优化单元测试配置,共用一套配置
|
||||
2. 新增Book Api项目, 从简单的脚本开发->配置分离->mvc模式->ioc业务分区 经历4个版本,讲解如何开发复杂项目。
|
||||
3. Vblog项目 新增部署,支持2中部署模式,1.前后端分离部署 与 前后端打包一体的部署。
|
||||
4. 优化其他几个项目,支持 可以通过 import的方式,快速使用。
|
||||
5. cmdb 云商凭证 支持加密存储
|
||||
|
||||
|
||||
## 业务分区的第一步 定义业务(RPC)
|
||||
|
||||
Book/Comment: 这个业务模块提供的功能
|
||||
|
||||
|
||||
|
1
book/v4/apps/README.md
Normal file
1
book/v4/apps/README.md
Normal file
@ -0,0 +1 @@
|
||||
# 业务分区
|
12
book/v4/apps/book/README.md
Normal file
12
book/v4/apps/book/README.md
Normal file
@ -0,0 +1,12 @@
|
||||
# Book 业务分区
|
||||
|
||||
定义Book业务逻辑
|
||||
|
||||
业务功能: CRUD
|
||||
1. 创建书籍(录入)
|
||||
2. Book列表查询
|
||||
3. Book详情查询
|
||||
4. Book更新
|
||||
5. Book删除
|
||||
|
||||
通过Go语言的里面的接口 来定义描述业务功能
|
53
book/v4/apps/book/interface.go
Normal file
53
book/v4/apps/book/interface.go
Normal file
@ -0,0 +1,53 @@
|
||||
package book
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/infraboard/mcube/v2/types"
|
||||
)
|
||||
|
||||
// book.Service, Book的业务定义
|
||||
type Service interface {
|
||||
// 1. 创建书籍(录入)
|
||||
CreateBook(context.Context, *CreateBookRequest) (*Book, error)
|
||||
// 2. Book列表查询
|
||||
QueryBook(context.Context, *QueryBookRequest) (*types.Set[*Book], error)
|
||||
// 3. Book详情查询
|
||||
// 4. Book更新
|
||||
// 5. Book删除
|
||||
}
|
||||
|
||||
type BookSet struct {
|
||||
// 总共多少个
|
||||
Total int64 `json:"total"`
|
||||
// book清单
|
||||
Items []*Book `json:"items"`
|
||||
}
|
||||
|
||||
func (b *BookSet) Add(item *Book) {
|
||||
b.Items = append(b.Items, item)
|
||||
}
|
||||
|
||||
// type CommentSet struct {
|
||||
// // 总共多少个
|
||||
// Total int64 `json:"total"`
|
||||
// // book清单
|
||||
// Items []*Comment `json:"items"`
|
||||
// }
|
||||
|
||||
// func (b *CommentSet) Add(item *Comment) {
|
||||
// b.Items = append(b.Items, item)
|
||||
// }
|
||||
|
||||
type CreateBookRequest struct {
|
||||
// type 用于要使用gorm 来自动创建和更新表的时候 才需要定义
|
||||
Title string `json:"title" gorm:"column:title;type:varchar(200)" validate:"required"`
|
||||
Author string `json:"author" gorm:"column:author;type:varchar(200);index" validate:"required"`
|
||||
Price float64 `json:"price" gorm:"column:price" validate:"required"`
|
||||
// bool false
|
||||
// nil 是零值, false
|
||||
IsSale *bool `json:"is_sale" gorm:"column:is_sale"`
|
||||
}
|
||||
|
||||
type QueryBookRequest struct {
|
||||
}
|
19
book/v4/apps/book/model.go
Normal file
19
book/v4/apps/book/model.go
Normal file
@ -0,0 +1,19 @@
|
||||
package book
|
||||
|
||||
import "github.com/infraboard/mcube/v2/tools/pretty"
|
||||
|
||||
type Book struct {
|
||||
// 对象Id
|
||||
Id uint `json:"id" gorm:"primaryKey;column:id"`
|
||||
|
||||
CreateBookRequest
|
||||
}
|
||||
|
||||
func (b *Book) String() string {
|
||||
return pretty.ToJSON(b)
|
||||
}
|
||||
|
||||
// books
|
||||
func (b *Book) TableName() string {
|
||||
return "books"
|
||||
}
|
16
book/v4/arch.drawio
Normal file
16
book/v4/arch.drawio
Normal file
@ -0,0 +1,16 @@
|
||||
<mxfile host="65bd71144e">
|
||||
<diagram id="Ak93bPvnUy-TIIMCp3wx" name="第 1 页">
|
||||
<mxGraphModel dx="777" dy="534" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="827" pageHeight="1169" math="0" shadow="0">
|
||||
<root>
|
||||
<mxCell id="0"/>
|
||||
<mxCell id="1" parent="0"/>
|
||||
<mxCell id="2" value="Book" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||
<mxGeometry x="90" y="260" width="170" height="90" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="3" value="Comment" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||
<mxGeometry x="380" y="260" width="170" height="90" as="geometry"/>
|
||||
</mxCell>
|
||||
</root>
|
||||
</mxGraphModel>
|
||||
</diagram>
|
||||
</mxfile>
|
BIN
book/v4/image.png
Normal file
BIN
book/v4/image.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 61 KiB |
43
skills/generate/generate.go
Normal file
43
skills/generate/generate.go
Normal file
@ -0,0 +1,43 @@
|
||||
package generate
|
||||
|
||||
func NewBookSet() *BookSet {
|
||||
return &BookSet{}
|
||||
}
|
||||
|
||||
type BookSet struct {
|
||||
// 总共多少个
|
||||
Total int64 `json:"total"`
|
||||
// book清单
|
||||
Items []string `json:"items"`
|
||||
}
|
||||
|
||||
func (b *BookSet) Add(item string) {
|
||||
b.Items = append(b.Items, item)
|
||||
}
|
||||
|
||||
type CommentSet struct {
|
||||
// 总共多少个
|
||||
Total int64 `json:"total"`
|
||||
// book清单
|
||||
Items []int `json:"items"`
|
||||
}
|
||||
|
||||
func (b *CommentSet) Add(item int) {
|
||||
b.Items = append(b.Items, item)
|
||||
}
|
||||
|
||||
func NewSet[T any]() *Set[T] {
|
||||
return &Set[T]{}
|
||||
}
|
||||
|
||||
// 使用[]来声明类型参数
|
||||
type Set[T any] struct {
|
||||
// 总共多少个
|
||||
Total int64 `json:"total"`
|
||||
// book清单
|
||||
Items []T `json:"items"`
|
||||
}
|
||||
|
||||
func (b *Set[T]) Add(item T) {
|
||||
b.Items = append(b.Items, item)
|
||||
}
|
19
skills/generate/generate_test.go
Normal file
19
skills/generate/generate_test.go
Normal file
@ -0,0 +1,19 @@
|
||||
package generate_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"122.51.31.227/go-course/go18/skills/generate"
|
||||
)
|
||||
|
||||
func TestStringSet(t *testing.T) {
|
||||
set := generate.NewSet[string]()
|
||||
set.Add("test")
|
||||
t.Log(set)
|
||||
}
|
||||
|
||||
func TestIntSet(t *testing.T) {
|
||||
set := generate.NewSet[int]()
|
||||
set.Add(10)
|
||||
t.Log(set)
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user