web-backset.cn/apps/server/src/controller/course.controller.ts
2023-03-24 10:51:37 +08:00

171 lines
5.1 KiB
TypeScript

import { Body, Controller, Inject, Post } from '@midwayjs/core';
import { Context } from '@midwayjs/koa';
import { BizCode } from '../biz/code';
import { WEB } from '../config/base.config';
import { CourseCreateDTO } from '../dto/course.dto';
import { Chapter } from '../entity/chapter.entity';
import { Course } from '../entity/course.entity';
import { Guide } from '../entity/guide.entity';
import { ChapterService } from '../service/chapter.service';
import { CourseService } from '../service/course.service';
import { GuideService } from '../service/guide.service';
import { UserService } from '../service/user.service';
import { decodeToken } from '../util/encrypt';
@Controller('/course')
export class CourseController {
@Inject()
ctx: Context;
@Inject()
courseService: CourseService;
@Inject()
chapterService: ChapterService;
@Inject()
guideService: GuideService;
@Inject()
userService: UserService;
@Post('/create')
async create(@Body() param: CourseCreateDTO) {
try {
const { course_chapterList, course_guide, ...rest } = param;
const courseId = await this.courseService.create({ ...rest });
await this.chapterService.create(
course_chapterList.map((i: any) => ({
...i,
chapter_course_id: courseId,
}))
);
await this.guideService.create({
...course_guide,
guide_course_id: courseId,
});
return { code: BizCode.OK };
} catch (error) {
this.ctx.logger.error(error);
return {
code: BizCode.ERROR,
msg: `[error] course/create error happened`,
};
}
}
@Post('/select/all')
async selectAll(@Body() params) {
const { all = false } = params;
const courseList = await this.courseService.selectAll(all);
return { code: BizCode.OK, data: courseList };
}
@Post('/detail/select')
async selectDetailByCourseId(@Body() params) {
const { course_id } = params;
try {
const token = this.ctx.cookies.get(WEB.SIGN);
const { user_login } = decodeToken(token);
const user = await this.userService.select({ user_login });
// 用户订阅鉴权
if (!user.user_sub)
return { code: BizCode.AUTH, msg: '无权访问订阅课程' };
if (+user.user_sub_expired < Date.now())
return { code: BizCode.AUTH, msg: '订阅已过期' };
const course = await this.courseService.select({ course_id });
const chapterList = await this.chapterService.select(course_id);
const guide = await this.guideService.select(course_id);
return { code: BizCode.OK, data: { chapterList, guide, course } };
} catch (error) {
this.ctx.logger.error(error);
return { code: BizCode.ERROR, msg: '[error] /chapter/select error' };
}
}
@Post('/update')
async updateCourse(@Body() course: Course) {
try {
await this.courseService.update(course);
return { code: BizCode.OK };
} catch (error) {
this.ctx.logger.error(error);
return { code: BizCode.ERROR, msg: error };
}
}
@Post('/chapter/select')
async selectChapterList(@Body() chapter: Chapter) {
const { chapter_course_id } = chapter;
const chapterList = await this.chapterService.select(chapter_course_id);
return { code: BizCode.OK, data: chapterList };
}
@Post('/chapter/update')
async updateChapter(@Body() chapter: Chapter) {
try {
await this.chapterService.update(chapter);
return { code: BizCode.OK };
} catch (error) {
this.ctx.logger.error(error);
return { code: BizCode.ERROR, msg: error };
}
}
@Post('/remove')
async remove(@Body() course: Course) {
const { course_id } = course;
try {
await this.courseService.remove(course_id);
await this.chapterService.removeByCourseId(course_id);
await this.guideService.removeByCourseId(course_id);
return { code: BizCode.OK };
} catch (error) {
this.ctx.logger.error(error);
return { code: BizCode.ERROR, msg: error };
}
}
@Post('/chapter/create')
async createChapter(@Body() chapterList: Chapter[]) {
try {
// order默认为-1
await this.chapterService.create(chapterList);
return { code: BizCode.OK };
} catch (error) {
this.ctx.logger.error(error);
return { code: BizCode.ERROR, msg: error };
}
}
@Post('/chapter/remove')
async removeChapter(@Body() chapter: Chapter) {
try {
const { chapter_id } = chapter;
await this.chapterService.remove(chapter_id);
return { code: BizCode.OK };
} catch (error) {
this.ctx.logger.error(error);
return { code: BizCode.ERROR, msg: error };
}
}
@Post('/guide/select')
async selectGuide(@Body() guide: Guide) {
const { guide_course_id: course_id } = guide;
const data = await this.guideService.select(course_id);
return { code: BizCode.OK, data };
}
@Post('/guide/update')
async updateGuide(@Body() guide: Guide) {
try {
await this.guideService.update(guide);
return { code: BizCode.OK };
} catch (error) {
this.ctx.logger.error(error);
return { code: BizCode.ERROR, msg: error };
}
}
}