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
2 changes: 2 additions & 0 deletions backend/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
.env
51 changes: 51 additions & 0 deletions backend/Routes/cardRoutes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
const express = require('express');
const Card = require('../model/card');

const router = express.Router();

// Create a new card
router.post('/cards', async (req, res) => {
try {
const { title, description } = req.body;
if (!title || !description) {
return res.status(400).json({ error: 'Title and description are required.' });
}
const newCard = new Card({ title, description });
await newCard.save();
res.status(201).json(newCard);
} catch (error) {
if (error.code === 11000) {
// Duplicate key error
res.status(409).json({ error: 'A card with this title already exists.' });
} else {
res.status(500).json({ error: 'Internal server error.' });
}
}
});

// Retrieve all cards
router.get('/cards', async (req, res) => {
try {
const cards = await Card.find();
res.json(cards);
} catch (error) {
res.status(500).json({ error: 'Internal server error.' });
}
});

// Retrieve a specific card by title
router.get('/cards/:title', async (req, res) => {
try {
const card = await Card.findOne({ title: req.params.title });
if (!card) {
return res.status(404).json({ error: 'Card not found.' });
}
res.json(card);
} catch (error) {
res.status(500).json({ error: 'Internal server error.' });
}
});

module.exports = router;


12 changes: 12 additions & 0 deletions backend/dbConnection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const mongoose = require('mongoose');

const connectDB = async () => {
try {
await mongoose.connect(process.env.MONGO_URL);
console.log(`mongoDb connected ${mongoose.connection.host}`)
} catch (error) {
console.log('mongoDb connection error')
}
}

module.exports = connectDB;
30 changes: 30 additions & 0 deletions backend/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const express = require('express');
const morgan = require('morgan');
const dotenv = require('dotenv');
const connectDB = require('./dbConnection');
const path = require("path");

const cardRoutes = require('./Routes/cardRoutes')

dotenv.config();

connectDB();

const app = express();

app.use(express.json());
app.use(morgan('dev'))
app.use(express.static(path.join(__dirname, './blog/build')))

app.use('/', cardRoutes)

app.get("*", (req, res) => {
res.sendFile(path.join(__dirname, './blog/build/index.html'));
})


const PORT = 8080;

app.listen(PORT, () => {
console.log('server is running on port 8080');
})
26 changes: 26 additions & 0 deletions backend/model/card.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// models/Card.js
const mongoose = require('mongoose');
const { Schema } = mongoose;

const cardSchema = new Schema({
id: {
type: String,
required: true,
unique: true,
default: () => new mongoose.Types.ObjectId().toString()
},
title: {
type: String,
required: true,
unique: true,
trim: true
},
description: {
type: String,
required: true,
trim: true
}
});

const Card = mongoose.model('Card', cardSchema);
module.exports = Card;
Loading