整理搜藏接口

This commit is contained in:
mlogclub
2022-12-03 15:52:19 +08:00
parent 89cc6b0a62
commit 282a513304
3 changed files with 43 additions and 8 deletions

View File

@@ -1,10 +1,13 @@
package api
import (
"errors"
"github.com/kataras/iris/v12"
"github.com/mlogclub/simple/web"
"github.com/mlogclub/simple/web/params"
"bbs-go/model/constants"
"bbs-go/pkg/errs"
"bbs-go/services"
)
@@ -13,11 +16,37 @@ type FavoriteController struct {
Ctx iris.Context
}
func (c *FavoriteController) PostAdd() *web.JsonResult {
var (
user = services.UserTokenService.GetCurrent(c.Ctx)
entityType = params.FormValue(c.Ctx, "entityType")
entityId = params.FormValueInt64Default(c.Ctx, "entityId", 0)
)
if user == nil {
return web.JsonError(errs.NotLogin)
}
var err error
if entityType == constants.EntityTopic {
err = services.FavoriteService.AddTopicFavorite(user.Id, entityId)
} else if entityType == constants.EntityArticle {
err = services.FavoriteService.AddArticleFavorite(user.Id, entityId)
} else {
err = errors.New("unsupproted")
}
if err != nil {
return web.JsonError(err)
}
return web.JsonSuccess()
}
// 取消收藏
func (c *FavoriteController) GetDelete() *web.JsonResult {
user := services.UserTokenService.GetCurrent(c.Ctx)
entityType := params.FormValue(c.Ctx, "entityType")
entityId := params.FormValueInt64Default(c.Ctx, "entityId", 0)
func (c *FavoriteController) PostDelete() *web.JsonResult {
var (
user = services.UserTokenService.GetCurrent(c.Ctx)
entityType = params.FormValue(c.Ctx, "entityType")
entityId = params.FormValueInt64Default(c.Ctx, "entityId", 0)
)
if user == nil {
return web.JsonError(errs.NotLogin)
}

View File

@@ -223,7 +223,7 @@ export default {
async addFavorite(articleId) {
try {
if (this.article.favorited) {
await this.$axios.get('/api/favorite/delete', {
await this.$axios.post('/api/favorite/delete', {
params: {
entityType: 'article',
entityId: articleId,
@@ -232,7 +232,10 @@ export default {
this.article.favorited = false
this.$message.success('已取消收藏')
} else {
await this.$axios.post('/api/article/favorite/' + articleId)
await this.$axios.post('/api/favorite/add', {
entityType: 'article',
entityId: articleId,
})
this.article.favorited = true
this.$message.success('收藏成功')
}

View File

@@ -259,7 +259,7 @@ export default {
async addFavorite(topicId) {
try {
if (this.topic.favorited) {
await this.$axios.get('/api/favorite/delete', {
await this.$axios.post('/api/favorite/delete', {
params: {
entityType: 'topic',
entityId: topicId,
@@ -268,7 +268,10 @@ export default {
this.topic.favorited = false
this.$message.success('已取消收藏')
} else {
await this.$axios.get('/api/topic/favorite/' + topicId)
await this.$axios.post('/api/favorite/add', {
entityType: 'topic',
entityId: topicId,
})
this.topic.favorited = true
this.$message.success('收藏成功')
}