-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: ramprasathmk <[email protected]>
- Loading branch information
1 parent
5b5e408
commit 4676c0d
Showing
8 changed files
with
135 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,48 @@ | ||
const Poem = require('../models/poem'); | ||
const Poem = require('../models/poem') | ||
|
||
// Error handling middleware wrapper | ||
const asyncHandler = (fn) => { | ||
return (req, res, next) => { | ||
fn(req, res, next).catch(next); | ||
}; | ||
}; | ||
return (req, res, next) => { | ||
fn(req, res, next).catch(next) | ||
} | ||
} | ||
|
||
exports.getAllPoems = asyncHandler(async (req, res) => { | ||
const poems = await Poem.find(); | ||
res.render('poems', { poems }); | ||
}); | ||
const poems = await Poem.find() | ||
res.render('poems', { poems }) | ||
}) | ||
|
||
exports.createPoem = asyncHandler(async (req, res) => { | ||
const { title, body, author } = req.body; | ||
await Poem.create({ title, body, author }); | ||
res.redirect('/poems'); | ||
}); | ||
const { title, body, author } = req.body | ||
await Poem.create({ title, body, author }) | ||
res.redirect('/poems') | ||
}) | ||
|
||
exports.getEditPoem = asyncHandler(async (req, res) => { | ||
const poem = await Poem.findById(req.params.id); | ||
if (!poem) { | ||
return res.status(404).send('Poem not found'); | ||
} | ||
res.render('edit', { poem }); | ||
}); | ||
const poem = await Poem.findById(req.params.id) | ||
if (!poem) { | ||
return res.status(404).send('Poem not found') | ||
} | ||
res.render('edit', { poem }) | ||
}) | ||
|
||
exports.updatePoem = asyncHandler(async (req, res) => { | ||
const { title, body, author } = req.body; | ||
const poem = await Poem.findByIdAndUpdate(req.params.id, { title, body, author }, { new: true }); | ||
if (!poem) { | ||
return res.status(404).send('Poem not found'); | ||
} | ||
res.redirect('/poems'); | ||
}); | ||
const { title, body, author } = req.body | ||
const poem = await Poem.findByIdAndUpdate( | ||
req.params.id, | ||
{ title, body, author }, | ||
{ new: true } | ||
) | ||
if (!poem) { | ||
return res.status(404).send('Poem not found') | ||
} | ||
res.redirect('/poems') | ||
}) | ||
|
||
exports.deletePoem = asyncHandler(async (req, res) => { | ||
const poem = await Poem.findByIdAndDelete(req.params.id); | ||
if (!poem) { | ||
return res.status(404).send('Poem not found'); | ||
} | ||
res.redirect('/poems'); | ||
}); | ||
const poem = await Poem.findByIdAndDelete(req.params.id) | ||
if (!poem) { | ||
return res.status(404).send('Poem not found') | ||
} | ||
res.redirect('/poems') | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
module.exports = { | ||
testEnvironment: 'node', | ||
testMatch: ['**/*.test.js'], | ||
globalSetup: './tests/setup.js', | ||
globalTeardown: './tests/teardown.js' | ||
}; | ||
testEnvironment: 'node', | ||
testMatch: ['**/*.test.js'], | ||
globalSetup: './tests/setup.js', | ||
globalTeardown: './tests/teardown.js' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,16 @@ | ||
const mongoose = require('mongoose'); | ||
const mongoose = require('mongoose') | ||
|
||
// poem Schema | ||
const poemSchema = new mongoose.Schema({ | ||
title: String, | ||
body: String, | ||
author: String, | ||
createdAt: { | ||
type: Date, | ||
default: Date.now | ||
} | ||
}); | ||
title: String, | ||
body: String, | ||
author: String, | ||
createdAt: { | ||
type: Date, | ||
default: Date.now | ||
} | ||
}) | ||
|
||
const Poem = mongoose.model('Poem', poemSchema); | ||
const Poem = mongoose.model('Poem', poemSchema) | ||
|
||
module.exports = Poem; | ||
module.exports = Poem |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,11 @@ | ||
const express = require('express'); | ||
const router = express.Router(); | ||
const poemsController = require('../controllers/poemsController'); | ||
const express = require('express') | ||
const router = express.Router() | ||
const poemsController = require('../controllers/poemsController') | ||
|
||
router.get('/', poemsController.getAllPoems); | ||
router.post('/', poemsController.createPoem); | ||
router.get('/:id/edit', poemsController.getEditPoem); | ||
router.post('/:id', poemsController.updatePoem); | ||
router.post('/:id/delete', poemsController.deletePoem); | ||
router.get('/', poemsController.getAllPoems) | ||
router.post('/', poemsController.createPoem) | ||
router.get('/:id/edit', poemsController.getEditPoem) | ||
router.post('/:id', poemsController.updatePoem) | ||
router.post('/:id/delete', poemsController.deletePoem) | ||
|
||
module.exports = router; | ||
module.exports = router |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,67 +1,68 @@ | ||
const path = require('node:path') | ||
const express = require("express"); | ||
const mongoose = require("mongoose"); | ||
const bodyParser = require("body-parser"); | ||
const cors = require("cors"); | ||
const compression = require('compression'); | ||
const dotenv = require("dotenv"); | ||
const Poem = require('./models/poem'); | ||
const poemRoutes = require("./routes/poems"); | ||
const express = require('express') | ||
const mongoose = require('mongoose') | ||
const bodyParser = require('body-parser') | ||
const cors = require('cors') | ||
const compression = require('compression') | ||
const dotenv = require('dotenv') | ||
const Poem = require('./models/poem') | ||
const poemRoutes = require('./routes/poems') | ||
|
||
dotenv.config(); | ||
dotenv.config() | ||
|
||
// Environment Variables | ||
const PORT = process.env.PORT; | ||
const DB_URL = process.env.MONGODB_URI; | ||
const PORT = process.env.PORT | ||
const DB_URL = process.env.MONGODB_URI | ||
|
||
// MongoDB Connection | ||
mongoose | ||
.connect(DB_URL) | ||
.then(() => console.log("Connected to MongoDB")) | ||
.catch((err) => console.error("Failed to connect to MongoDB", err)); | ||
.then(() => console.log('Connected to MongoDB')) | ||
.catch((err) => console.error('Failed to connect to MongoDB', err)) | ||
|
||
// Express Server | ||
const app = express(); | ||
app.use(compression()); | ||
app.use(cors({ | ||
origin: 'http://localhost:3000', | ||
methods: ["GET", "POST"], | ||
credentials: true | ||
})); | ||
app.use(bodyParser.urlencoded({ extended: true })); | ||
app.use(express.json()); | ||
app.use(express.static(path.join(__dirname, 'public'))); | ||
app.set("view engine", "ejs"); | ||
const app = express() | ||
app.use(compression()) | ||
app.use( | ||
cors({ | ||
origin: 'http://localhost:3000', | ||
methods: ['GET', 'POST'], | ||
credentials: true | ||
}) | ||
) | ||
app.use(bodyParser.urlencoded({ extended: true })) | ||
app.use(express.json()) | ||
app.use(express.static(path.join(__dirname, 'public'))) | ||
app.set('view engine', 'ejs') | ||
|
||
app.use('/poems', poemRoutes) | ||
|
||
app.use("/poems", poemRoutes); | ||
|
||
// Root Route | ||
app.get('/', (req, res) => { | ||
res.render('index'); | ||
}); | ||
// Root Route | ||
app.get('/', (req, res) => { | ||
res.render('index') | ||
}) | ||
|
||
// Home Route | ||
app.get('/home', (req, res) => { | ||
res.render('index'); | ||
}); | ||
app.get('/home', (req, res) => { | ||
res.render('index') | ||
}) | ||
|
||
// Search route | ||
app.get("/search", async (req, res) => { | ||
const query = req.query.query; | ||
app.get('/search', async (req, res) => { | ||
const query = req.query.query | ||
try { | ||
const poemsByTitle = await Poem.find({ title: new RegExp(query, "i") }); | ||
const poemsByAuthor = await Poem.find({ author: new RegExp(query, "i") }); | ||
res.render("search", { query, poemsByTitle, poemsByAuthor }); | ||
const poemsByTitle = await Poem.find({ title: new RegExp(query, 'i') }) | ||
const poemsByAuthor = await Poem.find({ author: new RegExp(query, 'i') }) | ||
res.render('search', { query, poemsByTitle, poemsByAuthor }) | ||
} catch (err) { | ||
console.error(err); | ||
res.status(500).send(err.message); | ||
console.error(err) | ||
res.status(500).send(err.message) | ||
} | ||
}); | ||
}) | ||
|
||
// Listen to the PORT | ||
const server = app.listen(PORT, () => { | ||
console.log(`Server is running on port \`${PORT}\``); | ||
}); | ||
console.log(`Server is running on port \`${PORT}\``) | ||
}) | ||
|
||
module.exports = { app, server }; | ||
module.exports = { app, server } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,31 @@ | ||
const request = require('supertest'); | ||
const { app, server } = require('../server'); | ||
const mongoose = require('mongoose'); | ||
const DB_URL = process.env.MONGODB_URI; | ||
const request = require('supertest') | ||
const { app, server } = require('../server') | ||
const mongoose = require('mongoose') | ||
const DB_URL = process.env.MONGODB_URI | ||
|
||
describe('GET /search', () => { | ||
it('should return 200 OK', async () => { | ||
const res = await request(app).get('/search?query=test'); | ||
expect(res.status).toBe(200); | ||
}); | ||
const res = await request(app).get('/search?query=test') | ||
expect(res.status).toBe(200) | ||
}) | ||
|
||
it('should return search results', async () => { | ||
const res = await request(app).get('/search?query=test'); | ||
expect(res.text).toContain('Search Results for'); | ||
}); | ||
}); | ||
const res = await request(app).get('/search?query=test') | ||
expect(res.text).toContain('Search Results for') | ||
}) | ||
}) | ||
|
||
afterAll(async () => { | ||
server.close(); | ||
await mongoose.connection.close(); | ||
}); | ||
|
||
server.close() | ||
await mongoose.connection.close() | ||
}) | ||
|
||
// MongoDB Database Connection Test | ||
describe('Database Connection', () => { | ||
it('should return database connection results', async () => { | ||
const database = await mongoose.connect(DB_URL); | ||
const database = await mongoose.connect(DB_URL) | ||
|
||
if (!database) | ||
throw mongoose.Error('Database is not Connected! :('); | ||
else console.log('MongoDB Connected Successfully :)'); | ||
}); | ||
}); | ||
if (!database) throw mongoose.Error('Database is not Connected! :(') | ||
else console.log('MongoDB Connected Successfully :)') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,11 @@ | ||
const mongoose = require('mongoose'); | ||
require('dotenv').config(); | ||
const mongoose = require('mongoose') | ||
require('dotenv').config() | ||
|
||
const DB_URL = process.env.MONGODB_URI; | ||
const DB_URL = process.env.MONGODB_URI | ||
|
||
module.exports = async () => { | ||
await mongoose.connect(DB_URL, { useNewUrlParser: true, useUnifiedTopology: true }); | ||
}; | ||
await mongoose.connect(DB_URL, { | ||
useNewUrlParser: true, | ||
useUnifiedTopology: true | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
const mongoose = require('mongoose'); | ||
const mongoose = require('mongoose') | ||
|
||
module.exports = async () => { | ||
await mongoose.connection.close(); | ||
}; | ||
await mongoose.connection.close() | ||
} |