최재은

Link mongodb and set environment & model for the project

1 +PORT=4500
2 +
3 +MONGO_URI=mongodb://localhost/facebookbot
...\ No newline at end of file ...\ No newline at end of file
1 +const mongoose = require('mongoose');
2 +
3 +// Define Schemes
4 +const lectureSchema = new mongoose.Schema({
5 + lectureid: { type: String, unique: true },
6 + lecturename: { type: String},
7 + proname: { type: String },
8 + avg_rate: { type: Number }
9 +});
10 +
11 +const reviewSchema = new mongoose.Schema({
12 + lectureid: { type: String, required: true, unique: true },
13 + review: { type: String, required: true }
14 +});
15 +
16 +// // Create new lecture document
17 +lectureSchema.statics.create = function (payload) {
18 + // this === Model
19 + const lecture = new this(payload);
20 + // return Promise
21 + return lecture.save();
22 +};
23 +
24 +// Find All
25 +lectureSchema.statics.findAll = function () {
26 + // return promise
27 + // V4부터 exec() 필요없음
28 + return this.find({});
29 +};
30 +
31 +// Find One by lectureid
32 +lectureSchema.statics.findOneBylectureid = function (lectureid) {
33 + return this.findOne({ lectureid });
34 +};
35 +
36 +// Update by lectureid
37 +lectureSchema.statics.updateBylectureid = function (lectureid, payload) {
38 + // { new: true }: return the modified document rather than the original. defaults to false
39 + return this.findOneAndUpdate({ lectureid }, payload, { new: true });
40 +};
41 +
42 +// Delete by lectureid
43 +lectureSchema.statics.deleteBylectureid = function (lectureid) {
44 + return this.remove({ lectureid });
45 +};
46 +
47 +// Create Model & Export
48 +module.exports = mongoose.model('Lecture', lectureSchema);
1 +const mongoose = require('mongoose');
2 +
3 +
4 +const reviewSchema = new mongoose.Schema({
5 + lectureid: { type: String, required: true, unique: true },
6 + review: { type: String, required: true }
7 +});
8 +
9 +// // Create new review document
10 +reviewSchema.statics.create = function (payload) {
11 + // this === Model
12 + const review = new this(payload);
13 + // return Promise
14 + return review.save();
15 +};
16 +
17 +// Find All
18 +reviewSchema.statics.findAll = function () {
19 + // return promise
20 + // V4부터 exec() 필요없음
21 + return this.find({});
22 +};
23 +
24 +// Find One by reviewid
25 +reviewSchema.statics.findOneByreviewid = function (reviewid) {
26 + return this.findOne({ reviewid });
27 +};
28 +
29 +// Update by reviewid
30 +reviewSchema.statics.updateByreviewid = function (reviewid, payload) {
31 + // { new: true }: return the modified document rather than the original. defaults to false
32 + return this.findOneAndUpdate({ reviewid }, payload, { new: true });
33 +};
34 +
35 +// Delete by reviewid
36 +reviewSchema.statics.deleteByreviewid = function (reviewid) {
37 + return this.remove({ reviewid });
38 +};
39 +
40 +// Create Model & Export
41 +module.exports = mongoose.model('Review', reviewSchema);
...\ No newline at end of file ...\ No newline at end of file
...@@ -177,6 +177,11 @@ ...@@ -177,6 +177,11 @@
177 "resolved": "https://registry.npmjs.org/destroy/-/destroy-1.0.4.tgz", 177 "resolved": "https://registry.npmjs.org/destroy/-/destroy-1.0.4.tgz",
178 "integrity": "sha1-l4hXRCxEdJ5CBmE+N5RiBYJqvYA=" 178 "integrity": "sha1-l4hXRCxEdJ5CBmE+N5RiBYJqvYA="
179 }, 179 },
180 + "dotenv": {
181 + "version": "8.0.0",
182 + "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-8.0.0.tgz",
183 + "integrity": "sha512-30xVGqjLjiUOArT4+M5q9sYdvuR4riM6yK9wMcas9Vbp6zZa+ocC9dp6QoftuhTPhFAiLK/0C5Ni2nou/Bk8lg=="
184 + },
180 "ecc-jsbn": { 185 "ecc-jsbn": {
181 "version": "0.1.2", 186 "version": "0.1.2",
182 "resolved": "https://registry.npmjs.org/ecc-jsbn/-/ecc-jsbn-0.1.2.tgz", 187 "resolved": "https://registry.npmjs.org/ecc-jsbn/-/ecc-jsbn-0.1.2.tgz",
......
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
11 "license": "ISC", 11 "license": "ISC",
12 "dependencies": { 12 "dependencies": {
13 "body-parser": "^1.19.0", 13 "body-parser": "^1.19.0",
14 + "dotenv": "^8.0.0",
14 "express": "^4.17.0", 15 "express": "^4.17.0",
15 "mongoose": "^5.5.11", 16 "mongoose": "^5.5.11",
16 "request": "^2.88.0" 17 "request": "^2.88.0"
......
1 +const router = require('express').Router();
2 +const Lecture = require('../models/lecture');
3 +
4 +// Find All
5 +router.get('/', (req, res) => {
6 + Lecture.findAll()
7 + .then((lectures) => {
8 + if (!lectures.length) return res.status(404).send({ err: 'Lecture not found' });
9 + res.send(`find successfully: ${lectures}`);
10 + })
11 + .catch(err => res.status(500).send(err));
12 +});
13 +
14 +// Find One by lectureid
15 +router.get('/lectureid/:lectureid', (req, res) => {
16 + Lecture.findOneBylectureid(req.params.lectureid)
17 + .then((lecture) => {
18 + if (!lecture) return res.status(404).send({ err: 'Lecture not found' });
19 + res.send(`findOne successfully: ${lecture}`);
20 + })
21 + .catch(err => res.status(500).send(err));
22 +});
23 +
24 +// Create new lecture document
25 +router.post('/', (req, res) => {
26 + console.log(req.body)
27 + Lecture.create(req.body)
28 + .then(lecture => res.send(lecture))
29 + .catch(err => res.status(500).send(err));
30 +});
31 +
32 +// Update by lectureid
33 +router.put('/lectureid/:lectureid', (req, res) => {
34 + Lecture.updateBylectureid(req.params.lectureid, req.body)
35 + .then(lecture => res.send(lecture))
36 + .catch(err => res.status(500).send(err));
37 +});
38 +
39 +// Delete by lectureid
40 +router.delete('/lectureid/:lectureid', (req, res) => {
41 + Lecture.deleteBylectureid(req.params.lectureid)
42 + .then(() => res.sendStatus(200))
43 + .catch(err => res.status(500).send(err));
44 +});
45 +
46 +module.exports = router;
...\ No newline at end of file ...\ No newline at end of file
1 +const router = require('express').Router();
2 +const Review = require('../models/review');
3 +
4 +// Find All
5 +router.get('/', (req, res) => {
6 + Review.findAll()
7 + .then((reviews) => {
8 + if (!reviews.length) return res.status(404).send({ err: 'Review not found' });
9 + res.send(`find successfully: ${reviews}`);
10 + })
11 + .catch(err => res.status(500).send(err));
12 +});
13 +
14 +// Find One by reviewid
15 +router.get('/reviewid/:reviewid', (req, res) => {
16 + Review.findOneByreviewid(req.params.reviewid)
17 + .then((review) => {
18 + if (!review) return res.status(404).send({ err: 'Review not found' });
19 + res.send(`findOne successfully: ${review}`);
20 + })
21 + .catch(err => res.status(500).send(err));
22 +});
23 +
24 +// Create new review document
25 +router.post('/', (req, res) => {
26 + console.log(req.body)
27 + Review.create(req.body)
28 + .then(review => res.send(review))
29 + .catch(err => res.status(500).send(err));
30 +});
31 +
32 +// Update by reviewid
33 +router.put('/reviewid/:reviewid', (req, res) => {
34 + Review.updateByreviewid(req.params.reviewid, req.body)
35 + .then(review => res.send(review))
36 + .catch(err => res.status(500).send(err));
37 +});
38 +
39 +// Delete by reviewid
40 +router.delete('/reviewid/:reviewid', (req, res) => {
41 + Review.deleteByreviewid(req.params.reviewid)
42 + .then(() => res.sendStatus(200))
43 + .catch(err => res.status(500).send(err));
44 +});
45 +
46 +module.exports = router;
...\ No newline at end of file ...\ No newline at end of file
1 +// ENV
2 +require('dotenv').config();
3 +
4 +// DEPENDENCIES
5 +const express = require('express');
6 +const mongoose = require('mongoose');
7 +const bodyParser = require('body-parser');
8 +
9 +const app = express();
10 +const port = process.env.PORT || 4500;
11 +
12 +// Static File Service
13 +app.use(express.static('public'));
14 +// Body-parser
15 +app.use(bodyParser.urlencoded({ extended: true }));
16 +app.use(bodyParser.json());
17 +
18 +// Node의 native Promise 사용
19 +mongoose.Promise = global.Promise;
20 +
21 +// Connect to MongoDB
22 +mongoose.connect(process.env.MONGO_URI)
23 + .then(() => console.log('Successfully connected to mongodb'))
24 + .catch(e => console.error(e));
25 +
26 +// ROUTERS
27 +app.use('/lectures', require('./routes/lectures'));
28 +app.use('/reviews', require('./routes/reviews'));
29 +app.listen(port, () => console.log(`Server listening on port ${port}`));
...\ No newline at end of file ...\ No newline at end of file