-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCommentDB.ts
More file actions
315 lines (297 loc) · 13.3 KB
/
Copy pathCommentDB.ts
File metadata and controls
315 lines (297 loc) · 13.3 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
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
import {SearchResultComment} from "../../../models/api/SearchResult";
import {Comment as APIComment} from "../../../models/api/Comment";
import {Comment, commentToAPI, DBAPIComment} from "../../../models/database/Comment";
import {submissionToAPI, DBAPISubmission} from "../../../models/database/Submission";
import {UUIDHelper} from "./helpers/UUIDHelper";
import {pool, extract, map, one, searchify, checkAvailable, pgDB, keyInMap, DBTools} from "./HelperDB";
import {commentsView} from "./ViewsDB";
/**
* Method attached to select statements to get information regarding comments from the database.
* commentID, commentThreadID, userID, created, edited, body
* @Author Rens Leendertz
*/
export class CommentDB {
//private static userselect = `name, email, globalRole`;
/**
*
* @param ids a list of comment thread IDs to retrieve the comments for
* @param client optional client object for when performing a transaction.
* @returns an object, with keys = ids, and values all comments made within that one comment thread.
*/
static async APIgetCommentsByThreads(ids: string[], client: pgDB = pool) {
const mapping: { [key: string]: APIComment[] } = {};
ids.forEach(element => {
mapping[element] = [];
});
const arg = [ids.map(UUIDHelper.toUUID)];
type argType = typeof arg;
const comments = await client.query<DBAPIComment, argType>(`
SELECT c.*
FROM "CommentsView" as c
WHERE c.commentThreadID = ANY($1)
`, arg)
.then(extract).then(map(commentToAPI));
comments.forEach(element => {
if (keyInMap(element.references.commentThreadID, mapping)) {
mapping[element.references.commentThreadID].push(element);
} else {
throw new Error("database concurrent modification exception");
}
});
return mapping;
}
/**
* All functions below are for this table (comments) only
*/
static async getAllComments(params: DBTools = {}) {
return CommentDB.filterComment(params);
}
static async getCommentsByThread(commentThreadID: string, params: DBTools = {}) {
return CommentDB.filterComment({...params, commentThreadID});
}
static async getCommentByID(commentID: string, client: pgDB = pool) {
return CommentDB.filterComment({commentID, client}).then(one);
}
static async getCommentsByThreadParticipation(
userID: string, courseID?: string, onlyReplies = false, params: DBTools = {}
) {
const {client = pool, limit = undefined, offset = undefined, after = undefined, before = undefined} = params;
const userid = UUIDHelper.toUUID(userID), courseid = UUIDHelper.toUUID(courseID);
return client.query(`
SELECT DISTINCT cv.* FROM "CommentsView" AS cv
JOIN "Comments" AS c ON c.commentThreadID = cv.commentThreadID
JOIN "CommentThreadView" AS ct ON ct.commentThreadID = cv.commentThreadID
WHERE c.userID = $1
AND ($2::uuid IS NULL OR cv.courseID = $2)
AND ($5::timestamp IS NULL OR cv.created > $5)
AND ($6::timestamp IS NULL OR cv.created < $6)
AND (NOT $7::boolean OR cv.created <> ct.created) -- only include replies, so comments that were created later
ORDER BY cv.created DESC
LIMIT $3 OFFSET $4
`, [userid, courseid, limit, offset, after, before, onlyReplies]).then(extract).then(map(commentToAPI));
}
static async getCommentsBySubmissionOwner(
submissionOwnerID: string, courseID?: string, onlyReplies = false, params: DBTools = {}
) {
const {client = pool, limit = undefined, offset = undefined, after = undefined, before = undefined} = params;
const userid = UUIDHelper.toUUID(submissionOwnerID), courseid = UUIDHelper.toUUID(courseID);
return client.query(`
SELECT cv.* FROM "CommentsView" AS cv
JOIN "Submissions" AS s ON s.submissionID = cv.submissionID
JOIN "CommentThreadView" AS ct ON ct.commentThreadID = cv.commentThreadID
WHERE s.userID = $1
AND ($2::uuid IS NULL OR cv.courseID = $2)
AND ($5::timestamp IS NULL OR cv.created > $5)
AND ($6::timestamp IS NULL OR cv.created < $6)
AND (NOT $7::boolean OR cv.created <> ct.created) -- only include replies, so comments that were created later
ORDER BY cv.created DESC
LIMIT $3 OFFSET $4
`, [userid, courseid, limit, offset, after, before, onlyReplies]).then(extract).then(map(commentToAPI));
}
/**
* return a subset of comments that pass the input filter
*
* @param comment contains everything to be filtered on.
* supplying a date filter willl return all comments new than that date.
* instead of literal comaprisons of the bodies, the body text will be searched
* 'limit' and 'offset' fields allow to manipulate the number of results
* the results are sorted by date, newest first
*/
static async filterComment(comment: Comment & { onlyReplies?: boolean }) {
const {
commentID = undefined,
commentThreadID = undefined,
submissionID = undefined,
courseID = undefined,
userID = undefined,
created = undefined,
edited = undefined,
body = undefined,
limit = undefined,
offset = undefined,
after = undefined,
before = undefined,
onlyReplies = false,
client = pool
} = comment;
const commentid = UUIDHelper.toUUID(commentID),
commentthreadid = UUIDHelper.toUUID(commentThreadID),
submissionid = UUIDHelper.toUUID(submissionID),
courseid = UUIDHelper.toUUID(courseID),
userid = UUIDHelper.toUUID(userID),
bodysearch = searchify(body);
const args = [commentid, commentthreadid, submissionid, courseid, userid,
created, edited, bodysearch, limit, offset, after, before, onlyReplies];
type argType = typeof args;
return client.query<DBAPIComment, argType>(`
SELECT c.*
FROM "CommentsView" as c
JOIN "CommentThreadView" AS ct ON ct.commentThreadID = c.commentThreadID
WHERE
($1::uuid IS NULL OR c.commentID=$1)
AND ($2::uuid IS NULL OR c.commentThreadID=$2)
AND ($3::uuid IS NULL OR c.submissionID=$3)
AND ($4::uuid IS NULL OR c.courseID=$4)
AND ($5::uuid IS NULL OR c.userID=$5)
AND ($6::timestamp IS NULL OR c.created >= $6)
AND ($7::timestamp IS NULL OR c.edited >= $7)
AND ($11::timestamp IS NULL OR c.created > $11)
AND ($12::timestamp IS NULL OR c.created < $12)
AND ($8::text IS NULL OR c.body ILIKE $8)
AND (NOT $13::boolean OR c.created <> ct.created) -- only include replies, so comments that were created later
ORDER BY c.created DESC, c.commentID --unique in case 2 comments same time
LIMIT $9
OFFSET $10
`, args)
.then(extract).then(map(commentToAPI));
}
/**
*
* @param searchString string to search for
* @param extras
* if a body is provided in extras, this will overwrite the searchstring.
*/
static async searchComments(searchString: string, extras: Comment): Promise<SearchResultComment[]> {
checkAvailable(["currentUserID", "courseID"], extras);
console.log(extras);
const {
commentID = undefined,
commentThreadID = undefined,
submissionID = undefined,
courseID = undefined,
userID = undefined,
created = undefined,
edited = undefined,
body = searchString,
limit = undefined,
offset = undefined,
currentUserID = undefined,
client = pool
} = extras;
const commentid = UUIDHelper.toUUID(commentID),
commentthreadid = UUIDHelper.toUUID(commentThreadID),
submissionid = UUIDHelper.toUUID(submissionID),
courseid = UUIDHelper.toUUID(courseID),
userid = UUIDHelper.toUUID(userID),
currentuserid = UUIDHelper.toUUID(currentUserID),
bodysearch = searchify(body);
const args = [commentid, commentthreadid, submissionid, courseid, userid,
created, edited, bodysearch, limit, offset, currentuserid];
type argType = typeof args;
return client.query<DBAPIComment & DBAPISubmission, argType>(`
SELECT c.*, s.*
FROM "CommentsView" as c, "SubmissionsView" as s, viewableSubmissions($11, $4) as opts
WHERE
c.submissionID = s.submissionID
AND ($1::uuid IS NULL OR c.commentID=$1)
AND ($2::uuid IS NULL OR c.commentThreadID=$2)
AND ($3::uuid IS NULL OR c.submissionID=$3)
AND ($4::uuid IS NULL OR c.courseID=$4)
AND ($5::uuid IS NULL OR c.userID=$5)
AND ($6::timestamp IS NULL OR c.created >= $6)
AND ($7::timestamp IS NULL OR c.edited >= $7)
AND ($8::text IS NULL OR c.body ILIKE $8)
AND c.submissionID = opts.submissionID
ORDER BY c.created DESC, c.commentID --unique when 2 comments same time
LIMIT $9
OFFSET $10
`, args)
.then(extract).then(map(entry => ({
submission: submissionToAPI(entry),
comment: commentToAPI(entry)
})));
}
/**
*
* @param comment the fields submissionID and courseID will be ignored, as well as limit and offset
* created/edited does not have to be supplied.
*/
static async addComment(comment: Comment) {
checkAvailable(["commentThreadID", "userID", "body"], comment);
const {
commentThreadID,
userID,
created = new Date(),
edited = new Date(),
body,
client = pool
} = comment;
const commentThreadid = UUIDHelper.toUUID(commentThreadID),
userid = UUIDHelper.toUUID(userID);
const args = [commentThreadid, userid, created, edited, body];
type argType = typeof args;
return client.query<DBAPIComment, argType>(`
with insert as (
INSERT INTO "Comments"
VALUES (DEFAULT, $1, $2, $3, $4, $5)
RETURNING *
)
${commentsView("insert")}
`, args)
.then(extract).then(map(commentToAPI)).then(one);
}
/**
* update a single comment, identified by its ID
* @param comment commentID is required and cannot be updated, all others are optional
* Updating all other IDs is strongly discouraged, though possible.
*/
static async updateComment(comment: Comment) {
checkAvailable(["commentID"], comment);
const {
commentID,
commentThreadID = undefined,
submissionID = undefined,
courseID = undefined,
userID = undefined,
created = undefined,
edited = undefined,
body = undefined,
client = pool
} = comment;
const commentid = UUIDHelper.toUUID(commentID),
commentThreadid = UUIDHelper.toUUID(commentThreadID),
submissionid = UUIDHelper.toUUID(submissionID),
courseid = UUIDHelper.toUUID(courseID),
userid = UUIDHelper.toUUID(userID);
if (commentid !== undefined
|| submissionid !== undefined
|| courseid !== undefined
|| userID !== undefined) {
console.warn("Updating IDs is almost never a good idea");
}
const args = [commentid, commentThreadid, userid, created, edited, body];
type argType = typeof args
return client.query<DBAPIComment, argType>(`
WITH update AS (
UPDATE "Comments" SET
commentThreadID = COALESCE($2, commentThreadID),
userID = COALESCE($3,userID),
created = COALESCE($4, created),
edited = COALESCE($5, edited),
body = COALESCE($6, body)
WHERE commentID =$1
RETURNING *
)
${commentsView("update")}
`, args)
.then(extract).then(map(commentToAPI)).then(one);
}
/**
* delete a single comment from the database.
* @param commentID ID of the comment to be deleted
* @param client optional; when using transactions, pass the client
* to this function so it can be used to perform this query.
*/
static async deleteComment(commentID: string, client: pgDB = pool) {
const commentid = UUIDHelper.toUUID(commentID);
return client.query<DBAPIComment, [string]>(`
WITH delete AS (
DELETE FROM "Comments"
WHERE commentID=$1
RETURNING *
)
${commentsView("delete")}
`, [commentid])
.then(extract).then(map(commentToAPI)).then(one);
}
}