fit use Animal Avatar as encounter Avatar
This commit is contained in:
parent
6496b24c14
commit
ffde5d691f
@ -48,8 +48,7 @@ func (e *Encounters) Create(context *gin.Context) {
|
||||
animals_id := data_transfer.GetFloat64Slice(context, "animals_id") // 由于是 Slice 就交给 EAlink 内部遍历时处理。
|
||||
// Real Insert - 2: EA LINK
|
||||
if encounter_id, ok := model.CreateEncounterFactory("").InsertDate(context); ok && encounter_id > 0 {
|
||||
if !model.CreateEncounterAnimalLinkFactory("").Insert(int(encounter_id), animals_id) {
|
||||
// TODO 异常处理。
|
||||
if !model.CreateEncounterAnimalLinkFactory("").Insert(int64(encounter_id), animals_id) {
|
||||
response.Fail(context, errcode.ErrEaLinkInstert, errcode.ErrMsg[errcode.ErrEaLinkInstert], "")
|
||||
return
|
||||
}
|
||||
|
@ -7,6 +7,7 @@ import (
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"go.uber.org/zap"
|
||||
|
||||
)
|
||||
|
||||
func CreateAnimalFactory(sqlType string) *Animal {
|
||||
@ -69,7 +70,7 @@ func (a *Animal) Show(attrs []string, gender []uint8, breed []uint8, sterilizati
|
||||
return
|
||||
}
|
||||
|
||||
func (a *Animal) ShowByID(id int) *Animal {
|
||||
func (a *Animal) ShowByID(id int64) *Animal {
|
||||
var temp Animal
|
||||
err := a.DB.Table(a.TableName()).Model(&temp).Where("id = ?", id).Scan(&temp).Error
|
||||
if err != nil {
|
||||
|
@ -9,6 +9,7 @@ import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"go.uber.org/zap"
|
||||
"gorm.io/gorm"
|
||||
|
||||
)
|
||||
|
||||
func CreateEncounterFactory(sqlType string) *Encounter {
|
||||
@ -77,7 +78,20 @@ func formatEncounterList(rows *gorm.DB) (temp []EncounterList, err error) {
|
||||
log.Println("扫描失败:", err)
|
||||
continue
|
||||
}
|
||||
// STAGE: Set link Status
|
||||
item.Like = ueLikeInt == 1
|
||||
// STAGE: Check Url exist
|
||||
if item.Avatar == "" {
|
||||
if animal_id, ok := CreateEncounterAnimalLinkFactory("").ShowByEncounterIdFirst(int64(item.Id)); ok {
|
||||
if animal := CreateAnimalFactory("").ShowByID(animal_id); animal != nil {
|
||||
item.Avatar = animal.Avatar
|
||||
item.AvatarHeight = int(animal.AvatarHeight)
|
||||
item.AvatarWidth = int(animal.AvatarWidth)
|
||||
item.UseAnimalAvatar = true
|
||||
}
|
||||
}
|
||||
}
|
||||
// append
|
||||
temp = append(temp, item)
|
||||
}
|
||||
|
||||
|
@ -12,19 +12,41 @@ func CreateEncounterAnimalLinkFactory(sqlType string) *EncounterAnimalLink {
|
||||
*/
|
||||
type EncounterAnimalLink struct {
|
||||
*gorm.DB `gorm:"-" json:"-"`
|
||||
EncounterId int `gorm:"column:encounter_id;index" json:"encounter_id"`
|
||||
EncounterId int64 `gorm:"column:encounter_id;index" json:"encounter_id"`
|
||||
Encounter Encounter
|
||||
AnimalId int `gorm:"column:animal_id;index" json:"animal_id"`
|
||||
AnimalId int64 `gorm:"column:animal_id;index" json:"animal_id"`
|
||||
Animal Animal
|
||||
}
|
||||
|
||||
func (e *EncounterAnimalLink) Insert(encounterId int, animalId []float64) bool {
|
||||
func (e *EncounterAnimalLink) Insert(encounterId int64, animalId []float64) bool {
|
||||
// Build Slice
|
||||
var results []EncounterAnimalLink
|
||||
for _, id := range animalId {
|
||||
results = append(results, EncounterAnimalLink{EncounterId: encounterId, AnimalId: int(id)})
|
||||
results = append(results, EncounterAnimalLink{EncounterId: encounterId, AnimalId: int64(id)})
|
||||
}
|
||||
|
||||
// 调用批处理插入方法
|
||||
return e.Create(&results).Error == nil
|
||||
}
|
||||
|
||||
func (e *EncounterAnimalLink) ShowByEncounterId(encounterId int64) ([]int64, bool) {
|
||||
var results []EncounterAnimalLink
|
||||
if err := e.Where("encounter_id = ?", encounterId).Find(&results).Error; err != nil {
|
||||
// 处理错误情况,例如日志记录或返回错误信息
|
||||
return nil, false
|
||||
}
|
||||
intSlice := make([]int64, len(results))
|
||||
for i, result := range results {
|
||||
intSlice[i] = result.AnimalId // 假设 AnimalId 是你需要提取的字段
|
||||
}
|
||||
return intSlice, true
|
||||
}
|
||||
|
||||
func (e *EncounterAnimalLink) ShowByEncounterIdFirst(encounterId int64) (int64, bool) {
|
||||
var results EncounterAnimalLink
|
||||
if err := e.Where("encounter_id = ?", encounterId).First(&results).Error; err != nil {
|
||||
// 处理错误情况,例如日志记录或返回错误信息
|
||||
return 0, false
|
||||
}
|
||||
return results.AnimalId, true
|
||||
}
|
||||
|
@ -11,10 +11,12 @@ type EncounterList struct {
|
||||
AvatarHeight int `form:"avatar_height" json:"height"`
|
||||
AvatarWidth int `form:"avatar_width" json:"width"`
|
||||
UpdatedAt *time.Time `form:"updated_at" json:"time"` // TIP 设为 *time.Time,omitempty 和 autoUpdated 就都可以生效
|
||||
Like bool `form:"ue_like" json:"like"`
|
||||
|
||||
UserName string `form:"user_name" json:"userName"`
|
||||
UserAvatar string `form:"user_avatar" json:"userAvatar"`
|
||||
Like bool `form:"ue_like" json:"like"`
|
||||
UseAnimalAvatar bool `form:"use_animal_avatar" json:"use_animal_avatar"`
|
||||
|
||||
UserName string `form:"user_name" json:"user_name"`
|
||||
UserAvatar string `form:"user_avatar" json:"user_avatar"`
|
||||
}
|
||||
|
||||
type EncounterDetail struct {
|
||||
|
@ -6,6 +6,7 @@ import (
|
||||
"catface/app/utils/query_handler"
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
)
|
||||
|
||||
func CreateAnimalsCurdFactory() *AnimalsCurd {
|
||||
@ -102,5 +103,5 @@ func (a *AnimalsCurd) Detail(id string) *model.Animal {
|
||||
return nil
|
||||
}
|
||||
|
||||
return model.CreateAnimalFactory("mysql").ShowByID(idInt)
|
||||
return model.CreateAnimalFactory("mysql").ShowByID(int64(idInt))
|
||||
}
|
||||
|
@ -4,6 +4,7 @@ import (
|
||||
"catface/app/model"
|
||||
"catface/app/utils/query_handler"
|
||||
"strconv"
|
||||
|
||||
)
|
||||
|
||||
func CreateEncounterCurdFactory() *EncounterCurd {
|
||||
@ -52,14 +53,15 @@ func (e *EncounterCurd) Detail(id string) *model.EncounterDetail {
|
||||
}
|
||||
|
||||
// 3. animals data
|
||||
// animals_id := query_handler.StringToint64Array(encounter.AnimalsId)
|
||||
// animals := model.CreateAnimalFactory("").ShowByIDs(animals_id, "avatar", "name", "id")
|
||||
// _ = animals
|
||||
var animals []model.Animal
|
||||
if animals_id, ok := model.CreateEncounterAnimalLinkFactory("").ShowByEncounterId(encounter.Id); ok {
|
||||
animals = model.CreateAnimalFactory("").ShowByIDs(animals_id, "avatar", "name", "id")
|
||||
}
|
||||
|
||||
// 4. 合并
|
||||
return &model.EncounterDetail{
|
||||
Encounter: *encounter,
|
||||
UsersModel: *user,
|
||||
// Animals: animals,
|
||||
Animals: animals,
|
||||
}
|
||||
}
|
||||
|
@ -4,10 +4,8 @@ package test
|
||||
import (
|
||||
"catface/app/model"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
)
|
||||
|
||||
func TestUsers(t *testing.T) {
|
||||
@ -117,67 +115,67 @@ func TestEaLink(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// 测试函数
|
||||
func TestInsertEncounterAnimalLinks(t *testing.T) {
|
||||
Init()
|
||||
// // 测试函数
|
||||
// func TestInsertEncounterAnimalLinks(t *testing.T) {
|
||||
// Init()
|
||||
|
||||
// 定义要插入的数据
|
||||
data := []struct {
|
||||
EncounterId int
|
||||
AnimalIds string
|
||||
}{
|
||||
{10, "4"},
|
||||
{11, "2,3"},
|
||||
{13, "4"},
|
||||
{14, "4"},
|
||||
{15, "4"},
|
||||
{16, "4"},
|
||||
{17, "4"},
|
||||
{18, "4"},
|
||||
{19, "4"},
|
||||
{20, "4"},
|
||||
}
|
||||
// // 定义要插入的数据
|
||||
// data := []struct {
|
||||
// EncounterId int
|
||||
// AnimalIds string
|
||||
// }{
|
||||
// {10, "4"},
|
||||
// {11, "2,3"},
|
||||
// {13, "4"},
|
||||
// {14, "4"},
|
||||
// {15, "4"},
|
||||
// {16, "4"},
|
||||
// {17, "4"},
|
||||
// {18, "4"},
|
||||
// {19, "4"},
|
||||
// {20, "4"},
|
||||
// }
|
||||
|
||||
// 插入数据
|
||||
for _, item := range data {
|
||||
animalIds := strings.Split(item.AnimalIds, ",")
|
||||
for _, animalIdStr := range animalIds {
|
||||
animalId, err := strconv.Atoi(animalIdStr)
|
||||
if err != nil {
|
||||
t.Errorf("Failed to convert animal Id: %v", err)
|
||||
continue
|
||||
}
|
||||
link := model.EncounterAnimalLink{
|
||||
EncounterId: item.EncounterId,
|
||||
AnimalId: animalId,
|
||||
}
|
||||
if err := DB.Create(&link).Error; err != nil {
|
||||
t.Errorf("Failed to insert link: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
// // 插入数据
|
||||
// for _, item := range data {
|
||||
// animalIds := strings.Split(item.AnimalIds, ",")
|
||||
// for _, animalIdStr := range animalIds {
|
||||
// animalId, err := strconv.Atoi(animalIdStr)
|
||||
// if err != nil {
|
||||
// t.Errorf("Failed to convert animal Id: %v", err)
|
||||
// continue
|
||||
// }
|
||||
// link := model.EncounterAnimalLink{
|
||||
// EncounterId: item.EncounterId,
|
||||
// AnimalId: animalId,
|
||||
// }
|
||||
// if err := DB.Create(&link).Error; err != nil {
|
||||
// t.Errorf("Failed to insert link: %v", err)
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// 验证数据是否正确插入
|
||||
var links []model.EncounterAnimalLink
|
||||
if err := DB.Find(&links).Error; err != nil {
|
||||
t.Errorf("Failed to fetch links: %v", err)
|
||||
}
|
||||
// // 验证数据是否正确插入
|
||||
// var links []model.EncounterAnimalLink
|
||||
// if err := DB.Find(&links).Error; err != nil {
|
||||
// t.Errorf("Failed to fetch links: %v", err)
|
||||
// }
|
||||
|
||||
expectedLinks := []model.EncounterAnimalLink{
|
||||
{EncounterId: 10, AnimalId: 4},
|
||||
{EncounterId: 11, AnimalId: 2},
|
||||
{EncounterId: 11, AnimalId: 3},
|
||||
{EncounterId: 13, AnimalId: 4},
|
||||
{EncounterId: 14, AnimalId: 4},
|
||||
{EncounterId: 15, AnimalId: 4},
|
||||
{EncounterId: 16, AnimalId: 4},
|
||||
{EncounterId: 17, AnimalId: 4},
|
||||
{EncounterId: 18, AnimalId: 4},
|
||||
{EncounterId: 19, AnimalId: 4},
|
||||
{EncounterId: 20, AnimalId: 4},
|
||||
}
|
||||
// expectedLinks := []model.EncounterAnimalLink{
|
||||
// {EncounterId: 10, AnimalId: 4},
|
||||
// {EncounterId: 11, AnimalId: 2},
|
||||
// {EncounterId: 11, AnimalId: 3},
|
||||
// {EncounterId: 13, AnimalId: 4},
|
||||
// {EncounterId: 14, AnimalId: 4},
|
||||
// {EncounterId: 15, AnimalId: 4},
|
||||
// {EncounterId: 16, AnimalId: 4},
|
||||
// {EncounterId: 17, AnimalId: 4},
|
||||
// {EncounterId: 18, AnimalId: 4},
|
||||
// {EncounterId: 19, AnimalId: 4},
|
||||
// {EncounterId: 20, AnimalId: 4},
|
||||
// }
|
||||
|
||||
if !reflect.DeepEqual(links, expectedLinks) {
|
||||
t.Errorf("Expected links: %v, but got: %v", expectedLinks, links)
|
||||
}
|
||||
}
|
||||
// if !reflect.DeepEqual(links, expectedLinks) {
|
||||
// t.Errorf("Expected links: %v, but got: %v", expectedLinks, links)
|
||||
// }
|
||||
// }
|
||||
|
Loading…
x
Reference in New Issue
Block a user