Skip to content

Commit

Permalink
Merge branch 'dev' into pagination_donation_items_backend
Browse files Browse the repository at this point in the history
  • Loading branch information
Madhu2244 authored Mar 4, 2024
2 parents 8a79a55 + b1ab817 commit 5eb97b5
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 8 deletions.
32 changes: 28 additions & 4 deletions routes/businessRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,40 @@ const businessRouter = express.Router();
// GET all businesses
businessRouter.get('/', async (req, res) => {
try {
const allBusinesses = await db.query(`
const { businessLimit, pageNum, tab } = req.query;
const tabsWhereClause = tab ? `WHERE status='${tab}'` : '';

const allBusinesses = await db.query(
`
SELECT *
FROM business;
`);
FROM business
${tabsWhereClause}
${businessLimit ? ` LIMIT ${businessLimit}` : ''}
${pageNum ? ` OFFSET ${(pageNum - 1) * businessLimit}` : ''};`,
{ businessLimit, pageNum },
);
res.status(200).send(allBusinesses);
} catch (err) {
res.status(500).send(err.message);
}
});

businessRouter.get('/totalBusinesses', async (req, res) => {
try {
const { tab } = req.query;
const tabsWhereClause = tab ? `WHERE status='${tab}'` : '';

const totalSites = await db.query(`
SELECT COUNT(*)
FROM business
${tabsWhereClause}
`);
res.status(200).send(totalSites);
} catch (err) {
res.status(500).send(err.message);
}
});

// GET business with matching ID
businessRouter.get('/:id', async (req, res) => {
try {
Expand Down Expand Up @@ -207,7 +231,7 @@ businessRouter.put('/:id', async (req, res) => {
}
}
query += updateFields.join(', ');
query += ` WHERE id = ${id}`;
query += `WHERE id = ${id}`;

await db.query(query);

Expand Down
92 changes: 90 additions & 2 deletions routes/donationRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,92 @@ donationRouter.get('/business/totals/:businessId', async (req, res) => {
}
});

// Generates the WHERE clause for the filter and search for totalDonations and filteredDonations routes
const generateWhereClause = (filter, search) => {
const columns = [
'business_id',
'donation_id',
'food_bank_donation',
'reporter',
'email',
'date',
'canned_dog_food_quantity',
'dry_dog_food_quantity',
'canned_cat_food_quantity',
'dry_cat_food_quantity',
'misc_items',
'volunteer_hours',
];
let filterQuery = '';
if (filter !== 'all') {
if (filter === 'month') {
filterQuery = "'1 month'";
} else if (filter === 'quarter') {
filterQuery = "'3 months'";
} else {
filterQuery = "'1 year'";
}
}
const tabsWhereClause = filterQuery ? `WHERE date > current_date - interval ${filterQuery}` : '';
let searchWhereClause = '';
if (search.length > 0) {
searchWhereClause = `${tabsWhereClause ? ` AND ` : ` WHERE `}`;
searchWhereClause += columns
.map((column) => {
return `CAST(${column} AS TEXT) ILIKE '%' || $(search) || '%'`;
})
.join(' OR ');
}
return { tabsWhereClause, searchWhereClause };
};

// Route for getting total number of donations by filter
donationRouter.get('/totalDonations/:filter', async (req, res) => {
try {
const { filter } = req.params;
const { searchTerm } = req.query;
const search = searchTerm.split('+').join(' ');
const { tabsWhereClause, searchWhereClause } = generateWhereClause(filter, search);
const numDonations = await db.query(
`
SELECT COUNT(*)
from donation_tracking
${tabsWhereClause}
${searchWhereClause};
`,
{ search },
);
res.status(200).send(numDonations);
} catch (error) {
res.status(500).send(error.message);
}
});

// Route for filtering donations made in the specified time frame and paginated
donationRouter.get('/filter/:filter', async (req, res) => {
const siteResultLimit = 10; // how many results we want to show
try {
let filterDonationSites = '';
const { filter } = req.params;
const { pageNum, searchTerm } = req.query;
const search = searchTerm ? searchTerm.split('+').join(' ') : '';
const page = pageNum || 1;
const { tabsWhereClause, searchWhereClause } = generateWhereClause(filter, search);
filterDonationSites = await db.query(
`SELECT * FROM donation_tracking
${tabsWhereClause}
${searchWhereClause}
ORDER BY date DESC
LIMIT ${siteResultLimit} OFFSET ${(page - 1) * siteResultLimit}
;`,
{ siteResultLimit, pageNum, search },
);
res.status(200).send(filterDonationSites);
} catch (error) {
res.status(500).send(error);
}
});

// POST a new donation
donationRouter.post('/', async (req, res) => {
try {
Expand All @@ -107,9 +193,13 @@ donationRouter.post('/', async (req, res) => {
} = req.body;

if (!business_id) throw new Error('Business ID is required.');

if (!food_bank_donation) throw new Error('Food bank donation is required.');

if (!reporter) throw new Error('Reporter is required.');

if (!email) throw new Error('Email is required.');

if (!date) throw new Error('Date is required.');

const newFacility = await db.query(
Expand Down Expand Up @@ -160,7 +250,6 @@ donationRouter.put('/:id', async (req, res) => {
miscItems,
volunteerHours,
} = req.body;

const updatedValue = await db.query(
`UPDATE donation_tracking
SET donation_id = $(id)
Expand Down Expand Up @@ -197,5 +286,4 @@ donationRouter.put('/:id', async (req, res) => {
return res.status(500).send(err.message);
}
});

module.exports = donationRouter;
19 changes: 18 additions & 1 deletion routes/valueRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,24 @@ valueRouter.put('/:id', async (req, res) => {
try {
return res.status(200).send(updateItem[0]);
} catch (err) {
return res.status(500).send(err.message);
res.status(500).send(err.message);
}
});

// GET values by category
valueRouter.get('/filter/:category', async (req, res) => {
try {
const { category } = req.params;
let query = 'SELECT * FROM fair_market_value';
if (category && category !== 'all') {
query += ' WHERE category = $(category)';
}
const items = await db.query(query, {
category,
});
res.status(200).send(items);
} catch (err) {
res.status(500).send(err.message);
}
});

Expand Down
2 changes: 1 addition & 1 deletion server/schema/notification.sql
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ CREATE TABLE IF NOT EXISTS public.fair_market_value
item_id integer NOT NULL DEFAULT nextval('fair_market_value_item_id_seq'::regclass),
item_name character varying(200) COLLATE pg_catalog."default" NOT NULL,
quantity_type character varying(100) COLLATE pg_catalog."default",
quantity integer,
price numeric,
category character varying(50) COLLATE pg_catalog."default",
CONSTRAINT fair_market_value_pkey PRIMARY KEY (item_id)
)

0 comments on commit 5eb97b5

Please sign in to comment.