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

fluxo-de-dados #6

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
114 changes: 88 additions & 26 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import express, { Request, Response } from 'express'
import cors from 'cors'
import { accounts } from './database'
import { ACCOUNT_TYPE } from './types'
import { ACCOUNT_TYPE, TAccount } from './types'

const app = express()

Expand All @@ -13,50 +13,112 @@ app.listen(3003, () => {
})

app.get("/ping", (req: Request, res: Response) => {
res.send("Pong!")
res.send("Poong!")
})

app.get("/accounts", (req: Request, res: Response) => {
res.send(accounts)
})

app.get("/accounts/:id", (req: Request, res: Response) => {
const id = req.params.id
app.get("/accounts/:id", (req: Request, res: Response): void => {
try {
const id: string = req.params.id

const result = accounts.find((account) => account.id === id)
const result: TAccount = accounts.find((account) => account.id === id)

res.status(200).send(result)
if(!result) {
res.statusCode = 404
throw new Error("Conta não encontrada. Verifique a 'id'.")
}

res.status(200).send(result)
} catch(error){ {
if( error instanceof Error){
res.send(error.message)
}
}

}

})

app.delete("/accounts/:id", (req: Request, res: Response) => {
const id = req.params.id
app.delete("/accounts/:id", (req: Request, res: Response): void => {
try {
const id: string = req.params.id

if(id[0] !== 'a'){
res.statusCode = 400
throw new Error("'id' inválido. Deve iniciar com a letra 'a'.")
}



const accountIndex = accounts.findIndex((account) => account.id === id)
const accountIndex: number = accounts.findIndex((account) => account.id === id)

if (accountIndex >= 0) {
accounts.splice(accountIndex, 1)
}

res.status(200).send("Item deletado com sucesso")
})

app.put("/accounts/:id", (req: Request, res: Response) => {
const id = req.params.id

const newId = req.body.id as string | undefined
const newOwnerName = req.body.ownerName as string | undefined
const newBalance = req.body.balance as number | undefined
const newType = req.body.type as ACCOUNT_TYPE | undefined

} catch (error) {
if(error instanceof Error) {
res.send(error.message)
}
}

const account = accounts.find((account) => account.id === id)

if (account) {
account.id = newId || account.id
account.ownerName = newOwnerName || account.ownerName
account.type = newType || account.type

})

account.balance = isNaN(newBalance) ? account.balance : newBalance
app.put("/accounts/:id", (req: Request, res: Response): void => {
try {
const id: string = req.params.id

const newId = req.body.id as string | undefined
const newOwnerName = req.body.ownerName as string | undefined
const newBalance = req.body.balance as number | undefined
const newType = req.body.type as ACCOUNT_TYPE | undefined

if (!newId.startsWith("a")) {
res.statusCode = 404
throw new Error("'id' deve começar com a letra 'a'")

}

if(typeof newOwnerName !== 'string' || newOwnerName.length <2) {
res.statusCode = 404
throw new Error("'OwerName' deve ter no mínimo 2 caracteres e também não pode ser um número!")
}

if (typeof newBalance !== 'number' || newBalance <= 0) {
res.statusCode = 404
throw new Error("'Balance' deve ser do tipo 'number' e maior que 0")
}

if(newType !== ACCOUNT_TYPE.BLACK && ACCOUNT_TYPE.GOLD && ACCOUNT_TYPE.PLATINUM){
res.statusCode = 404
throw new Error("'Type' inválido");
}

const account: TAccount | undefined = accounts.find((account) => account.id === id)

if (account) {
account.id = newId || account.id
account.ownerName = newOwnerName || account.ownerName
account.type = newType || account.type

account.balance = isNaN(newBalance) ? account.balance : newBalance
}

res.status(200).send("Atualização realizada com sucesso")
} catch (error) {
console.log(error)
if(error instanceof Error){
res.send(error.message)
}

}

res.status(200).send("Atualização realizada com sucesso")
})
})