2026-09-11 21:57:38 +08:00
|
|
|
import { Router } from 'express';
|
|
|
|
|
import { sendCreated, sendOk } from '../../lib/http.js';
|
|
|
|
|
import { idParamSchema } from '../../lib/schemas.js';
|
2026-10-11 11:49:32 +08:00
|
|
|
import { authenticate, getAuthUser, requirePermission } from '../../middleware/auth.js';
|
2026-09-11 21:57:38 +08:00
|
|
|
import { validate, validatedParams, validatedQuery } from '../../middleware/validate.js';
|
|
|
|
|
import { defineModule } from '../module.types.js';
|
|
|
|
|
import {
|
|
|
|
|
createCourseSchema,
|
|
|
|
|
listCoursesQuerySchema,
|
|
|
|
|
updateCourseSchema,
|
|
|
|
|
type CreateCourseInput,
|
|
|
|
|
type UpdateCourseInput,
|
|
|
|
|
} from './courses.schemas.js';
|
|
|
|
|
import * as courseService from './courses.service.js';
|
|
|
|
|
|
|
|
|
|
const router = Router();
|
|
|
|
|
router.use(authenticate());
|
|
|
|
|
|
|
|
|
|
/** GET /api/courses?classId= - 课程列表 */
|
|
|
|
|
router.get('/', validate({ query: listCoursesQuerySchema }), async (req, res) => {
|
|
|
|
|
const user = getAuthUser(req);
|
|
|
|
|
const { classId } = validatedQuery<{ classId?: string }>(req);
|
|
|
|
|
sendOk(res, await courseService.listCourses(user, classId), '获取课程列表成功');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
/** POST /api/courses - 新增课程 */
|
|
|
|
|
router.post(
|
|
|
|
|
'/',
|
2026-10-11 11:49:32 +08:00
|
|
|
requirePermission('schedule:write'),
|
2026-09-11 21:57:38 +08:00
|
|
|
validate({ body: createCourseSchema }),
|
|
|
|
|
async (req, res) => {
|
|
|
|
|
const user = getAuthUser(req);
|
|
|
|
|
sendCreated(res, await courseService.createCourse(user, req.body as CreateCourseInput), '课程创建成功');
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
/** PATCH /api/courses/:id - 修改课程 */
|
|
|
|
|
router.patch(
|
|
|
|
|
'/:id',
|
2026-10-11 11:49:32 +08:00
|
|
|
requirePermission('schedule:write'),
|
2026-09-11 21:57:38 +08:00
|
|
|
validate({ params: idParamSchema, body: updateCourseSchema }),
|
|
|
|
|
async (req, res) => {
|
|
|
|
|
const user = getAuthUser(req);
|
|
|
|
|
const { id } = validatedParams<{ id: string }>(req);
|
|
|
|
|
sendOk(res, await courseService.updateCourse(user, id, req.body as UpdateCourseInput), '课程更新成功');
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
/** DELETE /api/courses/:id - 删除课程 */
|
|
|
|
|
router.delete(
|
|
|
|
|
'/:id',
|
2026-10-11 11:49:32 +08:00
|
|
|
requirePermission('schedule:write'),
|
2026-09-11 21:57:38 +08:00
|
|
|
validate({ params: idParamSchema }),
|
|
|
|
|
async (req, res) => {
|
|
|
|
|
const user = getAuthUser(req);
|
|
|
|
|
const { id } = validatedParams<{ id: string }>(req);
|
|
|
|
|
await courseService.deleteCourse(user, id);
|
|
|
|
|
sendOk(res, { id }, '课程已删除');
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
export const coursesModule = defineModule({
|
|
|
|
|
name: 'courses',
|
|
|
|
|
basePath: '/courses',
|
|
|
|
|
router,
|
|
|
|
|
});
|