137 lines
4.4 KiB
JavaScript
137 lines
4.4 KiB
JavaScript
import express from "express";
|
||
|
||
// This array acts as temporary storage, so all changes are lost when the server restarts.
|
||
const DINOSAURS = [
|
||
{
|
||
id: 1,
|
||
name: "Tyrannosaurus",
|
||
description:
|
||
"Tyrannosaurus („tyranský ještěr“ – odvozeno ze starořeckého τύραννος – tyrannos = vládce, resp. krutovládce; v případě druhu T. rex pak latinského rex = král) byl jeden z největších masožravých dinosaurů (teropodů) a zároveň jedním z největších suchozemských predátorů všech dob.",
|
||
period: "křída",
|
||
wikipediaAddress: "https://cs.wikipedia.org/wiki/Tyrannosaurus",
|
||
},
|
||
{
|
||
id: 2,
|
||
name: "Stegosaurus",
|
||
description:
|
||
"Stegosaurus („zastřešený ještěr“) byl rod ptakopánvého dinosaura, který žil v období pozdní jury (asi před 150 miliony let) na území Severní Ameriky. Jeho pojmenování znamená „střechovitý či zastřešený ještěr“, toto označení dostal podle plochých desek, menší měl na krku a větší na hřbetě a přední části ocasu.",
|
||
period: "jura",
|
||
wikipediaAddress: "https://cs.wikipedia.org/wiki/Stegosaurus",
|
||
},
|
||
{
|
||
id: 3,
|
||
name: "Iguanodon",
|
||
description:
|
||
"Iguanodon (z latiny: „leguání zub“, český název je také iguanodon) je rod ornitopodního dinosaura, který se vývojově nacházel někde mezi prvními hbitými dvounohými hypsilofodonty a kachnozobými dinosaury, jimiž věk ornitopodů vrcholil.",
|
||
period: "křída",
|
||
wikipediaAddress: "https://cs.wikipedia.org/wiki/Iguanodon",
|
||
},
|
||
{
|
||
id: 4,
|
||
name: "Velociraptor",
|
||
description:
|
||
"Velociraptor byl rod poměrně malého teropodního dinosaura, patřícího mezi dravé dromeosauridy. Byl zástupcem skupiny velociraptorinů, menších „srpodrápých“ teropodů, obývajících severní kontinenty v období pozdní křídy.",
|
||
period: "křída",
|
||
wikipediaAddress: "https://cs.wikipedia.org/wiki/Velociraptor",
|
||
},
|
||
{
|
||
id: 5,
|
||
name: "Brontosaurus",
|
||
description:
|
||
"Brontosaurus (z řec. βροντή, brontē = hrom + σαυρος, sauros = ještěr: „hřmotný ještěr“ či „hromový ještěr“) byl rod velkého sauropodního dinosaura z čeledi Diplodocidae. Žil v období svrchní jury, asi před 155 až 152 miliony let na území západu Severní Ameriky.",
|
||
period: "jura",
|
||
wikipediaAddress: "https://cs.wikipedia.org/wiki/Brontosaurus_(dinosaurus)",
|
||
},
|
||
];
|
||
|
||
const app = express();
|
||
const port = Number(process.env.PORT ?? 3000);
|
||
|
||
app.use(express.json());
|
||
|
||
// Return the complete in-memory collection.
|
||
app.get("/dinosaurs", (_request, response) => {
|
||
response.json(DINOSAURS);
|
||
});
|
||
|
||
app.get("/dinosaurs/:id", (request, response) => {
|
||
const dinosaurId = Number(request.params.id);
|
||
|
||
const dinosaur = DINOSAURS.find((dinosaur) => dinosaur.id === dinosaurId);
|
||
|
||
if (!dinosaur) {
|
||
return response.status(404).json({
|
||
message: "Dinosaur not found",
|
||
});
|
||
}
|
||
|
||
response.json(dinosaur);
|
||
});
|
||
|
||
app.post("/dinosaurs", (request, response) => {
|
||
const dinosaur = request.body;
|
||
|
||
if (!dinosaur.name || !dinosaur.period) {
|
||
return response.status(400).json({
|
||
message: "The name and period fields are required",
|
||
});
|
||
}
|
||
|
||
const existingDinosaurIds = DINOSAURS.map((existingDinosaur) => existingDinosaur.id);
|
||
const newDinosaurId = Math.max(0, ...existingDinosaurIds) + 1;
|
||
|
||
const newDinosaur = {
|
||
...dinosaur,
|
||
id: newDinosaurId,
|
||
};
|
||
|
||
DINOSAURS.push(newDinosaur);
|
||
|
||
response.status(201).json(newDinosaur);
|
||
});
|
||
|
||
app.put("/dinosaurs/:id", (request, response) => {
|
||
const dinosaurId = Number(request.params.id);
|
||
const dinosaurBody = request.body;
|
||
|
||
if (!dinosaurBody.name || !dinosaurBody.period) {
|
||
return response.status(400).json({
|
||
message: "The name and period fields are required",
|
||
});
|
||
}
|
||
|
||
const dinosaurIndex = DINOSAURS.findIndex((dinosaur) => dinosaur.id === dinosaurId);
|
||
|
||
if (dinosaurIndex === -1) {
|
||
return response.status(404).json({
|
||
message: "Dinosaur not found",
|
||
});
|
||
}
|
||
|
||
DINOSAURS[dinosaurIndex] = {
|
||
...dinosaurBody,
|
||
id: dinosaurId,
|
||
};
|
||
|
||
response.status(204).send();
|
||
});
|
||
|
||
app.delete("/dinosaurs/:id", (request, response) => {
|
||
const dinosaurId = Number(request.params.id);
|
||
|
||
const dinosaurIndex = DINOSAURS.findIndex((dinosaur) => dinosaur.id === dinosaurId);
|
||
|
||
if (dinosaurIndex === -1) {
|
||
return response.status(404).json({
|
||
message: "Dinosaur not found",
|
||
});
|
||
}
|
||
|
||
DINOSAURS.splice(dinosaurIndex, 1);
|
||
response.status(204).send();
|
||
});
|
||
|
||
app.listen(port, () => {
|
||
console.log(`Server running on http://localhost:${port}`);
|
||
});
|