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 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 | 19x 19x 19x 19x 19x 78x 8x 8x 4x 4x 11x 11x 4x 4x 4x 5x 3x 3x 4x 4x 6x 6x 6x 23x 16x 4x 3x 3x 5x 6x 12x 5x 5x 12x 10x | import { ArticleApiService } from './article-api.service';
import { DEFAULT_ARTICLE_SEARCH_PAGE_SIZE } from './article.constants';
import { Injectable, inject } from '@angular/core';
import {
ApprovalStatus,
ArticleHistoryItem,
ArticleHistoryItemDto,
ArticleHistoryParams,
ArticleHistoryResponse,
ArticleHistoryResponseDto,
ArticleSearchParams,
ArticleSearchResponse,
ArticleSearchResult,
ArticleSearchResponseDto,
ArticleSearchResultDto,
ArticleVersion,
ArticleVersionDto,
ModerationResult,
SaveArticleVersionRequest,
SaveArticleVersionResponseDto,
SaveArticleVersionResult,
VersionForDiff,
VersionForDiffDto,
VersionPairs,
} from '@drevo-web/shared';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
/**
* Main service for article-related operations.
*
* This service is the single point of access for all article operations.
* It uses ArticleApiService internally for HTTP requests and handles
* data mapping from API models to frontend models.
*/
@Injectable({
providedIn: 'root',
})
export class ArticleService {
private readonly articleApiService = inject(ArticleApiService);
/**
* Get article by ID.
*
* @param id - Article ID
* @returns Observable with mapped article
*/
getArticle(id: number): Observable<ArticleVersion> {
return this.articleApiService.getArticle(id).pipe(
map(response =>
this.mapArticleVersion({
...response,
content: this.transformArticleLinks(response.content),
}),
),
);
}
/**
* Get article version for viewing (with formatted HTML and transformed links)
*
* @param versionId - Version ID
* @returns Observable with mapped article version
*/
getVersionShow(versionId: number): Observable<ArticleVersion> {
return this.articleApiService.getVersionShow(versionId).pipe(
map(response =>
this.mapArticleVersion({
...response,
content: this.transformArticleLinks(response.content),
}),
),
);
}
/**
* Get article version by ID (for editing)
*
* @param versionId - Version ID
* @returns Observable with mapped article version (raw content)
*/
getArticleVersion(versionId: number): Observable<ArticleVersion> {
return this.articleApiService
.getArticleVersion(versionId)
.pipe(map(response => this.mapArticleVersion(response)));
}
/**
* Search articles by title
*
* @param params - Search parameters (query is optional - empty returns all articles)
* @returns Observable with mapped search response
*/
searchArticles(params: ArticleSearchParams = {}): Observable<ArticleSearchResponse> {
const { query = '', page = 1, pageSize = DEFAULT_ARTICLE_SEARCH_PAGE_SIZE } = params;
return this.articleApiService
.searchArticles(query, page, pageSize)
.pipe(map(response => this.mapSearchResponse(response)));
}
/**
* Save article version
*
* @param request - Save request with versionId, content, and optional info
* @returns Observable with mapped save result
*/
saveArticleVersion(request: SaveArticleVersionRequest): Observable<SaveArticleVersionResult> {
return this.articleApiService.saveArticleVersion(request).pipe(map(response => this.mapSaveResponse(response)));
}
/**
* Moderate article version (approve or reject)
*
* @param versionId - Version ID to moderate
* @param approved - Approval status
* @param comment - Optional moderation comment
* @returns Observable with moderation result
*/
moderateVersion(versionId: number, approved: ApprovalStatus, comment = ''): Observable<ModerationResult> {
return this.articleApiService.moderateVersion({ versionId, approved, comment }).pipe(
map(dto => ({
versionId: dto.versionId,
articleId: dto.articleId,
approved: dto.approved,
comment: dto.comment ?? '',
})),
);
}
/**
* Preview formatted article content without saving
*
* @param content - Raw wiki content to format
* @param articleId - Article ID for internal links resolution
* @returns Observable with formatted HTML string (with transformed links)
*/
previewArticle(content: string, articleId: number): Observable<string> {
return this.articleApiService
.previewArticle({ content, articleId })
.pipe(map(response => this.transformArticleLinks(response.content)));
}
/**
* Get article version history
*
* @param params - History parameters (page, pageSize, approved, author)
* @returns Observable with mapped history response
*/
getArticlesHistory(params: ArticleHistoryParams = {}): Observable<ArticleHistoryResponse> {
const { page = 1, pageSize = DEFAULT_ARTICLE_SEARCH_PAGE_SIZE, approved, author, articleId } = params;
return this.articleApiService
.getArticlesHistory(page, pageSize, approved, author, articleId)
.pipe(map(response => this.mapHistoryResponse(response)));
}
/**
* Update article topics
*
* @param articleId - Article ID
* @param topics - Array of topic IDs
* @returns Observable with updated topic IDs
*/
updateTopics(articleId: number, topics: readonly number[]): Observable<readonly number[]> {
return this.articleApiService.updateTopics(articleId, topics);
}
private mapArticleVersion(response: ArticleVersionDto): ArticleVersion {
return {
articleId: response.articleId,
versionId: response.versionId,
title: response.title,
content: response.content,
author: response.author,
date: new Date(response.date),
redirect: response.redirect === 1,
new: response.new,
approved: response.approved,
info: response.info,
comment: response.comment,
topics: response.topics ?? [],
};
}
/**
* Transform legacy article links to Angular-friendly format.
* Converts href="/articles/8.html" to href="/articles/8"
* Converts href="/articles/8.html#S22" to href="/articles/8#S22"
*/
private transformArticleLinks(content: string): string {
// Match href attributes pointing to /articles/*.html with optional anchor
return content.replace(/href="\/articles\/(\d+)\.html(#[^"]+)?"/g, 'href="/articles/$1$2"');
}
private mapSearchResponse(response: ArticleSearchResponseDto): ArticleSearchResponse {
return {
items: response.items.map(item => this.mapSearchResult(item)),
total: response.total,
page: response.page,
pageSize: response.pageSize,
totalPages: response.totalPages,
};
}
private mapSearchResult(item: ArticleSearchResultDto): ArticleSearchResult {
return {
id: item.id,
title: item.title,
};
}
private mapSaveResponse(response: SaveArticleVersionResponseDto): SaveArticleVersionResult {
return {
articleId: response.articleId,
versionId: response.versionId,
title: response.title,
author: response.author,
date: new Date(response.date),
approved: response.approved,
};
}
private mapHistoryResponse(response: ArticleHistoryResponseDto): ArticleHistoryResponse {
return {
items: response.items.map(item => this.mapHistoryItem(item)),
total: response.total,
page: response.page,
pageSize: response.pageSize,
totalPages: response.totalPages,
};
}
/**
* Get two versions for diff comparison
*
* @param version1 - Primary version ID
* @param version2 - Optional secondary version ID (auto-detected if omitted)
* @returns Observable with mapped version pairs
*/
getVersionPairs(version1: number, version2?: number): Observable<VersionPairs> {
return this.articleApiService.getVersionPairs(version1, version2).pipe(
map(response => ({
current: this.mapVersionForDiff(response.current),
previous: this.mapVersionForDiff(response.previous),
})),
);
}
private mapHistoryItem(item: ArticleHistoryItemDto): ArticleHistoryItem {
return {
versionId: item.versionId,
articleId: item.articleId,
title: item.title,
author: item.author,
date: new Date(item.date),
approved: item.approved,
isNew: item.new,
info: item.info,
comment: item.comment,
};
}
private mapVersionForDiff(dto: VersionForDiffDto): VersionForDiff {
return {
articleId: dto.articleId,
versionId: dto.versionId,
content: dto.content,
author: dto.author,
date: new Date(dto.date),
title: dto.title,
info: dto.info,
approved: dto.approved,
comment: dto.comment ?? '',
};
}
}
|