-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.js
287 lines (255 loc) · 7.03 KB
/
db.js
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
const mongo = require("mongodb");
const { isObjectIdOrHexString, default: mongoose } = require("mongoose");
const Entry = require('./models/Post'); // 🟠 When the database is rebuilt, change to models/Entry
const User = require('./models/User');
const { limit, logError } = require("./util");
// * === error content
const errMsg = {
begin: "An error occurred when accessing ",
end: "It is logged for review. Apologies for any inconvenience.",
noResults: "There were no entries found using your request.",
pagination: `<strong class="warning">⚠ Pagination error.</strong> Try the Archive.`,
contact: `Contact your admin right away.`,
}
// * === returns archive date hierarchy in the sidebar
const getArchive = async (user) => {
const _now = new Date();
const filter = user ? { pubDate: {$ne: null } } : { publish: true, pubDate: { $lt: _now } };
let result;
try {
result = await Entry
.find(
filter,
{ title: 1, slug: 1, pubDate: 1 })
.sort({ pubDate: -1 })
.lean();
if(result.length === 0) throw { results: false };
} catch (err) {
// todo: add logging
result = {
error: true,
message: `${errMsg.begin} the archive data. ${errMsg.end}`
};
console.log(err); // 🟠
} finally {
return result;
}
}
// * === returns Topics section in the sidebar
const getCategories = async (user) => {
const _now = new Date();
const filter = user ? {} : { publish: true, pubDate: { $lt: _now } };
let result;
try {
result = await Entry
.find(
filter,
{ _id: 0, tags: 1 })
.lean();
if(result.length === 0) throw { results: false };
} catch (err) {
// todo: add logging
result = {
error: true,
message: `${errMsg.begin} the topics data. ${errMsg.end}`
}
} finally {
return result;
}
}
// * === returns ˙number of entries by topic or without topic
const getEntryCount = async (topic, user) => {
const _now = new Date();
const filterByTag = topic ? { tags: topic } : {};
const filterByUser = user ? {} : { publish: true, pubDate: { $lt: _now } };
let result;
try {
result = await Entry.countDocuments({ $and: [ filterByUser, filterByTag ] });
if(result === 0) throw { results: false };
} catch (err) {
// todo: add logging
result = {
error: true,
message: errMsg.pagination,
};
}
return result;
}
// * === returns Next/previoius navigation in the reader
const getAdjacents = async (date, user) => { // ! May be origin of unpub pagination error
let next, prev;
let filterNext = { publish: true };
let filterPrev = { publish: true };
if(!user) {
filterNext.pubDate = { $gt: date };
filterPrev.pubDate = { $lt: date };
}
try{
next = await Entry
.find(
filterNext,
{ slug: 1, title: 1 })
.lean()
.sort({ pubDate: 1 })
.limit(1);
prev = await Entry
.find(
filterPrev,
{ slug: 1, title: 1 })
.lean()
.sort({ pubDate: -1 })
.limit(1);
if(next.error || prev.error) throw { results: false };
return { next: next[0], prev: prev[0] };
} catch(err) {
// todo: add logging
return {
error: true,
message: errMsg.pagination,
}
}
}
// * === returns limited number of entries to populate list cards
const getListOfEntriesByDate = async (skip, user) => {
const _now = new Date();
const filter = user ? {} : { publish: true, pubDate: { $lt: _now } };
let result;
try {
result = await Entry
.find(filter)
.sort({ pubDate: -1 })
.skip(skip)
.limit(limit)
.lean();
if(result.length === 0) throw { results: 0 };
return result;
} catch(err) {
// todo: add logging
if(results = 0) {
result = [{
error: true,
message: errMsg.noResults
}];
return result;
}
result = [{
error: true,
message: `${errMsg.begin} the list of entries. ${errMsg.end}`,
}];
}
return result;
}
// * === returns unlimited entries by topic to populate list cards
const getListOfEntriesByCategory = async (topic, user) => {
const _now = new Date();
const filter = user ? { tags: topic } : { tags: topic, publish: true , pubDate: { $lt: _now } };
let result;
try {
result = await Entry
.find(filter)
.sort({ pubDate: -1 })
.lean();
if(result.length === 0 || result === null) throw { results: 0 };
return result;
} catch (err) {
// todo: add logging
if(err === 0) {
result = [{
error: true,
message: `${errMsg.begin} the topic requested. ${errMsg.end}`,
}];
return result;
}
result = [{
error: true,
message: `${errMsg.begin} the list of entries by topic. ${errMsg.end}`,
}];
}
return result;
}
// * === returns unlimited entries where publish is false to populate list cards
const getListOfUnpublishedEntries = async () => {
let result;
try {
result = await Entry
.find({ $or: [ {publish: false }, { pubDate: null } ] })
.lean()
.sort({ _id: -1 });
if(result.length === 0) throw { results: 0 };
} catch(err) {
// todo: add logging
if(err.results === 0){
result = [{
error: true,
description: `There appear to be no unplubished entries. Cool! Or, ${errMsg.title} the list of unpublished entries. ${errMsg.contact} ${errMsg.end}`,
}];
}
result = [{
error: true,
description: `${errMsg.begin} the entry you requested or the database. ${errMsg.contact} ${errMsg.end}`
}];
}
return result;
}
// * === returns one entry for reader or editor
const getOneEntry = async (slug, _id) => {
try {
const filter = slug ? { slug } : { _id };
const result = await Entry.findOne( filter ).lean();
if(!result) throw { results: 0 };
return result;
} catch (err) {
// todo: add logging
return {
error: true,
message: errMsg.noResults
}
}
}
// * === adds new entries or saves changes to exising
const addOrUpdateEntry = async (entry) => {
try {
await Entry.findOneAndUpdate(
entry.id ? { _id: entry.id } : {},
entry,
{ new: true, upsert: true }
);
return {
error: false,
message: "Entry data successfully saved to the database."
};
} catch(err){
// todo: add logging
return {
error: true,
message: `${errMsg.begin} the editor's entry saving process. ${errMsg.contact} ${errMsg.end}`
}
}
}
const deleteOneEntry = async _id => {
try {
await Entry.findByIdAndDelete( _id );
return {
error: false,
message: `Entry deleted. The action logged for review. Use [Upload] to create a new entry with a new ID.`
};
} catch (err) {
// todo: add logging
return {
error: true,
message: `${errMsg.begin} with deleting this entry. ${errMsg.contact} ${errMsg.end}`
};
}
}
module.exports = {
getArchive,
getCategories,
getAdjacents,
getEntryCount,
getListOfEntriesByDate,
getListOfEntriesByCategory,
getListOfUnpublishedEntries,
getOneEntry,
addOrUpdateEntry,
deleteOneEntry,
};