fix encounter #1
This commit is contained in:
parent
77afc86e0f
commit
18cc7efbbf
@ -4,6 +4,7 @@ import (
|
|||||||
"catface/app/global/consts"
|
"catface/app/global/consts"
|
||||||
"catface/app/global/variable"
|
"catface/app/global/variable"
|
||||||
"catface/app/http/validator/core/data_transfer"
|
"catface/app/http/validator/core/data_transfer"
|
||||||
|
"catface/app/model"
|
||||||
"catface/app/service/upload_file"
|
"catface/app/service/upload_file"
|
||||||
"catface/app/utils/response"
|
"catface/app/utils/response"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
@ -18,6 +19,7 @@ type Encounters struct {
|
|||||||
func (e *Encounters) Create(context *gin.Context) {
|
func (e *Encounters) Create(context *gin.Context) {
|
||||||
// TODO 处理 Photos 文件,然后处理出 Avatar,并获取压缩后的 宽高,以及文件的存储路径。
|
// TODO 处理 Photos 文件,然后处理出 Avatar,并获取压缩后的 宽高,以及文件的存储路径。
|
||||||
photos := data_transfer.GetStringSlice(context, "photos")
|
photos := data_transfer.GetStringSlice(context, "photos")
|
||||||
|
animals_id := data_transfer.GetFloat64Slice(context, "animals_id")
|
||||||
if len(photos) > 0 {
|
if len(photos) > 0 {
|
||||||
userId := strconv.Itoa(int(context.GetFloat64(consts.ValidatorPrefix + "user_id")))
|
userId := strconv.Itoa(int(context.GetFloat64(consts.ValidatorPrefix + "user_id")))
|
||||||
avatar := photos[0]
|
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_height", avatarHeight)
|
||||||
context.Set(consts.ValidatorPrefix+"avatar_width", int(avatarWidth))
|
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
|
// Real Insert
|
||||||
// if model.CreateEncounterFactory("").InsertDate(context) {
|
if model.CreateEncounterFactory("").InsertDate(context) {
|
||||||
// response.Success(context, consts.CurdStatusOkMsg, "")
|
response.Success(context, consts.CurdStatusOkMsg, "")
|
||||||
// } else {
|
} else {
|
||||||
// response.Fail(context, consts.CurdCreatFailCode, consts.CurdCreatFailMsg+",新增错误", "")
|
response.Fail(context, consts.CurdCreatFailCode, consts.CurdCreatFailMsg+",新增错误", "")
|
||||||
// }
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,7 +5,9 @@ import (
|
|||||||
"catface/app/global/variable"
|
"catface/app/global/variable"
|
||||||
"catface/app/http/validator/core/interf"
|
"catface/app/http/validator/core/interf"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
@ -66,10 +68,22 @@ func GetStringSlice(context *gin.Context, key string) (ss []string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// GetIntSlice 从 context 中获取整数切片。
|
// GetIntSlice 从 context 中获取整数切片。
|
||||||
func GetIntSlice(context *gin.Context, key string) (ss []int) {
|
func GetFloat64Slice(context *gin.Context, key string) (ss []float64) {
|
||||||
if val := getSlice(context, consts.ValidatorPrefix, key, reflect.TypeOf(0)); val != nil {
|
if val := getSlice(context, consts.ValidatorPrefix, key, reflect.TypeOf(float64(0))); val != nil {
|
||||||
ss = val.([]int)
|
ss = val.([]float64)
|
||||||
}
|
}
|
||||||
context.Set(consts.ValidatorPrefix+key, ss)
|
context.Set(consts.ValidatorPrefix+key, ss)
|
||||||
return
|
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 单次的记录。
|
type Encounter struct { // Encounter 或者称为 post,指的就是 Human 单次的记录。
|
||||||
BaseModel
|
BaseModel
|
||||||
// TAG 外键关联
|
// TAG 外键关联
|
||||||
UserId int
|
UsersModelId int `gorm:"column:user_id" json:"user_id"`
|
||||||
UsersModel UsersModel
|
UsersModel UsersModel
|
||||||
AnimalsId string `gorm:"size:20"` // TODO 关联对象存在上限
|
AnimalsId string `gorm:"size:20" json:"animals_id"` // TODO 关联对象存在上限
|
||||||
|
|
||||||
Title string `gorm:"size:20"`
|
Title string `gorm:"size:20;column:title" json:"title"`
|
||||||
Content string
|
Content string `json:"content"`
|
||||||
// Time 从 CreatedAt 中解析
|
// Time 从 CreatedAt 中解析
|
||||||
|
|
||||||
// TAG Avatar 最好是压缩后的备份图像
|
// TAG Avatar 最好是压缩后的备份图像
|
||||||
|
@ -2,6 +2,7 @@ package data_bind
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"catface/app/global/consts"
|
"catface/app/global/consts"
|
||||||
|
"catface/app/utils/model_handler"
|
||||||
"errors"
|
"errors"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
|
||||||
@ -34,7 +35,7 @@ func ShouldBindFormDataToModel(c *gin.Context, modelStruct interface{}) error {
|
|||||||
for i := 0; i < fieldNum; i++ {
|
for i := 0; i < fieldNum; i++ {
|
||||||
if !mtf.Field(i).Anonymous && mtf.Field(i).Type.Kind() != reflect.Struct {
|
if !mtf.Field(i).Anonymous && mtf.Field(i).Type.Kind() != reflect.Struct {
|
||||||
fieldSetValue(c, mValueOfEle, mtf, i)
|
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)))
|
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) {
|
func fieldSetValue(c *gin.Context, valueOf reflect.Value, typeOf reflect.Type, colIndex int) {
|
||||||
relaKey := typeOf.Field(colIndex).Tag.Get("json")
|
relaKey := typeOf.Field(colIndex).Tag.Get("json")
|
||||||
if relaKey != "-" {
|
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() {
|
switch typeOf.Field(colIndex).Type.Kind() {
|
||||||
case reflect.Int, reflect.Int16, reflect.Int32, reflect.Int64:
|
case reflect.Int, reflect.Int16, reflect.Int32, reflect.Int64:
|
||||||
valueOf.Field(colIndex).SetInt(int64(c.GetFloat64(relaKey)))
|
valueOf.Field(colIndex).SetInt(int64(c.GetFloat64(relaKey)))
|
||||||
|
@ -19,7 +19,7 @@ func camelToSnake(s string) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 获取处理后的 json 标签值
|
// 获取处理后的 json 标签值
|
||||||
func getProcessedJSONTag(field reflect.StructField) string {
|
func GetProcessedJSONTag(field reflect.StructField) string {
|
||||||
jsonTag := field.Tag.Get("json")
|
jsonTag := field.Tag.Get("json")
|
||||||
if jsonTag != "" {
|
if jsonTag != "" {
|
||||||
// INFO 移除 omitempty
|
// INFO 移除 omitempty
|
||||||
@ -38,7 +38,7 @@ func GetModelField(v interface{}) map[string]bool {
|
|||||||
fieldName := field.Name
|
fieldName := field.Name
|
||||||
|
|
||||||
// 获取 json 标签中的值
|
// 获取 json 标签中的值
|
||||||
jsonTag := getProcessedJSONTag(field)
|
jsonTag := GetProcessedJSONTag(field)
|
||||||
if jsonTag != "" {
|
if jsonTag != "" {
|
||||||
// 如果有 json 标签,则使用标签中的值
|
// 如果有 json 标签,则使用标签中的值
|
||||||
fieldMap[jsonTag] = false
|
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