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
3 changes: 3 additions & 0 deletions backend/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules/

.env
76 changes: 76 additions & 0 deletions backend/cardRouter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
const express = require("express");
const Card = require("./db");
const router = express.Router();

router.post("/cards", async (req, res) => {
try {
let { title, description } = req.body;

// check if both title and description are present
if (!title || !description) {
return res.status(400).json({
message: "Title and description are required",
});
}
const cardExists = await Card.findOne({ title }); //check if card with same title does not exist already
if (cardExists) {
return res.status(400).json({
message: "Card already exists",
});
}
//create the card
await Card.create({
title,
description,
});
res.status(200).json({
message: "Card Created Successfully",
});
} catch (error) {
res.status(500).json({
message: "Internal server Error",
});
}
});

router.get("/cards", async (req, res) => {
try {
const cards = await Card.find({});
if (cards.length === 0) {
return res.status(200).json({
message: "No cards found",
});
}
res.status(200).json({
cards: cards,
});
} catch (error) {
res.status(500).json({
message: "Internal Server Error",
});
}
});

router.get("/cards/:title", async (req, res) => {
try {
const title = req.params.title;
console.log(title);
const card = await Card.findOne({ title });

if (!card) {
return res.status(404).json({
message: "Card not found",
});
}

res.status(200).json({
card: card,
});
} catch (error) {
res.status(500).json({
message: "Internal Server Error",
});
}
});

module.exports = router;
29 changes: 29 additions & 0 deletions backend/db.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const mongoose = require("mongoose");
require("dotenv").config();

mongoose
.connect(`${process.env.MONGODB_URL}`)
.then(() => {
console.log("Connected to DB");
})
.catch((err) => {
console.error("Error connecting to DB:", err);
});

const cardSchema = new mongoose.Schema({
title: {
type: String,
required: true,
unique: true,
},
description: {
type: String,
required: true,
},
});



const Card = new mongoose.model("Card", cardSchema);

module.exports = Card;
16 changes: 16 additions & 0 deletions backend/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const express = require("express");
const cors = require("cors");
const cardRouter = require("./cardRouter");
require("dotenv").config();

const app = express();
app.use(express.json());
app.use(cors());

app.use("/", cardRouter);

const PORT = process.env.PORT || 3000;

app.listen(PORT, () => {
console.log("The server is Up and Running on PORT:" + PORT);
});
Loading