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

exercicio finalizado #1

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
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 39 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import express, { Request, Response } from 'express'
import cors from 'cors'
import { accounts } from './database'
import { ACCOUNT_TYPE } from './types'

const app = express()

Expand All @@ -18,3 +19,41 @@ app.get("/ping", (req: Request, res: Response) => {
app.get("/accounts", (req: Request, res: Response) => {
res.send(accounts)
})

app.get("/accounts/:id", (req: Request, res: Response)=>{
const id = req.params.id
const result = accounts.find((account)=>account.id === id)

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

app.delete("/accounts/:id", (req: Request, res: Response)=>{
const id = req.params.id
const findIndexToRemove = accounts.findIndex((account)=>account.id === id)

if(findIndexToRemove >= 0){
accounts.splice(findIndexToRemove, 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;

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

if(accountToEdit){
accountToEdit.id = newId || accountToEdit.id
accountToEdit.ownerName = newOwnerName || accountToEdit.ownerName
accountToEdit.type = newType || accountToEdit.type
accountToEdit.balance = isNaN(newBalance) ? accountToEdit.balance : newBalance
}

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