writes.js 1.51 KB
Newer Older
Kim, Subin's avatar
Kim, Subin committed
1
2
3
4
5
6
7
8
9
10
11
12
const express = require('express');
// const path = require('path');
// const multer = require('multer');
// const fs = require('fs');
const Notice = require('../schemas/notice');

const router = express.Router();

router.post('/', function (req, res, next) {
    console.log("writes req.body", req.body)
    const notice = new Notice({
        notice_title: req.body.title,
Kim, Subin's avatar
Kim, Subin committed
13
        notice_author: req.body.name,
Kim, Subin's avatar
Kim, Subin committed
14
15
16
17
18
19
20
21
22
23
24
25
26
27
        notice_content: req.body.content
    });
    console.log(notice);
    notice.save()
        .then((result) => {
            console.log(result);
            res.status(201).json(result);
        })
        .catch((err) => {
            console.error(err);
            next(err);
        });
});

28
router.put('/:id', function (req, res, next) {
Kim, Subin's avatar
Kim, Subin committed
29
    console.log('/writes put req.params', req.params.id)
30
31
    Notice.findOne({ _id: req.params.id }, function (err, notice) {
        if (err) return res.status(500).json({ error: err });
Kim, Subin's avatar
Kim, Subin committed
32
33
34
35
        if (req.body.title.indexOf("(수정)") === -1) {notice.notice_title = req.body.title + "  (수정)"}
        else {notice.notice_title = req.body.title}
        notice.post_date = new Date();
        notice.notice_author = req.body.name;
36
37
38
39
40
41
42
43
44
45
46
47
48
        notice.notice_content = req.body.content;
        notice.save()
            .then((result) => {
                console.log(result);
                res.status(201).json(result);
            })
            .catch((err) => {
                console.error(err);
                next(err);
            });
    });
});

Kim, Subin's avatar
Kim, Subin committed
49
module.exports = router;