路由设计问题反思
来源:10-1 获取期刊点赞信息

qq_野火燎原_0
2020-03-15
请问在获取期刊点赞信息接口的设计时,课程里是这么设计的: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
}
})
写回答
2回答
-
或许是鱼跃
2022-04-08
你这么设计的接口只能用来查询喜欢的期刊,不能查询喜欢的书籍信息,局限比较大,后期书籍要使用还需要再来一个接口才能实现
00 -
7七月
2020-03-16
传递两个参数为什么得出结论是调用两次接口?
012020-03-16
相似问题