lincms koa2实现添加收藏和取消收藏

学习笔记 yekong 83℃

api

collectionApi.linPost(
    'collection',
    '/:id',
    user.permission('收藏'),
    loginRequired,
    async ctx => {
        const v = await new PositiveIdValidator().validate(ctx);
        const pid = v.get('path.id'); // 文章id
        const uid = ctx.currentUser.dataValues.id; // 获取用户id
        const Collection = await CollectionDto.createCollection({pid: pid, uid: uid});
        if(Collection){
            ctx.success({
                code: 42
            });
        }else{
            ctx.success({
                code: 43
            });
        }
    }
);

dao

async createCollection(data) {
        const collection = await Collection.findOne({
            where: {
                pid: data.pid,
                uid: data.uid
            }
        });
        if (collection) {
            await collection.destroy();
            return false
        } else {
            const bk = new Collection();
            bk.pid = data.pid;
            bk.uid = data.uid;
            await bk.save();
            return true
        }
    }
喜欢 (0)