-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCourseDB.ts
More file actions
214 lines (201 loc) · 7.15 KB
/
Copy pathCourseDB.ts
File metadata and controls
214 lines (201 loc) · 7.15 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
import {Course, courseToAPIPartial, DBAPICourse, courseToAPI} from "../../../models/database/Course";
import {User} from "../../../models/database/User";
import {PermissionEnum} from "../../../models/enums/PermissionEnum";
import {CourseRole} from "../../../models/enums/CourseRoleEnum";
import {UUIDHelper} from "./helpers/UUIDHelper";
import {pool, extract, map, one, checkAvailable, pgDB, DBTools, searchify, toBin} from "./HelperDB";
import {CoursesView} from "./ViewsDB";
/**
* Methods for accessing and altering the course table in the database.
* @Author Rens Leendertz
*/
export class CourseDB {
/**
* get a list of all courses
* @param params optional; send some extra info, such as limit and offset.
*/
static async getAllCourses(params: DBTools = {}) {
return CourseDB.filterCourse(params);
}
/**
* fetches a single course with a given id
*/
static async getCourseByID(courseID: string, client: pgDB = pool) {
return CourseDB.filterCourse({courseID, client}).then(one);
}
/**
* Selects a course from the course view using a course and a user object to check access.
* @param course
*/
static async filterCourse(course: Course & User) {
const {
courseID = undefined,
courseName = undefined,
creatorID = undefined,
state = undefined,
//creator
userName = undefined,
email = undefined,
globalRole: role = undefined,
//dbtools
limit = undefined,
offset = undefined,
client = pool
} = course;
const courseid = UUIDHelper.toUUID(courseID),
creatorid = UUIDHelper.toUUID(creatorID);
const
searchCourse = searchify(courseName),
searchUser = searchify(userName);
const args = [courseid, creatorid, //ids
searchCourse, state, //course
searchUser, email, role, //creator
limit, offset
];
type argType = typeof args;
return client.query<DBAPICourse, argType>(`SELECT * FROM "CoursesView"
WHERE
($1::uuid IS NULL OR courseID=$1)
AND ($2::uuid IS NULL OR creator=$2)
--course
AND ($3::text IS NULL OR courseName ILIKE $3)
AND ($4::text IS NULL OR state=$4)
--creator
AND ($5::text IS NULL OR userName ILIKE $5)
AND ($6::text IS NULL OR email ILIKE $6)
AND ($7::text IS NULL OR globalRole=$7)
ORDER BY courseName, state, courseID
LIMIT $8
OFFSET $9`, args)
.then(extract).then(map(courseToAPIPartial));
}
/**
* Adds a single course to the course table.
*/
static async addCourse(course: Course) {
checkAvailable(["courseName", "state", "creatorID"], course);
const {
courseName,
state,
creatorID,
canvasCourseID,
client = pool
} = course;
const creatorid = UUIDHelper.toUUID(creatorID);
return client.query(`
WITH insert as (
INSERT INTO "Courses"
VALUES (DEFAULT, $1, $2, $3, $4)
RETURNING *
)
${CoursesView("insert")}
`, [courseName, state, creatorid, canvasCourseID])
.then(extract).then(map(courseToAPIPartial)).then(one)
.then(res => res);
}
/**
* Searches for a course given a search string.
* @TODO instead of being registered, check the permissions of a user
*/
static async searchCourse(searchString: string, extras: Course & User) {
const {
courseID = undefined,
courseName = searchString,
creatorID = undefined,
state = undefined,
//creator
userName = undefined,
email = undefined,
globalRole = undefined,
//current user
currentUserID = undefined,
//dbtools
limit = undefined,
offset = undefined,
client = pool
} = extras;
const courseid = UUIDHelper.toUUID(courseID),
creatorid = UUIDHelper.toUUID(creatorID),
currentuserid = UUIDHelper.toUUID(currentUserID);
const
searchCourse = searchify(courseName);
const args = [courseid, creatorid, //ids
searchCourse, state, //course
userName, email, globalRole, // creator
currentuserid, //current user
limit, offset
];
//type argType = typeof args;
return client.query(`
SELECT c.*, cu.courseRole as currentCourseRole, cu.globalRole as currentGlobalRole, cu.permission as currentPermission
FROM "CoursesView" as c, "CourseUsersViewAll" as cu
WHERE
--current user
cu.userID = $8
AND c.courseID = cu.courseID
AND
( -- either registered
cu.courseRole != '${CourseRole.unregistered}'
OR -- or permission
cu.permission & b'${toBin(2 ** PermissionEnum.viewAllCourses)}' = b'${toBin(2 ** PermissionEnum.viewAllCourses)}'
)
--ids
AND ($1::uuid IS NULL OR c.courseID=$1)
AND ($2::uuid IS NULL OR c.creator=$2)
--course
AND ($3::text IS NULL OR c.courseName ILIKE $3)
AND ($4::text IS NULL OR c.state=$4)
--creator
AND ($5::text IS NULL OR c.userName = $5)
AND ($6::text IS NULL OR c.email = $6)
AND ($7::text IS NULL OR c.globalRole=$7)
ORDER BY c.state, c.courseName, c.courseID
LIMIT $9
OFFSET $10
`, args)
.then(extract).then(map(courseToAPI));
}
/**
* Deletes a single course from the database using a course id
*/
static async deleteCourseByID(courseID: string, client: pgDB = pool) {
const courseid = UUIDHelper.toUUID(courseID);
return client.query(`
WITH delete AS (
DELETE FROM "Courses"
WHERE courseID=$1
RETURNING *
)
${CoursesView("delete")}
`, [courseid])
.then(extract).then(map(courseToAPIPartial)).then(one);
}
/**
* updates a single course
*/
static async updateCourse(course: Course) {
checkAvailable(["courseID"], course);
const {
courseID,
courseName = undefined,
state = undefined,
creatorID = undefined,
client = pool
} = course;
const courseid = UUIDHelper.toUUID(courseID),
creatorid = UUIDHelper.toUUID(creatorID);
return client.query(`
WITH update AS (
UPDATE "Courses" SET
courseName=COALESCE($2, courseName),
state=COALESCE($3,state),
creator=COALESCE($4,creator)
WHERE courseID=$1
RETURNING *
)
${CoursesView("update")}
`,
[courseid, courseName, state, creatorid])
.then(extract).then(map(courseToAPIPartial)).then(one);
}
}