選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

index.ts 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. const Product = require('../../../models/product');
  2. import dbConnect from '../../../utils/helpers/dbHelpers';
  3. import type { NextApiRequest, NextApiResponse } from 'next';
  4. import {
  5. ProductDataDB,
  6. ProductsResponse,
  7. } from '../../../utils/interface/productInterface';
  8. async function handler(
  9. req: NextApiRequest,
  10. res: NextApiResponse<ProductsResponse>
  11. ) {
  12. const { method } = req;
  13. await dbConnect();
  14. const pageIndex = parseInt(req.query.pageIndex as string);
  15. const category = req.query.category === 'All' ? '' : req.query.category;
  16. const filterType = req.query.filterType;
  17. if (pageIndex < 1) {
  18. res.status(422).json({
  19. message: 'Page does not exist ',
  20. });
  21. return;
  22. }
  23. switch (method) {
  24. case 'GET': {
  25. try {
  26. const productCount: number = await Product.find({
  27. category: { $regex: category },
  28. }).countDocuments();
  29. if (productCount === 0) {
  30. res.status(200).json({
  31. message: 'There are currently no products in our database.',
  32. product: [],
  33. productCount: 0,
  34. next: '',
  35. previous: '',
  36. });
  37. break;
  38. }
  39. if ((pageIndex - 1) * 9 >= productCount) {
  40. throw new Error('Page does not exist!');
  41. }
  42. const product: Array<ProductDataDB> = await Product.find({
  43. category: { $regex: category },
  44. })
  45. .skip((pageIndex - 1) * 9)
  46. .limit(9)
  47. .sort(filterType === 'asc' ? 'name' : '-name ');
  48. if (!product) {
  49. throw new Error('There are currently no products in our database.');
  50. }
  51. const previousUrl =
  52. pageIndex > 1
  53. ? `https://localhost:3000/api/product?pageIndex=${pageIndex - 1}`
  54. : '';
  55. const nextUrl =
  56. pageIndex * 9 < productCount
  57. ? `https://localhost:3000/api/product?pageIndex=${pageIndex + 1}`
  58. : '';
  59. res.status(200).json({
  60. message: 'All products from our database were fetched successfully.',
  61. product,
  62. productCount,
  63. next: nextUrl,
  64. previous: previousUrl,
  65. });
  66. } catch (error) {
  67. if (error instanceof Error)
  68. res.status(400).json({ message: error.message });
  69. else res.status(400).json({ message: 'Unexpected error' + error });
  70. }
  71. break;
  72. }
  73. case 'POST': {
  74. try {
  75. const product: ProductDataDB = await Product.create(req.body);
  76. res
  77. .status(201)
  78. .json({ message: 'Your product was created and stored!', product });
  79. } catch (error) {
  80. if (error instanceof Error)
  81. res.status(400).json({ message: error.message });
  82. else res.status(400).json({ message: 'Unexpected error' + error });
  83. }
  84. break;
  85. }
  86. default:
  87. res.status(405).json({ message: 'Method not allowed' });
  88. break;
  89. }
  90. }
  91. export default handler;