fit use Animal Avatar as encounter Avatar

This commit is contained in:
Havoc412 2024-11-02 12:32:33 +08:00
parent 6496b24c14
commit ffde5d691f
8 changed files with 116 additions and 77 deletions

View File

@ -48,8 +48,7 @@ func (e *Encounters) Create(context *gin.Context) {
animals_id := data_transfer.GetFloat64Slice(context, "animals_id") // 由于是 Slice 就交给 EAlink 内部遍历时处理。 animals_id := data_transfer.GetFloat64Slice(context, "animals_id") // 由于是 Slice 就交给 EAlink 内部遍历时处理。
// Real Insert - 2: EA LINK // Real Insert - 2: EA LINK
if encounter_id, ok := model.CreateEncounterFactory("").InsertDate(context); ok && encounter_id > 0 { if encounter_id, ok := model.CreateEncounterFactory("").InsertDate(context); ok && encounter_id > 0 {
if !model.CreateEncounterAnimalLinkFactory("").Insert(int(encounter_id), animals_id) { if !model.CreateEncounterAnimalLinkFactory("").Insert(int64(encounter_id), animals_id) {
// TODO 异常处理。
response.Fail(context, errcode.ErrEaLinkInstert, errcode.ErrMsg[errcode.ErrEaLinkInstert], "") response.Fail(context, errcode.ErrEaLinkInstert, errcode.ErrMsg[errcode.ErrEaLinkInstert], "")
return return
} }

View File

@ -7,6 +7,7 @@ import (
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"go.uber.org/zap" "go.uber.org/zap"
) )
func CreateAnimalFactory(sqlType string) *Animal { func CreateAnimalFactory(sqlType string) *Animal {
@ -69,7 +70,7 @@ func (a *Animal) Show(attrs []string, gender []uint8, breed []uint8, sterilizati
return return
} }
func (a *Animal) ShowByID(id int) *Animal { func (a *Animal) ShowByID(id int64) *Animal {
var temp Animal var temp Animal
err := a.DB.Table(a.TableName()).Model(&temp).Where("id = ?", id).Scan(&temp).Error err := a.DB.Table(a.TableName()).Model(&temp).Where("id = ?", id).Scan(&temp).Error
if err != nil { if err != nil {

View File

@ -9,6 +9,7 @@ import (
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"go.uber.org/zap" "go.uber.org/zap"
"gorm.io/gorm" "gorm.io/gorm"
) )
func CreateEncounterFactory(sqlType string) *Encounter { func CreateEncounterFactory(sqlType string) *Encounter {
@ -77,7 +78,20 @@ func formatEncounterList(rows *gorm.DB) (temp []EncounterList, err error) {
log.Println("扫描失败:", err) log.Println("扫描失败:", err)
continue continue
} }
// STAGE: Set link Status
item.Like = ueLikeInt == 1 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) temp = append(temp, item)
} }

View File

@ -12,19 +12,41 @@ func CreateEncounterAnimalLinkFactory(sqlType string) *EncounterAnimalLink {
*/ */
type EncounterAnimalLink struct { type EncounterAnimalLink struct {
*gorm.DB `gorm:"-" json:"-"` *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 Encounter Encounter
AnimalId int `gorm:"column:animal_id;index" json:"animal_id"` AnimalId int64 `gorm:"column:animal_id;index" json:"animal_id"`
Animal Animal Animal Animal
} }
func (e *EncounterAnimalLink) Insert(encounterId int, animalId []float64) bool { func (e *EncounterAnimalLink) Insert(encounterId int64, animalId []float64) bool {
// Build Slice // Build Slice
var results []EncounterAnimalLink var results []EncounterAnimalLink
for _, id := range animalId { 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 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
}

View File

@ -11,10 +11,12 @@ type EncounterList struct {
AvatarHeight int `form:"avatar_height" json:"height"` AvatarHeight int `form:"avatar_height" json:"height"`
AvatarWidth int `form:"avatar_width" json:"width"` AvatarWidth int `form:"avatar_width" json:"width"`
UpdatedAt *time.Time `form:"updated_at" json:"time"` // TIP 设为 *time.Timeomitempty 和 autoUpdated 就都可以生效 UpdatedAt *time.Time `form:"updated_at" json:"time"` // TIP 设为 *time.Timeomitempty 和 autoUpdated 就都可以生效
Like bool `form:"ue_like" json:"like"`
UserName string `form:"user_name" json:"userName"` Like bool `form:"ue_like" json:"like"`
UserAvatar string `form:"user_avatar" json:"userAvatar"` 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 { type EncounterDetail struct {

View File

@ -6,6 +6,7 @@ import (
"catface/app/utils/query_handler" "catface/app/utils/query_handler"
"fmt" "fmt"
"strconv" "strconv"
) )
func CreateAnimalsCurdFactory() *AnimalsCurd { func CreateAnimalsCurdFactory() *AnimalsCurd {
@ -102,5 +103,5 @@ func (a *AnimalsCurd) Detail(id string) *model.Animal {
return nil return nil
} }
return model.CreateAnimalFactory("mysql").ShowByID(idInt) return model.CreateAnimalFactory("mysql").ShowByID(int64(idInt))
} }

View File

@ -4,6 +4,7 @@ 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 {
@ -52,14 +53,15 @@ func (e *EncounterCurd) Detail(id string) *model.EncounterDetail {
} }
// 3. animals data // 3. animals data
// animals_id := query_handler.StringToint64Array(encounter.AnimalsId) var animals []model.Animal
// animals := model.CreateAnimalFactory("").ShowByIDs(animals_id, "avatar", "name", "id") if animals_id, ok := model.CreateEncounterAnimalLinkFactory("").ShowByEncounterId(encounter.Id); ok {
// _ = animals animals = model.CreateAnimalFactory("").ShowByIDs(animals_id, "avatar", "name", "id")
}
// 4. 合并 // 4. 合并
return &model.EncounterDetail{ return &model.EncounterDetail{
Encounter: *encounter, Encounter: *encounter,
UsersModel: *user, UsersModel: *user,
// Animals: animals, Animals: animals,
} }
} }

View File

@ -4,10 +4,8 @@ package test
import ( import (
"catface/app/model" "catface/app/model"
"fmt" "fmt"
"reflect"
"strconv"
"strings"
"testing" "testing"
) )
func TestUsers(t *testing.T) { func TestUsers(t *testing.T) {
@ -117,67 +115,67 @@ func TestEaLink(t *testing.T) {
} }
} }
// 测试函数 // // 测试函数
func TestInsertEncounterAnimalLinks(t *testing.T) { // func TestInsertEncounterAnimalLinks(t *testing.T) {
Init() // Init()
// 定义要插入的数据 // // 定义要插入的数据
data := []struct { // data := []struct {
EncounterId int // EncounterId int
AnimalIds string // AnimalIds string
}{ // }{
{10, "4"}, // {10, "4"},
{11, "2,3"}, // {11, "2,3"},
{13, "4"}, // {13, "4"},
{14, "4"}, // {14, "4"},
{15, "4"}, // {15, "4"},
{16, "4"}, // {16, "4"},
{17, "4"}, // {17, "4"},
{18, "4"}, // {18, "4"},
{19, "4"}, // {19, "4"},
{20, "4"}, // {20, "4"},
} // }
// 插入数据 // // 插入数据
for _, item := range data { // for _, item := range data {
animalIds := strings.Split(item.AnimalIds, ",") // animalIds := strings.Split(item.AnimalIds, ",")
for _, animalIdStr := range animalIds { // for _, animalIdStr := range animalIds {
animalId, err := strconv.Atoi(animalIdStr) // animalId, err := strconv.Atoi(animalIdStr)
if err != nil { // if err != nil {
t.Errorf("Failed to convert animal Id: %v", err) // t.Errorf("Failed to convert animal Id: %v", err)
continue // continue
} // }
link := model.EncounterAnimalLink{ // link := model.EncounterAnimalLink{
EncounterId: item.EncounterId, // EncounterId: item.EncounterId,
AnimalId: animalId, // AnimalId: animalId,
} // }
if err := DB.Create(&link).Error; err != nil { // if err := DB.Create(&link).Error; err != nil {
t.Errorf("Failed to insert link: %v", err) // t.Errorf("Failed to insert link: %v", err)
} // }
} // }
} // }
// 验证数据是否正确插入 // // 验证数据是否正确插入
var links []model.EncounterAnimalLink // var links []model.EncounterAnimalLink
if err := DB.Find(&links).Error; err != nil { // if err := DB.Find(&links).Error; err != nil {
t.Errorf("Failed to fetch links: %v", err) // t.Errorf("Failed to fetch links: %v", err)
} // }
expectedLinks := []model.EncounterAnimalLink{ // expectedLinks := []model.EncounterAnimalLink{
{EncounterId: 10, AnimalId: 4}, // {EncounterId: 10, AnimalId: 4},
{EncounterId: 11, AnimalId: 2}, // {EncounterId: 11, AnimalId: 2},
{EncounterId: 11, AnimalId: 3}, // {EncounterId: 11, AnimalId: 3},
{EncounterId: 13, AnimalId: 4}, // {EncounterId: 13, AnimalId: 4},
{EncounterId: 14, AnimalId: 4}, // {EncounterId: 14, AnimalId: 4},
{EncounterId: 15, AnimalId: 4}, // {EncounterId: 15, AnimalId: 4},
{EncounterId: 16, AnimalId: 4}, // {EncounterId: 16, AnimalId: 4},
{EncounterId: 17, AnimalId: 4}, // {EncounterId: 17, AnimalId: 4},
{EncounterId: 18, AnimalId: 4}, // {EncounterId: 18, AnimalId: 4},
{EncounterId: 19, AnimalId: 4}, // {EncounterId: 19, AnimalId: 4},
{EncounterId: 20, AnimalId: 4}, // {EncounterId: 20, AnimalId: 4},
} // }
if !reflect.DeepEqual(links, expectedLinks) { // if !reflect.DeepEqual(links, expectedLinks) {
t.Errorf("Expected links: %v, but got: %v", expectedLinks, links) // t.Errorf("Expected links: %v, but got: %v", expectedLinks, links)
} // }
} // }