✨ finish half animal ES
This commit is contained in:
parent
ac5d516c1e
commit
656bcf7239
@ -6,6 +6,7 @@ import (
|
|||||||
"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/model"
|
||||||
|
"catface/app/model_es"
|
||||||
"catface/app/service/animals/curd"
|
"catface/app/service/animals/curd"
|
||||||
"catface/app/service/upload_file"
|
"catface/app/service/upload_file"
|
||||||
"catface/app/utils/query_handler"
|
"catface/app/utils/query_handler"
|
||||||
@ -190,6 +191,7 @@ func (a *Animals) Create(context *gin.Context) {
|
|||||||
context.Set(consts.ValidatorPrefix+"tags", extra["tags"])
|
context.Set(consts.ValidatorPrefix+"tags", extra["tags"])
|
||||||
nickNames = data_transfer.GetStringSlice(context, "nick_names")
|
nickNames = data_transfer.GetStringSlice(context, "nick_names")
|
||||||
tags = data_transfer.GetStringSlice(context, "tags")
|
tags = data_transfer.GetStringSlice(context, "tags")
|
||||||
|
context.Set(consts.ValidatorPrefix+"nick_names_list", nickNames)
|
||||||
context.Set(consts.ValidatorPrefix+"nick_names", nickNames)
|
context.Set(consts.ValidatorPrefix+"nick_names", nickNames)
|
||||||
context.Set(consts.ValidatorPrefix+"tags", tags) // UPDATE 有点冗余,但是不用复杂代码;
|
context.Set(consts.ValidatorPrefix+"tags", tags) // UPDATE 有点冗余,但是不用复杂代码;
|
||||||
}
|
}
|
||||||
@ -213,16 +215,21 @@ func (a *Animals) Create(context *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
// STAGE-3
|
// STAGE-3
|
||||||
if anm_id, ok := model.CreateAnimalFactory("").InsertDate(context); ok {
|
if animal, ok := model.CreateAnimalFactory("").InsertDate(context); ok {
|
||||||
// 转移 photos 到 anm;采用 rename dir 的方式
|
// 转移 photos 到 anm;采用 rename dir 的方式
|
||||||
oldName := filepath.Join(variable.ConfigYml.GetString("FileUploadSetting.UploadFileSavePath"), "catsPhotos", "hum_"+userId)
|
oldName := filepath.Join(variable.ConfigYml.GetString("FileUploadSetting.UploadFileSavePath"), "catsPhotos", "hum_"+userId)
|
||||||
newName := filepath.Join(variable.ConfigYml.GetString("FileUploadSetting.UploadFileSavePath"), "catsPhotos", "anm_"+strconv.FormatInt(anm_id, 10))
|
newName := filepath.Join(variable.ConfigYml.GetString("FileUploadSetting.UploadFileSavePath"), "catsPhotos", "anm_"+strconv.FormatInt(animal.Id, 10))
|
||||||
err := os.Rename(oldName, newName)
|
err := os.Rename(oldName, newName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// TODO 特殊返回,成功了一半?或者需要清空原有的操作?不过感觉这一步几乎不会出错。
|
// TODO 特殊返回,成功了一半?或者需要清空原有的操作?不过感觉这一步几乎不会出错。
|
||||||
|
// TODO 或许直接采用 go 会比较好呢?
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 2. 将部分数据插入 ES;
|
||||||
|
go model_es.CreateAnimalESFactory(&animal).InsertDocument()
|
||||||
|
|
||||||
response.Success(context, consts.CurdStatusOkMsg, gin.H{
|
response.Success(context, consts.CurdStatusOkMsg, gin.H{
|
||||||
"anm_id": anm_id,
|
"anm_id": animal.Id,
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
response.Fail(context, consts.CurdCreatFailCode, consts.CurdCreatFailMsg+",新增错误", "")
|
response.Fail(context, consts.CurdCreatFailCode, consts.CurdCreatFailMsg+",新增错误", "")
|
||||||
|
@ -15,18 +15,19 @@ func CreateAnimalFactory(sqlType string) *Animal {
|
|||||||
|
|
||||||
type Animal struct {
|
type Animal struct {
|
||||||
// UPDATE 或者这里都应该采取外键连接?
|
// UPDATE 或者这里都应该采取外键连接?
|
||||||
BaseModel // 假设 BaseModel 中不需要添加 omitempty 标签
|
BaseModel // 假设 BaseModel 中不需要添加 omitempty 标签
|
||||||
Name string `gorm:"type:varchar(20)" json:"name,omitempty"` // 名称
|
Name string `gorm:"type:varchar(20)" json:"name,omitempty"` // 名称
|
||||||
Birthday string `gorm:"size:10" json:"birthday,omitempty"` // 生日;就简单存string就好
|
Birthday string `gorm:"size:10" json:"birthday,omitempty"` // 生日;就简单存string就好
|
||||||
Gender uint8 `gorm:"default:1" json:"gender,omitempty"` // 性别
|
Gender uint8 `gorm:"default:1" json:"gender,omitempty"` // 性别
|
||||||
Breed uint8 `gorm:"default:1" json:"breed,omitempty"` // 品种
|
Breed uint8 `gorm:"default:1" json:"breed,omitempty"` // 品种
|
||||||
Sterilization uint8 `gorm:"default:1" json:"sterilization,omitempty"` // 1 不明 2 未绝育 3 已绝育
|
Sterilization uint8 `gorm:"default:1" json:"sterilization,omitempty"` // 1 不明 2 未绝育 3 已绝育
|
||||||
Vaccination uint8 `gorm:"default:1" json:"vaccination,omitempty"` // 免疫状态
|
Vaccination uint8 `gorm:"default:1" json:"vaccination,omitempty"` // 免疫状态
|
||||||
Deworming uint8 `gorm:"default:1" json:"deworming,omitempty"` // 驱虫状态
|
Deworming uint8 `gorm:"default:1" json:"deworming,omitempty"` // 驱虫状态
|
||||||
NickNames string `gorm:"type:varchar(31)" json:"nick_names,omitempty"` // 别称,辅助查询;存储上采取 , 间隔符的方式; VARCHAR 会比较合适
|
NickNames string `gorm:"type:varchar(31)" json:"nick_names,omitempty"` // 别称,辅助查询;存储上采取 , 间隔符的方式; VARCHAR 会比较合适
|
||||||
Status uint8 `gorm:"default:1" json:"status,omitempty"` // 状态
|
NickNamesList []string `gorm:"-" json:"nick_names_list,omitempty"`
|
||||||
Description string `gorm:"column:description;type:varchar(255)" json:"description,omitempty"` // 简明介绍
|
Status uint8 `gorm:"default:1" json:"status,omitempty"` // 状态
|
||||||
Tags string `json:"tags,omitempty"`
|
Description string `gorm:"column:description;type:varchar(255)" json:"description,omitempty"` // 简明介绍
|
||||||
|
Tags string `json:"tags,omitempty"`
|
||||||
// TAG imaegs
|
// TAG imaegs
|
||||||
Avatar string `gorm:"type:varchar(50)" json:"avatar,omitempty"` // 缩略图 url,为 Go 获取 Photo 之后压缩处理后的图像,单独存储。
|
Avatar string `gorm:"type:varchar(50)" json:"avatar,omitempty"` // 缩略图 url,为 Go 获取 Photo 之后压缩处理后的图像,单独存储。
|
||||||
AvatarHeight uint16 `json:"avatar_height,omitempty"` // 为了方便前端在加载图像前的骨架图 & 瀑布流展示。 // INFO 暂时没用到
|
AvatarHeight uint16 `json:"avatar_height,omitempty"` // 为了方便前端在加载图像前的骨架图 & 瀑布流展示。 // INFO 暂时没用到
|
||||||
@ -119,18 +120,16 @@ func (a *Animal) ShowByName(name string, attrs ...string) (temp []Animal) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *Animal) InsertDate(c *gin.Context) (int64, bool) {
|
func (a *Animal) InsertDate(c *gin.Context) (tmp Animal, ok bool) {
|
||||||
var tmp Animal
|
|
||||||
if err := data_bind.ShouldBindFormDataToModel(c, &tmp); err == nil {
|
if err := data_bind.ShouldBindFormDataToModel(c, &tmp); err == nil {
|
||||||
if res := a.Create(&tmp); res.Error == nil {
|
if res := a.Create(&tmp); res.Error == nil {
|
||||||
// 获取插入的 ID
|
// 获取插入的 ID
|
||||||
insertedID := tmp.Id
|
return tmp, true
|
||||||
return insertedID, true
|
|
||||||
} else {
|
} else {
|
||||||
variable.ZapLog.Error("Animal 数据新增出错", zap.Error(res.Error))
|
variable.ZapLog.Error("Animal 数据新增出错", zap.Error(res.Error))
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
variable.ZapLog.Error("Animal 数据绑定出错", zap.Error(err))
|
variable.ZapLog.Error("Animal 数据绑定出错", zap.Error(err))
|
||||||
}
|
}
|
||||||
return 0, false
|
return tmp, false
|
||||||
}
|
}
|
||||||
|
@ -33,7 +33,7 @@ type Encounter struct { // Encounter 或者称为 post,指的就是 Human 单
|
|||||||
AvatarHeight uint16 `json:"avatar_height,omitempty"` // 为了方便前端在加载图像前的骨架图 & 瀑布流展示。
|
AvatarHeight uint16 `json:"avatar_height,omitempty"` // 为了方便前端在加载图像前的骨架图 & 瀑布流展示。
|
||||||
AvatarWidth uint16 `json:"avatar_width,omitempty"`
|
AvatarWidth uint16 `json:"avatar_width,omitempty"`
|
||||||
Photos string `gorm:"type:varchar(100)" json:"photos,omitempty"` // 图片数组
|
Photos string `gorm:"type:varchar(100)" json:"photos,omitempty"` // 图片数组
|
||||||
PhotosSlice []string `gorm:"-" json:"photos_list,omitempty"` // TIP GORM 忽略
|
PhotosList []string `gorm:"-" json:"photos_list,omitempty"` // TIP GORM 忽略
|
||||||
// POI
|
// POI
|
||||||
Latitude float64 `json:"latitude,omitempty"` // POI 位置相关
|
Latitude float64 `json:"latitude,omitempty"` // POI 位置相关
|
||||||
Longitude float64 `json:"longitude,omitempty"`
|
Longitude float64 `json:"longitude,omitempty"`
|
||||||
|
@ -1 +1,72 @@
|
|||||||
package model_es
|
package model_es
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"catface/app/global/variable"
|
||||||
|
"catface/app/model"
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/elastic/go-elasticsearch/v8/esapi"
|
||||||
|
)
|
||||||
|
|
||||||
|
func CreateAnimalESFactory(animal *model.Animal) *Animal {
|
||||||
|
if animal == nil {
|
||||||
|
return &Animal{}
|
||||||
|
}
|
||||||
|
return &Animal{
|
||||||
|
Id: animal.Id,
|
||||||
|
Name: animal.Name,
|
||||||
|
NickNames: animal.NickNamesList,
|
||||||
|
Description: animal.Description,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type Animal struct {
|
||||||
|
Id int64 `json:"id"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
NickNames []string `json:"nick_names"`
|
||||||
|
Description string `json:"description"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *Animal) IndexName() string {
|
||||||
|
return "catface_animals"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *Animal) InsertDocument() error {
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
data, err := json.Marshal(a)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
req := esapi.IndexRequest{
|
||||||
|
Index: a.IndexName(),
|
||||||
|
// DocumentID: fmt.Sprintf("%d", a.Id),
|
||||||
|
Body: bytes.NewReader(data),
|
||||||
|
Refresh: "true",
|
||||||
|
}
|
||||||
|
|
||||||
|
res, err := req.Do(ctx, variable.ElasticClient)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer res.Body.Close()
|
||||||
|
|
||||||
|
if res.IsError() {
|
||||||
|
var e map[string]interface{}
|
||||||
|
if err := json.NewDecoder(res.Body).Decode(&e); err != nil {
|
||||||
|
return fmt.Errorf("error parsing the response body: %s", err)
|
||||||
|
} else {
|
||||||
|
return fmt.Errorf("[%s] %s: %s",
|
||||||
|
res.Status(),
|
||||||
|
e["error"].(map[string]interface{})["type"],
|
||||||
|
e["error"].(map[string]interface{})["reason"],
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
@ -4,7 +4,6 @@ import (
|
|||||||
"catface/app/model"
|
"catface/app/model"
|
||||||
"catface/app/utils/query_handler"
|
"catface/app/utils/query_handler"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func CreateEncounterCurdFactory() *EncounterCurd {
|
func CreateEncounterCurdFactory() *EncounterCurd {
|
||||||
@ -22,8 +21,8 @@ func (e *EncounterCurd) List(num, skip, user_id int, mode string) (result []mode
|
|||||||
|
|
||||||
var likedAnimalIds []int
|
var likedAnimalIds []int
|
||||||
switch mode {
|
switch mode {
|
||||||
case "liked":
|
case "liked":
|
||||||
likedAnimalIds = model.CreateAnimalLikeFactory("").LikedCats(user_id)
|
likedAnimalIds = model.CreateAnimalLikeFactory("").LikedCats(user_id)
|
||||||
}
|
}
|
||||||
result = model.CreateEncounterFactory("").Show(num, skip, user_id, likedAnimalIds)
|
result = model.CreateEncounterFactory("").Show(num, skip, user_id, likedAnimalIds)
|
||||||
return
|
return
|
||||||
@ -43,7 +42,7 @@ func (e *EncounterCurd) Detail(id string) *model.EncounterDetail {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 1.1 处理 Photos 为 []string,同时忽略原本的 Photos 字段。
|
// 1.1 处理 Photos 为 []string,同时忽略原本的 Photos 字段。
|
||||||
encounter.PhotosSlice = query_handler.StringToStringArray(encounter.Photos)
|
encounter.PhotosList = query_handler.StringToStringArray(encounter.Photos)
|
||||||
encounter.Photos = "" // 清空。
|
encounter.Photos = "" // 清空。
|
||||||
|
|
||||||
// 2. user data
|
// 2. user data
|
||||||
|
Loading…
x
Reference in New Issue
Block a user