-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathquestionRepository.js
More file actions
78 lines (65 loc) · 2.21 KB
/
questionRepository.js
File metadata and controls
78 lines (65 loc) · 2.21 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
const config = require('../../config.js');
const getDBClient = require('./dbClient');
const dbClient = getDBClient(config.getConnections().mysql);
const questionRepository = {
getMaterial: async function (materialId, transaction) {
const query =
`SELECT DISTINCT MU.Id, MU.SubjectId, MU.UserId, MU.Title, MU.FilledDate,
MU.MaterialLink, MU.Tags, MU.MaterialListId
FROM dbo.MaterialUpload MU
WHERE MU.Id = :materialId;`;
var connection = await dbClient.getConnectionAsync(transaction);
var result = await connection.queryAsync(query, {
materialId
});
return result[0];
},
getQuestion: async function (questionId) {
const query =
`SELECT Id, Text, Closed, AnswersCount, ModerationType, PremiumAnswerId, Archived
FROM Question
WHERE Id = :questionId;`;
var result = await dbClient.queryAsync(query, {
questionId
});
return result[0];
},
getLastQuestion: async function (studentId) {
const query =
`SELECT M.Name as Title, Text from Question Q
INNER JOIN Material M ON M.Id = Q.Id
WHERE M.UserId = :studentId
ORDER BY M.CreationDate DESC
LIMIT 1;`;
var result = await dbClient.queryAsync(query, {
studentId
});
return result[0];
},
createQuestion: async function (material, transaction) {
const query =
`INSERT INTO dbo.Question
(Id, Text, Closed, AnswersCount, ModerationType, PremiumAnswerId, Archived)
VALUES (:Id, :Text, :Closed, :AnswersCount, :ModerationType, :PremiumAnswerId, Archived);`;
var connection = await dbClient.getConnectionAsync(transaction);
var result = await connection.queryAsync(query, material);
return result[0];
},
updateQuestion: async function (question, transaction) {
const query =
`UPDATE dbo.Question
SET
Text = :Text,
Closed = :Closed,
AnswersCount = :AnswersCount,
ModerationType = :ModerationType,
PremiumAnswerId = :PremiumAnswerId,
Archived = :Archived
WHERE Id = :Id;`;
var connection = await dbClient.getConnectionAsync(transaction);
var result = await connection.queryAsync(query, question);
return result[0];
}
}
module.exports =
questionRepository;