Skip to content
This repository was archived by the owner on Aug 17, 2024. It is now read-only.

Conversation

Junitalama
Copy link

Volunteers: Are you marking this coursework? You can find a guide on how to mark this coursework in HOW_TO_MARK.md in the root of this repository

Your Details

  • Your Name:
  • Your City:
  • Your Slack Name:

Homework Details

  • Module:
  • Week:

Notes

  • What did you find easy?

  • What did you find hard?

  • What do you still not understand?

  • Any other notes?

Copy link

@ShayanMahnam ShayanMahnam left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great job, Junita! Your work is amazing. I'll provide some advice and feedback in further comments.

const messages = [welcomeMessage];

app.get("/", function (request, response) {
response.sendFile(__dirname + "/index.html");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have your own front end now you dont need this anymore


//send messages

app.post("/messages", function (request, response) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can do error handling like this and generate id for msgs here:(generate id here its just a example in real world we dont use this we will use uuid)

app.post("/messages", function (request, response) {
  const newMessage = request.body;

  // Check if the 'from' and 'text' properties are present in the request body
  if (!newMessage.from || !newMessage.text) {
    return response.status(400).json({ error: "Both 'from' and 'text' are required." });
  } else {
    // Generate a new unique id for the message
    newMessage.id = messages.length;
    messages.push(newMessage);
    response.status(201).json(newMessage);
    console.log(newMessage);
  }
});

response.json(messages);
});

app.get("/messages/:id", function (request, response) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can validate once more to see if the msg with that id is there

app.get("/messages/:id", function (request, response) {
  const idToFind = Number(request.params.id);
  const message = messages.find((msg) => msg.id === idToFind);

  // Check if the message with the provided ID exists
  if (message) {
    response.json(message);
  } else {
    response.status(404).json({ error: "Message not found." });
  }
});


//delete messages

app.delete("/messages/:id", function (request, response) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same check check if the message exists

app.delete("/messages/:id", function (request, response) {
  const idToDelete = Number(request.params.id);
  const messageIndex = messages.findIndex((msg) => msg.id === idToDelete);

  // Check if the message with the provided ID exists
  if (messageIndex !== -1) {
    const deletedMessage = messages.splice(messageIndex, 1);
    response.json(deletedMessage[0]);
  } else {
    response.status(404).json({ error: "Message not found." });
  }
});


//latest messages

app.get("/messages/latest", (req, res) => {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

app.get("/messages/latest", (req, res) => {
  const startIndex = Math.max(messages.length - 10, 0);
  const latestMessages = messages.slice(startIndex);
  res.json(latestMessages);
});

res.json(result);
});

app.get("/messages/search", (req, res) => {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

app.get("/messages/search", (req, res) => {
  const searchText = req.query.text.toLowerCase();
  const result = messages.filter((msg) => msg.text.toLowerCase().includes(searchText));
  res.json(result);
});

);
res.send(result);
});

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

its good to have some msgs for app.linsten to make sure when you run it, its working

const port = 8080;
app.listen(port, () => {
  console.log(`Server is running on http://localhost:${port}`);
});

@Junitalama
Copy link
Author

Great job, Junita! Your work is amazing. I'll provide some advice and feedback in further comments.

@ShayanMahnam Thank you so much for reviewing it.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants