-
Notifications
You must be signed in to change notification settings - Fork 4
/
ethno-art.repository.ts
127 lines (116 loc) · 3.04 KB
/
ethno-art.repository.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
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
import { Injectable } from '@nestjs/common';
import { Query } from 'cypher-query-builder';
import {
DuplicateException,
ID,
PaginatedListType,
ServerException,
Session,
UnsecuredDto,
} from '~/common';
import { DbTypeOf, DtoRepository } from '~/core';
import {
createNode,
matchProps,
merge,
paginate,
sorting,
} from '~/core/database/query';
import {
ScriptureReferenceRepository,
ScriptureReferenceService,
} from '../scripture';
import {
CreateEthnoArt,
EthnoArt,
EthnoArtListInput,
UpdateEthnoArt,
} from './dto';
@Injectable()
export class EthnoArtRepository extends DtoRepository(EthnoArt) {
constructor(
private readonly scriptureRefsRepository: ScriptureReferenceRepository,
private readonly scriptureRefsService: ScriptureReferenceService,
) {
super();
}
async create(input: CreateEthnoArt, session: Session) {
if (!(await this.isUnique(input.name))) {
throw new DuplicateException(
'ethnoArt.name',
'Ethno art with this name already exists',
);
}
const initialProps = {
name: input.name,
canDelete: true,
};
const result = await this.db
.query()
.apply(await createNode(EthnoArt, { initialProps }))
.return<{ id: ID }>('node.id as id')
.first();
if (!result) {
throw new ServerException('Failed to create ethno art');
}
await this.scriptureRefsService.create(
result.id,
input.scriptureReferences,
session,
);
return await this.readOne(result.id);
}
async update(input: UpdateEthnoArt) {
const { id, name, scriptureReferences } = input;
await this.updateProperties({ id }, { name });
if (scriptureReferences !== undefined) {
await this.scriptureRefsService.update(id, scriptureReferences);
}
return await this.readOne(input.id);
}
async readOne(id: ID) {
return (await super.readOne(id)) as UnsecuredDto<EthnoArt>;
}
async readMany(
ids: readonly ID[],
): Promise<ReadonlyArray<UnsecuredDto<EthnoArt>>> {
const items = await super.readMany(ids);
return items.map((r) => ({
...r,
scriptureReferences: this.scriptureRefsService.parseList(
r.scriptureReferences,
),
}));
}
async list({
filter,
...input
}: EthnoArtListInput): Promise<PaginatedListType<UnsecuredDto<EthnoArt>>> {
const result = await this.db
.query()
.matchNode('node', 'EthnoArt')
.apply(sorting(EthnoArt, input))
.apply(paginate(input, this.hydrate()))
.first();
return {
...result!,
items: result!.items.map((r) => ({
...r,
scriptureReferences: this.scriptureRefsService.parseList(
r.scriptureReferences,
),
})),
};
}
protected hydrate() {
return (query: Query) =>
query
.apply(matchProps())
.subQuery('node', this.scriptureRefsRepository.list())
.return<{ dto: DbTypeOf<EthnoArt> }>(
merge('props', {
scriptureReferences: 'scriptureReferences',
}).as('dto'),
);
}
}