Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 101 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
const express = require("express");
const app = express();
const cors = require("cors");
app.use(express.json());
app.use(cors());

let chatList = require("./data.js");

let newID = require("uuid4");

const port = 1000;

//* To get all chats
app.get("/chats", (req, res) => {
res.json({ chatList });
});

//* To get a chat based on a username.
app.get("/chats/:username", (req, res) => {
try {
const username = req.params.username.trim();

const chatText = chatList.find(
(list) => list.username === username.toLowerCase()
);

if (chatText) {
res.json(chatText);
} else {
res.status(404).json({ error: "Chat not found" });
}
} catch (error) {
res.status(500).json({ error: "SERVER ERROR" });
}
});

//* For the user to create a chat
app.post("/chats", (req, res) => {
const { username, text } = req.body;

if (!username) {
res.status(401).json({ error: "username is required" });
return;
}
if (!text) {
res.status(401).json({ error: "text is required" });
return;
}

const newChat = { ...req.body, id: newID() };
chatList.push(newChat);
res.json({ chatList });

try {
} catch (error) {
res.status(500).json({ error: "SERVER ERROR" });
}
});

//* For the user to update the text field and return the updated chat
app.put("/chats/:id", (req, res) => {
try {
const { id } = req.params;
const { text } = req.body;

const chatMessage = chatList.find((chat) => chat.id === id);

if (!chatMessage) {
return res.status(404).json({ error: "Chat not found" });
}

chatMessage.text = text;

res.json(chatMessage);
} catch (error) {
res.status(500).json({ error: "SERVER ERROR" });
}
});

//* For the user to Delete a Chat based on ID
app.delete("/chats/:id", (req, res) => {
try {
const id = req.params.id;

if (!id) {
res.json({ error: "Invalid ID" });
return;
}

chatList = chatList.filter((list) => {
return list.id !== id;
});

res.json(chatList);
} catch (error) {
res.status(500).json({ error: "SERVER ERROR" });
}
});
app.listen(port, () => {
console.log(`This server runs in port ${port}`);
});
28 changes: 22 additions & 6 deletions data.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,24 @@
const cahtList = [
{ id: 1, text: "Why did the scarecrow win an award? Because he was outstanding in his field!", username: "jasmia" },
{ id: 2, text: "What do you call fake spaghetti? An impasta!", username: "hamza" },
{ id: 3, text: "Why don't skeletons fight each other? They don't have the guts!", username: "kadhmia" },
{ id: 4, text: "What did one plate say to the other plate? Dinner's on me!", username: "john" }
const chatList = [
{
username: "jasmia",
text: "Why did the scarecrow win an award? Because he was outstanding in his field!",
id: "13a5663d-1000-41ad-aff2-c3d7263632f1",
},
{
username: "hamza",
text: "What do you call fake spaghetti? An impasta!",
id: "8e455717-0c43-462e-80de-7b149ad2eef3",
},
{
username: "kadhmia",
text: "Why don't skeletons fight each other? They don't have the guts!",
id: "710c4859-2dbf-43e9-88b8-232066405f46",
},
{
username: "john",
text: "What did one plate say to the other plate? Dinner's on me!",
id: "77f23a28-b548-4aaa-be1d-78b95583a705",
},
];

module.exports = cahtList
module.exports = chatList;
Loading