Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
1.1 kB
2
Indexable
Never
app.post("/equipment", async (req, res) => {
  try {
    const equipment = new Equipment(req.body);
    await equipment.save();
    res.status(201).send(equipment);
  } catch (error) {
    res.status(400).send(error);
  }
});

app.get("/equipment", async (req, res) => {
  try {
    const equipmentList = await Equipment.find();
    res.send(equipmentList);
  } catch (error) {
    res.status(500).send(error);
  }
});

app.patch("/equipment/:id", async (req, res) => {
  try {
    const equipment = await Equipment.findByIdAndUpdate(req.params.id, req.body, {
      new: true,
      runValidators: true,
    });

    if (!equipment) {
      return res.status(404).send();
    }

    res.send(equipment);
  } catch (error) {
    res.status(400).send(error);
  }
});

app.delete("/equipment/:id", async (req, res) => {
  try {
    const equipment = await Equipment.findByIdAndDelete(req.params.id);

    if (!equipment) {
      return res.status(404).send();
    }

    res.send(equipment);
  } catch (error) {
    res.status(500).send(error);
  }
});