fix encounter #1
This commit is contained in:
parent
77afc86e0f
commit
18cc7efbbf
@ -4,6 +4,7 @@ import (
|
||||
"catface/app/global/consts"
|
||||
"catface/app/global/variable"
|
||||
"catface/app/http/validator/core/data_transfer"
|
||||
"catface/app/model"
|
||||
"catface/app/service/upload_file"
|
||||
"catface/app/utils/response"
|
||||
"path/filepath"
|
||||
@ -18,6 +19,7 @@ type Encounters struct {
|
||||
func (e *Encounters) Create(context *gin.Context) {
|
||||
// TODO 处理 Photos 文件,然后处理出 Avatar,并获取压缩后的 宽高,以及文件的存储路径。
|
||||
photos := data_transfer.GetStringSlice(context, "photos")
|
||||
animals_id := data_transfer.GetFloat64Slice(context, "animals_id")
|
||||
if len(photos) > 0 {
|
||||
userId := strconv.Itoa(int(context.GetFloat64(consts.ValidatorPrefix + "user_id")))
|
||||
avatar := photos[0]
|
||||
@ -34,10 +36,23 @@ func (e *Encounters) Create(context *gin.Context) {
|
||||
context.Set(consts.ValidatorPrefix+"avatar_height", avatarHeight)
|
||||
context.Set(consts.ValidatorPrefix+"avatar_width", int(avatarWidth))
|
||||
}
|
||||
// 将 Array 转化为 string 类型
|
||||
if res, err := data_transfer.ConvertSliceToString(animals_id); err == nil {
|
||||
context.Set(consts.ValidatorPrefix+"animals_id", res)
|
||||
} else {
|
||||
response.Fail(context, consts.ValidatorParamsCheckFailCode, consts.ValidatorParamsCheckFailMsg, "")
|
||||
return
|
||||
}
|
||||
if res, err := data_transfer.ConvertSliceToString(photos); err == nil {
|
||||
context.Set(consts.ValidatorPrefix+"photos", res)
|
||||
} else {
|
||||
response.Fail(context, consts.ValidatorParamsCheckFailCode, consts.ValidatorParamsCheckFailMsg, "")
|
||||
return
|
||||
}
|
||||
// Real Insert
|
||||
// if model.CreateEncounterFactory("").InsertDate(context) {
|
||||
// response.Success(context, consts.CurdStatusOkMsg, "")
|
||||
// } else {
|
||||
// response.Fail(context, consts.CurdCreatFailCode, consts.CurdCreatFailMsg+",新增错误", "")
|
||||
// }
|
||||
if model.CreateEncounterFactory("").InsertDate(context) {
|
||||
response.Success(context, consts.CurdStatusOkMsg, "")
|
||||
} else {
|
||||
response.Fail(context, consts.CurdCreatFailCode, consts.CurdCreatFailMsg+",新增错误", "")
|
||||
}
|
||||
}
|
||||
|
@ -5,7 +5,9 @@ import (
|
||||
"catface/app/global/variable"
|
||||
"catface/app/http/validator/core/interf"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
@ -66,10 +68,22 @@ func GetStringSlice(context *gin.Context, key string) (ss []string) {
|
||||
}
|
||||
|
||||
// GetIntSlice 从 context 中获取整数切片。
|
||||
func GetIntSlice(context *gin.Context, key string) (ss []int) {
|
||||
if val := getSlice(context, consts.ValidatorPrefix, key, reflect.TypeOf(0)); val != nil {
|
||||
ss = val.([]int)
|
||||
func GetFloat64Slice(context *gin.Context, key string) (ss []float64) {
|
||||
if val := getSlice(context, consts.ValidatorPrefix, key, reflect.TypeOf(float64(0))); val != nil {
|
||||
ss = val.([]float64)
|
||||
}
|
||||
context.Set(consts.ValidatorPrefix+key, ss)
|
||||
return
|
||||
}
|
||||
|
||||
// ConvertSliceToString 是一个泛型函数,可以接受任何类型的切片
|
||||
func ConvertSliceToString[T any](slice []T) (string, error) {
|
||||
var strBuilder strings.Builder
|
||||
for i, v := range slice {
|
||||
if i > 0 {
|
||||
strBuilder.WriteString(",")
|
||||
}
|
||||
strBuilder.WriteString(fmt.Sprintf("%v", v))
|
||||
}
|
||||
return strBuilder.String(), nil
|
||||
}
|
||||
|
@ -15,12 +15,12 @@ func CreateEncounterFactory(sqlType string) *Encounter {
|
||||
type Encounter struct { // Encounter 或者称为 post,指的就是 Human 单次的记录。
|
||||
BaseModel
|
||||
// TAG 外键关联
|
||||
UserId int
|
||||
UsersModel UsersModel
|
||||
AnimalsId string `gorm:"size:20"` // TODO 关联对象存在上限
|
||||
UsersModelId int `gorm:"column:user_id" json:"user_id"`
|
||||
UsersModel UsersModel
|
||||
AnimalsId string `gorm:"size:20" json:"animals_id"` // TODO 关联对象存在上限
|
||||
|
||||
Title string `gorm:"size:20"`
|
||||
Content string
|
||||
Title string `gorm:"size:20;column:title" json:"title"`
|
||||
Content string `json:"content"`
|
||||
// Time 从 CreatedAt 中解析
|
||||
|
||||
// TAG Avatar 最好是压缩后的备份图像
|
||||
|
@ -2,6 +2,7 @@ package data_bind
|
||||
|
||||
import (
|
||||
"catface/app/global/consts"
|
||||
"catface/app/utils/model_handler"
|
||||
"errors"
|
||||
"reflect"
|
||||
|
||||
@ -34,7 +35,7 @@ func ShouldBindFormDataToModel(c *gin.Context, modelStruct interface{}) error {
|
||||
for i := 0; i < fieldNum; i++ {
|
||||
if !mtf.Field(i).Anonymous && mtf.Field(i).Type.Kind() != reflect.Struct {
|
||||
fieldSetValue(c, mValueOfEle, mtf, i)
|
||||
} else if mtf.Field(i).Type.Kind() == reflect.Struct {
|
||||
} else if mtf.Field(i).Type.Kind() == reflect.Struct { // INFO 处理结构体。
|
||||
//处理结构体(有名+匿名)
|
||||
mValueOfEle.Field(i).Set(analysisAnonymousStruct(c, mValueOfEle.Field(i)))
|
||||
}
|
||||
@ -59,7 +60,8 @@ func analysisAnonymousStruct(c *gin.Context, value reflect.Value) reflect.Value
|
||||
func fieldSetValue(c *gin.Context, valueOf reflect.Value, typeOf reflect.Type, colIndex int) {
|
||||
relaKey := typeOf.Field(colIndex).Tag.Get("json")
|
||||
if relaKey != "-" {
|
||||
relaKey = consts.ValidatorPrefix + typeOf.Field(colIndex).Tag.Get("json")
|
||||
// relaKey = consts.ValidatorPrefix + typeOf.Field(colIndex).Tag.Get("json")
|
||||
relaKey = consts.ValidatorPrefix + model_handler.GetProcessedJSONTag(typeOf.Field(colIndex))
|
||||
switch typeOf.Field(colIndex).Type.Kind() {
|
||||
case reflect.Int, reflect.Int16, reflect.Int32, reflect.Int64:
|
||||
valueOf.Field(colIndex).SetInt(int64(c.GetFloat64(relaKey)))
|
||||
|
@ -19,7 +19,7 @@ func camelToSnake(s string) string {
|
||||
}
|
||||
|
||||
// 获取处理后的 json 标签值
|
||||
func getProcessedJSONTag(field reflect.StructField) string {
|
||||
func GetProcessedJSONTag(field reflect.StructField) string {
|
||||
jsonTag := field.Tag.Get("json")
|
||||
if jsonTag != "" {
|
||||
// INFO 移除 omitempty
|
||||
@ -38,7 +38,7 @@ func GetModelField(v interface{}) map[string]bool {
|
||||
fieldName := field.Name
|
||||
|
||||
// 获取 json 标签中的值
|
||||
jsonTag := getProcessedJSONTag(field)
|
||||
jsonTag := GetProcessedJSONTag(field)
|
||||
if jsonTag != "" {
|
||||
// 如果有 json 标签,则使用标签中的值
|
||||
fieldMap[jsonTag] = false
|
||||
|
27
test/models_test.go
Normal file
27
test/models_test.go
Normal file
@ -0,0 +1,27 @@
|
||||
// add_test.go
|
||||
package test
|
||||
|
||||
import (
|
||||
"catface/app/model"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// func TestUsers(t *testing.T) {
|
||||
// Init()
|
||||
|
||||
// user := model.UsersModel{}
|
||||
// err := DB.AutoMigrate(&user)
|
||||
// if err != nil {
|
||||
// t.Error(err)
|
||||
// }
|
||||
// }
|
||||
|
||||
func TestEncouner(t *testing.T) {
|
||||
Init()
|
||||
|
||||
encounter := model.Encounter{}
|
||||
err := DB.AutoMigrate(&encounter)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user