Skip to content

Commit 83c9012

Browse files
author
“Tezz03”
committed
fix:lesson_10
1 parent d7ca3b3 commit 83c9012

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

lesson_10/libraries/src/loaders/loaders.module.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ import { Module } from '@nestjs/common';
22

33
import { AnthonyMaysLoader } from './anthony_mays_loader.js';
44
import { DylanLaffertysLoader } from './dylan_lafferty_loaders.js';
5+
import { MontezBLoader } from './montez_b_loaders.js';
56

67
export const Loaders = Symbol.for('Loaders');
78

8-
const LOADER_PROVIDERS = [AnthonyMaysLoader, DylanLaffertysLoader];
9+
const LOADER_PROVIDERS = [AnthonyMaysLoader, DylanLaffertysLoader, MontezBLoader];
910

1011
@Module({
1112
providers: [
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import csv from 'csv-parser';
2+
import fs from 'fs';
3+
import { Credit, MediaItem } from '../models/index.js';
4+
import { Loader } from './loader.js';
5+
6+
export class MontezBLoader implements Loader {
7+
getLoaderName(): string {
8+
return 'montezb';
9+
}
10+
11+
async loadData(): Promise<MediaItem[]> {
12+
const credits = await this.loadCredits();
13+
const mediaItems = await this.loadMediaItems();
14+
15+
console.log(
16+
`Loaded ${credits.length} credits and ${mediaItems.length} media items`,
17+
);
18+
19+
return [...mediaItems.values()];
20+
}
21+
22+
async loadMediaItems(): Promise<MediaItem[]> {
23+
// TODO: Implement this method.
24+
const mediaItems = [];
25+
const readable = fs
26+
.createReadStream('data/media_items.csv', 'utf-8')
27+
.pipe(csv());
28+
29+
for await (const row of readable) {
30+
const { id, title, type, year } = row;
31+
mediaItems.push(new MediaItem(id, title, type, year, []));
32+
}
33+
return mediaItems;
34+
}
35+
36+
async loadCredits(): Promise<Credit[]> {
37+
const credits = [];
38+
const readable = fs
39+
.createReadStream('data/credits.csv', 'utf-8')
40+
.pipe(csv());
41+
for await (const row of readable) {
42+
const { media_item_id, role, name } = row;
43+
credits.push(new Credit(media_item_id, name, role));
44+
}
45+
return credits;
46+
}
47+
}

0 commit comments

Comments
 (0)