mirror of
https://github.com/johndoe6345789/metabuilder.git
synced 2026-04-25 22:34:56 +00:00
code: pages,list,dbal (1 files)
This commit is contained in:
@@ -2,42 +2,40 @@
|
||||
* @file list-pages.ts
|
||||
* @description List pages with filtering and pagination
|
||||
*/
|
||||
import type { PageView, ListOptions, Result } from '../types';
|
||||
import type { InMemoryStore } from '../store/in-memory-store';
|
||||
import type { ListOptions, PageView, Result } from '../../types'
|
||||
import type { InMemoryStore } from '../../store/in-memory-store'
|
||||
|
||||
/**
|
||||
* List pages with filtering and pagination
|
||||
*/
|
||||
export async function listPages(
|
||||
export const listPages = async (
|
||||
store: InMemoryStore,
|
||||
options: ListOptions = {}
|
||||
): Promise<Result<PageView[]>> {
|
||||
const { filter = {}, sort = {}, page = 1, limit = 20 } = options;
|
||||
): Promise<Result<PageView[]>> => {
|
||||
const { filter = {}, sort = {}, page = 1, limit = 20 } = options
|
||||
|
||||
let pages = Array.from(store.pages.values());
|
||||
let pages = Array.from(store.pages.values())
|
||||
|
||||
// Apply filters
|
||||
if (filter.isActive !== undefined) {
|
||||
pages = pages.filter((p) => p.isActive === filter.isActive);
|
||||
}
|
||||
if (filter.level !== undefined) {
|
||||
pages = pages.filter((p) => p.level === filter.level);
|
||||
pages = pages.filter((p) => p.isActive === filter.isActive)
|
||||
}
|
||||
|
||||
if (filter.level !== undefined) {
|
||||
pages = pages.filter((p) => p.level === filter.level)
|
||||
}
|
||||
|
||||
// Apply sorting
|
||||
if (sort.title) {
|
||||
pages.sort((a, b) => (sort.title === 'asc' ? a.title.localeCompare(b.title) : b.title.localeCompare(a.title)));
|
||||
pages.sort((a, b) =>
|
||||
sort.title === 'asc' ? a.title.localeCompare(b.title) : b.title.localeCompare(a.title)
|
||||
)
|
||||
} else if (sort.createdAt) {
|
||||
pages.sort((a, b) =>
|
||||
sort.createdAt === 'asc'
|
||||
? a.createdAt.getTime() - b.createdAt.getTime()
|
||||
: b.createdAt.getTime() - a.createdAt.getTime()
|
||||
);
|
||||
sort.createdAt === 'asc' ? a.createdAt.getTime() - b.createdAt.getTime() : b.createdAt.getTime() - a.createdAt.getTime()
|
||||
)
|
||||
}
|
||||
|
||||
// Apply pagination
|
||||
const start = (page - 1) * limit;
|
||||
const paginated = pages.slice(start, start + limit);
|
||||
const start = (page - 1) * limit
|
||||
const paginated = pages.slice(start, start + limit)
|
||||
|
||||
return { success: true, data: paginated };
|
||||
return { success: true, data: paginated }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user