-
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.
Merge branch 'ft-product-managment' of https://github.com/atlp-rwanda…
…/knights-ecomm-be into ft-product-managment
- Loading branch information
Showing
10 changed files
with
128 additions
and
4 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
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
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,32 @@ | ||
import { Request, Response } from 'express'; | ||
import { Product } from '../../entities/Product'; | ||
import { getRepository } from 'typeorm'; | ||
import { responseError, responseSuccess } from '../../utils/response.utils'; | ||
|
||
|
||
export const deleteProductService = async (req: Request, res: Response) => { | ||
try { | ||
const { id } = req.params; | ||
|
||
const productRepository = getRepository(Product); | ||
|
||
const product = await productRepository.findOne({ | ||
where: { | ||
id: id, | ||
vendor: { | ||
id: req.user?.id | ||
} | ||
} | ||
}); | ||
|
||
if (product) { | ||
await productRepository.remove(product); | ||
return responseSuccess(res, 200, 'Product successfully deleted'); | ||
} | ||
|
||
return responseError(res, 404, 'Product not found'); | ||
|
||
} catch (error) { | ||
responseError(res, 400, (error as Error).message); | ||
} | ||
}; |
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,58 @@ | ||
import { Request, Response } from 'express'; | ||
import { Product } from '../../entities/Product'; | ||
import { getRepository } from 'typeorm'; | ||
import { responseError, responseSuccess } from '../../utils/response.utils'; | ||
|
||
|
||
export const readProductsService = async (req: Request, res: Response) => { | ||
try { | ||
|
||
// Define pagination parameters | ||
const page = req.query.page ? Number(req.query.page) : 1; | ||
const limit = req.query.limit ? Number(req.query.limit) : 10; | ||
const skip = (page - 1) * limit; | ||
|
||
// Retrieve products | ||
const productRepository = getRepository(Product); | ||
const products = await productRepository.find({ | ||
where: { | ||
vendor: { | ||
id: req.user?.id | ||
} | ||
}, | ||
skip, | ||
take: limit | ||
}); | ||
|
||
if (!products) { | ||
return responseError(res, 404, 'No Products available'); | ||
} | ||
return responseSuccess(res, 200, 'Products retrieved', { products }); | ||
} catch (error) { | ||
responseError(res, 400, (error as Error).message); | ||
} | ||
}; | ||
|
||
export const readProductService = async (req: Request, res: Response) => { | ||
try { | ||
const { id } = req.params; | ||
|
||
const productRepository = getRepository(Product); | ||
const product = await productRepository.findOne({ | ||
where: { | ||
id: id, | ||
vendor: { | ||
id: req.user?.id | ||
} | ||
} | ||
}); | ||
|
||
if (!product) { | ||
return responseError(res, 404, 'Product not found'); | ||
} | ||
|
||
return responseSuccess(res, 200, 'Product retrieved', { product }); | ||
} catch (error) { | ||
responseError(res, 400, (error as Error).message); | ||
} | ||
}; |
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 |
---|---|---|
|
@@ -17,7 +17,7 @@ export const responseSuccess = ( | |
jsend.success({ | ||
code: statusCode, | ||
message, | ||
data, | ||
...data, | ||
}) | ||
); | ||
}; | ||
|