-
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.
feat: Product router, controller, service and repository created
- Loading branch information
Showing
13 changed files
with
367 additions
and
80 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
type PrismaProduct = { | ||
id: string; | ||
title: string; | ||
size: string; | ||
color: string; | ||
description: string; | ||
gender: string; | ||
category: string; | ||
price: number; | ||
imageUrl: string; | ||
createdAt: Date; | ||
updatedAt: Date; | ||
}; | ||
|
||
type PrismaProducts = PrismaProduct[]; | ||
|
||
abstract class ProductProvider { | ||
abstract getProducts(): Promise<PrismaProducts | Error>; | ||
|
||
abstract getProductById(id: string): Promise<PrismaProduct | Error>; | ||
|
||
abstract createProduct( | ||
title: string, | ||
size: string, | ||
color: string, | ||
description: string, | ||
gender: string, | ||
category: string, | ||
price: number, | ||
imageUrl: string | ||
): Promise<PrismaProduct | Error>; | ||
|
||
abstract deleteProduct(id: string): Promise<PrismaProduct | Error>; | ||
} |
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,70 @@ | ||
import { Request, Response, NextFunction } from "express"; | ||
|
||
class ProductController { | ||
constructor(private service: ProductProvider) { | ||
this.service = service; | ||
} | ||
|
||
getProducts = async ( | ||
req: Request, | ||
res: Response, | ||
next: NextFunction | ||
): Promise<Response<any, Record<string, any>> | void> => { | ||
try { | ||
const products = await this.service.getProducts(); | ||
return res.status(200).json(products); | ||
} catch (e) { | ||
next(e); | ||
} | ||
}; | ||
|
||
getProductById = async ( | ||
req: Request, | ||
res: Response, | ||
next: NextFunction | ||
): Promise<Response<any, Record<string, any>> | void> => { | ||
try { | ||
const product = await this.service.getProductById(req.params.id); | ||
return res.status(200).json(product); | ||
} catch (e) { | ||
next(e); | ||
} | ||
}; | ||
|
||
postProduct = async ( | ||
req: Request, | ||
res: Response, | ||
next: NextFunction | ||
): Promise<Response<any, Record<string, any>> | void> => { | ||
try { | ||
const newProduct = await this.service.createProduct( | ||
req.body.title, | ||
req.body.size, | ||
req.body.color, | ||
req.body.description, | ||
req.body.gender, | ||
req.body.category, | ||
req.body.price, | ||
req.body.imageUrl | ||
); | ||
return res.status(201).json(newProduct); | ||
} catch (e) { | ||
next(e); | ||
} | ||
}; | ||
|
||
deleteProduct = async ( | ||
req: Request, | ||
res: Response, | ||
next: NextFunction | ||
): Promise<Response<any, Record<string, any>> | void> => { | ||
try { | ||
const product = await this.service.deleteProduct(req.params.id); | ||
return res.status(200).json(product); | ||
} catch (e) { | ||
next(e); | ||
} | ||
}; | ||
} | ||
|
||
export default ProductController; |
This file was deleted.
Oops, something went wrong.
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,7 @@ | ||
export class HttpBadRequestError extends Error {} | ||
|
||
export class HttpUnauthorizedError extends Error {} | ||
|
||
export class HttpForbiddenError extends Error {} | ||
|
||
export class HttpNotFoundError extends Error {} |
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,11 @@ | ||
export class PrismaClientKnownRequestError extends Error {} | ||
|
||
export class PrismaClientUnknownRequestError extends Error {} | ||
|
||
export class PrismaClientRustPanicError extends Error {} | ||
|
||
export class PrismaClientInitializationError extends Error {} | ||
|
||
export class PrismaClientValidationError extends Error {} | ||
|
||
export class PrismaGenericError extends Error {} |
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 @@ | ||
export class ProductNotFoundError extends Error {} |
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 |
---|---|---|
@@ -1,30 +1,45 @@ | ||
import { Request, Response, NextFunction } from "express"; | ||
import { | ||
BadRequestError, | ||
ForbiddenError, | ||
NotFoundError, | ||
UnauthorizedError, | ||
} from "../errors/errors.js"; | ||
|
||
HttpBadRequestError, | ||
HttpForbiddenError, | ||
HttpNotFoundError, | ||
HttpUnauthorizedError, | ||
} from "../errors/http.error.js"; | ||
import { | ||
PrismaClientInitializationError, | ||
PrismaClientRustPanicError, | ||
PrismaClientUnknownRequestError, | ||
PrismaClientValidationError, | ||
PrismaGenericError, | ||
} from "../errors/prisma.error.js"; | ||
import { ProductNotFoundError } from "../errors/product.error.js"; | ||
export default function errorHandler( | ||
err: Error, | ||
e: Error, | ||
req: Request, | ||
res: Response, | ||
next: NextFunction | ||
) { | ||
if (err instanceof BadRequestError) { | ||
res.status(400).json(err.message); | ||
} else if (err instanceof UnauthorizedError) { | ||
res.status(401).json(err.message); | ||
} else if (err instanceof ForbiddenError) { | ||
res.status(403).json(err.message); | ||
} else if (err instanceof NotFoundError) { | ||
res.status(404).json(err.message); | ||
if (e instanceof HttpBadRequestError) { | ||
res.status(400).json({ error: "Bad Request Error" }); | ||
} else if (e instanceof HttpUnauthorizedError) { | ||
res.status(401).json({ error: "Unauthorized Error" }); | ||
} else if (e instanceof HttpForbiddenError) { | ||
res.status(403).json({ error: "Forbidden Error" }); | ||
} else if (e instanceof HttpNotFoundError) { | ||
res.status(404).json({ error: "Not Found Error" }); | ||
} else if (e instanceof PrismaClientUnknownRequestError) { | ||
res.status(500).json({ error: "Prisma Client Unknown Request Error" }); | ||
} else if (e instanceof PrismaClientRustPanicError) { | ||
res.status(500).json({ error: "Prisma Client Rust Panic Error" }); | ||
} else if (e instanceof PrismaClientInitializationError) { | ||
res.status(500).json({ error: "Prisma Client Initialization Error" }); | ||
} else if (e instanceof PrismaClientValidationError) { | ||
res.status(500).json({ error: "Prisma Client Validation Error" }); | ||
} else if (e instanceof PrismaGenericError) { | ||
res.status(500).json({ error: "Prisma Generic Error" }); | ||
} else if (e instanceof ProductNotFoundError) { | ||
res.status(500).json({ error: "Product Not Found Error" }); | ||
} else { | ||
res.status(500).json({ | ||
name: err.name, | ||
message: err.message, | ||
stack: err.stack, | ||
}); | ||
res.status(500).json({ error: "Unexpected error" }); | ||
} | ||
} |
Oops, something went wrong.