Skip to content

Commit 23c9948

Browse files
committed
User profile fetch and edit,and total user doantion lists implemented
1 parent ba37797 commit 23c9948

11 files changed

Lines changed: 822 additions & 538 deletions

File tree

backend/controllers/admincontroller.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,22 @@ export const FetchReports = async (req, res) => {
7373
res.status(500).json({ message: "Internal Server Error" });
7474
}
7575
};
76+
77+
export const GetStatistics = async (req, res) => {
78+
try {
79+
const totalUsers = await user.countDocuments();
80+
const totalItems = await item.countDocuments();
81+
const totalTransactions = await transactions.countDocuments();
82+
const totalReports = await reports.countDocuments();
83+
84+
res.status(200).json({
85+
totalUsers,
86+
totalItems,
87+
totalTransactions,
88+
totalReports,
89+
});
90+
} catch (error) {
91+
console.error("Error fetching statistics:", error);
92+
res.status(500).json({ message: "Internal Server Error" });
93+
}
94+
};
Lines changed: 30 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,35 @@
1-
import Item from '../models/items.js';
1+
import Item from "../models/items.js";
22

3-
// **Simplified Filter:** Only check for essential display fields (Name, Description).
4-
// This guarantees that any recently donated item with basic text data will show up.
5-
const ESSENTIAL_FILTER = {
6-
name: { $exists: true, $ne: "" },
7-
description: { $exists: true, $ne: "" }
3+
const ESSENTIAL_FILTER = {
4+
name: { $exists: true, $ne: "" },
5+
description: { $exists: true, $ne: "" },
86
};
97

10-
11-
128
export const getHomePage = async (req, res) => {
13-
try {
14-
// Find the latest 5 items that meet the ESSENTIAL_FILTER
15-
/*const frequentItems = await Item.find(ESSENTIAL_FILTER)
16-
.sort({ createdAt: -1 }) // Sort by creation date (latest first)
17-
.limit(5) // Limit to 5 items
18-
.select('name description imageURL')
19-
.exec();*/
20-
21-
22-
const frequentItems = await Item.find(ESSENTIAL_FILTER)
23-
.sort({ createdAt: -1 })
24-
.limit(5)
25-
.populate('donorId', 'displayName email')
26-
.populate('categoryId', 'name')
27-
.select('name description imageURL donorId categoryId createdAt')
28-
.exec();
29-
30-
31-
32-
// 🎯 DEBUG LINE: Log the number of items found
33-
console.log(`[HOMEPAGE DEBUG] Items Found: ${frequentItems.length}`);
34-
35-
// 🎯 DEBUG LINE: Log the actual items (first item only for brevity)
36-
if (frequentItems.length > 0) {
37-
console.log(`[HOMEPAGE DEBUG] Latest Item Name: ${frequentItems[0].name}`);
38-
}
39-
40-
res.render("home", {
41-
frequentItems: frequentItems
42-
});
43-
44-
} catch (error) {
45-
console.error("Error fetching essential items for homepage:", error);
46-
res.render("home", {
47-
frequentItems: []
48-
});
9+
try {
10+
const frequentItems = await Item.find(ESSENTIAL_FILTER)
11+
.sort({ createdAt: -1 })
12+
.limit(5)
13+
.populate("donorId", "displayName email")
14+
.populate("categoryId", "name")
15+
.select("name description imageURL donorId categoryId createdAt")
16+
.exec();
17+
18+
console.log(`[HOMEPAGE DEBUG] Items Found: ${frequentItems.length}`);
19+
20+
if (frequentItems.length > 0) {
21+
console.log(
22+
`[HOMEPAGE DEBUG] Latest Item Name: ${frequentItems[0].name}`
23+
);
4924
}
50-
};
25+
26+
res.render("home", {
27+
frequentItems: frequentItems,
28+
});
29+
} catch (error) {
30+
console.error("Error fetching essential items for homepage:", error);
31+
res.render("home", {
32+
frequentItems: [],
33+
});
34+
}
35+
};
Lines changed: 35 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,45 @@
11
import express from "express";
2-
import mongoose from "mongoose";
2+
import mongoose from "mongoose";
33
import User from "../models/users.js";
4-
import Item from '../models/items.js';
5-
import Category from '../models/categories.js';
4+
import Item from "../models/items.js";
5+
import Category from "../models/categories.js";
66

7+
export const getSearchResults = async (req, res) => {
8+
const query = req.query.q; // e.g., "Stationery" or "baby doll"
79

8-
export const getSearchResults = async (req, res) => {
9-
const query = req.query.q; // e.g., "Stationery" or "baby doll"
10+
if (!query) {
11+
return res.render("../frontend/views/listing.ejs", { items: [] });
12+
}
1013

11-
if (!query) {
12-
return res.render("../frontend/views/listing.ejs", { items: [] });
13-
}
14+
const regexQuery = new RegExp(query, "i");
15+
let items = [];
1416

15-
16-
const regexQuery = new RegExp(query, 'i');
17-
let items = [];
17+
try {
18+
const category = await Category.findOne({ name: regexQuery });
1819

19-
try {
20-
// --- STEP 1: Search by Category Name ---
21-
const category = await Category.findOne({ name: regexQuery });
22-
23-
if (category) {
24-
// If the search query is a CATEGORY name (e.g., "Stationery"),
25-
// return ALL items belonging to that category ID.
26-
items = await Item.find({ categoryId: category._id })
27-
.populate('categoryId', 'name')
28-
.populate('donorId', 'displayName email')
29-
.exec();
30-
}
31-
32-
// --- STEP 2: Fallback Search (Item Name or Subcategory) ---
33-
if (items.length === 0) {
34-
// Search across item name, description, AND subcategory.
35-
items = await Item.find({
36-
$or: [
37-
{ name: regexQuery },
38-
{ description: regexQuery },
39-
{ subcategory: regexQuery } // <-- Checks the subcategory field!
40-
]
41-
})
42-
.populate('categoryId', 'name')
43-
.populate('donorId', 'displayName email')
44-
.exec();
45-
}
46-
47-
// 3. Render the results page
48-
res.render("itemListing.ejs", { items });
20+
if (category) {
21+
items = await Item.find({ categoryId: category._id })
22+
.populate("categoryId", "name")
23+
.populate("donorId", "displayName email")
24+
.exec();
25+
}
4926

50-
} catch (error) {
51-
console.error("Search error:", error);
52-
res.status(500).send("Error performing search.");
27+
if (items.length === 0) {
28+
items = await Item.find({
29+
$or: [
30+
{ name: regexQuery },
31+
{ description: regexQuery },
32+
{ subcategory: regexQuery },
33+
],
34+
})
35+
.populate("categoryId", "name")
36+
.populate("donorId", "displayName email")
37+
.exec();
5338
}
39+
40+
res.render("itemListing.ejs", { items });
41+
} catch (error) {
42+
console.error("Search error:", error);
43+
res.status(500).send("Error performing search.");
44+
}
5445
};

0 commit comments

Comments
 (0)