Skip to content

Commit

Permalink
feat: delete user & product APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
HungLV46 committed Aug 19, 2024
1 parent 93c6095 commit 24b98c5
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 3 deletions.
1 change: 1 addition & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ services:
condition: service_healthy
ports:
- '3000:3000'
- '5555:5555'
environment:
HOST: 0.0.0.0
PORT: 3000
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
-- DropForeignKey
ALTER TABLE "product_attributes" DROP CONSTRAINT "product_attributes_product_id_fkey";

-- DropForeignKey
ALTER TABLE "product_collections" DROP CONSTRAINT "product_collections_collection_id_fkey";

-- DropForeignKey
ALTER TABLE "product_collections" DROP CONSTRAINT "product_collections_product_id_fkey";

-- AddForeignKey
ALTER TABLE "product_attributes" ADD CONSTRAINT "product_attributes_product_id_fkey" FOREIGN KEY ("product_id") REFERENCES "products"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "product_collections" ADD CONSTRAINT "product_collections_product_id_fkey" FOREIGN KEY ("product_id") REFERENCES "products"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "product_collections" ADD CONSTRAINT "product_collections_collection_id_fkey" FOREIGN KEY ("collection_id") REFERENCES "collections"("id") ON DELETE CASCADE ON UPDATE CASCADE;
6 changes: 3 additions & 3 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ model ProductAttribute {
value String
product_id Int
product Product @relation(fields: [product_id], references: [id])
product Product @relation(fields: [product_id], references: [id], onDelete: Cascade)
@@unique([name, value, product_id])
@@index([name, value])
Expand Down Expand Up @@ -121,8 +121,8 @@ model ProductCollection {
product_id Int
collection_id Int
product Product @relation(fields: [product_id], references: [id])
collection Collection @relation(fields: [collection_id], references: [id])
product Product @relation(fields: [product_id], references: [id], onDelete: Cascade)
collection Collection @relation(fields: [collection_id], references: [id], onDelete: Cascade)
@@unique([product_id, collection_id])
@@index([product_id])
Expand Down
2 changes: 2 additions & 0 deletions src/apis/routes/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
export * from '#apis/routes/products/update';
export * from '#apis/routes/products/create';
export * from '#apis/routes/products/get';
export * from '#apis/routes/products/delete';

export * from '#apis/routes/users/create';
export * from '#apis/routes/users/update';
export * from '#apis/routes/users/delete';

// Search API
export * from '#apis/routes/elastic-search/search';
Expand Down
37 changes: 37 additions & 0 deletions src/apis/routes/products/delete.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { Prisma, PrismaClient } from '@prisma/client';
import Hapi from '@hapi/hapi';
import Joi from 'joi';
import Boom from '@hapi/boom';

const prisma = new PrismaClient();

export const deleteProductRoute: Hapi.ServerRoute = {
method: 'DELETE',
path: '/products/{id}',
options: {
description: 'Delete product by its ID',
notes: 'Delete product from database',
tags: ['api', 'product', 'delete'],
plugins: { 'hapi-swagger': {} },
validate: {
params: Joi.object({
id: Joi.number().required().example(1),
}),
},
},
handler: async (request: Hapi.Request) => {
try {
const id = Number(request.params.id);

await prisma.product.delete({ where: { id } });

return { data: { id } };
} catch (error: any) {
if (error instanceof Prisma.PrismaClientKnownRequestError) {
if (error.code === 'P2025') {
throw Boom.badRequest('Product does not exist');
}
}
}
},
};
42 changes: 42 additions & 0 deletions src/apis/routes/users/delete.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import Joi from 'joi';
import Hapi from '@hapi/hapi';
import { prisma } from '#common/db';
import { Prisma } from '@prisma/client';
import Boom from '@hapi/boom';

export const deleteUserRoute: Hapi.ServerRoute = {
method: 'DELETE',
path: '/users/{id}',
options: {
description: 'Delete user by its ID',
notes: 'Delete user from database',
tags: ['api', 'user', 'delete'],
plugins: { 'hapi-swagger': {} },
validate: {
params: Joi.object({
id: Joi.number().required().example(1),
}),
},
},
handler: async (request: Hapi.Request) => {
try {
const id = Number(request.params.id);

await prisma.user.delete({ where: { id } });

return { data: { id } };
} catch (error: any) {
if (error instanceof Prisma.PrismaClientKnownRequestError) {
if (error.code === 'P2003') {
throw Boom.badRequest('Related product exists');
}

if (error.code === 'P2025') {
throw Boom.badRequest('User does not exist');
}
}

throw error;
}
},
};

0 comments on commit 24b98c5

Please sign in to comment.