Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exercícios aula de criação API #3

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions src/database.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { COURSE_STACK, TCourse } from "./types"
import { COURSE_STACK, TCourse, TEstudent } from "./types"

export const courses: TCourse[] = [
{
Expand All @@ -19,4 +19,16 @@ export const courses: TCourse[] = [
lessons: 5,
stack: COURSE_STACK.BACK
}
]
]

export const estudents: TEstudent[] = [
{
id: "est001",
name: "Luiz Paulo",
age: 36
},
{
id: "est002",
name: "Thalita",
age: 29
}]
81 changes: 81 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import express, { Request, Response } from 'express'
import cors from 'cors'

import{courses, estudents} from './database'
import { COURSE_STACK, TCourse, TEstudent } from './types'

const app = express()

app.use(express.json())
Expand All @@ -13,3 +16,81 @@ app.listen(3003, () => {
app.get('/ping', (req: Request, res: Response) => {
res.send('Pong!')
})

app.get('/courses', (req: Request, res: Response)=>{
res.status(200).send(courses)
})

app.get('/courses/search', (req:Request,res:Response)=>{
const q = req.query.q as string

const result = courses.filter((course)=>{
return course.name.toLowerCase().includes(q.toLowerCase())
})

res.status(200).send(result)
})

// Criação de curso - Terceiro endpoint
app.post('/courses', (req:Request, res: Response)=>{

// const iq = 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 {id,name,lessons,stack} = req.body as TCourse



const newCourse = {
id: id,
name: name,
lessons: lessons,
stack: stack
}

courses.push(newCourse)

res.status(201).send('Curso registrado com sucesso')

})

// Estudantes

app.get('/estudents', (req: Request, res: Response)=>{
res.status(200).send(estudents)
})

app.get('/estudents/search', (req:Request,res:Response)=>{
const q = req.query.q as string

const result = estudents.filter((estudent)=>{
return estudent.name.toLowerCase().includes(q.toLowerCase())
})

res.status(200).send(result)
})

app.post('/estudents', (req:Request, res: Response)=>{

// const iq = 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 {id,name,age} = req.body as TEstudent



const newEstudent = {
id: id,
name: name,
age: age
}

estudents.push(newEstudent)

res.status(201).send('Estudante registrado com sucesso')

})
9 changes: 9 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,18 @@ export enum COURSE_STACK {
BACK = "Back-end"
}



export type TCourse = {
id: string,
name: string,
lessons: number,
stack: COURSE_STACK
}

export type TEstudent = {
id: string,
name: string,
age: number,

}