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

创建作品


控制器:app/controller/video.js

js
const rules = {
    title: {
        type: 'string',
        required: true,
        desc: '视频标题'
    },
    cover: {
        type: 'string',
        required: true,
        desc: '封面'
    },
    category_id: {
        type: 'int',
        required: true,
        desc: '分类ID'
    },
    desc: {
        type: 'string',
        required: true,
        desc: '描述'
    },
};
	// 创建作品
	async save() {
        let { ctx, app } = this;
        let currentUser = ctx.authUser;

        ctx.validate(rules);

        let {
            title,
            cover,
            category_id,
            desc,
        } = ctx.request.body;

        let video = await app.model.Video.create({
            title,
            cover,
            category_id,
            desc,
            user_id: currentUser.id
        });

        ctx.apiSuccess(video);
    }

路由:app/router.js

js
// 添加视频
router.post("/video", controller.video.save);