generated from UnBArqDsw2021-1/RepositorioTemplate
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(#8) Adiciona tipagens no orderCntroller e nova forma de iterar nod f…
…ields
- Loading branch information
1 parent
e99dd5e
commit 4c14d2e
Showing
1 changed file
with
71 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,100 +1,125 @@ | ||
import { Request, Response } from "express"; | ||
import database from "../db"; | ||
|
||
const create = async (request, response) => { | ||
const create = async ( | ||
request: Request, | ||
response: Response | ||
): Promise<Response> => { | ||
try { | ||
const fieldsToValidate = ['status', 'idTable', 'idClient', 'nameClient']; | ||
for (let field of fieldsToValidate) { | ||
const fieldsToValidate = ["status", "idTable", "idClient", "nameClient"]; | ||
|
||
fieldsToValidate.forEach((field: string): Response => { | ||
if (!request.body.order[field]) { | ||
return response.status(400).json({ | ||
success: false, | ||
message: `O campo ${field} é obrigatório` | ||
message: `O campo ${field} é obrigatório.`, | ||
}); | ||
} | ||
} | ||
return null; | ||
}); | ||
|
||
return response.json({ | ||
success: true, | ||
order: await database.order.create(request.body.order) | ||
order: await database.order.create(request.body.order), | ||
}); | ||
} catch (error) { | ||
console.log('ERROR ---> ', error); | ||
console.log("ERROR ---> ", error); | ||
return response.status(500).json({ | ||
success: false, | ||
message: 'Ocorreu um erro ao realizar a operação, tente novamente mais tarde.', | ||
error: error.toString() | ||
message: | ||
"Ocorreu um erro ao realizar a operação, tente novamente mais tarde.", | ||
error: error.toString(), | ||
}); | ||
} | ||
} | ||
}; | ||
|
||
const getAll = async (request, response) => { | ||
const getAll = async ( | ||
request: Request, | ||
response: Response | ||
): Promise<Response> => { | ||
try { | ||
let filters = {} | ||
if (request.query.category) | ||
filters.category = request.query.category | ||
const filters = {}; | ||
if (request.query.category) filters.category = request.query.category; | ||
|
||
return response.json({ | ||
success: true, | ||
orders: await database.order.findAll({ | ||
where: { | ||
...filters | ||
} | ||
}) | ||
...filters, | ||
}, | ||
}), | ||
}); | ||
} catch (error) { | ||
console.log('ERROR ---> ', error); | ||
console.log("ERROR ---> ", error); | ||
return response.status(500).json({ | ||
success: false, | ||
message: 'Ocorreu um erro ao realizar a operação, tente novamente mais tarde.', | ||
error: error.toString() | ||
}) | ||
message: | ||
"Ocorreu um erro ao realizar a operação, tente novamente mais tarde.", | ||
error: error.toString(), | ||
}); | ||
} | ||
} | ||
}; | ||
|
||
const getOne = async (request, response) => { | ||
const getOne = async ( | ||
request: Request, | ||
response: Response | ||
): Promise<Response> => { | ||
try { | ||
return response.json({ | ||
success: true, | ||
order: await database.order.findByPk(request.params.id) | ||
order: await database.order.findByPk(request.params.id), | ||
}); | ||
} catch (error) { | ||
console.log('ERROR ---> ', error); | ||
console.log("ERROR ---> ", error); | ||
return response.status(500).json({ | ||
success: false, | ||
message: 'Ocorreu um erro ao realizar a operação, tente novamente mais tarde.', | ||
error: error.toString() | ||
}) | ||
message: | ||
"Ocorreu um erro ao realizar a operação, tente novamente mais tarde.", | ||
error: error.toString(), | ||
}); | ||
} | ||
} | ||
}; | ||
|
||
const edit = async (request, response) => { | ||
const edit = async ( | ||
request: Request, | ||
response: Response | ||
): Promise<Response> => { | ||
try { | ||
await database.order.update(request.body.order, { where: { idOrder: request.params.id } }) | ||
await database.order.update(request.body.order, { | ||
where: { idOrder: request.params.id }, | ||
}); | ||
return response.json({ | ||
success: true | ||
success: true, | ||
}); | ||
} catch (error) { | ||
console.log('ERROR ---> ', error); | ||
console.log("ERROR ---> ", error); | ||
return response.status(500).json({ | ||
success: false, | ||
message: 'Ocorreu um erro ao realizar a operação, tente novamente mais tarde.', | ||
error: error.toString() | ||
}) | ||
message: | ||
"Ocorreu um erro ao realizar a operação, tente novamente mais tarde.", | ||
error: error.toString(), | ||
}); | ||
} | ||
} | ||
}; | ||
|
||
const destroy = async (request, response) => { | ||
const destroy = async ( | ||
request: Request, | ||
response: Response | ||
): Promise<Response> => { | ||
try { | ||
await database.order.destroy({ where: { idOrder: request.params.id } }) | ||
await database.order.destroy({ where: { idOrder: request.params.id } }); | ||
return response.json({ | ||
success: true | ||
success: true, | ||
}); | ||
} catch (error) { | ||
console.log('ERROR ---> ', error); | ||
console.log("ERROR ---> ", error); | ||
return response.status(500).json({ | ||
success: false, | ||
message: 'Ocorreu um erro ao realizar a operação, tente novamente mais tarde.', | ||
error: error.toString() | ||
}) | ||
message: | ||
"Ocorreu um erro ao realizar a operação, tente novamente mais tarde.", | ||
error: error.toString(), | ||
}); | ||
} | ||
} | ||
}; | ||
|
||
export default { create, getAll, getOne, edit, destroy } | ||
export default { create, getAll, getOne, edit, destroy }; |