finish encounter List
This commit is contained in:
parent
2a0c8f1b8a
commit
c9a08d1800
@ -10,4 +10,6 @@ const (
|
||||
ErrGeneralStart = ErrGeneral + iota
|
||||
ErrInvalidData
|
||||
ErrInternalError
|
||||
|
||||
ErrDataNoFound
|
||||
)
|
||||
|
@ -15,4 +15,5 @@ func GeneralMsgInit(m msg) {
|
||||
m[0] = ""
|
||||
m[ErrInvalidData] = "参数无效"
|
||||
m[ErrInternalError] = "内部服务器错误"
|
||||
m[ErrDataNoFound] = "无数据查询"
|
||||
}
|
||||
|
@ -2,9 +2,11 @@ package web
|
||||
|
||||
import (
|
||||
"catface/app/global/consts"
|
||||
"catface/app/global/errcode"
|
||||
"catface/app/global/variable"
|
||||
"catface/app/http/validator/core/data_transfer"
|
||||
"catface/app/model"
|
||||
"catface/app/service/encounter/curd"
|
||||
"catface/app/service/upload_file"
|
||||
"catface/app/utils/response"
|
||||
"path/filepath"
|
||||
@ -17,7 +19,6 @@ type Encounters struct {
|
||||
}
|
||||
|
||||
func (e *Encounters) Create(context *gin.Context) {
|
||||
// TODO 处理 Photos 文件,然后处理出 Avatar,并获取压缩后的 宽高,以及文件的存储路径。
|
||||
photos := data_transfer.GetStringSlice(context, "photos")
|
||||
if len(photos) > 0 {
|
||||
userId := strconv.Itoa(int(context.GetFloat64(consts.ValidatorPrefix + "user_id")))
|
||||
@ -56,3 +57,15 @@ func (e *Encounters) Create(context *gin.Context) {
|
||||
response.Fail(context, consts.CurdCreatFailCode, consts.CurdCreatFailMsg+",新增错误", "")
|
||||
}
|
||||
}
|
||||
|
||||
func (e *Encounters) List(context *gin.Context) {
|
||||
num := context.GetFloat64(consts.ValidatorPrefix + "num")
|
||||
skip := context.GetFloat64(consts.ValidatorPrefix + "skip")
|
||||
|
||||
encounters := curd.CreateEncounterCurdFactory().List(int(num), int(skip))
|
||||
if encounters != nil {
|
||||
response.Success(context, consts.CurdStatusOkMsg, encounters)
|
||||
} else {
|
||||
response.Fail(context, errcode.ErrDataNoFound, errcode.ErrMsg[errcode.ErrDataNoFound], "")
|
||||
}
|
||||
}
|
||||
|
@ -56,5 +56,6 @@ func WebRegisterValidator() {
|
||||
// TAG Encounter
|
||||
key = consts.ValidatorPrefix + "EncounterStore"
|
||||
containers.Set(key, encounter.Create{})
|
||||
|
||||
key = consts.ValidatorPrefix + "EncounterList"
|
||||
containers.Set(key, encounter.List{})
|
||||
}
|
||||
|
32
app/http/validator/web/encounter/list.go
Normal file
32
app/http/validator/web/encounter/list.go
Normal file
@ -0,0 +1,32 @@
|
||||
package encounter
|
||||
|
||||
import (
|
||||
"catface/app/global/consts"
|
||||
"catface/app/http/controller/web"
|
||||
"catface/app/http/validator/core/data_transfer"
|
||||
"catface/app/utils/response"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type List struct {
|
||||
Num int `form:"num" json:"num"`
|
||||
Skip int `form:"skip" json:"skip"`
|
||||
}
|
||||
|
||||
func (l List) CheckParams(context *gin.Context) {
|
||||
if err := context.ShouldBind(&l); err != nil { // INFO 这一条是必要的,看来对数据的解析页在其中。
|
||||
// 将表单参数验证器出现的错误直接交给错误翻译器统一处理即可
|
||||
response.ValidatorError(context, err)
|
||||
return
|
||||
}
|
||||
|
||||
// 该函数主要是将本结构体的字段(成员)按照 consts.ValidatorPrefix+ json标签对应的 键 => 值 形式绑定在上下文,便于下一步(控制器)可以直接通过 context.Get(键) 获取相关值
|
||||
extraAddBindDataContext := data_transfer.DataAddContext(l, consts.ValidatorPrefix, context)
|
||||
if extraAddBindDataContext == nil {
|
||||
response.ErrorSystem(context, "encounterList表单验证器json化失败", "")
|
||||
} else {
|
||||
// 验证完成,调用控制器,并将验证器成员(字段)递给控制器,保持上下文数据一致性
|
||||
(&web.Encounters{}).List(extraAddBindDataContext)
|
||||
}
|
||||
}
|
@ -6,7 +6,6 @@ import (
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"go.uber.org/zap"
|
||||
|
||||
)
|
||||
|
||||
func CreateEncounterFactory(sqlType string) *Encounter {
|
||||
@ -52,3 +51,13 @@ func (e *Encounter) InsertDate(c *gin.Context) bool {
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (e *Encounter) Show(num, skip int) (temp []EncounterList) {
|
||||
sql := `SELECT user_id, title, avatar, avatar_height, avatar_width, e.updated_at,
|
||||
user_name, user_avatar FROM encounters e JOIN tb_users u ON e.user_id = u.id
|
||||
LIMIT ? OFFSET ?
|
||||
`
|
||||
_ = e.Raw(sql, num, skip).Scan(&temp)
|
||||
|
||||
return
|
||||
}
|
||||
|
14
app/model/encounter_result.go
Normal file
14
app/model/encounter_result.go
Normal file
@ -0,0 +1,14 @@
|
||||
package model
|
||||
|
||||
import "time"
|
||||
|
||||
type EncounterList struct {
|
||||
UserId int `form:"user_id" json:"user_id"`
|
||||
Title string
|
||||
Avatar string
|
||||
AvatarHeight int `form:"avatar_height" json:"avatar_height"`
|
||||
AvatarWidth int `form:"avatar_width" json:"avatar_width"`
|
||||
UpdatedAt *time.Time `json:"updated_at,omitempty"` // TIP 设为 *time.Time,omitempty 和 autoUpdated 就都可以生效
|
||||
|
||||
UserName string `form:"user_name" json:"user_name"`
|
||||
}
|
@ -9,3 +9,10 @@ func CreateEncounterCurdFactory() *EncounterCurd {
|
||||
type EncounterCurd struct {
|
||||
encounter *model.Encounter
|
||||
}
|
||||
|
||||
func (e *EncounterCurd) List(num, skip int) []model.EncounterList {
|
||||
if num == 0 {
|
||||
num = 15
|
||||
}
|
||||
return model.CreateEncounterFactory("").Show(num, skip)
|
||||
}
|
||||
|
@ -130,7 +130,7 @@ func InitWebRouter() *gin.Engine {
|
||||
encounter := backend.Group("encounter")
|
||||
{
|
||||
// GET 获取列表; POST 上传
|
||||
// encounter.GET("", validatorFactory.Create(consts.ValidatorPrefix+"EncounterList"))
|
||||
encounter.GET("", validatorFactory.Create(consts.ValidatorPrefix+"EncounterList"))
|
||||
encounter.POST("", validatorFactory.Create(consts.ValidatorPrefix+"EncounterStore"))
|
||||
}
|
||||
// }
|
||||
|
Loading…
x
Reference in New Issue
Block a user