Initial commit
This commit is contained in:
@@ -0,0 +1,159 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/kataras/iris/v12"
|
||||
"github.com/kataras/iris/v12/context"
|
||||
"io"
|
||||
"pet-house.com/business/models"
|
||||
"pet-house.com/core/server/database"
|
||||
"pet-house.com/core/server/web/web_iris"
|
||||
)
|
||||
|
||||
type UserPetInfo struct {
|
||||
PetInfo models.Pet `json:"petInfo"`
|
||||
PetBaseInfo models.PetBaseInfo `json:"petBaseInfo"`
|
||||
}
|
||||
|
||||
func GetUserPets(uId int64) []UserPetInfo {
|
||||
var userPetList []models.Pet
|
||||
database.Instance().Model(&models.Pet{}).Where("uid = ?", uId).Find(&userPetList)
|
||||
var userPets []UserPetInfo
|
||||
if len(userPetList) > 0 {
|
||||
for _, pet := range userPetList {
|
||||
userPets = append(userPets, UserPetInfo{pet, PetBaseInfoMap[pet.PetId]})
|
||||
}
|
||||
}
|
||||
return userPets
|
||||
}
|
||||
|
||||
func GetUserPet(uId int64, pId int64) UserPetInfo {
|
||||
var userPet models.Pet
|
||||
database.Instance().Model(&models.Pet{}).Where("uid = ? and id = ?", uId, pId).Find(&userPet)
|
||||
return UserPetInfo{userPet, PetBaseInfoMap[userPet.PetId]}
|
||||
}
|
||||
|
||||
type PetListResponse struct {
|
||||
UserPets []UserPetInfo `json:"userPets"`
|
||||
}
|
||||
|
||||
// 宠物列表
|
||||
func (p DefParty) petList() web_iris.Party {
|
||||
return web_iris.Party{Perfix: p.Perfix, PartyFunc: func(index iris.Party) {
|
||||
index.Post(PetBase+"/petList", func(ctx *context.Context) {
|
||||
headerBaseInfo := GetHeaderBaseInfo(ctx)
|
||||
userPets := GetUserPets(headerBaseInfo.Uid)
|
||||
Success(ctx, headerBaseInfo, PetListResponse{
|
||||
userPets,
|
||||
})
|
||||
})
|
||||
}}
|
||||
}
|
||||
|
||||
type PetInfoRequest struct {
|
||||
Id int64
|
||||
}
|
||||
|
||||
type PetInfoResponse struct {
|
||||
UserPet UserPetInfo `json:"userPet"`
|
||||
}
|
||||
|
||||
func (p DefParty) petInfo() web_iris.Party {
|
||||
return web_iris.Party{Perfix: p.Perfix, PartyFunc: func(index iris.Party) {
|
||||
index.Post(PetBase+"/petInfo", func(ctx *context.Context) {
|
||||
headerBaseInfo := GetHeaderBaseInfo(ctx)
|
||||
body, _ := io.ReadAll(ctx.Request().Body)
|
||||
var petInfoRequest PetInfoRequest
|
||||
json.Unmarshal(body, &petInfoRequest)
|
||||
userPet := GetUserPet(headerBaseInfo.Uid, petInfoRequest.Id)
|
||||
Success(ctx, petInfoRequest, PetInfoResponse{userPet})
|
||||
})
|
||||
}}
|
||||
}
|
||||
|
||||
type PetAddOrEditRequest struct {
|
||||
Id int64 //Id 不为0表示修改
|
||||
NickName string //宠物昵称
|
||||
HeadImgType int //头像类型 0远程头像 1本地头像
|
||||
HeadImgUrl string //宠物头像
|
||||
Desc string //宠物描述
|
||||
Precaution string //注意事项
|
||||
Gender int //性别 0男 1女
|
||||
Birthday string //生日
|
||||
PetId int //宠物类型
|
||||
Eunuch int //是否绝育 0否 1是 2未知
|
||||
}
|
||||
|
||||
type PetAddOrEditResponse struct {
|
||||
UserPets []UserPetInfo `json:"userPets"`
|
||||
}
|
||||
|
||||
// 宠物添加或编辑
|
||||
func (p DefParty) petAddOrEdit() web_iris.Party {
|
||||
return web_iris.Party{Perfix: p.Perfix, PartyFunc: func(index iris.Party) {
|
||||
index.Post(PetBase+"/petAddOrEdit", func(ctx *context.Context) {
|
||||
headerBaseInfo := GetHeaderBaseInfo(ctx)
|
||||
body, _ := io.ReadAll(ctx.Request().Body)
|
||||
var petAddOrEditRequest PetAddOrEditRequest
|
||||
json.Unmarshal(body, &petAddOrEditRequest)
|
||||
if PetBaseInfoMap[petAddOrEditRequest.PetId].Id == 0 {
|
||||
PetBaseNotExistError.Fail(ctx, petAddOrEditRequest)
|
||||
return
|
||||
}
|
||||
pet := models.Pet{
|
||||
Uid: headerBaseInfo.Uid,
|
||||
NickName: petAddOrEditRequest.NickName,
|
||||
HeadImgType: petAddOrEditRequest.HeadImgType,
|
||||
HeadImgUrl: petAddOrEditRequest.HeadImgUrl,
|
||||
Desc: petAddOrEditRequest.Desc,
|
||||
Precaution: petAddOrEditRequest.Precaution,
|
||||
Gender: petAddOrEditRequest.Gender,
|
||||
Birthday: petAddOrEditRequest.Birthday,
|
||||
PetId: petAddOrEditRequest.PetId,
|
||||
Eunuch: petAddOrEditRequest.Eunuch,
|
||||
}
|
||||
if petAddOrEditRequest.Id == 0 {
|
||||
database.Instance().Model(&models.Pet{}).Create(&pet)
|
||||
} else {
|
||||
pet.Id = petAddOrEditRequest.Id
|
||||
var userPetInfo models.Pet
|
||||
database.Instance().Model(&models.Pet{}).Where("id = ? and uid = ?", pet.Id, headerBaseInfo.Uid).Find(&userPetInfo)
|
||||
if userPetInfo.Id == 0 {
|
||||
PetNotExistError.Fail(ctx, petAddOrEditRequest)
|
||||
return
|
||||
}
|
||||
updateValues := map[string]interface{}{
|
||||
"NickName": pet.NickName,
|
||||
"HeadImgType": pet.HeadImgType,
|
||||
"HeadImgUrl": pet.HeadImgUrl,
|
||||
"Desc": pet.Desc,
|
||||
"Precaution": pet.Precaution,
|
||||
"Gender": pet.Gender,
|
||||
"Birthday": pet.Birthday,
|
||||
"PetId": pet.PetId,
|
||||
"Eunuch": pet.Eunuch,
|
||||
}
|
||||
database.Instance().Model(&pet).Updates(&updateValues)
|
||||
}
|
||||
userPets := GetUserPets(headerBaseInfo.Uid)
|
||||
Success(ctx, petAddOrEditRequest, PetAddOrEditResponse{userPets})
|
||||
})
|
||||
}}
|
||||
}
|
||||
|
||||
type PetTypeListResponse struct {
|
||||
PetBaseInfoList []models.PetBaseInfo `json:"petBaseInfoList"`
|
||||
}
|
||||
|
||||
// 宠物基础信息列表
|
||||
func (p DefParty) petTypeList() web_iris.Party {
|
||||
return web_iris.Party{Perfix: p.Perfix, PartyFunc: func(index iris.Party) {
|
||||
index.Post(PetBase+"/petTypeList", func(ctx *context.Context) {
|
||||
var petBaseInfoList []models.PetBaseInfo
|
||||
for _, petBaseInfo := range PetBaseInfoMap {
|
||||
petBaseInfoList = append(petBaseInfoList, petBaseInfo)
|
||||
}
|
||||
Success(ctx, nil, PetTypeListResponse{petBaseInfoList})
|
||||
})
|
||||
}}
|
||||
}
|
||||
Reference in New Issue
Block a user