|
| 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; |
0 commit comments