Skip to content

Commit e4bab58

Browse files
committed
feat: update delete and reorder api
1 parent 13e5493 commit e4bab58

File tree

1 file changed

+37
-2
lines changed
  • apps/book-server/src/services/PageService

1 file changed

+37
-2
lines changed

apps/book-server/src/services/PageService/index.mts

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -335,10 +335,21 @@ export class PageService implements Service {
335335
})
336336
: null
337337

338-
if (parentPage && parentPage.fk_book_id !== book.id) {
338+
if (parentPage && parentPage?.fk_book_id !== book.id) {
339339
throw new ConfilctError('Not related parent page')
340340
}
341341

342+
if (parentPage && parentPage.type !== 'folder') {
343+
await this.mongo.page.update({
344+
where: {
345+
id: parentPage.id,
346+
},
347+
data: {
348+
type: 'folder',
349+
},
350+
})
351+
}
352+
342353
const targetPage = await this.mongo.page.update({
343354
where: {
344355
id: page.id,
@@ -408,7 +419,7 @@ export class PageService implements Service {
408419
throw new BadRequestError('Already deleted')
409420
}
410421

411-
await this.mongo.page.update({
422+
const deletedPage = await this.mongo.page.update({
412423
where: {
413424
id: page.id,
414425
},
@@ -418,6 +429,30 @@ export class PageService implements Service {
418429
updated_at: new Date(),
419430
},
420431
})
432+
433+
const siblings = await this.mongo.page.findMany({
434+
where: {
435+
parent_id: page.parent_id,
436+
is_deleted: false,
437+
id: {
438+
not: deletedPage.id,
439+
},
440+
},
441+
orderBy: [{ index: 'asc' }, { updated_at: 'desc' }],
442+
})
443+
444+
const updateSiblings = siblings.map((sibling, index) => {
445+
return this.mongo.page.update({
446+
where: {
447+
id: sibling.id,
448+
},
449+
data: {
450+
index,
451+
},
452+
})
453+
})
454+
455+
await Promise.all(updateSiblings)
421456
}
422457
}
423458

0 commit comments

Comments
 (0)