-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
102 additions
and
3 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
17 changes: 17 additions & 0 deletions
17
prisma/migrations/20240819015833_alter_delete_cascade/migration.sql
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 |
---|---|---|
@@ -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; |
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
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
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 |
---|---|---|
@@ -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'); | ||
} | ||
} | ||
} | ||
}, | ||
}; |
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 |
---|---|---|
@@ -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; | ||
} | ||
}, | ||
}; |