All files / app/services/pictures picture.service.ts

93.02% Statements 40/43
92.85% Branches 39/42
90% Functions 27/30
91.66% Lines 33/36

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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 19014x 14x 14x                             14x                           14x 117x           6x   6x   6x             6x                                       3x               3x               3x             2x 2x 2x                   1x       1x       1x             1x   2x       9x 3x   6x       9x       6x 5x                 17x   17x                           2x             5x 5x 5x                                    
import { PictureApiService } from './picture-api.service';
import { DEFAULT_PICTURES_PAGE_SIZE } from './picture.constants';
import { Injectable, inject } from '@angular/core';
import {
    Picture,
    PictureArticle,
    PictureArticleDto,
    PictureBatchResponse,
    PictureDto,
    PictureListParams,
    PictureListResponse,
    PicturePending,
    PicturePendingDto,
    PicturePendingListResponse,
    PicturesListResponseDto,
} from '@drevo-web/shared';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
 
export interface PictureEditResult {
    readonly picture: Picture | undefined;
    readonly pending: PicturePending | undefined;
}
 
/**
 * Domain service for picture-related operations.
 * Maps API DTOs to frontend models.
 */
@Injectable({
    providedIn: 'root',
})
export class PictureService {
    private readonly pictureApiService = inject(PictureApiService);
 
    /**
     * Get paginated list of pictures with optional search
     */
    getPictures(params: PictureListParams = {}): Observable<PictureListResponse> {
        const { query = '', page = 1, pageSize = DEFAULT_PICTURES_PAGE_SIZE } = params;
 
        return this.pictureApiService
            .getPictures(query, page, pageSize)
            .pipe(map(response => this.mapListResponse(response)));
    }
 
    /**
     * Get picture by ID
     */
    getPicture(id: number): Observable<Picture> {
        return this.pictureApiService.getPicture(id).pipe(map(dto => this.mapPicture(dto)));
    }
 
    /**
     * Get multiple pictures by IDs in a single request
     */
    getPicturesBatch(ids: readonly number[]): Observable<PictureBatchResponse> {
        return this.pictureApiService.getPicturesBatch(ids).pipe(
            map(response => ({
                items: response.items.map(dto => this.mapPicture(dto)),
                notFoundIds: response.notFound,
            })),
        );
    }
 
    /**
     * Update picture title (PATCH).
     * Returns picture for moderators, pending for regular users.
     */
    updateTitle(id: number, title: string): Observable<PictureEditResult> {
        return this.pictureApiService.updateTitle(id, title).pipe(map(dto => this.mapEditResponse(dto)));
    }
 
    /**
     * Replace picture file (and optionally title) via PUT.
     * Returns picture for moderators, pending for regular users.
     */
    editPicture(id: number, formData: FormData): Observable<PictureEditResult> {
        return this.pictureApiService.editPicture(id, formData).pipe(map(dto => this.mapEditResponse(dto)));
    }
 
    /**
     * Delete picture.
     * Returns picture for moderators, pending for regular users.
     */
    deletePicture(id: number): Observable<PictureEditResult> {
        return this.pictureApiService.deletePicture(id).pipe(map(dto => this.mapEditResponse(dto)));
    }
 
    /**
     * Get paginated list of pending picture changes
     */
    getPending(page = 1, pageSize = DEFAULT_PICTURES_PAGE_SIZE): Observable<PicturePendingListResponse> {
        return this.pictureApiService.getPending(page, pageSize).pipe(
            map(response => ({
                items: response.items.map(dto => this.mapPicturePending(dto)),
                total: response.total,
                page: response.page,
                pageSize: response.pageSize,
                totalPages: response.totalPages,
            })),
        );
    }
 
    approvePending(pendingId: number): Observable<void> {
        return this.pictureApiService.approvePending(pendingId);
    }
 
    rejectPending(pendingId: number): Observable<void> {
        return this.pictureApiService.rejectPending(pendingId);
    }
 
    cancelPending(pendingId: number): Observable<void> {
        return this.pictureApiService.cancelPending(pendingId);
    }
 
    /**
     * Get articles that use a specific picture
     */
    getPictureArticles(pictureId: number): Observable<readonly PictureArticle[]> {
        return this.pictureApiService
            .getPictureArticles(pictureId)
            .pipe(map(dtos => dtos.map(dto => this.mapPictureArticle(dto))));
    }
 
    private mapEditResponse(dto: PictureDto | PicturePendingDto): PictureEditResult {
        if (this.isPendingDto(dto)) {
            return { picture: undefined, pending: this.mapPicturePending(dto) };
        }
        return { picture: this.mapPicture(dto), pending: undefined };
    }
 
    private isPendingDto(dto: PictureDto | PicturePendingDto): dto is PicturePendingDto {
        return 'pending' in dto && dto.pending === true;
    }
 
    private mapListResponse(response: PicturesListResponseDto): PictureListResponse {
        return {
            items: response.items.map(item => this.mapPicture(item)),
            total: response.total,
            page: response.page,
            pageSize: response.pageSize,
            totalPages: response.totalPages,
        };
    }
 
    private mapPicture(dto: PictureDto): Picture {
        const paddedId = String(dto.pic_id).padStart(6, '0');
 
        return {
            id: dto.pic_id,
            folder: dto.pic_folder,
            title: dto.pic_title,
            user: dto.pic_user,
            date: new Date(dto.pic_date),
            width: dto.pic_width ?? undefined,
            height: dto.pic_height ?? undefined,
            imageUrl: `/images/${dto.pic_folder}/${paddedId}.jpg`,
            thumbnailUrl: `/pictures/thumbs/${dto.pic_folder}/${paddedId}.jpg`,
        };
    }
 
    private mapPictureArticle(dto: PictureArticleDto): PictureArticle {
        return {
            id: dto.id,
            title: dto.title,
        };
    }
 
    private mapPicturePending(dto: PicturePendingDto): PicturePending {
        const paddedPicId = String(dto.pp_pic_id).padStart(6, '0');
        const hasPendingFile = dto.pp_type === 'edit_file' || dto.pp_type === 'edit_both';
        return {
            id: dto.pp_id,
            pictureId: dto.pp_pic_id,
            pendingType: dto.pp_type,
            title: dto.pp_title ?? undefined,
            width: dto.pp_width ?? undefined,
            height: dto.pp_height ?? undefined,
            user: dto.pp_user,
            date: new Date(dto.pp_date),
            currentTitle: dto.pic_title,
            currentImageUrl: `/images/${dto.pic_folder}/${paddedPicId}.jpg`,
            currentThumbnailUrl: `/pictures/thumbs/${dto.pic_folder}/${paddedPicId}.jpg`,
            currentWidth: dto.pic_width ?? undefined,
            currentHeight: dto.pic_height ?? undefined,
            pendingImageUrl: hasPendingFile ? `/images/pending/${dto.pp_pic_id}_pp${dto.pp_id}.jpg` : undefined,
        };
    }
}