writes.js 1.41 KB
Newer Older
Yoon, Daeki's avatar
Yoon, Daeki committed
1
const express = require('express')
Yoon, Daeki's avatar
Yoon, Daeki committed
2
3
4
5
6
const Notice = require('../models/notice');

const router = express.Router();

router.post('/', function (req, res, next) {
Yoon, Daeki's avatar
Yoon, Daeki committed
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
    console.log("writes req.body", req.body)
    const notice = new Notice({
        notice_title: req.body.title,
        notice_author: req.body.name,
        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);
        });
});

router.put('/:id', function (req, res, next) {
    console.log('/writes put req.params', req.params.id)
    Notice.findOne({ _id: req.params.id }, function (err, notice) {
        if (err) return res.status(500).json({ error: err });
        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;
        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);
            });
Yoon, Daeki's avatar
Yoon, Daeki committed
43
44
45
46
    });
});

module.exports = router;