请问在获取期刊点赞信息接口的设计时,课程里是这么设计的:GET: /:type/:id/favor, 需要传递两个参数,一个type,一个id
router.get('/:type/:id/favor', new Auth().m, async ctx => {
const v = await new ClassicValidator().validate(ctx)
const { type, id } = v.get('path')
const art = await Art.getData(id, type)
const { uid } = ctx.auth
const likeStatus = await Favor.userLikeIt(id, type, uid)
if (!art) {
throw new global.errs.NotFound()
}
ctx.body = {
favNums: art.favNums,
likeStatus
}
})
想问,如果只用期刊号(index),也能实现,从我的角度看,这种方式多调用了一次查询接口,但相对前端来说,我传递的参数都是index,对前端更友好,不是吗? 想请问下,这样设计有什么不合理的地方吗?
下面是通过index来设计的接口:
router.get('/:index/favor', new Auth().m, async ctx => {
const v = await new ClassicValidator().validate(ctx, {
id: 'index'
})
const { index } = v.get('path')
const flow = await Flow.findOne({
where: {
index
}
})
if (!flow) {
throw new global.errs.NotFound()
}
const { artId, type } = flow
const art = await Art.getData(artId, type)
const { uid } = ctx.auth
const likeStatus = await Favor.userLikeIt(artId, type, uid)
if (!art) {
throw new global.errs.NotFound()
}
ctx.body = {
favNums: art.favNums,
likeStatus
}
})