-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmicro.js
155 lines (141 loc) · 3.9 KB
/
micro.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
import express from 'express';
import mongoose from 'mongoose';
import { updateTagsOnCreate, updateTagsOnEdit } from '../helpers/tags.js';
import { ShortformEntry } from '../models/entry.model.js';
import { FeedEntryMicro } from '../models/feed/feedEntry.model.js';
import MicroEntry from '../models/micro/microEntry.model.js';
import { authenticated } from './auth.js';
const router = express.Router();
router.post(
'/entry',
authenticated({ redirectTo: '/micro/login' }),
async function (req, res) {
try {
// Update tags (create new ones if necessary, increment usage frequency)
// Has to happen before the entry is created, or else we don't have the tag IDs to reference
const tags = await updateTagsOnCreate(req.body.tags, 'micro');
// Make entry
const microEntryId = new mongoose.Types.ObjectId();
const entryPromise = new MicroEntry({
...req.body,
tags,
_id: microEntryId,
}).save();
// Add entry to feed
const feedEntryPromise = new FeedEntryMicro({
type: 'micro',
micro: microEntryId,
tags,
}).save();
const [entryResult] = await Promise.all([entryPromise, feedEntryPromise]);
res.json(entryResult);
} catch (err) {
console.log(err);
res.status(500).send({ error: err });
}
},
);
router.post(
'/entry/:id',
authenticated({ redirectTo: '/micro/login' }),
async function (req, res) {
try {
const entry = await MicroEntry.findById(req.params.id);
const newEntry = req.body;
newEntry.tags = await updateTagsOnEdit(
entry.tags,
req.body.tags,
'micro',
);
if (!entry) {
res.sendStatus(404);
return;
}
Object.keys(newEntry)
.filter(
(key) => !['_id', 'createdAt', 'updatedAt', '__v'].includes(key),
)
.forEach((key) => {
if (entry[key] !== newEntry[key]) {
entry[key] = newEntry[key];
}
});
const entryResult = await entry.save();
res.json(entryResult);
} catch (err) {
console.log(err);
res.status(500).send({ error: err });
}
},
);
router.delete(
'/entry/:id',
authenticated({ redirectTo: '/micro/login' }),
async function (req, res) {
const microEntry = await MicroEntry.findById(req.params.id);
const feedEntry = await FeedEntryMicro.findOne({ micro: req.params.id });
if (!microEntry) {
res.sendStatus(404);
return;
} else {
// Decrement tag frequency
await updateTagsOnEdit(microEntry.tags, [], 'micro');
// Clear micro entry
for (let key of [
'title',
'post',
'tags',
'relatedPost',
'relatedPostModel',
'socialLinks',
]) {
microEntry[key] = undefined;
}
microEntry.deletedAt = new Date();
// Clear feed entry
feedEntry.tags = undefined;
feedEntry.deletedAt = new Date();
await Promise.all([microEntry.save(), feedEntry.save()]);
res.sendStatus(204);
}
},
);
// Get a single entry
router.get('/entry/:slug', async (req, res) => {
const entry = await MicroEntry.findOne({ slug: req.params.slug });
if (entry) {
res.json(entry);
} else {
res.sendStatus(404);
}
});
// Get related posts for dropdown
router.get('/relatedPosts', async (req, res) => {
const results = await ShortformEntry.aggregate([
{ $sort: { entryAdded: -1 } },
{ $limit: 10 },
{
$project: {
sortBy: '$entryAdded',
title: 1,
_id: 1,
type: 'ShortformEntry',
},
},
{
$unionWith: {
coll: 'press',
pipeline: [
{ $sort: { date: -1 } },
{ $limit: 5 },
{
$project: { sortBy: '$date', title: 1, _id: 1, type: 'PressEntry' },
},
],
},
},
{ $sort: { sortBy: -1 } },
]);
res.json(results);
});
export default router;