API: add encounter like

This commit is contained in:
Havoc412 2024-10-23 10:55:05 +08:00
parent 571bf9e689
commit 11596906a9
6 changed files with 99 additions and 0 deletions

View File

@ -0,0 +1,24 @@
package web
import (
"catface/app/global/consts"
"catface/app/model"
"catface/app/utils/response"
"github.com/gin-gonic/gin"
)
type EncounterLike struct {
}
func (e *EncounterLike) Create(context *gin.Context) {
userId := context.GetFloat64(consts.ValidatorPrefix + "user_id")
encounterId := context.GetFloat64(consts.ValidatorPrefix + "encounter_id")
if model.CreateEncounterLikeFactory("").Create(int(userId), int(encounterId)) {
response.Success(context, "点赞成功", nil)
} else {
response.Fail(context, consts.CurdCreatFailCode, consts.CurdCreatFailMsg+",新增错误", "")
}
}

View File

@ -7,6 +7,7 @@ import (
"catface/app/http/validator/common/websocket"
"catface/app/http/validator/web/animal"
"catface/app/http/validator/web/encounter"
"catface/app/http/validator/web/encounter_like"
"catface/app/http/validator/web/users"
)
@ -61,4 +62,7 @@ func WebRegisterValidator() {
key = consts.ValidatorPrefix + "EncounterDetail"
containers.Set(key, encounter.Detial{})
key = consts.ValidatorPrefix + "EncounterLikeCreate"
containers.Set(key, encounter_like.Create{})
}

View File

@ -0,0 +1,30 @@
package encounter_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 int64 `form:"user_id" json:"user_id"`
EncounterId int64 `form:"encounter_id" json:"encounter_id"`
}
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, "EncounterLike Create表单验证器json化失败", "")
} else {
// 验证完成,调用控制器,并将验证器成员(字段)递给控制器,保持上下文数据一致性
(&web.EncounterLike{}).Create(extraAddBindDataContext)
}
}

View File

@ -1,5 +1,9 @@
package model
func CreateEncounterLikeFactory(sqlType string) *EncounterLike {
return &EncounterLike{BaseModel: BaseModel{DB: UseDbConn(sqlType)}}
}
type EncounterLike struct {
BaseModel
UsersModelId int `gorm:"column:user_id" json:"user_id"`
@ -8,3 +12,25 @@ type EncounterLike struct {
Encounter Encounter
DeletedAt
}
func (e *EncounterLike) Create(userId, encounterId int) bool {
e.UsersModelId = userId
e.EncounterId = encounterId
e.Where("encounter_id = ?", e.EncounterId).First(e)
e.IsDel = 0 //
if err := e.Save(e).Error; err != nil {
return false
}
return true
}
func (e *EncounterLike) Delete(userId, encounterId int) bool {
e.UsersModelId = userId
e.EncounterId = encounterId
//
if err := e.Where("encounter_id = ?", e.EncounterId).Delete(e).Error; err != nil {
return false
}
return true
}

View File

@ -0,0 +1,11 @@
package curd
import "catface/app/model"
func CreateEncounterLikeCurdFactory() *EncounterLikeCurd {
return &EncounterLikeCurd{model.CreateEncounterLikeFactory("")}
}
type EncounterLikeCurd struct {
encounterLike *model.EncounterLike
}

View File

@ -132,6 +132,10 @@ func InitWebRouter() *gin.Engine {
encounter.GET(":encounter_id", validatorFactory.Create(consts.ValidatorPrefix+"EncounterDetail"))
encounter.POST("", validatorFactory.Create(consts.ValidatorPrefix+"EncounterStore"))
// Like / unlike
encounter.GET("like", validatorFactory.Create(consts.ValidatorPrefix+"EncounterLikeCreate"))
// encounter.DELETE("like", validatorFactory.Create(consts.ValidatorPrefix+"EncounterLikeDestroy"))
}
// }
}