-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
121 lines (109 loc) · 3.99 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
const express = require('express');
const app = express();
const port = process.env.PORT || 3000;
// Sample product data (in-memory array)
const catalogs = [
{
systemId: 1,
upc: '846336000242',
sku: 'T920EZ',
category: 'Concentrates',
feedType:'Textured',
brand: 'Tribute',
manufacturer: 'Kalmbach Feeds, Inc',
productName: 'Kalm N Ez',
price: 22.99,
weightPerUnitPurchased:50,
description:'A low sugar and starch (NSC), pelleted feed with a small inclusion of whole oats.',
url: "https://tributeequinenutrition.com/products/kalm-n-ez-textured",
dateAdded: '1776-07-04',
isImported: 'true'
},
{
systemId: 2,
upc: '846336000211',
sku: '920P',
category: 'Concentrates',
feedType:'Pelleted',
brand: 'Tribute',
manufacturer: 'Kalmbach Feeds, Inc',
productName: 'Kalm N Ez',
price: 22.99,
weightPerUnitPurchased:50,
description: 'A low sugar and starch (NSC), pelleted horse feed for all classes of adult horses.',
url: "https://tributeequinenutrition.com/products/kalm-n-ez-pellet",
dateAdded: '1776-07-04',
isImported: 'true'
},
{
systemId: 3,
upc: '846336004813',
sku: '928EK30',
category: 'Concentrates',
feedType:'Pelleted',
brand: 'Tribute',
manufacturer: 'Kalmbach Feeds, Inc',
productName: 'Essential K',
price: 24.99,
weightPerUnitPurchased:50,
description: 'A low NSC ration balancer for idle, breeding, growing and performance horses.',
url: "https://tributeequinenutrition.com/products/essential-k",
dateAdded: '1776-07-04',
isImported: 'true'
},
{
systemId: 4,
upc: '736816326439 ',
sku: '',
category: 'Concentrates',
feedType: 'Pelleted',
brand: "Producer's Pride",
manufacturer: "Producer's Pride",
productName: '12% Sweet Horse Feed',
price: 14.49,
weightPerUnitPurchased: 50,
description: 'A sweet horse feed suitable for various life stages.',
url: "https://www.tractorsupply.com/tsc/product/producers-pride-12-sweet-feed-50-lb",
dateAdded: '1776-07-04',
isImported: 'true'
}
// Add more products
];
// Get a specific product by Product Type
app.get('/api/catalogs/category/:category', (req, res) => {
const requestedCategory = req.params.category;
const filteredCatalogs = catalogs.filter(p => p.category === requestedCategory);
res.json(filteredCatalogs.length ? filteredCatalogs : { error: 'No records with this catagory were found' });
});
// Get catalogs that were added on or after date passed in.
app.get('/api/catalogs/bydate/:date', (req, res) => {
const date = new Date(req.params.date);
if (isNaN(date)) {
return res.status(400).json({ error: 'Invalid date format' });
}
const filteredcatalogs = catalogs.filter(p => new Date(p.dateAdded) >= date);
res.json(filteredcatalogs.length ? filteredcatalogs : { error: 'catalogs after date not found' });
});
// Get all catalogs
app.get('/api/catalogs', (req, res) => {
res.json(catalogs);
});
// Get a specific catalog by systemId
app.get('/api/catalogs/:systemId', (req, res) => {
const apiId = parseInt(req.params.systemId);
const catalog = catalogs.find(p => p.systemId === apiId);
res.json(catalog || { error: 'Catalog with this ID was not found' });
});
// // Get a specific catalog by barcode
// app.get('/api/catalogs/upc/:upc', (req, res) => {
// const requestedUpc = req.params.upc;
// const catalogsMatchingUpc = catalogs.find(p => p.upc === requestedUpc);
// if (!catalogsMatchingUpc) {
// res.status(404).json({ error: 'catalogs with this UPC were not found' });
// } else {
// res.json(catalogsMatchingUpc);
// }
// });
app.listen(port, () => {
console.log(`API is now listening`);
});