Skip to content

Commit

Permalink
feat(api-hub): support update online course pack
Browse files Browse the repository at this point in the history
  • Loading branch information
cuixiaorui committed Jun 23, 2024
1 parent c29dce2 commit 0bb23ea
Show file tree
Hide file tree
Showing 4 changed files with 396 additions and 15 deletions.
40 changes: 40 additions & 0 deletions packages/api-hub/src/routes/course-pack/handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { RouteHandler } from "fastify";

import type {
CreateCoursePack,
DeleteCoursePack,
UpdateCoursePackBody,
UpdateCoursePackParams,
} from "./schema";
import { createCoursePack, deleteCoursePack, updateCoursePack } from "./service";

export const createCoursePackHandler: RouteHandler<{
Body: CreateCoursePack;
}> = async function (req, reply) {
const result = await createCoursePack(req.body);
reply.code(201).send({
state: 1, // 1 代表的是成功发布
data: {
...result,
},
});
};

export const deleteCoursePackHandler: RouteHandler<{
Params: DeleteCoursePack;
}> = async function (req, reply) {
const coursePackId = req.params.id;
const result = await deleteCoursePack(coursePackId);
reply.code(200).send({
state: result,
});
};

export const updateCoursePackHandler: RouteHandler<{
Body: UpdateCoursePackBody;
Params: UpdateCoursePackParams;
}> = async function (req, reply) {
const coursePackId = req.params.id;
const result = await updateCoursePack(coursePackId, req.body);
reply.code(200).send(result);
};
30 changes: 15 additions & 15 deletions packages/api-hub/src/routes/course-pack/index.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import { asc } from "drizzle-orm";
import { FastifyPluginAsync } from "fastify";
import { FastifyInstance } from "fastify";

import { coursePack as coursePackSchema } from "@earthworm/schema";
import { db } from "~/db";
import {
createCoursePackHandler,
deleteCoursePackHandler,
updateCoursePackHandler,
} from "./handler";
import { coursePackSchema, deleteCoursePackSchema, updateCoursePackParamsSchema } from "./schema";

const coursePack: FastifyPluginAsync = async (fastify) => {
fastify.get("/", async function () {
console.log(db);
const coursePacks = await db.query.coursePack.findMany({
orderBy: asc(coursePackSchema.order),
});

return coursePacks;
});
export default async (fastify: FastifyInstance) => {
fastify.post("/", { schema: { body: coursePackSchema } }, createCoursePackHandler);
fastify.delete("/:id", { schema: { params: deleteCoursePackSchema } }, deleteCoursePackHandler);
fastify.put(
"/:id",
{ schema: { body: coursePackSchema, params: updateCoursePackParamsSchema } },
updateCoursePackHandler,
);
};

export default coursePack;
63 changes: 63 additions & 0 deletions packages/api-hub/src/routes/course-pack/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { FromSchema } from "json-schema-to-ts";

const statementSchema = {
type: "object",
required: ["english", "phonetic", "chinese"],
properties: {
english: { type: "string" },
phonetic: { type: "string" },
chinese: { type: "string" },
},
} as const;

export const coursePackSchema = {
type: "object",
required: ["title", "description", "cover", "courses"],
properties: {
title: { type: "string" },
description: { type: "string" },
cover: { type: "string" },
courses: {
type: "array",
items: {
type: "object",
required: ["title", "description", "statements"],
properties: {
title: { type: "string" },
description: { type: "string" },
statements: {
type: "array",
items: {
...statementSchema,
},
},
},
},
},
},
} as const;

export const updateCoursePackParamsSchema = {
type: "object",
properties: {
id: { type: "string" },
},
required: ["id"],
} as const;

export type CreateCoursePack = FromSchema<typeof coursePackSchema>;

export type UpdateCoursePackBody = FromSchema<typeof coursePackSchema>;

export type UpdateCoursePackParams = FromSchema<typeof updateCoursePackParamsSchema>;
export type Statement = FromSchema<typeof statementSchema>;

export const deleteCoursePackSchema = {
type: "object",
properties: {
id: { type: "string" },
},
required: ["id"],
} as const;

export type DeleteCoursePack = FromSchema<typeof deleteCoursePackSchema>;
Loading

0 comments on commit 0bb23ea

Please sign in to comment.