File size: 1,021 Bytes
22df730
 
 
 
 
 
 
 
 
 
c995cfc
22df730
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
import { FileManager } from '../utils/file-manager';
import { parseTxtRow } from '../utils/file.utils';
import { Link } from './Link';

export abstract class LinkManager<T> {
  protected abstract fileName: string;
  protected abstract tableName: string;

  private getFile() {
    return new FileManager(`../data/${this.fileName}`);
  }

  toLink(id: string): Link {
    return new Link(this.tableName, id);
  }

  async resolve(link: Link): Promise<T | null> {
    if (link.table !== this.tableName)
      throw new Error(`Expected link to ${this.tableName}, got ${link.table}`);

    const file = this.getFile();
    const rows = await file.readLines();
    const items = rows.map(parseTxtRow) as T[];

    return items.find((i: any) => i.id === link.id) || null;
  }

  async resolveMany(links: Link[]): Promise<T[]> {
    const resolved = await Promise.all(links.map((l) => this.resolve(l)));
    return resolved.filter((i) => i !== null) as T[];
  }
}