-
Notifications
You must be signed in to change notification settings - Fork 0
/
tagController.ts
56 lines (45 loc) · 1.05 KB
/
tagController.ts
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
import { __ } from "#helpers/i18n";
import prisma from "#instances/prisma";
import { Request, Response } from "express";
import { EnTagType } from '@prisma/client';
const getItems = async (req: Request, res: Response) => {
const tags = await prisma.tag.findMany();
return res.json(tags);
}
const createItem = async (req: Request, res: Response) => {
const name = req.body.name as string;
const serieId = req.body.serieId as number | null;
if (serieId) {
const findSerie = await prisma.serie.findUnique({
where: {
id: serieId,
}
});
if (!findSerie) {
return res.status(400).send({ error: 'Серия не существует'});
}
}
const findTag = await prisma.tag.findUnique({
where: {
name_type: {
name,
type: EnTagType.COLLECTION
}
}
});
if (findTag) {
return res.status(400).send({ error: 'Тег уже существует'});
}
const tag = await prisma.tag.create({
data: {
name,
serieId,
type: EnTagType.COLLECTION,
}
});
return res.json(tag);
}
export default {
createItem,
getItems,
}