page.js
4.28 KB
1
2
3
4
5
6
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
const Router = require("@koa/router");
const checkLoggedIn = require("../../src/lib/checkLoggedIn");
// const pageCtrl = require("./page.ctrl");
const Joi = require('@hapi/joi');
const page = new Router();
const Page = require("../models/page");
const User = require("../models/user");
//const index = require('../../../src/index');
//const render = require('koa-ejs');
//Page api
/*
page.get('/',pageCtrl.getPage); // show a list of user's pages
page.post('/',checkLoggedIn,pageCtrl.addPage); // add page
page.patch('/', checkLoggedIn,pageCtrl.updatePage); // modify page information
page.delete('/',checkLoggedIn,pageCtrl.deletePage); // delete book
page.get('/search', pageCtrl.search); // /search?title=search_query&petType=petType
//page.post('/search/filter', pageCtrl.searchFilter); //아직 구현 안함
page.post('/:id', pageCtrl.detailPage); // detail recipe page
//page.get('/recipe/slide', pageCtrl.slidRecipe); // 5 recommended videos in main page
// /recipe/scroll?filter=filter_query&renewal=count (filter_query: 추천순 or 조회순)
page.get('/recipe/scroll', pageCtrl.scrollPage); // video list sorted by 추천순 or 조회순 in main page
*/
//For test
page.get('/pagelist', async (ctx) => {
try{
const page = await Page.find({}).sort({createDate:-1}).exec();
ctx.body = page;
}catch(e){
ctx.throw(500, e);
}
});
page.get('/', async (ctx) => {
try{
const page = await Page.find({}).sort({createDate:-1}).exec();
console.log(page);
await ctx.render('posts/index', {page});
}catch(e){
ctx.throw(500, e);
}
});
page.get('/new', async (ctx) => {
try{
await ctx.render('posts/new');
}catch(e){
ctx.throw(500, e);
}
});
// create
page.put('/', checkLoggedIn, async (ctx, next) => {
const {
title,
author,
contents,
cover,
hashtag,
} = ctx.request.body;
const schema = Joi.object().keys({
title: Joi.string().required(),
author: Joi.string(),
contents: Joi.string().allow(null, ''),
hashtag: Joi.string().allow(null, ''),
cover: Joi.allow(null, ''),
});
try {
await schema.validateAsync(ctx.request.body);
} catch (err) {
console.log('add book validaton' + err);
ctx.status = 400;
return;
}
ctx.request.body.author = ctx.state.user;
const book = new Book(ctx.request.body);
try {
book.save(async (err) => {
if (err) throw err;
const user = await User.findById(ctx.state.user._id).exec();
console.log(book._id);
await ctx.redirect('/page');
});
} catch (e) {
ctx.throw(500, e);
}
console.log('저장 성공!');
ctx.status = 200;
await ctx.redirect('/page');
});
// show
page.get('/:id', async (ctx, next) => {
try{
var id = ctx.params.id;
const page = await Page.findById(id).exec();
const user = await User.findById(page.author).exec();
await ctx.render('posts/show', {page:page,user:user});
ctx.status = 200;
}catch(e){
ctx.throw(500,e);
}
});
// update
page.patch('/:id',checkLoggedIn, async (ctx, next) => {
const id = ctx.params.id;
if(ctx.request.files != undefined) // when user add a new pet image
ctx.request.body.image = fs.readFileSync(file.image.path);
else
ctx.request.body.image = ""
ctx.body.updateDate = Date.now();
var page = ctx.request.body;
try {
const mypage = await Page.findOne({ _id: id });//require page's _id
if(ctx.state.user._id == mypage.author){
await mypage.updateP(page);
console.log(mypage);
await ctx.redirect("/page/"+id);
ctx.status = 200;}
} catch (e) {
ctx.throw(500, e);
ctx.body = {
message: "작성자가 아닙니다. " }
}
});
// destroy
page.delete('/:id', async (ctx, next) => {
try{
const page = await Page.deleteOne({_id:ctx.params.id})
//await ctx.redirect('/page');
}catch(e){
ctx.throw(500, e);
}
});
//page.post('/postinfo', pageCtrl.uploadInfo);
//page.get('/info',pageCtrl.getbyurl);//url로 recipe정보 가져오기 (flutter 내 레시피에서 쓰임.)
module.exports = page;