Skip to content
关注公众号,获取新课通知

收藏or取消收藏视频


控制器:app/controller/fava.js

js
	// 收藏/取消收藏视频
    async video() {
        const { ctx, app } = this;
        let currentUser = ctx.authUser;

        ctx.validate({
            video_id: {
                type: "int",
                required: true,
                desc: "视频ID"
            },
        });

        let {
            video_id,
        } = ctx.request.body;

        let fava = await app.model.Fava.findOne({
            where: {
                video_id,
                user_id: currentUser.id
            }
        });

        if (fava) {
            fava.destroy()
            return ctx.apiSuccess({
                status: false,
                msg: "取消收藏成功"
            });
        }

        let video = await app.model.Video.findOne({
            where: {
                id: video_id
            }
        });

        if (!video) {
            return ctx.apiFail('视频不存在');
        }

        await app.model.Fava.create({
            video_id,
            user_id: currentUser.id
        });

        ctx.apiSuccess({
            status: true,
            msg: "收藏成功"
        });
    }

路由:app/router.js

js
// 收藏/取消收藏视频
router.post("/fava/video", controller.fava.video);