finish encounter like & unlike
This commit is contained in:
parent
11596906a9
commit
f48c12ee4d
@ -22,3 +22,15 @@ func (e *EncounterLike) Create(context *gin.Context) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
func (e *EncounterLike) Delete(context *gin.Context) {
|
||||
userId := context.GetFloat64(consts.ValidatorPrefix + "user_id")
|
||||
encounterId := context.GetFloat64(consts.ValidatorPrefix + "encounter_id")
|
||||
|
||||
if model.CreateEncounterLikeFactory("").SoftDelete(int(userId), int(encounterId)) {
|
||||
response.Success(context, "取消点赞成功", nil)
|
||||
} else {
|
||||
response.Fail(context, consts.CurdDeleteFailCode, consts.CurdDeleteFailMsg+",删除错误", "")
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -64,5 +64,7 @@ func WebRegisterValidator() {
|
||||
|
||||
key = consts.ValidatorPrefix + "EncounterLikeCreate"
|
||||
containers.Set(key, encounter_like.Create{})
|
||||
key = consts.ValidatorPrefix + "EncounterLikeDelete"
|
||||
containers.Set(key, encounter_like.Delete{})
|
||||
|
||||
}
|
||||
|
30
app/http/validator/web/encounter_like/delete.go
Normal file
30
app/http/validator/web/encounter_like/delete.go
Normal 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 Delete struct {
|
||||
UserId int64 `form:"user_id" json:"user_id"`
|
||||
EncounterId int64 `form:"encounter_id" json:"encounter_id"`
|
||||
}
|
||||
|
||||
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, "EncounterLike Delete 表单验证器json化失败", "")
|
||||
} else {
|
||||
// 验证完成,调用控制器,并将验证器成员(字段)递给控制器,保持上下文数据一致性
|
||||
(&web.EncounterLike{}).Delete(extraAddBindDataContext)
|
||||
}
|
||||
}
|
@ -17,7 +17,7 @@ func (e *EncounterLike) Create(userId, encounterId int) bool {
|
||||
e.UsersModelId = userId
|
||||
e.EncounterId = encounterId
|
||||
|
||||
e.Where("encounter_id = ?", e.EncounterId).First(e)
|
||||
e.Unscoped().Where("encounter_id = ?", e.EncounterId).First(e)
|
||||
e.IsDel = 0 //
|
||||
if err := e.Save(e).Error; err != nil {
|
||||
return false
|
||||
@ -25,11 +25,11 @@ func (e *EncounterLike) Create(userId, encounterId int) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (e *EncounterLike) Delete(userId, encounterId int) bool {
|
||||
func (e *EncounterLike) SoftDelete(userId, encounterId int) bool {
|
||||
e.UsersModelId = userId
|
||||
e.EncounterId = encounterId
|
||||
//
|
||||
if err := e.Where("encounter_id = ?", e.EncounterId).Delete(e).Error; err != nil {
|
||||
e.Where("encounter_id = ?", encounterId).First(e)
|
||||
if err := e.Delete(e).Error; err != nil {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
|
@ -3,10 +3,12 @@ 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"
|
||||
@ -135,7 +137,7 @@ func InitWebRouter() *gin.Engine {
|
||||
|
||||
// Like / unlike
|
||||
encounter.GET("like", validatorFactory.Create(consts.ValidatorPrefix+"EncounterLikeCreate"))
|
||||
// encounter.DELETE("like", validatorFactory.Create(consts.ValidatorPrefix+"EncounterLikeDestroy"))
|
||||
encounter.DELETE("like", validatorFactory.Create(consts.ValidatorPrefix+"EncounterLikeDelete"))
|
||||
}
|
||||
// }
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user