Skip to content

Commit 0bb23ea

Browse files
committed
feat(api-hub): support update online course pack
1 parent c29dce2 commit 0bb23ea

File tree

4 files changed

+396
-15
lines changed

4 files changed

+396
-15
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { RouteHandler } from "fastify";
2+
3+
import type {
4+
CreateCoursePack,
5+
DeleteCoursePack,
6+
UpdateCoursePackBody,
7+
UpdateCoursePackParams,
8+
} from "./schema";
9+
import { createCoursePack, deleteCoursePack, updateCoursePack } from "./service";
10+
11+
export const createCoursePackHandler: RouteHandler<{
12+
Body: CreateCoursePack;
13+
}> = async function (req, reply) {
14+
const result = await createCoursePack(req.body);
15+
reply.code(201).send({
16+
state: 1, // 1 代表的是成功发布
17+
data: {
18+
...result,
19+
},
20+
});
21+
};
22+
23+
export const deleteCoursePackHandler: RouteHandler<{
24+
Params: DeleteCoursePack;
25+
}> = async function (req, reply) {
26+
const coursePackId = req.params.id;
27+
const result = await deleteCoursePack(coursePackId);
28+
reply.code(200).send({
29+
state: result,
30+
});
31+
};
32+
33+
export const updateCoursePackHandler: RouteHandler<{
34+
Body: UpdateCoursePackBody;
35+
Params: UpdateCoursePackParams;
36+
}> = async function (req, reply) {
37+
const coursePackId = req.params.id;
38+
const result = await updateCoursePack(coursePackId, req.body);
39+
reply.code(200).send(result);
40+
};
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
import { asc } from "drizzle-orm";
2-
import { FastifyPluginAsync } from "fastify";
1+
import { FastifyInstance } from "fastify";
32

4-
import { coursePack as coursePackSchema } from "@earthworm/schema";
5-
import { db } from "~/db";
3+
import {
4+
createCoursePackHandler,
5+
deleteCoursePackHandler,
6+
updateCoursePackHandler,
7+
} from "./handler";
8+
import { coursePackSchema, deleteCoursePackSchema, updateCoursePackParamsSchema } from "./schema";
69

7-
const coursePack: FastifyPluginAsync = async (fastify) => {
8-
fastify.get("/", async function () {
9-
console.log(db);
10-
const coursePacks = await db.query.coursePack.findMany({
11-
orderBy: asc(coursePackSchema.order),
12-
});
13-
14-
return coursePacks;
15-
});
10+
export default async (fastify: FastifyInstance) => {
11+
fastify.post("/", { schema: { body: coursePackSchema } }, createCoursePackHandler);
12+
fastify.delete("/:id", { schema: { params: deleteCoursePackSchema } }, deleteCoursePackHandler);
13+
fastify.put(
14+
"/:id",
15+
{ schema: { body: coursePackSchema, params: updateCoursePackParamsSchema } },
16+
updateCoursePackHandler,
17+
);
1618
};
17-
18-
export default coursePack;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import { FromSchema } from "json-schema-to-ts";
2+
3+
const statementSchema = {
4+
type: "object",
5+
required: ["english", "phonetic", "chinese"],
6+
properties: {
7+
english: { type: "string" },
8+
phonetic: { type: "string" },
9+
chinese: { type: "string" },
10+
},
11+
} as const;
12+
13+
export const coursePackSchema = {
14+
type: "object",
15+
required: ["title", "description", "cover", "courses"],
16+
properties: {
17+
title: { type: "string" },
18+
description: { type: "string" },
19+
cover: { type: "string" },
20+
courses: {
21+
type: "array",
22+
items: {
23+
type: "object",
24+
required: ["title", "description", "statements"],
25+
properties: {
26+
title: { type: "string" },
27+
description: { type: "string" },
28+
statements: {
29+
type: "array",
30+
items: {
31+
...statementSchema,
32+
},
33+
},
34+
},
35+
},
36+
},
37+
},
38+
} as const;
39+
40+
export const updateCoursePackParamsSchema = {
41+
type: "object",
42+
properties: {
43+
id: { type: "string" },
44+
},
45+
required: ["id"],
46+
} as const;
47+
48+
export type CreateCoursePack = FromSchema<typeof coursePackSchema>;
49+
50+
export type UpdateCoursePackBody = FromSchema<typeof coursePackSchema>;
51+
52+
export type UpdateCoursePackParams = FromSchema<typeof updateCoursePackParamsSchema>;
53+
export type Statement = FromSchema<typeof statementSchema>;
54+
55+
export const deleteCoursePackSchema = {
56+
type: "object",
57+
properties: {
58+
id: { type: "string" },
59+
},
60+
required: ["id"],
61+
} as const;
62+
63+
export type DeleteCoursePack = FromSchema<typeof deleteCoursePackSchema>;

0 commit comments

Comments
 (0)