add project update route
This commit is contained in:
@@ -7,14 +7,14 @@ const router = Router();
|
|||||||
export const apiNamespace = "/projects";
|
export const apiNamespace = "/projects";
|
||||||
|
|
||||||
router.get(apiNamespace, async (req, res) => {
|
router.get(apiNamespace, async (req, res) => {
|
||||||
const projects = await ProjectService.listAllByUserId(req.userId)
|
const projects = await ProjectService.listAllByUserId(req.userId);
|
||||||
|
|
||||||
const response: ProjectDto[] = projects.map<ProjectDto>(project => ({
|
const response: ProjectDto[] = projects.map<ProjectDto>((project) => ({
|
||||||
id: project.id,
|
id: project.id,
|
||||||
name: project.name
|
name: project.name,
|
||||||
}))
|
}));
|
||||||
|
|
||||||
res.json(response)
|
res.json(response);
|
||||||
});
|
});
|
||||||
|
|
||||||
router.post(apiNamespace, (req, res) => {
|
router.post(apiNamespace, (req, res) => {
|
||||||
@@ -22,42 +22,73 @@ router.post(apiNamespace, (req, res) => {
|
|||||||
|
|
||||||
ProjectService.create({
|
ProjectService.create({
|
||||||
name,
|
name,
|
||||||
userId: req.userId
|
userId: req.userId,
|
||||||
}).then(project => {
|
|
||||||
const respose: ProjectDto = {
|
|
||||||
id: project.id,
|
|
||||||
name: project.name
|
|
||||||
}
|
|
||||||
|
|
||||||
res.json(respose);
|
|
||||||
}).catch(err => {
|
|
||||||
res.status(422).json({
|
|
||||||
error: err.message
|
|
||||||
});
|
|
||||||
})
|
})
|
||||||
|
.then((project) => {
|
||||||
|
const respose: ProjectDto = {
|
||||||
|
id: project.id,
|
||||||
|
name: project.name,
|
||||||
|
};
|
||||||
|
|
||||||
|
res.json(respose);
|
||||||
|
})
|
||||||
|
.catch((err) => {
|
||||||
|
res.status(422).json({
|
||||||
|
error: err.message,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
router.put(`${apiNamespace}/:id`, async (req, res) => {
|
||||||
|
const { id } = req.params;
|
||||||
|
const { name } = req.body;
|
||||||
|
const projectId = parseInt(id);
|
||||||
|
|
||||||
|
const userProjects = await ProjectService.listAllByUserId(req.userId);
|
||||||
|
const projectToBeUpdated = userProjects.find(
|
||||||
|
(project) => project.id === projectId
|
||||||
|
);
|
||||||
|
|
||||||
|
if (projectToBeUpdated) {
|
||||||
|
ProjectService.update(projectToBeUpdated, {
|
||||||
|
name,
|
||||||
|
}).then((success) => {
|
||||||
|
if (success) {
|
||||||
|
res.status(204).json();
|
||||||
|
} else {
|
||||||
|
res.status(402).json();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
res.status(404).json({
|
||||||
|
error: "Project not found",
|
||||||
|
});
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
router.delete(`${apiNamespace}/:id`, async (req, res, next) => {
|
router.delete(`${apiNamespace}/:id`, async (req, res, next) => {
|
||||||
const { id } = req.params;
|
const { id } = req.params;
|
||||||
const projectId = parseInt(id)
|
const projectId = parseInt(id);
|
||||||
|
|
||||||
const userProjects = await ProjectService.listAllByUserId(req.userId)
|
const userProjects = await ProjectService.listAllByUserId(req.userId);
|
||||||
const projecToBeDeleted = userProjects.find(project => project.id === projectId)
|
const projecToBeDeleted = userProjects.find(
|
||||||
|
(project) => project.id === projectId
|
||||||
|
);
|
||||||
|
|
||||||
if (projecToBeDeleted) {
|
if (projecToBeDeleted) {
|
||||||
const success = await ProjectService.destroy(projecToBeDeleted)
|
const success = await ProjectService.destroy(projecToBeDeleted);
|
||||||
|
|
||||||
if (success) {
|
if (success) {
|
||||||
res.json({ success })
|
res.json({ success });
|
||||||
} else {
|
} else {
|
||||||
res.status(422).json({
|
res.status(422).json({
|
||||||
error: "Could not delete project"
|
error: "Could not delete project",
|
||||||
})
|
});
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
res.status(404).json({
|
res.status(404).json({
|
||||||
error: "Project not found"
|
error: "Project not found",
|
||||||
})
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
export type UpdateProjectDto = {
|
export type UpdateProjectDto = {
|
||||||
id: string
|
|
||||||
name: string
|
name: string
|
||||||
}
|
}
|
||||||
@@ -1,6 +1,11 @@
|
|||||||
import { IsNotEmpty } from "class-validator";
|
import { IsNotEmpty } from "class-validator";
|
||||||
import {
|
import {
|
||||||
Column, Entity, JoinColumn, ManyToOne, OneToMany, PrimaryGeneratedColumn
|
Column,
|
||||||
|
Entity,
|
||||||
|
JoinColumn,
|
||||||
|
ManyToOne,
|
||||||
|
OneToMany,
|
||||||
|
PrimaryGeneratedColumn,
|
||||||
} from "typeorm";
|
} from "typeorm";
|
||||||
import { Task } from "./task.entity";
|
import { Task } from "./task.entity";
|
||||||
import { User } from "./user.entity";
|
import { User } from "./user.entity";
|
||||||
@@ -14,7 +19,10 @@ export class Project {
|
|||||||
@IsNotEmpty()
|
@IsNotEmpty()
|
||||||
name: string;
|
name: string;
|
||||||
|
|
||||||
@ManyToOne((_type) => User, (user) => user.projects)
|
@ManyToOne((_type) => User, (user) => user.projects, {
|
||||||
|
onUpdate: "CASCADE",
|
||||||
|
onDelete: "CASCADE",
|
||||||
|
})
|
||||||
@JoinColumn()
|
@JoinColumn()
|
||||||
@IsNotEmpty()
|
@IsNotEmpty()
|
||||||
user: User;
|
user: User;
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import { validate } from "class-validator";
|
import { validate } from "class-validator";
|
||||||
import { NewProjectDto } from "../dto/newProject.dto";
|
import { NewProjectDto } from "../dto/newProject.dto";
|
||||||
|
import { UpdateProjectDto } from "../dto/updateProject.dto";
|
||||||
import { Project } from "../entity/project.entity";
|
import { Project } from "../entity/project.entity";
|
||||||
import { User } from "../entity/user.entity";
|
import { User } from "../entity/user.entity";
|
||||||
import { projectRepository } from "../repository/project.repository";
|
import { projectRepository } from "../repository/project.repository";
|
||||||
@@ -10,7 +11,7 @@ async function create(newProject: NewProjectDto): Promise<Project> {
|
|||||||
const user = await UserService.findUserById(newProject.userId);
|
const user = await UserService.findUserById(newProject.userId);
|
||||||
|
|
||||||
project.name = newProject.name;
|
project.name = newProject.name;
|
||||||
project.user = user
|
project.user = user;
|
||||||
|
|
||||||
const errors = await validate(project);
|
const errors = await validate(project);
|
||||||
|
|
||||||
@@ -28,17 +29,28 @@ async function listAllByUserId(userId: User["id"]): Promise<Project[]> {
|
|||||||
return query.getMany();
|
return query.getMany();
|
||||||
}
|
}
|
||||||
|
|
||||||
async function destroy(project: Project): Promise<boolean>{
|
async function destroy(project: Project): Promise<boolean> {
|
||||||
const query = projectRepository.createQueryBuilder();
|
const query = projectRepository.createQueryBuilder();
|
||||||
query.where('"id" = :projectId', { projectId: project.id });
|
query.where('"id" = :projectId', { projectId: project.id });
|
||||||
|
|
||||||
const result = await query.delete().execute()
|
const result = await query.delete().execute();
|
||||||
|
|
||||||
return result.affected > 0;
|
return result.affected > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function update(
|
||||||
|
project: Project,
|
||||||
|
updateProjectDto: UpdateProjectDto
|
||||||
|
): Promise<boolean> {
|
||||||
|
projectRepository.merge(project, updateProjectDto);
|
||||||
|
const updateResult = await projectRepository.save(project);
|
||||||
|
|
||||||
|
return !!updateResult;
|
||||||
|
}
|
||||||
|
|
||||||
export const ProjectService = {
|
export const ProjectService = {
|
||||||
create,
|
create,
|
||||||
listAllByUserId,
|
listAllByUserId,
|
||||||
destroy
|
destroy,
|
||||||
|
update,
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user