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 | 1x 1x 1x 1x 1x 1x 16x 16x 16x 7x 14x 7x 7x 11x 11x 11x 4x 4x 7x 5x 3x 1x 1x 1x 1x 4x 3x 2x 2x 2x 3x 2x 1x 1x 1x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | import type { DraftDatabase } from './draft-database';
import { DRAFT_USER_ID_PROVIDER } from './draft-user-id.token';
import { Draft, DraftInput } from './draft.model';
import { LoggerService } from '../logging/logger.service';
import { isPlatformBrowser } from '@angular/common';
import { inject, Injectable, PLATFORM_ID } from '@angular/core';
const NOT_AUTHENTICATED_ERROR = 'DraftStorageService: user is not authenticated';
@Injectable({ providedIn: 'root' })
export class DraftStorageService {
private readonly isBrowser = isPlatformBrowser(inject(PLATFORM_ID));
private readonly logger = inject(LoggerService).withContext('DraftStorageService');
private readonly getUserIdFn = inject(DRAFT_USER_ID_PROVIDER);
private db: DraftDatabase | undefined;
private async getDatabase(): Promise<DraftDatabase> {
Eif (!this.db) {
const { DraftDatabase } = await import('./draft-database');
this.db = new DraftDatabase();
}
return this.db;
}
private getUserId(): string | undefined {
return this.getUserIdFn();
}
private requireUserId(): string {
const userId = this.getUserId();
if (userId === undefined) {
this.logger.error(NOT_AUTHENTICATED_ERROR);
throw new Error(NOT_AUTHENTICATED_ERROR);
}
return userId;
}
async save(input: DraftInput): Promise<void> {
if (!this.isBrowser) return;
const userId = this.requireUserId();
const draft: Draft = {
userId,
route: input.route,
title: input.title,
text: input.text,
time: Date.now(),
};
this.logger.debug('Saving draft', { route: input.route });
const db = await this.getDatabase();
await db.saveDraft(draft);
}
async getByRoute(route: string): Promise<Draft | undefined> {
if (!this.isBrowser) return undefined;
const userId = this.requireUserId();
this.logger.debug('Getting draft by route', { route });
const db = await this.getDatabase();
return db.getDraft(userId, route);
}
async getAll(): Promise<Draft[]> {
if (!this.isBrowser) return [];
const userId = this.requireUserId();
this.logger.debug('Getting all drafts');
const db = await this.getDatabase();
return db.getAllDrafts(userId);
}
async getCount(): Promise<number> {
if (!this.isBrowser) return 0;
const userId = this.requireUserId();
this.logger.debug('Getting draft count');
const db = await this.getDatabase();
return db.countDrafts(userId);
}
async deleteByRoute(route: string): Promise<void> {
Iif (!this.isBrowser) return;
const userId = this.requireUserId();
this.logger.debug('Deleting draft', { route });
const db = await this.getDatabase();
await db.deleteDraft(userId, route);
}
async deleteAll(): Promise<void> {
Iif (!this.isBrowser) return;
const userId = this.requireUserId();
this.logger.debug('Deleting all drafts');
const db = await this.getDatabase();
await db.deleteAllDrafts(userId);
}
}
|