mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-25 22:27:32 +00:00
refactor(collections): parsing and writing a list's links moves into collections helpers
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
import type { CollectionLink } from '@trek/shared';
|
||||
|
||||
/**
|
||||
* Pure helpers of the collections domain, moved verbatim out of
|
||||
* collections.service.ts. Nothing here touches the DB, so they stay plain
|
||||
* exports rather than methods on CollectionsService.
|
||||
*/
|
||||
|
||||
/** Links are stored as a JSON TEXT column; parse on read, stringify on write. */
|
||||
export function parseLinks(raw: unknown): CollectionLink[] | undefined {
|
||||
if (typeof raw !== 'string' || !raw) return undefined;
|
||||
try {
|
||||
const v = JSON.parse(raw);
|
||||
return Array.isArray(v) ? (v as CollectionLink[]) : undefined;
|
||||
} catch {
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
export function serializeLinks(links: CollectionLink[] | undefined): string | null {
|
||||
return links && links.length ? JSON.stringify(links) : null;
|
||||
}
|
||||
@@ -62,7 +62,6 @@ import type {
|
||||
CollectionMember,
|
||||
CollectionMembership,
|
||||
CollectionPlace,
|
||||
CollectionLink,
|
||||
CollectionCreateRequest,
|
||||
CollectionUpdateRequest,
|
||||
CollectionSavePlaceRequest,
|
||||
@@ -76,22 +75,7 @@ import type {
|
||||
import { NotificationsService } from '../notifications/notifications.service';
|
||||
import { PlaceImportService, type ExportedCollectionFile } from '../place-import/place-import.service';
|
||||
import { UnitOfWork } from '../database/unit-of-work';
|
||||
|
||||
/** Links are stored as a JSON TEXT column; parse on read, stringify on write. */
|
||||
function parseLinks(raw: unknown): CollectionLink[] | undefined {
|
||||
if (typeof raw !== 'string' || !raw) return undefined;
|
||||
try {
|
||||
const v = JSON.parse(raw);
|
||||
return Array.isArray(v) ? (v as CollectionLink[]) : undefined;
|
||||
} catch {
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
function serializeLinks(links: CollectionLink[] | undefined): string | null {
|
||||
return links && links.length ? JSON.stringify(links) : null;
|
||||
}
|
||||
|
||||
import { parseLinks, serializeLinks } from './collections.helpers';
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Errors — thrown as plain Errors carrying a status; TrekExceptionFilter maps
|
||||
|
||||
Reference in New Issue
Block a user