This repository has been archived by the owner on Nov 7, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
post.repository.ts
175 lines (159 loc) Β· 6.26 KB
/
post.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
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
import { Repository, SelectQueryBuilder } from 'typeorm';
import { EntityRepository } from 'typeorm/decorator/EntityRepository';
import { Post } from './post.entity';
import { Character } from '../character/character.entity';
import { Corporation } from '../corporation/corporation.entity';
import { Alliance } from '../alliance/alliance.entity';
@EntityRepository(Post)
export class PostRepository extends Repository<Post> {
public async getCharacterWall(
character: Character,
limit: number,
page: number,
): Promise<[Post[], number]> {
return this.getAll(limit, page)
.where(
`
post."characterWallId" = :characterId OR
(authorCharacter.id = :characterId AND post."characterWallId" IS NULL)
`,
{ characterId: character.id })
.getManyAndCount();
}
public async getCorporationWall(
corporation: Corporation,
limit: number,
page: number,
): Promise<[Post[], number]> {
return this.getAll(limit, page)
.where(
`
post."corporationWallId" = :corporationId OR
(authorCorporation.id = :corporationId AND post."corporationWallId" IS NULL)
`,
{ corporationId: corporation.id })
.getManyAndCount();
}
public async getAllianceWall(
alliance: Alliance,
limit: number,
page: number,
): Promise<[Post[], number]> {
return this.getAll(limit, page)
.where(
`
post."allianceWallId" = :allianceId OR
(authorAlliance.id = :allianceId AND post."allianceWallId" IS NULL)
`,
{ allianceId: alliance.id })
.getManyAndCount();
}
public async getByLocation(
locationId: number,
limit: number,
page: number,
): Promise<[Post[], number]> {
return this.getAll(limit, page)
.where('location."id" = :locationId', { locationId })
.getManyAndCount();
}
public async getByHashtag(
hashtag: string,
limit: number,
page: number,
): Promise<[Post[], number]> {
return this.getAll(limit, page)
.where('hashtag."name" = :hashtag', { hashtag })
.getManyAndCount();
}
public async getLatest(
limit: number,
page: number,
): Promise<[Post[], number]> {
return this.getAll(limit, page)
.getManyAndCount();
}
public async getParticipants(
post: Post,
): Promise<{ characters: Character[], corporations: Corporation[], alliances: Alliance[] }> {
const postWithComments = await this.createQueryBuilder('post')
.leftJoinAndSelect('post.character', 'characterAuthor')
.leftJoinAndSelect('post.corporation', 'corporationAuthor')
.leftJoinAndSelect('post.alliance', 'allianceAuthor')
.leftJoinAndSelect('post.comments', 'comment')
.leftJoinAndSelect('comment.character', 'character')
.leftJoinAndSelect('comment.corporation', 'corporation')
.leftJoinAndSelect('comment.alliance', 'alliance')
.where('post.id = :postId', { postId: post.id })
.getOne();
const characters = postWithComments.comments
.map(comment => comment.character)
.filter(character => character);
if (postWithComments.character) characters.push(postWithComments.character);
const corporations = postWithComments.comments
.map(comment => comment.corporation)
.filter(corporation => corporation);
if (postWithComments.corporation) corporations.push(postWithComments.corporation);
const alliances = postWithComments.comments
.map(comment => comment.alliance)
.filter(alliance => alliance);
if (postWithComments.alliance) alliances.push(postWithComments.alliance);
return {
characters: characters.filter((v, i, a) => a.findIndex(v1 => v1.id === v.id) === i), // unique
corporations: corporations.filter((v, i, a) => a.findIndex(v1 => v1.id === v.id) === i), // unique
alliances: alliances.filter((v, i, a) => a.findIndex(v1 => v1.id === v.id) === i), // unique
};
}
public getNumOfPostsByCharacter(
character: Character,
): Promise<number> {
return this.count({ where: { character } })
}
public getNumOfPostsByCorporation(
corporation: Corporation,
): Promise<number> {
return this.count({ where: { corporation } })
}
public getNumOfPostsByAlliance(
alliance: Alliance,
): Promise<number> {
return this.count({ where: { alliance } })
}
/**
* Wrapper for querying posts
* @param {number} limit
* @param {number} page
* @returns {SelectQueryBuilder<Post>}
*/
private getAll(limit: number, page: number): SelectQueryBuilder<Post> {
return this.createQueryBuilder('post')
.leftJoinAndSelect('post.character', 'authorCharacter')
.leftJoinAndSelect('authorCharacter.corporation', 'authorCharacterCorporation')
.leftJoinAndSelect('authorCharacterCorporation.alliance', 'authorCharacterAlliance')
.leftJoinAndSelect('post.corporation', 'authorCorporation')
.leftJoinAndSelect('authorCorporation.alliance', 'authorCorporationAlliance')
.leftJoinAndSelect('post.alliance', 'authorAlliance')
.leftJoinAndSelect('post.characterWall', 'onCharacterWall')
.leftJoinAndSelect('onCharacterWall.corporation', 'onCharacterWallCorporation')
.leftJoinAndSelect('onCharacterWallCorporation.alliance', 'onCharacterWallAlliance')
.leftJoinAndSelect('post.corporationWall', 'onCorporationWall')
.leftJoinAndSelect('onCorporationWall.alliance', 'onCorporationWallAlliance')
.leftJoinAndSelect('post.allianceWall', 'onAllianceWall')
.leftJoinAndSelect('post.killmail', 'killmail')
.leftJoinAndSelect('post.hashtags', 'hashtag')
.leftJoinAndSelect('post.location', 'location')
.leftJoinAndSelect('killmail.participants', 'killmailP')
.leftJoinAndSelect('killmailP.character', 'killmailPCharacter')
.leftJoinAndSelect('killmailPCharacter.corporation', 'killmailPCorporation')
.leftJoinAndSelect('killmailPCorporation.alliance', 'killmailPAlliance')
.leftJoinAndSelect('killmailP.ship', 'killmailPShip')
.leftJoinAndSelect('killmailPShip.group', 'killmailPShipGroup')
.leftJoinAndSelect('killmailPShipGroup.category', 'killmailPShipGroupCategory')
.leftJoinAndSelect('killmailP.weapon', 'killmailPWeapon')
.leftJoinAndSelect('killmailPWeapon.group', 'killmailPWeaponGroup')
.leftJoinAndSelect('killmailPWeaponGroup.category', 'killmailPWeaponGroupCategory')
.orderBy({ 'post."createdAt"': 'DESC' })
.offset(limit * page)
.limit(limit);
}
}