All files / app/features/article/services article-page.service.ts

84.84% Statements 28/33
85.71% Branches 12/14
87.5% Functions 7/8
82.75% Lines 24/29

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 636x 6x       6x 14x   14x 14x   14x 14x 14x   14x 14x 3x 3x       9x 9x 9x             4x 4x 4x                                 3x 3x 1x     2x 2x            
import { computed, inject, Injectable, signal } from '@angular/core';
import { LoggerService } from '@drevo-web/core';
import { ApprovalStatus, ArticleVersion } from '@drevo-web/shared';
 
@Injectable()
export class ArticlePageService {
    private readonly logger = inject(LoggerService).withContext('ArticlePageService');
 
    private readonly _article = signal<ArticleVersion | undefined>(undefined);
    private readonly _error = signal<string | undefined>(undefined);
 
    readonly article = this._article.asReadonly();
    readonly error = this._error.asReadonly();
    readonly articleId = computed(() => this.article()?.articleId);
 
    readonly title = computed(() => this.article()?.title);
    readonly editUrl = computed(() => {
        const article = this.article();
        return article ? `/articles/${article.articleId}/version/${article.versionId}/edit` : undefined;
    });
 
    setArticle(article: ArticleVersion): void {
        this._article.set(article);
        this._error.set(undefined);
        this.logger.info('Article set from resolver', {
            id: article.articleId,
            title: article.title,
        });
    }
 
    setError(message: string): void {
        this._article.set(undefined);
        this._error.set(message);
        this.logger.error('Article error', { message });
    }
 
    updateTopics(topics: ReadonlyArray<number>): void {
        const current = this._article();
        if (!current) {
            return;
        }
 
        this._article.set({ ...current, topics });
        this.logger.info('Article topics updated', {
            id: current.articleId,
            topics,
        });
    }
 
    updateApproval(approved: ApprovalStatus, comment: string): void {
        const current = this._article();
        if (!current) {
            return;
        }
 
        this._article.set({ ...current, approved, comment });
        this.logger.info('Article approval updated', {
            id: current.articleId,
            approved,
        });
    }
}