67 lines
1.6 KiB
Go
Raw Normal View History

2024-10-17 22:43:20 +08:00
package curd
2024-10-21 16:16:03 +08:00
import (
"catface/app/model"
"catface/app/utils/query_handler"
"strconv"
)
2024-10-17 22:43:20 +08:00
func CreateEncounterCurdFactory() *EncounterCurd {
return &EncounterCurd{model.CreateEncounterFactory("")}
}
type EncounterCurd struct {
encounter *model.Encounter
}
2024-10-20 21:01:12 +08:00
2024-11-02 11:06:27 +08:00
func (e *EncounterCurd) List(num, skip, user_id int, mode string) (result []model.EncounterList) {
2024-10-20 21:01:12 +08:00
if num == 0 {
2024-11-02 11:06:27 +08:00
num = 10
2024-10-20 21:01:12 +08:00
}
2024-11-02 11:06:27 +08:00
var likedAnimalIds []int
switch mode {
2024-11-13 19:43:26 +08:00
case "liked":
likedAnimalIds = model.CreateAnimalLikeFactory("").LikedCats(user_id)
2024-11-02 11:06:27 +08:00
}
result = model.CreateEncounterFactory("").Show(num, skip, user_id, likedAnimalIds)
return
2024-10-20 21:01:12 +08:00
}
2024-10-21 16:16:03 +08:00
2024-10-21 16:31:53 +08:00
func (e *EncounterCurd) Detail(id string) *model.EncounterDetail {
2024-10-21 16:16:03 +08:00
// 0. check id
idInt, err := strconv.Atoi(id)
if err != nil {
return nil
}
// 1. encounter data
encounter, err := model.CreateEncounterFactory("").ShowByID(int64(idInt))
if err != nil {
return nil
}
2024-10-23 01:33:34 +08:00
// 1.1 处理 Photos 为 []string同时忽略原本的 Photos 字段。
2024-11-13 19:43:26 +08:00
encounter.PhotosList = query_handler.StringToStringArray(encounter.Photos)
2024-11-02 14:19:43 +08:00
encounter.Photos = "" // 清空。
2024-10-23 01:33:34 +08:00
2024-10-21 16:16:03 +08:00
// 2. user data
2024-10-23 01:33:34 +08:00
user, err := model.CreateUserFactory("").ShowByID(encounter.UsersModelId, "user_avatar", "user_name", "id")
2024-10-21 16:16:03 +08:00
if err != nil {
return nil
}
// 3. animals data
var animals []model.Animal
if animals_id, ok := model.CreateEncounterAnimalLinkFactory("").ShowByEncounterId(encounter.Id); ok {
animals = model.CreateAnimalFactory("").ShowByIDs(animals_id, "avatar", "name", "id")
}
2024-10-21 16:16:03 +08:00
// 4. 合并
2024-10-21 16:31:53 +08:00
return &model.EncounterDetail{
Encounter: *encounter,
UsersModel: *user,
Animals: animals,
2024-10-21 16:31:53 +08:00
}
2024-10-21 16:16:03 +08:00
}