Skip to content

Commit e9ac8c2

Browse files
Merge pull request #2068 from OneCommunityGlobal/Akshith-create-garden-inventory
Akshith - Create Backend API endpoints to add and store seeds, trees and bushes and animals in the farm
2 parents b5cfaf7 + d6b6939 commit e9ac8c2

8 files changed

Lines changed: 521 additions & 0 deletions

File tree

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
/* eslint-disable max-lines-per-function */
2+
const mongoose = require('mongoose');
3+
const Animal = require('../../models/kitchenInventory/animal');
4+
5+
const animalController = function () {
6+
// GET /animals
7+
const getAllAnimals = async (req, res) => {
8+
try {
9+
const results = await Animal.find().sort({ createdAt: -1 }).lean();
10+
res.status(200).json(results);
11+
} catch (err) {
12+
res.status(500).json(err);
13+
}
14+
};
15+
16+
// GET /animals/:id
17+
const getAnimalById = async function (req, res) {
18+
try {
19+
const { animalId } = req.params;
20+
const animal = await Animal.findById(animalId);
21+
22+
if (!animal) {
23+
res.status(404).json({
24+
error: 'Animal not found',
25+
});
26+
return;
27+
}
28+
29+
res.status(200).json({
30+
_serverMessage: 'Animal retrieved successfully',
31+
animal,
32+
});
33+
} catch (error) {
34+
res.status(500).json({
35+
error: 'Failed to retrieve animal',
36+
details: error.message,
37+
});
38+
}
39+
};
40+
41+
// POST /animals
42+
const createAnimal = async (req, res) => {
43+
try {
44+
const {
45+
name,
46+
breed,
47+
count,
48+
purpose,
49+
location,
50+
health,
51+
acquiredDate,
52+
species,
53+
notes,
54+
vaccinations,
55+
} = req.body;
56+
57+
if (!name || !count || !location) {
58+
res.status(400).json({
59+
error: 'Missing required fields: name, count, and location are required',
60+
});
61+
return;
62+
}
63+
64+
if (count <= 0) {
65+
res.status(400).json({
66+
error: 'Count must be greater than 0',
67+
});
68+
return;
69+
}
70+
71+
const newAnimal = new Animal({
72+
name,
73+
breed,
74+
count,
75+
purpose,
76+
location,
77+
health: health || 'Healthy',
78+
acquiredDate,
79+
species,
80+
notes,
81+
vaccinations,
82+
});
83+
84+
const savedAnimal = await newAnimal.save();
85+
res.status(201).json(savedAnimal);
86+
} catch (error) {
87+
res.status(500).json(error);
88+
}
89+
};
90+
91+
// PUT /animals/:id
92+
const updateAnimal = async (req, res) => {
93+
try {
94+
const { animalId } = req.params;
95+
const updateData = {
96+
...req.body,
97+
updatedAt: Date.now(),
98+
};
99+
100+
const updatedAnimal = await Animal.findByIdAndUpdate(animalId, updateData, {
101+
new: true,
102+
runValidators: true,
103+
});
104+
105+
if (!updatedAnimal) {
106+
return res.status(404).json('Animal Not Found');
107+
}
108+
109+
res.status(200).json('Animal Updated Successfully');
110+
} catch (error) {
111+
res.status(500).json(error);
112+
}
113+
};
114+
115+
// DELETE /animals/:id
116+
const deleteAnimal = async (req, res) => {
117+
try {
118+
const { animalId } = req.params;
119+
const deletedAnimal = await Animal.findByIdAndDelete(animalId);
120+
121+
if (!deletedAnimal) {
122+
return res.status(404).json('Animal Not Found');
123+
}
124+
res.status(200).json('Animal Deleted Successfully');
125+
} catch (error) {
126+
res.status(500).json('Failed To Delete Animal');
127+
}
128+
};
129+
130+
return {
131+
getAllAnimals,
132+
getAnimalById,
133+
createAnimal,
134+
updateAnimal,
135+
deleteAnimal,
136+
};
137+
};
138+
139+
module.exports = animalController;
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
/* eslint-disable max-lines-per-function */
2+
const mongoose = require('mongoose');
3+
const Seed = require('../../models/kitchenInventory/seed');
4+
5+
const seedController = function () {
6+
// GET /seeds
7+
const getAllSeeds = async (req, res) => {
8+
try {
9+
const results = await Seed.find().sort({ createdAt: -1 }).lean();
10+
res.status(200).json(results);
11+
} catch (err) {
12+
res.status(500).json(err);
13+
}
14+
};
15+
16+
// GET /seeds/:id
17+
const getSeedById = async function (req, res) {
18+
try {
19+
const { seedId } = req.params;
20+
const seed = await Seed.findById(seedId);
21+
22+
if (!seed) {
23+
res.status(404).json({
24+
error: 'Seed not found',
25+
});
26+
return;
27+
}
28+
29+
res.status(200).json({
30+
_serverMessage: 'Seed retrieved successfully',
31+
seed,
32+
});
33+
} catch (error) {
34+
res.status(500).json({
35+
error: 'Failed to retrieve seed',
36+
details: error.message,
37+
});
38+
}
39+
};
40+
41+
// POST /seeds
42+
const createSeed = async (req, res) => {
43+
try {
44+
const {
45+
name,
46+
collectedDate,
47+
quantityCollected,
48+
seedType,
49+
source,
50+
storageLocation,
51+
expiryDate,
52+
notes,
53+
} = req.body;
54+
55+
if (!name || quantityCollected === undefined) {
56+
res.status(400).json({ error: 'Missing required fields: name and quantity collected' });
57+
return;
58+
}
59+
60+
const newSeed = new Seed({
61+
name,
62+
collectedDate: collectedDate || Date.now(),
63+
quantityCollected,
64+
seedType,
65+
source,
66+
storageLocation,
67+
expiryDate,
68+
notes,
69+
});
70+
71+
const savedSeed = await newSeed.save();
72+
res.status(201).json(savedSeed);
73+
} catch (error) {
74+
res.status(500).json(error);
75+
}
76+
};
77+
78+
// PUT /seeds/:id
79+
const updateSeed = async (req, res) => {
80+
try {
81+
const { seedId } = req.params;
82+
const updateData = {
83+
...req.body,
84+
updatedAt: Date.now(),
85+
};
86+
87+
const updatedSeed = await Seed.findByIdAndUpdate(seedId, updateData, {
88+
new: true,
89+
runValidators: true,
90+
});
91+
92+
if (!updatedSeed) {
93+
return res.status(404).json('Seed Not Found');
94+
}
95+
96+
res.status(200).json('Seed Updated Successfully');
97+
} catch (error) {
98+
res.status(500).json(error);
99+
}
100+
};
101+
102+
// DELETE /seeds/:id
103+
const deleteSeed = async (req, res) => {
104+
try {
105+
const { seedId } = req.params;
106+
const deletedSeed = await Seed.findByIdAndDelete(seedId);
107+
108+
if (!deletedSeed) {
109+
return res.status(404).json('Seed Not Found');
110+
}
111+
res.status(200).json('Seed Deleted Successfully');
112+
} catch (error) {
113+
res.status(500).json('Failed To Delete Seed');
114+
}
115+
};
116+
117+
return {
118+
getAllSeeds,
119+
getSeedById,
120+
createSeed,
121+
updateSeed,
122+
deleteSeed,
123+
};
124+
};
125+
126+
module.exports = seedController;

0 commit comments

Comments
 (0)