1
1
import { InjectRepository } from '@nestjs/typeorm' ;
2
- import { Repository } from 'typeorm' ;
2
+ import { Connection , DataSource , MoreThan , Repository } from 'typeorm' ;
3
3
import { Injectable } from '@nestjs/common' ;
4
4
import { Project } from './entity/project.entity' ;
5
5
import { ProjectToMember } from './entity/project-member.entity' ;
@@ -9,6 +9,7 @@ import { Link } from './entity/link.entity.';
9
9
import { Epic , EpicColor } from './entity/epic.entity' ;
10
10
import { Story , StoryStatus } from './entity/story.entity' ;
11
11
import { Task , TaskStatus } from './entity/task.entity' ;
12
+ import { LexoRank } from 'lexorank' ;
12
13
13
14
@Injectable ( )
14
15
export class ProjectRepository {
@@ -29,6 +30,7 @@ export class ProjectRepository {
29
30
private readonly storyRepository : Repository < Story > ,
30
31
@InjectRepository ( Task )
31
32
private readonly taskRepository : Repository < Task > ,
33
+ private readonly dataSource : DataSource ,
32
34
) { }
33
35
34
36
create ( project : Project ) : Promise < Project > {
@@ -117,8 +119,27 @@ export class ProjectRepository {
117
119
return result . affected ? result . affected : 0 ;
118
120
}
119
121
120
- createEpic ( epic : Epic ) : Promise < Epic > {
121
- return this . epicRepository . save ( epic ) ;
122
+ async createEpic ( epic : Epic ) : Promise < Epic > {
123
+ try {
124
+ return await this . epicRepository . save ( epic ) ;
125
+ } catch ( e ) {
126
+ if (
127
+ e . code === 'ER_DUP_ENTRY' &&
128
+ e . sqlMessage . includes ( 'EPIC_UQ_RANK_VALUE_AND_PROJECT_ID' )
129
+ )
130
+ throw new Error ( 'DUPLICATED RANK VALUE' ) ;
131
+ throw e ;
132
+ }
133
+ }
134
+
135
+ getNextEpicByRankValue ( projectId : number , rankValue : string ) {
136
+ return this . epicRepository . findOne ( {
137
+ where : {
138
+ projectId,
139
+ rankValue : MoreThan ( rankValue ) ,
140
+ } ,
141
+ order : { rankValue : 'ASC' } ,
142
+ } ) ;
122
143
}
123
144
124
145
async deleteEpic ( project : Project , epicId : number ) : Promise < number > {
@@ -149,11 +170,20 @@ export class ProjectRepository {
149
170
updateData . rankValue = rankValue ;
150
171
}
151
172
152
- const result = await this . epicRepository . update (
153
- { id, project : { id : project . id } } ,
154
- updateData ,
155
- ) ;
156
- return ! ! result . affected ;
173
+ try {
174
+ const result = await this . epicRepository . update (
175
+ { id, project : { id : project . id } } ,
176
+ updateData ,
177
+ ) ;
178
+ return ! ! result . affected ;
179
+ } catch ( e ) {
180
+ if (
181
+ e . code === 'ER_DUP_ENTRY' &&
182
+ e . sqlMessage . includes ( 'EPIC_UQ_RANK_VALUE_AND_PROJECT_ID' )
183
+ )
184
+ throw new Error ( 'DUPLICATED RANK VALUE' ) ;
185
+ throw e ;
186
+ }
157
187
}
158
188
159
189
getEpicById ( project : Project , id : number ) {
@@ -162,8 +192,27 @@ export class ProjectRepository {
162
192
} ) ;
163
193
}
164
194
165
- createStory ( story : Story ) : Promise < Story > {
166
- return this . storyRepository . save ( story ) ;
195
+ async createStory ( story : Story ) : Promise < Story > {
196
+ try {
197
+ return await this . storyRepository . save ( story ) ;
198
+ } catch ( e ) {
199
+ if (
200
+ e . code === 'ER_DUP_ENTRY' &&
201
+ e . sqlMessage . includes ( 'STORY_UQ_RANK_VALUE_AND_EPIC_ID' )
202
+ )
203
+ throw new Error ( 'DUPLICATED RANK VALUE' ) ;
204
+ throw e ;
205
+ }
206
+ }
207
+
208
+ getNextStoryByRankValue ( epicId : number , rankValue : string ) {
209
+ return this . storyRepository . findOne ( {
210
+ where : {
211
+ epicId,
212
+ rankValue : MoreThan ( rankValue ) ,
213
+ } ,
214
+ order : { rankValue : 'ASC' } ,
215
+ } ) ;
167
216
}
168
217
169
218
async deleteStory ( project : Project , storyId : number ) : Promise < number > {
@@ -201,11 +250,20 @@ export class ProjectRepository {
201
250
updateData . rankValue = rankValue ;
202
251
}
203
252
204
- const result = await this . storyRepository . update (
205
- { id, project : { id : project . id } } ,
206
- updateData ,
207
- ) ;
208
- return ! ! result . affected ;
253
+ try {
254
+ const result = await this . storyRepository . update (
255
+ { id, project : { id : project . id } } ,
256
+ updateData ,
257
+ ) ;
258
+ return ! ! result . affected ;
259
+ } catch ( e ) {
260
+ if (
261
+ e . code === 'ER_DUP_ENTRY' &&
262
+ e . sqlMessage . includes ( 'STORY_UQ_RANK_VALUE_AND_EPIC_ID' )
263
+ )
264
+ throw new Error ( 'DUPLICATED RANK VALUE' ) ;
265
+ throw e ;
266
+ }
209
267
}
210
268
211
269
getStoryById ( project : Project , id : number ) {
@@ -224,8 +282,27 @@ export class ProjectRepository {
224
282
return targetProject . displayIdCount ;
225
283
}
226
284
227
- async createTask ( task : Task ) {
228
- return this . taskRepository . save ( task ) ;
285
+ async createTask ( task : Task ) : Promise < Task > {
286
+ try {
287
+ return await this . taskRepository . save ( task ) ;
288
+ } catch ( e ) {
289
+ if (
290
+ e . code === 'ER_DUP_ENTRY' &&
291
+ e . sqlMessage . includes ( 'TASK_UQ_RANK_VALUE_AND_STORY_ID' )
292
+ )
293
+ throw new Error ( 'DUPLICATED RANK VALUE' ) ;
294
+ throw e ;
295
+ }
296
+ }
297
+
298
+ getNextTaskByRankValue ( storyId : number , rankValue : string ) {
299
+ return this . taskRepository . findOne ( {
300
+ where : {
301
+ storyId,
302
+ rankValue : MoreThan ( rankValue ) ,
303
+ } ,
304
+ order : { rankValue : 'ASC' } ,
305
+ } ) ;
229
306
}
230
307
231
308
async deleteTask ( project : Project , taskId : number ) : Promise < number > {
@@ -271,11 +348,20 @@ export class ProjectRepository {
271
348
updateData . rankValue = rankValue ;
272
349
}
273
350
274
- const result = await this . taskRepository . update (
275
- { id, project : { id : project . id } } ,
276
- updateData ,
277
- ) ;
278
- return ! ! result . affected ;
351
+ try {
352
+ const result = await this . taskRepository . update (
353
+ { id, project : { id : project . id } } ,
354
+ updateData ,
355
+ ) ;
356
+ return ! ! result . affected ;
357
+ } catch ( e ) {
358
+ if (
359
+ e . code === 'ER_DUP_ENTRY' &&
360
+ e . sqlMessage . includes ( 'TASK_UQ_RANK_VALUE_AND_STORY_ID' )
361
+ )
362
+ throw new Error ( 'DUPLICATED RANK VALUE' ) ;
363
+ throw e ;
364
+ }
279
365
}
280
366
281
367
getProjectBacklog ( project : Project ) {
0 commit comments