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,
) {}
@HttpCode(HttpStatus.OK)
@Post()
async getSpacePages(@Body() spaceIdDto: SpaceIdDto) {
return this.pageService.getSidebarPagesBySpaceId(spaceIdDto.spaceId);
}
@HttpCode(HttpStatus.OK)
@Post('/info')
async getPage(@Body() pageIdDto: PageIdDto) {
@ -81,22 +87,16 @@ export class PageController {
return this.pageService.getRecentSpacePages(spaceIdDto.spaceId, pagination);
}
@HttpCode(HttpStatus.OK)
@Post()
async getSpacePages(spaceId: string) {
return this.pageService.getSidebarPagesBySpaceId(spaceId);
}
@HttpCode(HttpStatus.OK)
@Post('ordering')
async getSpacePageOrder(spaceId: string) {
return this.pageOrderService.getSpacePageOrder(spaceId);
async getSpacePageOrder(@Body() spaceIdDto: SpaceIdDto) {
return this.pageOrderService.getSpacePageOrder(spaceIdDto.spaceId);
}
@HttpCode(HttpStatus.OK)
@Post('tree')
async spacePageTree(@Body() { spaceId }) {
return this.pageOrderService.convertToTree(spaceId);
async spacePageTree(@Body() spaceIdDto: SpaceIdDto) {
return this.pageOrderService.convertToTree(spaceIdDto.spaceId);
}
// TODO: scope to workspaces

View File

@ -27,7 +27,6 @@ export class PageOrderingService {
) {}
// TODO: scope to workspace and space
async movePage(dto: MovePageDto, trx?: KyselyTransaction): Promise<void> {
await executeTx(
this.db,
@ -59,8 +58,6 @@ export class PageOrderingService {
);
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
.updateTable('pageOrdering')
.set(spaceOrdering)
@ -146,7 +143,7 @@ export class PageOrderingService {
trx,
);
} else {
await this.addToSpacePageOrder(spaceId, pageId, trx);
await this.addToSpacePageOrder(pageId, spaceId, trx);
}
},
trx,
@ -154,8 +151,8 @@ export class PageOrderingService {
}
async addToSpacePageOrder(
spaceId: string,
pageId: string,
spaceId: string,
trx: KyselyTransaction,
) {
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(
parentId: 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(
entityId: string,
entityType: string,

View File

@ -44,9 +44,16 @@ export class PageService {
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
const createdPage = await this.pageRepo.insertPage({
...createPageDto,
id: pageId,
creatorId: userId,
workspaceId: workspaceId,
lastUpdatedById: userId,
@ -54,7 +61,7 @@ export class PageService {
await this.pageOrderingService.addPageToOrder(
createPageDto.spaceId,
createPageDto.pageId,
pageId,
createPageDto.parentPageId,
);
@ -65,16 +72,17 @@ export class PageService {
pageId: string,
updatePageDto: UpdatePageDto,
userId: string,
): Promise<void> {
): Promise<Page> {
await this.pageRepo.updatePage(
{
...updatePageDto,
title: updatePageDto.title,
icon: updatePageDto.icon,
lastUpdatedById: userId,
},
pageId,
);
//return await this.pageRepo.findById(pageId);
return await this.pageRepo.findById(pageId);
}
async updateState(

View File

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