今天不分享技术,也没学到啥技术…….

今天做了一晚上的阅读理解

起因是老板让我写 project/ 级别的公共库操作

其实这个project/ 级别的操作跟之前 清晨写的 lib 公共库一毛一样

我还以为我需要直接全部把代码搬过来

然后新建一堆数据库进行操作,没想到老板是让我在原来的数据库上进行操作,并且把清晨的代码改进一下–> 从只有system版本 到 有两个版本

然后我就蒙蔽了,他让我写路由,写尼玛,没有丝毫头绪,如果是在同一张表上面修改,就是在scope上面做文章

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
type LibScore struct {
model.Base
Name string `gorm:"comment:指标名称"` // 2022-2023-1全校绩点
Tags datatypes.JSONSlice[string] `gorm:"comment:指标标签"`
Scope string `gorm:"comment:'指标等级: 系统级 system, 项目级 project/xxx'"`
Content []LibScoreContent `gorm:"foreignkey:LibScoreID;"`
}

type LibScoreContent struct {
model.Base
LibScoreID string `gorm:"size:26;index;comment:所属指标id"`
StaffId string `gorm:"size:19;comment:用户工号"`
Score string `gorm:"comment:指标内容分数/等级"`
Extra datatypes.JSON `gorm:"comment:额外信息"`
}

然后让我最蒙蔽的事情来了,他们说引入一个 projectId ,我一直误以为这个变量可能是,LibScore 的 ID 或者是某个成绩表的某个ID

然后我就蒙蔽了半天,准确来说是有半天的实践,反正几个小时吧。

后来才发现 projectId 原来是 随便输入的一个ID,跟这两个结构体都没有关系,真尼玛啊

后来的代码就可以顺理成章的写出来了。。。。。。

果真只是 代码复用而已

不得不说老板太聪明了,能想到这种办法。

代码分享

随便看看吧,我只是当作记录成果用的

System状态下

  1. 展现成绩列表
  2. 展现某张表的所有成绩
  3. 展现某张表的某个人的成绩
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// system级别路由            清晨写的
func AppLibInit(e *flamego.Flame) {
e.Group("/lib", func() {
e.Group("/score", func() {
e.Combo("").
Get(web.InjectPaginate(), handler.HandleLibList). // lib列表 ok
Post(binding.JSON(dto.CreateLibScoreRequest{}), handler.HandleLibCreate) // 创建lib ok

e.Combo("/{id}").
Get(handler.HandleLibDetail). // lib详情 ok
Put(binding.JSON(dto.CreateLibScoreRequest{}), handler.HandleLibUpdate). // id修改lib ok
Delete(handler.HandleLibDelete) // id删除lib ok

e.Combo("/{id}/content").
Get(web.InjectPaginate(),
handler.HandleLibContentList). // 根据id和staffid获取lib内容列表 展示create时间
Post(binding.JSON([]dto.CreateLibContentRequest{}),
handler.HandleLibContentCreate) // 创建lib内容

e.Post("/{id}/content/delete", binding.JSON(dto.LibScoreContentDeleteRequest{}),
handler.HandleLibContentDeleteBatch) // 根据id和staffid删除lib内容(批量删除

e.Combo("/{id}/content/{contentId}").
Get(handler.HandleLibContentDetail). // 根据id获取单条lib内容详情 (额外返回一下相同staffid的其他记录) {detail: {}, extra:{}}
Put(binding.JSON(dto.CreateLibContentRequest{}), handler.HandleLibContentUpdate) //. // 根据id修改单条lib内容
//Delete(handler.HandleLibContentDelete) // 根据id删除单条lib内容

e.Post("/{id}/content/upload", handler.HandleLibContentCreateBatch) // 批量创建lib内容
})

e.Group("/template", func() {
e.Post("/{type}/list", binding.JSON(dto.LibTemplateListRequest{}), web.InjectPaginate(),
handler.HandleLibTemplateList)
e.Post("/{type}", binding.JSON(dto.CreateLibTemplateRequest{}), handler.HandleLibTemplateCreate) // 创建lib模板

e.Combo("/{type}/{id}").
Get(handler.HandleLibTemplateDetail). // lib模板详情
Put(binding.JSON(dto.CreateLibTemplateRequest{}), handler.HandleLibTemplateUpdate). // 修改lib模板 type不能改
Delete(handler.HandleLibTemplateDelete) // 删除lib模板
// list api:通过类型/名称/tag筛选模板 不返回content
// 增删改(name,content,tags) type不能改
// 查详细信息
})
}, web.Authorization)
}

project 状态下

  1. 某个project级别下的多张表
  2. 一张表的所有成绩
  3. 某张表的某个人的成绩

所以两张表思路是一样的,但是路由变一下就改变了许多东西,只能说巧妙

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// project/级别路由                我抄上面的
e.Group("/{projectId}", func() {
e.Group("/lib", func() {
e.Group("/score", func() {
e.Combo("").
Get(web.InjectPaginate(), libHandler.HandleLibList). // lib列表 ok
Post(binding.JSON(libDto.CreateLibScoreRequest{}), libHandler.HandleLibCreate) // 创建lib ok

e.Combo("/{id}").
Get(libHandler.HandleLibDetail). // lib详情 ok
Put(binding.JSON(libDto.CreateLibScoreRequest{}), libHandler.HandleLibUpdate). // id修改lib ok
Delete(libHandler.HandleLibDelete) // id删除lib ok

e.Combo("/{id}/content").
Get(web.InjectPaginate(),
libHandler.HandleLibContentList). // 根据id和staffid获取lib内容列表 展示create时间
Post(binding.JSON([]libDto.CreateLibContentRequest{}),
libHandler.HandleLibContentCreate) // 创建lib内容

e.Post("/{id}/content/delete", binding.JSON(libDto.LibScoreContentDeleteRequest{}),
libHandler.HandleLibContentDeleteBatch) // 根据id和staffid删除lib内容(批量删除

e.Combo("/{id}/content/{contentId}").
Get(libHandler.HandleLibContentDetail). // 根据id获取单条lib内容详情 (额外返回一下相同staffid的其他记录) {detail: {}, extra:{}}
Put(binding.JSON(libDto.CreateLibContentRequest{}), libHandler.HandleLibContentUpdate) //. // 根据id修改单条lib内容
//Delete(handler.HandleLibContentDelete) // 根据id删除单条lib内容

e.Post("/{id}/content/upload", libHandler.HandleLibContentCreateBatch) // 批量创建lib内容
})

e.Group("/template", func() {
e.Post("/{type}/list", binding.JSON(libDto.LibTemplateListRequest{}), web.InjectPaginate(),
libHandler.HandleLibTemplateList)
e.Post("/{type}", binding.JSON(libDto.CreateLibTemplateRequest{}), libHandler.HandleLibTemplateCreate) // 创建lib模板

e.Combo("/{type}/{id}").
Get(libHandler.HandleLibTemplateDetail). // lib模板详情
Put(binding.JSON(libDto.CreateLibTemplateRequest{}), libHandler.HandleLibTemplateUpdate). // 修改lib模板 type不能改
Delete(libHandler.HandleLibTemplateDelete) // 删除lib模板
// list api:通过类型/名称/tag筛选模板 不返回content
// 增删改(name,content,tags) type不能改
// 查详细信息
})
})
})
//。。。

具体逻辑就是,通过 projectId判断是哪个级别的,如果projectId 传入了值就是 project级别的,否则就是system级别的

然后具体路由就可以修改了(呈现一二):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
func HandleLibList(c flamego.Context, r flamego.Render, paginate page.Paginate) {
var list []dto.LibScoreListItem
var count int64
projectId := c.Param("projectId")
err := dao.Lib.Model(&model.LibScore{}).Scopes(paginate.Paginate(),
func(db *gorm.DB) *gorm.DB {
if projectId == "" {
return db.Where("scope = ?", "system")
} else {
return db.Where("scope = ?", "project/"+projectId)
}
}).Count(&count).Find(&list).
Offset(-1).Limit(-1).Error
if err != nil {
response.ServiceErr(r, err)
return
}

response.HTTPSuccess(r, map[string]any{
"total": count,
"list": list,
})
}

func HandleLibCreate(c flamego.Context, r flamego.Render, req dto.CreateLibScoreRequest, errs binding.Errors) {
if errs != nil {
response.InValidParam(r, errs)
return
}
projectId := c.Param("projectId")
var scope string
if projectId == "" {
scope = "system"
} else {
scope = "project/" + projectId
}
err := dao.Lib.WithContext(c.Request().Context()).Model(&model.LibScore{}).
Create(&model.LibScore{
Name: req.Name,
Tags: req.Tags,
Scope: scope,
}).Error
if err != nil {
response.ServiceErr(r, err)
return
}
response.HTTPSuccess(r, nil)
}

总结

阅读理解很重要–需求的阅读

今天还去母校返校演讲了,哈哈哈哈哈哈哈啊哈,给高中生讲电专生活,逃课啥的太有意思了

典型的反面教材。。