@feardread/fear 1.1.0 → 1.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -59,22 +59,13 @@ exports.all = tryCatch(async (Model, req, res) => {
59
59
  exports.read = tryCatch(async (Model, req, res) => {
60
60
  const { id } = req.params;
61
61
 
62
- if (!id) {
62
+ if (!id || !isValidObjectId(id)) {
63
63
  return res.status(400).json({
64
64
  result: null,
65
65
  success: false,
66
- message: "Document ID is required"
66
+ message: "Invalid or Missing Document ID"
67
67
  });
68
68
  }
69
-
70
- if (!isValidObjectId(id)) {
71
- return res.status(400).json({
72
- result: null,
73
- success: false,
74
- message: "Invalid document ID format"
75
- });
76
- }
77
-
78
69
  const result = await Model.findById(id).exec();
79
70
 
80
71
  if (!result) {
@@ -128,6 +128,25 @@ exports.getProductStats = tryCatch(async (req, res) => {
128
128
  });
129
129
  });
130
130
 
131
+
132
+ /**
133
+ * Get trending products (latest products with high ratings)
134
+ * @param {Object} req - Express request object
135
+ * @param {Object} res - Express response object
136
+ */
137
+ exports.featured = async (req, res) => {
138
+ console.log('Featured Product route called ');
139
+ const featuredProducts = Product.find({isFeatured: true})
140
+ console.log('Featured Product route called ', featuredProducts);
141
+ return res.status(200).json({
142
+ success: true,
143
+ message: `Top ${featuredProducts.length} trending products`,
144
+ result: featuredProducts,
145
+ count: featuredProducts.length,
146
+ sortBy
147
+ });
148
+ };
149
+
131
150
  exports.productSearch = tryCatch(async (req, res) => {
132
151
  return await productSearch(req.query, Product)
133
152
  .then((result) => {
package/models/product.js CHANGED
@@ -12,6 +12,7 @@ const productSchema = new mongoose.Schema({
12
12
  quantity: { type: Number, required: true },
13
13
  sold: { type: Number, default: 0 },
14
14
  reviews: [Review.schema],
15
+ isFeatured: {type: Boolean, required: false, default: true}
15
16
  images: [{
16
17
  public_id: String,
17
18
  url: String
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@feardread/fear",
3
- "version": "1.1.0",
3
+ "version": "1.1.2",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/routes/product.js CHANGED
@@ -10,8 +10,8 @@ module.exports = (fear) => {
10
10
  .post("/new", Product.create)
11
11
  .get("/edit/:id", Product.read);
12
12
 
13
- router.route("/search").post(Product.search);
14
- router.route('/search/all').get(Product.productSearch);
13
+ router.route("/search").post(handler.async(Product.search);
14
+ router.route("/featured").get(handler.async(Product.featured));
15
15
  router.route("/one").get(handler.async(Product.read));
16
16
  router.route("/:id")
17
17
  .get(Product.read)