page.ctrl.js
4.77 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
const Page = require("../../models/page");
const User = require("../../models/user");
const Book = require("../../models/book");
const Joi = require('@hapi/joi');
const config = require('../../lib/config');
const { Mongoose } = require('mongoose');
exports.search = async (ctx) => {
const { title } = ctx.query; //search word
console.log(title)
try {
var pages = await Page.findBySearchWord(title);
ctx.status = 200;
ctx.body = pages;
} catch (e) {
ctx.throw(500, e);
}
ctx.status = 200;
};
exports.detailPage = async (ctx) => {
try{
var ObjectId = require('mongodb').ObjectId;
var id = req.params.id;
var o_id = new ObjectId(id);
const page = await db.Page.find({_id:o_id});
await ctx.redirect('/api/page/detail/'+req.params.id);
ctx.body = page;
ctx.status = 200;
}catch(e){
ctx.throw(500,e);
}
};
// parameters: array with book objects as elements
// return image, title and author of book DB information
//list에 보여질 정보 일부분
async function bookInfo (arr) {
let result = []
arr.map(obj => {
temp = new Object();
temp.image = obj.image;
temp.title = obj.title;
temp.author = obj.author;
temp.views = obj.views;
result.push(temp);
delete temp;
})
return result;
}
async function pageInfo (arr) {
let result = []
arr.map(obj => {
temp = new Object();
temp.image = obj.image;
temp.title = obj.title;
temp.author = obj.author;
temp.views = obj.views;
result.push(temp);
delete temp;
})
return result;
}
exports.scrollPage = async (ctx) => {
//클라이언트에서 미리 요청하는 방법 사용하자
//select: 조회순 or 추천순
//renewal: 앱 화면 바닥에 도달한 횟수 (처음으로 데이터를 추가로 가져올때 1, 그다음 2 ...)
const {filter, renewal} = ctx.query
if(filter === "조회순") { //조회순
try {
const pages = await Page.find().sort({'views': -1}).skip(parseInt(renewal)*10).limit(10)
let result = await pageInfo(pages);
ctx.status = 200;
ctx.body = result;
} catch (e) {
ctx.throw(500, e);
}
}
else if(filter === "추천순") { //추천순
}
};
//pet
exports.addPage = async (ctx) => {
//const file = ctx.request.files;
// console.log("file\n", file)
// console.log(file.image.path)
//ctx.request.body.image = fs.readFileSync(file.image.path);
if( ctx.request.files==undefined)
console.log("파일없음");
else
ctx.request.body.cover = ""
const {
title,
author,
contents,
cover,
hashtag,
book,
} = ctx.request.body;
const schema = Joi.object().keys({
title: Joi.string().required(),
author: Joi.string(),
contents: Joi.string().required(),
hashtag: Joi.string().allow(null, ''),
cover: Joi.allow(null, ''),
book: Joi.string().allow(null, ''),
});
//검증 결과
try {
await schema.validateAsync(ctx.request.body);
} catch (err) {
console.log('add page validaton' + err);
ctx.status = 400;
return;
}
ctx.request.body.author = ctx.state.user;
const page = new Page(ctx.request.body);
try {
page.save(async (err, page) => {
if (err) throw err;
const user = await User.findById(ctx.state.user._id).exec();
if(page.book!='')
{
const result = await Book.findOneAndUpdate({_id: page.book}, {$push: {pages: page._id}});
console.log(result);
}
});
} catch (e) {
ctx.throw(500, e);
}
ctx.body = page;
ctx.status = 200;
};
exports.updatePage = async (ctx) => {
// validation추가 필요
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 = ""
var page = ctx.request.body;
try {
const mypage = await Page.findOne({ _id: page._id });//require page's _id
if(ctx.state.user._id == mypage.author){
mypage.updateP(book);
ctx.body = mypage;
console.log(mypage);
ctx.status = 200;}
} catch (e) {
ctx.throw(500, e);
ctx.body = {
message: "작성자가 아닙니다. " }
}
ctx.status = 200;
};
exports.deletePage = async (ctx) => {
try {
var b = await Page.find(ctx.params.id);
await Page.deleteMany({"pages":{$in:b.pages}});
} catch (e) {
if(e.name === 'CastError') {
ctx.status = 400;
return;
}
}
ctx.status = 204; // No Content
};