All files / app/features/picture/resolvers picture.resolver.ts

95% Statements 19/20
100% Branches 11/11
66.66% Functions 2/3
100% Lines 18/18

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 421x 1x 1x     1x 1x   1x               1x       9x 9x 1x     8x 8x 4x     4x   3x 1x   2x         1x  
import { PictureService } from '../../../services/pictures/picture.service';
import { HttpErrorResponse } from '@angular/common/http';
import { inject } from '@angular/core';
import { ActivatedRouteSnapshot, ResolveFn } from '@angular/router';
import { Picture } from '@drevo-web/shared';
import { Observable, of } from 'rxjs';
import { catchError } from 'rxjs/operators';
 
const NOT_FOUND_STATUS = 404;
 
export type PictureResolveResult = Picture | 'not-found' | 'load-error';
 
/**
 * Pure function for resolving picture data from route params.
 * Extracted for testability without injection context.
 */
export function resolvePicture(
    pictureService: PictureService,
    route: ActivatedRouteSnapshot,
): Observable<PictureResolveResult> {
    const idParam = route.paramMap.get('id');
    if (!idParam) {
        return of('not-found' as const);
    }
 
    const id = Number(idParam);
    if (isNaN(id) || id <= 0 || !Number.isInteger(id)) {
        return of('not-found' as const);
    }
 
    return pictureService.getPicture(id).pipe(
        catchError((error: unknown) => {
            if (error instanceof HttpErrorResponse && error.status === NOT_FOUND_STATUS) {
                return of('not-found' as const);
            }
            return of('load-error' as const);
        }),
    );
}
 
export const pictureResolver: ResolveFn<PictureResolveResult> = route => resolvePicture(inject(PictureService), route);