new animal like #1

This commit is contained in:
Havoc412 2024-10-23 12:27:38 +08:00
parent 6d6d26f387
commit 3c93299f5e
7 changed files with 138 additions and 2 deletions

View File

@ -0,0 +1,32 @@
package web
import (
"catface/app/global/consts"
"catface/app/model"
"catface/app/utils/response"
"github.com/gin-gonic/gin"
)
type AnimalLike struct {
}
func (a *AnimalLike) Create(context *gin.Context) {
userId := context.GetFloat64(consts.ValidatorPrefix + "user_id")
animalId := context.GetFloat64(consts.ValidatorPrefix + "animal_id")
if model.CreateAnimalLikeFactory("").Create(int(userId), int(animalId)) {
response.Success(context, "关注成功", nil)
} else {
response.Fail(context, consts.CurdCreatFailCode, consts.CurdCreatFailMsg+",新增错误", "")
}
}
func (a *AnimalLike) Delete(context *gin.Context) {
userId := context.GetFloat64(consts.ValidatorPrefix + "user_id")
animalId := context.GetFloat64(consts.ValidatorPrefix + "animal_id")
if model.CreateAnimalLikeFactory("").SoftDelete(int(userId), int(animalId)) {
response.Success(context, "取消关注成功", nil)
} else {
response.Fail(context, consts.CurdDeleteFailCode, consts.CurdDeleteFailMsg+",删除错误", "")
}
}

View File

@ -6,6 +6,7 @@ import (
"catface/app/http/validator/common/upload_files"
"catface/app/http/validator/common/websocket"
"catface/app/http/validator/web/animal"
"catface/app/http/validator/web/animal_like"
"catface/app/http/validator/web/encounter"
"catface/app/http/validator/web/encounter_like"
"catface/app/http/validator/web/users"
@ -54,6 +55,11 @@ func WebRegisterValidator() {
key = consts.ValidatorPrefix + "AnimalDetail"
containers.Set(key, animal.Detail{})
key = consts.ValidatorPrefix + "AnimalLikeCreate"
containers.Set(key, animal_like.Create{})
key = consts.ValidatorPrefix + "AnimalLikeDelete"
containers.Set(key, animal_like.Delete{})
// TAG Encounter
key = consts.ValidatorPrefix + "EncounterStore"
containers.Set(key, encounter.Create{})

View File

@ -0,0 +1,29 @@
package animal_like
import (
"catface/app/global/consts"
"catface/app/http/controller/web"
"catface/app/http/validator/core/data_transfer"
"catface/app/utils/response"
"github.com/gin-gonic/gin"
)
type Create struct {
UserId int `form:"user_id" json:"user_id" binding:"required,min=1"`
AnimalId int `form:"animal_id" json:"animal_id" binding:"required,min=1"`
}
func (c Create) CheckParams(context *gin.Context) {
if err := context.ShouldBind(&c); err != nil {
response.ValidatorError(context, err)
return
}
extraAddBindDataContext := data_transfer.DataAddContext(c, consts.ValidatorPrefix, context)
if extraAddBindDataContext == nil {
response.ErrorSystem(context, "AnimalLike Create表单验证器json化失败", "")
} else {
// 验证完成,调用控制器,并将验证器成员(字段)递给控制器,保持上下文数据一致性
(&web.AnimalLike{}).Create(extraAddBindDataContext)
}
}

View File

@ -0,0 +1,29 @@
package animal_like
import (
"catface/app/global/consts"
"catface/app/http/controller/web"
"catface/app/http/validator/core/data_transfer"
"catface/app/utils/response"
"github.com/gin-gonic/gin"
)
type Delete struct {
UserId int `form:"user_id" json:"user_id" binding:"required,min=1"`
AnimalId int `form:"animal_id" json:"animal_id" binding:"required,min=1"`
}
func (d Delete) CheckParams(context *gin.Context) {
if err := context.ShouldBind(&d); err != nil {
response.ValidatorError(context, err)
return
}
extraAddBindDataContext := data_transfer.DataAddContext(d, consts.ValidatorPrefix, context)
if extraAddBindDataContext == nil {
response.ErrorSystem(context, "AnimalLike Delete 表单验证器json化失败", "")
} else {
// 验证完成,调用控制器,并将验证器成员(字段)递给控制器,保持上下文数据一致性
(&web.AnimalLike{}).Delete(extraAddBindDataContext)
}
}

30
app/model/animal_like.go Normal file
View File

@ -0,0 +1,30 @@
package model
func CreateAnimalLikeFactory(sqlType string) *AnimalLike {
return &AnimalLike{BaseModel: BaseModel{DB: UseDbConn(sqlType)}}
}
type AnimalLike struct {
BaseModel
UsersModelId int `gorm:"column:user_id" json:"user_id"`
UsersModel UsersModel
AnimalId int `gorm:"column:animal_id" json:"animal_id"`
Animal Animal
DeletedAt
}
func (a *AnimalLike) Create(userId, animalId int) bool {
a.UsersModelId = userId
a.AnimalId = animalId
a.Unscoped().Where("animal_id = ? AND user_id = ?", a.AnimalId, a.UsersModelId).First(a)
a.IsDel = 0
return a.Save(a).Error == nil
}
func (a *AnimalLike) SoftDelete(userId, animalId int) bool {
a.UsersModelId = userId
a.Unscoped().Where("animal_id = ? AND user_id = ?", a.AnimalId, a.UsersModelId).First(a)
return a.Delete(a).Error == nil
}

View File

@ -3,12 +3,10 @@ package routers
import (
"catface/app/global/consts"
"catface/app/global/variable"
// "catface/app/http/controller/captcha" // 验证码组件
// "catface/app/http/middleware/authorization"
"catface/app/http/middleware/cors"
validatorFactory "catface/app/http/validator/core/factory"
// TODO validatorFactory "catface/app/http/validator/core/factory"
"catface/app/utils/gin_release"
"net/http"
@ -123,6 +121,9 @@ func InitWebRouter() *gin.Engine {
{
animal.GET("", validatorFactory.Create(consts.ValidatorPrefix+"AnimalList"))
animal.GET(":anm_id", validatorFactory.Create(consts.ValidatorPrefix+"AnimalDetail"))
animal.POST("like", validatorFactory.Create(consts.ValidatorPrefix+"AnimalLikeCreate"))
animal.DELETE("like", validatorFactory.Create(consts.ValidatorPrefix+"AnimalLikeDelete"))
}
// backend.Use(authorization.CheckTokenAuth()) // INFO token 检查

View File

@ -95,3 +95,12 @@ func TestEncounterLike_Create_and_Delete(t *testing.T) {
res2 := DB.Save(&encounterLike)
fmt.Println(res2.RowsAffected)
}
func TestAnimalLike(t *testing.T) {
Init()
animalLike := model.AnimalLike{}
err := DB.AutoMigrate(&animalLike)
if err != nil {
t.Error(err)
}
}