-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
99 lines (85 loc) · 3.14 KB
/
server.js
File metadata and controls
99 lines (85 loc) · 3.14 KB
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
// This is a CommonJS file for json-server
// ESLint disabled for this file in .eslintrc.js
const jsonServer = require('json-server');
const server = jsonServer.create();
const router = jsonServer.router('db.json');
const middlewares = jsonServer.defaults();
server.use(middlewares);
server.use(jsonServer.bodyParser);
// Stubbed login endpoint
server.post('/auth/login', (req, res) => {
res.jsonp({
token: 'fake-jwt-token',
user: { id: 1, name: 'Test User', email: req.body.email },
});
});
// Enhanced campaigns endpoint with category filtering
server.get('/api/campaigns', (req, res) => {
const db = router.db.getState();
let campaigns = db.campaigns || [];
// Add a default category to each campaign for testing if it doesn't have one
campaigns = campaigns.map(campaign => {
if (!campaign.category) {
// Use tax_eligibility as category if available, otherwise use status
const category = campaign.tax_eligibility || campaign.status || 'general';
return { ...campaign, category };
}
return campaign;
});
// Handle category filter if provided
const category = req.query.category;
if (category) {
campaigns = campaigns.filter(
campaign =>
campaign.category === category ||
(campaign.categories && campaign.categories.includes(category))
);
}
// Handle pagination
const page = parseInt(req.query._page) || 1;
const limit = parseInt(req.query._limit) || 10;
const startIndex = (page - 1) * limit;
const endIndex = page * limit;
const paginatedCampaigns = campaigns.slice(startIndex, endIndex);
// Set total count header for pagination
res.header('X-Total-Count', campaigns.length.toString());
res.jsonp(paginatedCampaigns);
});
// Follow/unfollow campaign endpoint
server.post('/api/campaigns/:id/follow', (req, res) => {
const campaignId = parseInt(req.params.id);
const userId = req.body.userId || 1; // Default to user 1 for demo
// In a real implementation, you would update a database
// For now, just return success response
res.jsonp({
success: true,
message: `User ${userId} is now following campaign ${campaignId}`,
followed: true,
campaignId,
userId,
});
});
server.post('/api/campaigns/:id/unfollow', (req, res) => {
const campaignId = parseInt(req.params.id);
const userId = req.body.userId || 1; // Default to user 1 for demo
// In a real implementation, you would update a database
// For now, just return success response
res.jsonp({
success: true,
message: `User ${userId} has unfollowed campaign ${campaignId}`,
followed: false,
campaignId,
userId,
});
});
// You can add more custom endpoints here if needed
server.use(router);
server.listen(3000, '0.0.0.0', () => {
console.log('JSON Server is running on http://0.0.0.0:3000');
console.log('Available endpoints:');
console.log('- Authentication: /auth/login');
console.log('- Campaigns: /api/campaigns (supports pagination and category filtering)');
console.log('- Follow campaign: /api/campaigns/:id/follow');
console.log('- Unfollow campaign: /api/campaigns/:id/unfollow');
console.log('- Default json-server routes: /campaigns, etc.');
});