-
Notifications
You must be signed in to change notification settings - Fork 0
/
words.controller.js
132 lines (132 loc) · 5.58 KB
/
words.controller.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
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.appendClipToWord = exports.listClipsForWord = exports.getWordWithTheLeastClips = exports.appendWord = exports.QueryWord = void 0;
const zod_1 = require("zod");
const prismadb_1 = __importDefault(require("../utils/prismadb"));
const http_status_codes_1 = __importDefault(require("http-status-codes"));
const getQueryItem_1 = __importDefault(require("../utils/getQueryItem"));
const uuid_1 = require("uuid");
const QueryWordSchema = zod_1.z.object({
wordID: zod_1.z.string().uuid("Invalid wordID"),
});
const appendWordSchema = zod_1.z.object({
word: zod_1.z.string(),
});
function QueryWord(req, res) {
return __awaiter(this, void 0, void 0, function* () {
const s = QueryWordSchema.safeParse(req.query);
if (!s.success)
return res.status(http_status_codes_1.default.UNPROCESSABLE_ENTITY).send(s.error.errors);
const word = yield prismadb_1.default.word.findFirst({
where: {
id: (0, getQueryItem_1.default)(req.query.wordID),
},
});
return res.status(http_status_codes_1.default.OK).send(word);
});
}
exports.QueryWord = QueryWord;
function appendWord(req, res) {
var _a;
return __awaiter(this, void 0, void 0, function* () {
const checkAppendWord = appendWordSchema.safeParse(req.query);
if (!checkAppendWord.success)
return res.status(http_status_codes_1.default.BAD_REQUEST).json(checkAppendWord.error.errors);
const word = yield (prismadb_1.default === null || prismadb_1.default === void 0 ? void 0 : prismadb_1.default.word.create({
data: {
ar: (0, getQueryItem_1.default)(req.query.word),
// @ts-ignore
userId: (_a = req === null || req === void 0 ? void 0 : req.session) === null || _a === void 0 ? void 0 : _a.id,
id: (0, uuid_1.v4)(),
},
}));
return res.status(http_status_codes_1.default.OK).send(word);
});
}
exports.appendWord = appendWord;
function getWordWithTheLeastClips(req, res) {
return __awaiter(this, void 0, void 0, function* () {
const word = yield (prismadb_1.default === null || prismadb_1.default === void 0 ? void 0 : prismadb_1.default.word.findFirst({
select: {
ar: true,
id: true,
createBy: { select: { name: true, id: true } },
},
orderBy: {
clips: {
_count: "asc",
},
},
take: 1,
}));
return res.json(word);
});
}
exports.getWordWithTheLeastClips = getWordWithTheLeastClips;
const listClipsSchema = zod_1.z.object({
wordID: zod_1.z.string().uuid(),
});
function listClipsForWord(req, res) {
return __awaiter(this, void 0, void 0, function* () {
const ChecklistClipsSchema = listClipsSchema.safeParse(req);
if (!ChecklistClipsSchema.success) {
res.status(http_status_codes_1.default.NOT_ACCEPTABLE).json(ChecklistClipsSchema.error.errors);
return;
}
const clips = yield (prismadb_1.default === null || prismadb_1.default === void 0 ? void 0 : prismadb_1.default.word.findFirst({
where: {
id: (0, getQueryItem_1.default)(req.query.wordID),
},
select: {
ar: true,
clips: {
select: {
createBy: {
select: {
name: true,
},
},
id: true,
path: true,
},
take: 15,
},
},
}));
res.status(http_status_codes_1.default.OK).send(clips);
});
}
exports.listClipsForWord = listClipsForWord;
function appendClipToWord(req, res, next) {
return __awaiter(this, void 0, void 0, function* () {
if (typeof req.file === "undefined") {
return next({
code: http_status_codes_1.default.UNPROCESSABLE_ENTITY,
msg: http_status_codes_1.default.getStatusText(http_status_codes_1.default.UNPROCESSABLE_ENTITY),
});
}
const word = yield (prismadb_1.default === null || prismadb_1.default === void 0 ? void 0 : prismadb_1.default.clip.create({
data: {
path: req.file.filename,
// @ts-ignore
userID: req.session.id,
wordID: (0, getQueryItem_1.default)(req.body.wordID),
id: (0, uuid_1.v4)(),
},
}));
return res.status(http_status_codes_1.default.OK).json(word);
});
}
exports.appendClipToWord = appendClipToWord;