From b1f89f007ab665cde40f6c2592662a827b3dc98f Mon Sep 17 00:00:00 2001 From: ELIAS RICARDO MESQUITA Date: Tue, 10 Jan 2023 13:35:02 -0400 Subject: [PATCH] addet --- package-lock.json | 4 ++-- src/index.ts | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/package-lock.json b/package-lock.json index a77165b..b485b6c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,11 +1,11 @@ { - "name": "apis-e-express-template", + "name": "aprofundamento-express-template", "version": "1.0.0", "lockfileVersion": 2, "requires": true, "packages": { "": { - "name": "apis-e-express-template", + "name": "aprofundamento-express-template", "version": "1.0.0", "license": "ISC", "dependencies": { diff --git a/src/index.ts b/src/index.ts index e85866e..3ecae4e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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() @@ -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!') +}) \ No newline at end of file