-
-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathplantingController.js
More file actions
53 lines (46 loc) · 1.47 KB
/
plantingController.js
File metadata and controls
53 lines (46 loc) · 1.47 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
const plantingController = function (PlantingEvent) {
const postEvent = async (req, res) => {
try {
const { name, related_to, count, date, location } = req.body;
if (!name || !related_to || !count || !date || !location) {
return res.status(400).send({
error: 'Name, related_to, count, date, and location are required.',
});
}
const validModules = ['Garden', 'Orchard', 'Animals'];
if (!validModules.includes(related_to)) {
return res.status(400).send({
error: `Invalid related_to value. Must be one of: ${validModules.join(', ')}`,
});
}
const event = new PlantingEvent({
name,
related_to,
count,
date,
location,
});
await event.save();
return res.status(201).send(event);
} catch (err) {
// eslint-disable-next-line no-console
console.error('Error creating planting event:', err);
return res.status(500).send({ error: 'Internal Server Error' });
}
};
const getEvents = async (req, res) => {
try {
const events = await PlantingEvent.find().sort({ date: 1 });
return res.status(200).send(events);
} catch (err) {
// eslint-disable-next-line no-console
console.error('Error fetching planting events:', err);
return res.status(500).send({ error: 'Internal Server Error' });
}
};
return {
postEvent,
getEvents,
};
};
module.exports = plantingController;