Skip to content

Commit

Permalink
Ex 3.15 - 3.18
Browse files Browse the repository at this point in the history
  • Loading branch information
vreima committed Aug 12, 2023
1 parent baf474c commit 0323e60
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 34 deletions.
95 changes: 64 additions & 31 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,27 @@ const express = require("express");
const Person = require("./models/person");

const app = express();

const cors = require("cors");
app.use(cors());

app.use(express.static("build"));

app.use(express.json());

const morgan = require("morgan");

morgan.token("payload", (req) =>
req.method === "POST" ? JSON.stringify(req.body) : null
);
const morgan_conf =
":method :url :status :res[content-length] - :response-time ms :payload";

app.use(cors());
app.use(express.json());
app.use(morgan(morgan_conf));
app.use(express.static("build"));

app.get("/api/persons", (req, res) => {
Person.find({}).then((persons) => {
res.json(persons);
});
});

app.post("/api/persons", (req, res) => {
app.post("/api/persons", (req, res, next) => {
const data = req.body;

if (!data.name) {
Expand Down Expand Up @@ -55,36 +53,53 @@ app.post("/api/persons", (req, res) => {
number: data.number,
});

newPerson.save().then((savedPerson) => res.status(201).json(savedPerson));
newPerson
.save()
.then((savedPerson) => res.status(201).json(savedPerson))
.catch((error) => next(error));
});
});

// people.persons = people.persons.concat(newPerson);

// return res.status(201).end();
app.delete("/api/persons/:id", (req, res, next) => {
Person.findOneAndDelete({ _id: req.params.id })
.then((deleted) => res.status(204).end())
.catch((error) => next(error));
});

app.delete("/api/persons/:id", (req, res) => {
const deleted = Person.findOneAndDelete({ _id: req.params.id }).catch(
(error) => res.status(204).end()
);
// const id = Number(req.params.id);
// people.persons = people.persons.filter((person) => person.id !== id);
app.put("/api/persons/:id", (req, res, next) => {
const id = req.params.id;

res.status(204).end();
const person = {
name: req.body.name,
number: req.body.number,
};

Person.findByIdAndUpdate(id, person, { new: true })
.then((result) => {
if (result) res.json(result);
else res.status(404).end();
})
.catch((error) => next(error));
});

app.get("/api/persons/:id", (req, res) => {
// This is stupid, but I just did not manage to
// get findById() to work.
Person.find({}).then((persons) => {
persons.forEach((person) => {
if (person._id.toString() === req.params.id) {
res.json(person);
}
});
res.status(404).end();
});
// if (!person) return res.status(404).end();
app.get("/api/persons/:id", (req, res, next) => {
Person.findById(req.params.id)
.then((person) => {
if (person) res.json(person);
else res.status(404).end();
})
.catch((error) => next(error));

// Person.find({})
// .then((persons) => {
// persons.forEach((person) => {
// if (person._id.toString() === req.params.id) {
// res.json(person);
// }
// });
// res.status(404).end();
// })
// .catch((error) => next(error));

// res.json(person);
// const id = Number(req.params.id);
Expand All @@ -103,6 +118,24 @@ app.get("/info", (req, res) => {
});
});

const errorHandler = (error, request, response, next) => {
console.error(error.message);

if (error.name === "CastError") {
return response.status(400).send({ error: "malformatted id" });
}

next(error);
};

const unknownEndpoint = (request, response) => {
response.status(404).send({ error: "unknown endpoint" });
};

// tämä tulee kaikkien muiden middlewarejen rekisteröinnin jälkeen!
app.use(errorHandler);
app.use(unknownEndpoint);

const PORT = process.env.PORT || 3001;
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
Expand Down
20 changes: 17 additions & 3 deletions requests/index.rest
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ http://localhost:3001/info

###

http://localhost:3001/api/persons/64d689f781e89ffddfd8f8c0
http://localhost:3001/api/persons/64d665036a502c419c7c4555

###

Expand All @@ -20,10 +20,24 @@ POST http://localhost:3001/api/persons
Content-Type: application/json

{
"name": "Testi Henkilö",
"name": "Testi Henkilöz",
"number": "044 123 4567"
}

###

DELETE http://localhost:3001/api/persons/64d68c8a95a8a05db2b8206b
PUT http://localhost:3001/api/persons/64d69ed4be96156257024f0c
Content-Type: application/json

{
"name": "ABCD",
"number": "044 123 4567"
}

###

DELETE http://localhost:3001/api/persons/64d68c8a95a8a05db2b8206b

###

http://localhost:3001/api/xxx

0 comments on commit 0323e60

Please sign in to comment.