fix backend page ordering

This commit is contained in:
Philipinho
2024-04-13 05:40:34 +01:00
parent 90ae750d48
commit 912fe6474b
4 changed files with 59 additions and 52 deletions

View File

@ -28,6 +28,12 @@ export class PageController {
private readonly pageHistoryService: PageHistoryService, private readonly pageHistoryService: PageHistoryService,
) {} ) {}
@HttpCode(HttpStatus.OK)
@Post()
async getSpacePages(@Body() spaceIdDto: SpaceIdDto) {
return this.pageService.getSidebarPagesBySpaceId(spaceIdDto.spaceId);
}
@HttpCode(HttpStatus.OK) @HttpCode(HttpStatus.OK)
@Post('/info') @Post('/info')
async getPage(@Body() pageIdDto: PageIdDto) { async getPage(@Body() pageIdDto: PageIdDto) {
@ -81,22 +87,16 @@ export class PageController {
return this.pageService.getRecentSpacePages(spaceIdDto.spaceId, pagination); return this.pageService.getRecentSpacePages(spaceIdDto.spaceId, pagination);
} }
@HttpCode(HttpStatus.OK)
@Post()
async getSpacePages(spaceId: string) {
return this.pageService.getSidebarPagesBySpaceId(spaceId);
}
@HttpCode(HttpStatus.OK) @HttpCode(HttpStatus.OK)
@Post('ordering') @Post('ordering')
async getSpacePageOrder(spaceId: string) { async getSpacePageOrder(@Body() spaceIdDto: SpaceIdDto) {
return this.pageOrderService.getSpacePageOrder(spaceId); return this.pageOrderService.getSpacePageOrder(spaceIdDto.spaceId);
} }
@HttpCode(HttpStatus.OK) @HttpCode(HttpStatus.OK)
@Post('tree') @Post('tree')
async spacePageTree(@Body() { spaceId }) { async spacePageTree(@Body() spaceIdDto: SpaceIdDto) {
return this.pageOrderService.convertToTree(spaceId); return this.pageOrderService.convertToTree(spaceIdDto.spaceId);
} }
// TODO: scope to workspaces // TODO: scope to workspaces

View File

@ -27,7 +27,6 @@ export class PageOrderingService {
) {} ) {}
// TODO: scope to workspace and space // TODO: scope to workspace and space
async movePage(dto: MovePageDto, trx?: KyselyTransaction): Promise<void> { async movePage(dto: MovePageDto, trx?: KyselyTransaction): Promise<void> {
await executeTx( await executeTx(
this.db, this.db,
@ -59,8 +58,6 @@ export class PageOrderingService {
); );
orderPageList(spaceOrdering.childrenIds, dto); orderPageList(spaceOrdering.childrenIds, dto);
// it should save or update right?
// await manager.save(spaceOrdering); //TODO: to update or create new record? pretty confusing
await trx await trx
.updateTable('pageOrdering') .updateTable('pageOrdering')
.set(spaceOrdering) .set(spaceOrdering)
@ -146,7 +143,7 @@ export class PageOrderingService {
trx, trx,
); );
} else { } else {
await this.addToSpacePageOrder(spaceId, pageId, trx); await this.addToSpacePageOrder(pageId, spaceId, trx);
} }
}, },
trx, trx,
@ -154,8 +151,8 @@ export class PageOrderingService {
} }
async addToSpacePageOrder( async addToSpacePageOrder(
spaceId: string,
pageId: string, pageId: string,
spaceId: string,
trx: KyselyTransaction, trx: KyselyTransaction,
) { ) {
await this.upsertOrdering( await this.upsertOrdering(
@ -167,6 +164,39 @@ export class PageOrderingService {
); );
} }
async upsertOrdering(
entityId: string,
entityType: string,
childId: string,
spaceId: string,
trx: KyselyTransaction,
) {
let ordering = await this.getEntityOrdering(entityId, entityType, trx);
console.log(ordering);
console.log('oga1');
if (!ordering) {
ordering = await this.createPageOrdering(
entityId,
entityType,
spaceId,
trx,
);
}
if (!ordering.childrenIds.includes(childId)) {
ordering.childrenIds.unshift(childId);
console.log(childId);
console.log('childId above');
await trx
.updateTable('pageOrdering')
.set(ordering)
.where('id', '=', ordering.id)
.execute();
//await manager.save(PageOrdering, ordering);
}
}
async removeFromParent( async removeFromParent(
parentId: string, parentId: string,
childId: string, childId: string,
@ -217,35 +247,6 @@ export class PageOrderingService {
} }
} }
async upsertOrdering(
entityId: string,
entityType: string,
childId: string,
spaceId: string,
trx: KyselyTransaction,
) {
let ordering = await this.getEntityOrdering(entityId, entityType, trx);
if (!ordering) {
ordering = await this.createPageOrdering(
entityId,
entityType,
spaceId,
trx,
);
}
if (!ordering.childrenIds.includes(childId)) {
ordering.childrenIds.unshift(childId);
await trx
.updateTable('pageOrdering')
.set(ordering)
.where('id', '=', ordering.id)
.execute();
//await manager.save(PageOrdering, ordering);
}
}
async getEntityOrdering( async getEntityOrdering(
entityId: string, entityId: string,
entityType: string, entityType: string,

View File

@ -44,9 +44,16 @@ export class PageService {
if (!parentPage) throw new NotFoundException('Parent page not found'); if (!parentPage) throw new NotFoundException('Parent page not found');
} }
let pageId = undefined;
if (createPageDto.pageId) {
pageId = createPageDto.pageId;
delete createPageDto.pageId;
}
//TODO: should be in a transaction //TODO: should be in a transaction
const createdPage = await this.pageRepo.insertPage({ const createdPage = await this.pageRepo.insertPage({
...createPageDto, ...createPageDto,
id: pageId,
creatorId: userId, creatorId: userId,
workspaceId: workspaceId, workspaceId: workspaceId,
lastUpdatedById: userId, lastUpdatedById: userId,
@ -54,7 +61,7 @@ export class PageService {
await this.pageOrderingService.addPageToOrder( await this.pageOrderingService.addPageToOrder(
createPageDto.spaceId, createPageDto.spaceId,
createPageDto.pageId, pageId,
createPageDto.parentPageId, createPageDto.parentPageId,
); );
@ -65,16 +72,17 @@ export class PageService {
pageId: string, pageId: string,
updatePageDto: UpdatePageDto, updatePageDto: UpdatePageDto,
userId: string, userId: string,
): Promise<void> { ): Promise<Page> {
await this.pageRepo.updatePage( await this.pageRepo.updatePage(
{ {
...updatePageDto, title: updatePageDto.title,
icon: updatePageDto.icon,
lastUpdatedById: userId, lastUpdatedById: userId,
}, },
pageId, pageId,
); );
//return await this.pageRepo.findById(pageId); return await this.pageRepo.findById(pageId);
} }
async updateState( async updateState(

View File

@ -9,7 +9,6 @@ import {
} from '@docmost/db/types/entity.types'; } from '@docmost/db/types/entity.types';
import { sql } from 'kysely'; import { sql } from 'kysely';
import { PaginationOptions } from '@docmost/db/pagination/pagination-options'; import { PaginationOptions } from '@docmost/db/pagination/pagination-options';
import { OrderingEntity } from 'src/core/page/page.util';
import { executeWithPagination } from '@docmost/db/pagination/pagination'; import { executeWithPagination } from '@docmost/db/pagination/pagination';
// TODO: scope to space/workspace // TODO: scope to space/workspace
@ -69,7 +68,7 @@ export class PageRepo {
.updateTable('pages') .updateTable('pages')
.set(updatablePage) .set(updatablePage)
.where('id', '=', pageId) .where('id', '=', pageId)
.execute(); .executeTakeFirst();
} }
async insertPage( async insertPage(
@ -106,8 +105,7 @@ export class PageRepo {
async getSpaceSidebarPages(spaceId: string, limit: number) { async getSpaceSidebarPages(spaceId: string, limit: number) {
const pages = await this.db const pages = await this.db
.selectFrom('pages as page') .selectFrom('pages as page')
.innerJoin('pageOrdering as ordering', 'ordering.entityId', 'page.id') .leftJoin('pageOrdering as ordering', 'ordering.entityId', 'page.id')
.where('ordering.entityType', '=', OrderingEntity.PAGE)
.where('page.spaceId', '=', spaceId) .where('page.spaceId', '=', spaceId)
.select([ .select([
'page.id', 'page.id',