-
Notifications
You must be signed in to change notification settings - Fork 757
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(api-hub): support update online course pack
- Loading branch information
1 parent
c29dce2
commit 0bb23ea
Showing
4 changed files
with
396 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>; |
Oops, something went wrong.