최재은

Link mongodb and set environment & model for the project

PORT=4500
MONGO_URI=mongodb://localhost/facebookbot
\ No newline at end of file
const mongoose = require('mongoose');
// Define Schemes
const lectureSchema = new mongoose.Schema({
lectureid: { type: String, unique: true },
lecturename: { type: String},
proname: { type: String },
avg_rate: { type: Number }
});
const reviewSchema = new mongoose.Schema({
lectureid: { type: String, required: true, unique: true },
review: { type: String, required: true }
});
// // Create new lecture document
lectureSchema.statics.create = function (payload) {
// this === Model
const lecture = new this(payload);
// return Promise
return lecture.save();
};
// Find All
lectureSchema.statics.findAll = function () {
// return promise
// V4부터 exec() 필요없음
return this.find({});
};
// Find One by lectureid
lectureSchema.statics.findOneBylectureid = function (lectureid) {
return this.findOne({ lectureid });
};
// Update by lectureid
lectureSchema.statics.updateBylectureid = function (lectureid, payload) {
// { new: true }: return the modified document rather than the original. defaults to false
return this.findOneAndUpdate({ lectureid }, payload, { new: true });
};
// Delete by lectureid
lectureSchema.statics.deleteBylectureid = function (lectureid) {
return this.remove({ lectureid });
};
// Create Model & Export
module.exports = mongoose.model('Lecture', lectureSchema);
const mongoose = require('mongoose');
const reviewSchema = new mongoose.Schema({
lectureid: { type: String, required: true, unique: true },
review: { type: String, required: true }
});
// // Create new review document
reviewSchema.statics.create = function (payload) {
// this === Model
const review = new this(payload);
// return Promise
return review.save();
};
// Find All
reviewSchema.statics.findAll = function () {
// return promise
// V4부터 exec() 필요없음
return this.find({});
};
// Find One by reviewid
reviewSchema.statics.findOneByreviewid = function (reviewid) {
return this.findOne({ reviewid });
};
// Update by reviewid
reviewSchema.statics.updateByreviewid = function (reviewid, payload) {
// { new: true }: return the modified document rather than the original. defaults to false
return this.findOneAndUpdate({ reviewid }, payload, { new: true });
};
// Delete by reviewid
reviewSchema.statics.deleteByreviewid = function (reviewid) {
return this.remove({ reviewid });
};
// Create Model & Export
module.exports = mongoose.model('Review', reviewSchema);
\ No newline at end of file
......@@ -177,6 +177,11 @@
"resolved": "https://registry.npmjs.org/destroy/-/destroy-1.0.4.tgz",
"integrity": "sha1-l4hXRCxEdJ5CBmE+N5RiBYJqvYA="
},
"dotenv": {
"version": "8.0.0",
"resolved": "https://registry.npmjs.org/dotenv/-/dotenv-8.0.0.tgz",
"integrity": "sha512-30xVGqjLjiUOArT4+M5q9sYdvuR4riM6yK9wMcas9Vbp6zZa+ocC9dp6QoftuhTPhFAiLK/0C5Ni2nou/Bk8lg=="
},
"ecc-jsbn": {
"version": "0.1.2",
"resolved": "https://registry.npmjs.org/ecc-jsbn/-/ecc-jsbn-0.1.2.tgz",
......
......@@ -11,6 +11,7 @@
"license": "ISC",
"dependencies": {
"body-parser": "^1.19.0",
"dotenv": "^8.0.0",
"express": "^4.17.0",
"mongoose": "^5.5.11",
"request": "^2.88.0"
......
const router = require('express').Router();
const Lecture = require('../models/lecture');
// Find All
router.get('/', (req, res) => {
Lecture.findAll()
.then((lectures) => {
if (!lectures.length) return res.status(404).send({ err: 'Lecture not found' });
res.send(`find successfully: ${lectures}`);
})
.catch(err => res.status(500).send(err));
});
// Find One by lectureid
router.get('/lectureid/:lectureid', (req, res) => {
Lecture.findOneBylectureid(req.params.lectureid)
.then((lecture) => {
if (!lecture) return res.status(404).send({ err: 'Lecture not found' });
res.send(`findOne successfully: ${lecture}`);
})
.catch(err => res.status(500).send(err));
});
// Create new lecture document
router.post('/', (req, res) => {
console.log(req.body)
Lecture.create(req.body)
.then(lecture => res.send(lecture))
.catch(err => res.status(500).send(err));
});
// Update by lectureid
router.put('/lectureid/:lectureid', (req, res) => {
Lecture.updateBylectureid(req.params.lectureid, req.body)
.then(lecture => res.send(lecture))
.catch(err => res.status(500).send(err));
});
// Delete by lectureid
router.delete('/lectureid/:lectureid', (req, res) => {
Lecture.deleteBylectureid(req.params.lectureid)
.then(() => res.sendStatus(200))
.catch(err => res.status(500).send(err));
});
module.exports = router;
\ No newline at end of file
const router = require('express').Router();
const Review = require('../models/review');
// Find All
router.get('/', (req, res) => {
Review.findAll()
.then((reviews) => {
if (!reviews.length) return res.status(404).send({ err: 'Review not found' });
res.send(`find successfully: ${reviews}`);
})
.catch(err => res.status(500).send(err));
});
// Find One by reviewid
router.get('/reviewid/:reviewid', (req, res) => {
Review.findOneByreviewid(req.params.reviewid)
.then((review) => {
if (!review) return res.status(404).send({ err: 'Review not found' });
res.send(`findOne successfully: ${review}`);
})
.catch(err => res.status(500).send(err));
});
// Create new review document
router.post('/', (req, res) => {
console.log(req.body)
Review.create(req.body)
.then(review => res.send(review))
.catch(err => res.status(500).send(err));
});
// Update by reviewid
router.put('/reviewid/:reviewid', (req, res) => {
Review.updateByreviewid(req.params.reviewid, req.body)
.then(review => res.send(review))
.catch(err => res.status(500).send(err));
});
// Delete by reviewid
router.delete('/reviewid/:reviewid', (req, res) => {
Review.deleteByreviewid(req.params.reviewid)
.then(() => res.sendStatus(200))
.catch(err => res.status(500).send(err));
});
module.exports = router;
\ No newline at end of file
// ENV
require('dotenv').config();
// DEPENDENCIES
const express = require('express');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const app = express();
const port = process.env.PORT || 4500;
// Static File Service
app.use(express.static('public'));
// Body-parser
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
// Node의 native Promise 사용
mongoose.Promise = global.Promise;
// Connect to MongoDB
mongoose.connect(process.env.MONGO_URI)
.then(() => console.log('Successfully connected to mongodb'))
.catch(e => console.error(e));
// ROUTERS
app.use('/lectures', require('./routes/lectures'));
app.use('/reviews', require('./routes/reviews'));
app.listen(port, () => console.log(`Server listening on port ${port}`));
\ No newline at end of file