diff --git a/src/database.ts b/src/database.ts index c51b037..d90f999 100644 --- a/src/database.ts +++ b/src/database.ts @@ -1,4 +1,4 @@ -import { COURSE_STACK, TCourse } from "./types" +import { COURSE_STACK, TCourse, TStudent } from "./types" export const courses: TCourse[] = [ { @@ -19,4 +19,25 @@ export const courses: TCourse[] = [ lessons: 5, stack: COURSE_STACK.BACK } -] \ No newline at end of file +] + +export const students: TStudent[] = [ + { + id: "c001", + name: "Fernanda", + age: 48 + }, + { + id: "c002", + name: "Erik", + age: 48 + }, + { + id: "c003", + name: "Maia", + age: 14 + } +] + + + diff --git a/src/index.ts b/src/index.ts index 3e9ff30..586895a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,7 @@ import express, { Request, Response } from 'express' import cors from 'cors' +import { courses, students } from './database' +import { COURSE_STACK, TStudent } from './types' const app = express() @@ -13,3 +15,71 @@ app.listen(3003, () => { app.get('/ping', (req: Request, res: Response) => { res.send('Pong!') }) + +// getAllCourses +app.get("/courses", (req: Request, res: Response) => { + res.send(courses) +}) + +// findCourseByName +app.get("/courses/search", (req: Request, res: Response) => { + const nameToFind = req.query.name as string + + const result = courses.filter((course) => { + return course.name.toLowerCase().includes(nameToFind.toLowerCase()) + }) + + res.send(result) +}) + +// craeteNewCourse +app.post("/courses", (req: Request, res: Response) => { + const id = req.body.id as string + const name = req.body.name as string + const lessons = req.body.lessons as number + const stack = req.body.stack as COURSE_STACK + + const newCourse = { + id, + name, + lessons, + stack + } + courses.push(newCourse) + + res.status(201).send("Curso registrado com sucesso") +}) + +// -------------------------------------------- + +// getAllStudents +app.get("/students", (req: Request, res: Response) => { + res.send(students) +}) + +// findStudentByName +app.get("/students/search", (req: Request, res: Response) => { + const nameToFind = req.query.name as string + + const result = students.filter((student) => { + return student.name.toLowerCase().includes(nameToFind.toLowerCase()) + }) + + res.send(result) +}) + +// craeteNewStudent +app.post("/students", (req: Request, res: Response) => { + const id = req.body.id as string + const name = req.body.name as string + const age = req.body.age as number + + const newStudent: TStudent = { + id, + name, + age + } + students.push(newStudent) + + res.status(201).send("Estudante cadastrado com sucesso") +}) \ No newline at end of file diff --git a/src/types.ts b/src/types.ts index 1957a4b..e3abadd 100644 --- a/src/types.ts +++ b/src/types.ts @@ -9,3 +9,9 @@ export type TCourse = { lessons: number, stack: COURSE_STACK } + +export type TStudent = { + id: string, + name: string, + age: number +}