animal like & list #1
This commit is contained in:
parent
3c93299f5e
commit
babd68def0
@ -21,8 +21,9 @@ func (a *Animals) List(context *gin.Context) {
|
|||||||
status := context.GetString(consts.ValidatorPrefix + "status")
|
status := context.GetString(consts.ValidatorPrefix + "status")
|
||||||
num := context.GetFloat64(consts.ValidatorPrefix + "num")
|
num := context.GetFloat64(consts.ValidatorPrefix + "num")
|
||||||
skip := context.GetFloat64(consts.ValidatorPrefix + "skip")
|
skip := context.GetFloat64(consts.ValidatorPrefix + "skip")
|
||||||
|
userId := context.GetFloat64(consts.ValidatorPrefix + "user_id")
|
||||||
|
|
||||||
animals := curd.CreateAnimalsCurdFactory().List(attrs, gender, breed, sterilzation, status, int(num), int(skip))
|
animals := curd.CreateAnimalsCurdFactory().List(attrs, gender, breed, sterilzation, status, int(num), int(skip), int(userId))
|
||||||
if animals != nil {
|
if animals != nil {
|
||||||
response.Success(context, consts.CurdStatusOkMsg, animals)
|
response.Success(context, consts.CurdStatusOkMsg, animals)
|
||||||
} else {
|
} else {
|
||||||
|
@ -72,5 +72,4 @@ func WebRegisterValidator() {
|
|||||||
containers.Set(key, encounter_like.Create{})
|
containers.Set(key, encounter_like.Create{})
|
||||||
key = consts.ValidatorPrefix + "EncounterLikeDelete"
|
key = consts.ValidatorPrefix + "EncounterLikeDelete"
|
||||||
containers.Set(key, encounter_like.Delete{})
|
containers.Set(key, encounter_like.Delete{})
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -17,6 +17,7 @@ type List struct {
|
|||||||
Status string `form:"status" json:"status"`
|
Status string `form:"status" json:"status"`
|
||||||
Num int `form:"num" json:"num"`
|
Num int `form:"num" json:"num"`
|
||||||
Skip int `form:"skip" json:"skip"`
|
Skip int `form:"skip" json:"skip"`
|
||||||
|
UserId int `form:"user_id" json:"user_id" binding:"required,min=1"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l List) CheckParams(context *gin.Context) {
|
func (l List) CheckParams(context *gin.Context) {
|
||||||
|
@ -25,6 +25,17 @@ func (a *AnimalLike) Create(userId, animalId int) bool {
|
|||||||
func (a *AnimalLike) SoftDelete(userId, animalId int) bool {
|
func (a *AnimalLike) SoftDelete(userId, animalId int) bool {
|
||||||
a.UsersModelId = userId
|
a.UsersModelId = userId
|
||||||
|
|
||||||
a.Unscoped().Where("animal_id = ? AND user_id = ?", a.AnimalId, a.UsersModelId).First(a)
|
a.Unscoped().Where("animal_id = ? AND user_id = ?", animalId, a.UsersModelId).First(a)
|
||||||
return a.Delete(a).Error == nil
|
return a.Delete(a).Error == nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @description: 查询是否存在关注记录
|
||||||
|
* @param {*} userId
|
||||||
|
* @param {int} animalId
|
||||||
|
* @return {*}
|
||||||
|
*/
|
||||||
|
func (a *AnimalLike) Liked(userId, animalId int) bool {
|
||||||
|
// 需要考虑 IsDel = 0;
|
||||||
|
return a.Where("animal_id = ? AND user_id = ?", animalId, userId).First(a).RowsAffected > 0
|
||||||
|
}
|
||||||
|
6
app/model/animal_result.go
Normal file
6
app/model/animal_result.go
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
package model
|
||||||
|
|
||||||
|
type AnimalWithLikeList struct {
|
||||||
|
Animal Animal `json:"animal"`
|
||||||
|
Like bool `json:"like"`
|
||||||
|
}
|
@ -48,7 +48,7 @@ func getSelectAttrs(attrs string) (validSelectedFields []string) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *AnimalsCurd) List(attrs string, gender string, breed string, sterilzation string, status string, num int, skip int) []model.Animal {
|
func (a *AnimalsCurd) List(attrs string, gender string, breed string, sterilzation string, status string, num int, skip int, userId int) (temp []model.AnimalWithLikeList) {
|
||||||
validSelectedFields := getSelectAttrs(attrs)
|
validSelectedFields := getSelectAttrs(attrs)
|
||||||
genderArray := query_handler.StringToUint8Array(gender)
|
genderArray := query_handler.StringToUint8Array(gender)
|
||||||
breedArray := query_handler.StringToUint8Array(breed)
|
breedArray := query_handler.StringToUint8Array(breed)
|
||||||
@ -59,7 +59,16 @@ func (a *AnimalsCurd) List(attrs string, gender string, breed string, sterilzati
|
|||||||
num = 10
|
num = 10
|
||||||
}
|
}
|
||||||
|
|
||||||
return model.CreateAnimalFactory("mysql").Show(validSelectedFields, genderArray, breedArray, sterilzationArray, statusArray, num, skip)
|
animals := model.CreateAnimalFactory("").Show(validSelectedFields, genderArray, breedArray, sterilzationArray, statusArray, num, skip)
|
||||||
|
|
||||||
|
for i := range animals {
|
||||||
|
animalWithLike := model.AnimalWithLikeList{
|
||||||
|
Animal: animals[i],
|
||||||
|
Like: model.CreateAnimalLikeFactory("").Liked(userId, int(animals[i].Id)),
|
||||||
|
}
|
||||||
|
temp = append(temp, animalWithLike)
|
||||||
|
}
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *AnimalsCurd) Detail(id string) *model.Animal {
|
func (a *AnimalsCurd) Detail(id string) *model.Animal {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user