general code improvements

This commit is contained in:
João Geonizeli
2022-07-09 11:12:29 -03:00
parent e0dd6c2307
commit b94dd8ee9b
11 changed files with 69 additions and 35 deletions

View File

@@ -4,21 +4,20 @@ import { ProjectService } from "../service/project.service";
const router = Router();
const BASE_PATH = "/projects";
export const apiNamespace = "/projects";
export const getAllPath = BASE_PATH;
router.get(getAllPath, async (req, res) => {
router.get(apiNamespace, async (req, res) => {
const projects = await ProjectService.listAllByUserId(req.userId)
const response: ProjectDto[] = projects.map<ProjectDto>(project => ({
id: project.id,
name: project.name
}))
res.json(response)
});
export const createPath = BASE_PATH;
router.post(createPath, (req, res) => {
router.post(apiNamespace, (req, res) => {
const { name } = req.body;
ProjectService.create({
@@ -26,6 +25,7 @@ router.post(createPath, (req, res) => {
userId: req.userId
}).then(project => {
const respose: ProjectDto = {
id: project.id,
name: project.name
}
@@ -37,4 +37,28 @@ router.post(createPath, (req, res) => {
})
});
router.delete(`${apiNamespace}/:id`, async (req, res, next) => {
const { id } = req.params;
const projectId = parseInt(id)
const userProjects = await ProjectService.listAllByUserId(req.userId)
const projecToBeDeleted = userProjects.find(project => project.id === projectId)
if (projecToBeDeleted) {
const success = await ProjectService.destroyProject(projecToBeDeleted)
if (success) {
res.json({ success })
} else {
res.status(422).json({
error: "Could not delete project"
})
}
} else {
res.status(404).json({
error: "Project not found"
})
}
});
export const ProjectRoutes = router;