angular-t9n
Version:
A translation tool for Angular i18n
86 lines (75 loc) • 2.39 kB
text/typescript
import {
Body,
Controller,
Delete,
Get,
HttpCode,
HttpStatus,
NotFoundException,
Param,
Query,
} from '@nestjs/common';
import { LinkHelper } from '../link-helper';
import {
FilterableBuilder,
PaginationResponse,
SortableBuilder,
SourceOrphanRequest,
SourceOrphanResponse,
TranslationSource,
TranslationSourceOrphan,
} from '../models';
import { OrphanRegistry } from '../persistence';
export class SourceOrphansController {
private _sourceOrphanSortables = new SortableBuilder<TranslationSourceOrphan>((o) => o.unit)
.addSortables('id', 'source')
.addSafeSortables('description', 'meaning')
.build();
private _sourceOrphanFilterables = new FilterableBuilder<TranslationSourceOrphan>((o) => o.unit)
.addFilterables('id', 'description', 'meaning', 'source')
.build();
constructor(
private _source: TranslationSource,
private _orphanRegistry: OrphanRegistry,
private _linkHelper: LinkHelper,
) {}
getPagination(
queryParams: any,
): PaginationResponse<TranslationSourceOrphan, SourceOrphanResponse> {
return new PaginationResponse({
query: queryParams,
entries: this._orphanRegistry.orphans,
responseMapper: (orphan) => new SourceOrphanResponse(orphan, this._linkHelper),
urlFactory: (query) => this._linkHelper.sourceOrphans(query),
sortables: this._sourceOrphanSortables,
filterables: this._sourceOrphanFilterables,
});
}
getOrphan( id: string): SourceOrphanResponse {
const orphan = this._orphanRegistry.orphanMap.get(id);
if (!orphan) {
throw new NotFoundException('Orphan does not exist');
}
return new SourceOrphanResponse(orphan, this._linkHelper);
}
deleteOrphan( id: string, body?: SourceOrphanRequest): void {
const orphan = this._orphanRegistry.orphanMap.get(id);
if (!orphan) {
throw new NotFoundException('Orphan does not exist');
}
if (!body || !body.id) {
this._orphanRegistry.deleteOrphan(orphan);
return;
}
const sourceUnit = this._source.unitMap.get(body.id);
if (!sourceUnit) {
throw new NotFoundException('Migration unit does not exist');
}
this._orphanRegistry.migrateOrphan(orphan, sourceUnit);
}
}