Created
July 1, 2020 10:25
-
-
Save jbergant/b0ce028a7a222810ac41c801a4ebe36c to your computer and use it in GitHub Desktop.
Deno - simple API
This file contains hidden or 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
| import { Course } from "../models/course.ts"; | |
| import { courses } from "../data/data.ts"; | |
| // deno-lint-ignore no-explicit-any | |
| export const getCourses = ({ response }: { response: any }) => { | |
| response.body = courses; | |
| }; | |
| export const getCourse = ({ | |
| params, | |
| response, | |
| }: { | |
| params: { | |
| title: string; | |
| }; | |
| // deno-lint-ignore no-explicit-any | |
| response: any; | |
| }) => { | |
| const course = courses.filter((course) => course.title === params.title); | |
| if (course.length) { | |
| response.status = 200; | |
| response.body = course[0]; | |
| return; | |
| } | |
| response.status = 400; | |
| response.body = { msg: `Cannot find course ${params.title}` }; | |
| }; | |
| export const addCourse = async ({ | |
| request, | |
| response, | |
| }: { | |
| // deno-lint-ignore no-explicit-any | |
| request: any; | |
| // deno-lint-ignore no-explicit-any | |
| response: any; | |
| }) => { | |
| const body = await request.body(); | |
| const course: Course = body.value; | |
| courses.push(course); | |
| response.body = { msg: "OK" }; | |
| response.status = 200; | |
| }; | |
| export const updateCourse = async ({ | |
| params, | |
| request, | |
| response, | |
| }: { | |
| params: { | |
| title: string; | |
| }; | |
| // deno-lint-ignore no-explicit-any | |
| request: any; | |
| // deno-lint-ignore no-explicit-any | |
| response: any; | |
| }) => { | |
| const temp = courses.filter((existingDCourse) => | |
| existingDCourse.title === params.title | |
| ); | |
| const body = await request.body(); | |
| const { rating }: { rating: number } = body.value; | |
| if (temp.length) { | |
| temp[0].rating = rating; | |
| response.status = 200; | |
| response.body = { msg: "OK" }; | |
| return; | |
| } | |
| response.status = 400; | |
| response.body = { msg: `Cannot find course ${params.title}` }; | |
| }; | |
| export const deleteCourse = ({ | |
| params, | |
| response, | |
| }: { | |
| params: { | |
| title: string; | |
| }; | |
| // deno-lint-ignore no-explicit-any | |
| response: any; | |
| }) => { | |
| const lengthBefore = courses.length; | |
| const temp = courses.filter((course) => course.title !== params.title); | |
| if (temp.length === lengthBefore) { | |
| response.status = 400; | |
| response.body = { msg: `Cannot find course ${params.title}` }; | |
| return; | |
| } | |
| response.body = { msg: "OK" }; | |
| response.status = 200; | |
| }; |
This file contains hidden or 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
| import { Course } from "../models/course.ts"; | |
| export let courses: Array<Course> = [ | |
| { | |
| title: "Chatbots", | |
| author: "Jana Bergant", | |
| rating: 9, | |
| }, | |
| { | |
| title: "Google Assistant app", | |
| author: "Jana Bergant", | |
| rating: 8, | |
| }, | |
| { | |
| title: "Blog with Jekyll", | |
| author: "Jana Bergant", | |
| rating: 8, | |
| }, | |
| ]; |
This file contains hidden or 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
| import { Application } from "https://deno.land/x/oak/mod.ts"; | |
| import router from "./routes.ts"; | |
| const env = Deno.env.toObject(); | |
| const PORT = Number(env.PORT) || 8000; | |
| const HOST = env.HOST || "localhost"; | |
| const app = new Application(); | |
| app.use(router.routes()); | |
| app.use(router.allowedMethods()); | |
| console.log(`Listening on ${HOST}:${PORT}...`); | |
| await app.listen(`${HOST}:${PORT}`); |
This file contains hidden or 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
| export interface Course { | |
| title: string; | |
| author: string; | |
| rating: number; | |
| } |
This file contains hidden or 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
| import { Router } from "https://deno.land/x/oak/mod.ts"; | |
| import { | |
| getCourses, | |
| getCourse, | |
| addCourse, | |
| updateCourse, | |
| deleteCourse, | |
| } from "./controllers/courses.ts"; | |
| const router = new Router(); | |
| router | |
| .get("/courses", getCourses) | |
| .get("/courses/:title", getCourse) | |
| .post("/courses", addCourse) | |
| .put("/courses/:title", updateCourse) | |
| .delete("/courses/:title", deleteCourse); | |
| export default router; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment