From 6e0b8feb799736113bb44bb7e07ce64e1cb800aa Mon Sep 17 00:00:00 2001 From: n9mi Date: Mon, 16 Sep 2024 14:35:16 +0700 Subject: [PATCH] feat: delete contact --- __tests__/contact.spec.ts | 44 +++++++++++++++++++++++++++++++++++++++ src/controller/contact.ts | 16 ++++++++++++++ src/router/contact.ts | 2 ++ src/service/contact.ts | 21 +++++++++++++++++++ 4 files changed, 83 insertions(+) diff --git a/__tests__/contact.spec.ts b/__tests__/contact.spec.ts index 4beb3d3..a28caea 100644 --- a/__tests__/contact.spec.ts +++ b/__tests__/contact.spec.ts @@ -368,6 +368,50 @@ describe("PUT /contacts/:id", () => { }); }); +describe("DELETE /contacts/:id", () => { + let token: string = ""; + let createdContact: Contact = {} as Contact; + + beforeAll(async () => { + token = await ContactTestUtil.getToken(); + createdContact = await ContactTestUtil.createContact(); + }); + + afterAll(async () => { + await ContactTestUtil.deleteContact(); + await ContactTestUtil.deleteUser(); + }); + + it("should return 200 - success deleting a contact", async () => { + const res = await supertest(web) + .delete(`${basePath}/contacts/${createdContact.id}`) + .set('Authorization', `Bearer ${token}`); + + logger.info(res.body); + expect(res.status).toBe(200); + expect(res.body.status).toBe(true); + }); + + it("should return 404 - contact doesn't exists", async () => { + const res = await supertest(web) + .delete(`${basePath}/contacts/1000`) + .set('Authorization', `Bearer ${token}`); + + logger.info(res.body); + expect(res.status).toBe(404); + expect(res.body.errors).toBeDefined(); + }); + + it("should return 401 - empty authorization", async () => { + const res = await supertest(web) + .delete(`${basePath}/contacts/${createdContact.id}`); + + logger.info(res.body); + expect(res.status).toBe(401); + expect(res.body.errors).toBeDefined(); + }); +}) + class ContactTestUtil { static user = { name: "user_test_contact", diff --git a/src/controller/contact.ts b/src/controller/contact.ts index dcd6407..98daf51 100644 --- a/src/controller/contact.ts +++ b/src/controller/contact.ts @@ -69,4 +69,20 @@ export class ContactController { next(e); } } + + static async delete(req: Request, res: Response, next: NextFunction) { + try { + if (isNaN(Number(req.params.id))) { + throw new ResponseError(404, "contact doesn't exists"); + } + const deleteRes = await ContactService.delete(res.locals.username, Number(req.params.id)); + + res.status(200) + .json({ + status: deleteRes + }); + } catch (e) { + next(e); + } + } } \ No newline at end of file diff --git a/src/router/contact.ts b/src/router/contact.ts index d991c4c..6350992 100644 --- a/src/router/contact.ts +++ b/src/router/contact.ts @@ -9,6 +9,8 @@ export const getContactRouter = (basePath: string) => { contactRouter.get(`${basePath}/contacts/:id`, ContactController.getById); contactRouter.post(`${basePath}/contacts`, ContactController.create); contactRouter.put(`${basePath}/contacts/:id`, ContactController.update); + contactRouter.delete(`${basePath}/contacts/:id`, ContactController.delete); + return contactRouter; } \ No newline at end of file diff --git a/src/service/contact.ts b/src/service/contact.ts index bbd29b7..f8ee9cc 100644 --- a/src/service/contact.ts +++ b/src/service/contact.ts @@ -137,4 +137,25 @@ export default class ContactService { return toContactResponse(contactUpdated); } + + static async delete(username: string, id: number): Promise { + const isExists = await prisma.contact.count({ + where: { + id: id, + username: username + } + }) == 1; + if (!isExists) { + throw new ResponseError(404, "contact doesn't exists"); + } + + await prisma.contact.delete({ + where: { + id: id, + username: username + } + }); + + return true; + } } \ No newline at end of file