Skip to content

Commit 13b7bc9

Browse files
committed
feat: add lesson_10 HW
1 parent 89614e9 commit 13b7bc9

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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 DavisDLoader implements Loader {
7+
getLoaderName(): string {
8+
return 'davis';
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+
for await (const row of readable) {
29+
const { id, title, type, year } = row;
30+
MediaItems.push(new MediaItem(id, title, type, year, []));
31+
}
32+
return MediaItems;
33+
}
34+
35+
async loadCredits(): Promise<Credit[]> {
36+
const credits = [];
37+
const readable = fs
38+
.createReadStream('data/credits.csv', 'utf-8')
39+
.pipe(csv());
40+
for await (const row of readable) {
41+
const { media_item_id, role, name } = row;
42+
credits.push(new Credit(media_item_id, name, role));
43+
}
44+
return credits;
45+
}
46+
}

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

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

33
import { AnthonyMaysLoader } from './anthony_mays_loader.js';
4+
import { DavisDLoader } from './davis_d_loader.js';
45
import { DylanLaffertysLoader } from './dylan_lafferty_loaders.js';
56

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

8-
const LOADER_PROVIDERS = [AnthonyMaysLoader, DylanLaffertysLoader];
9+
const LOADER_PROVIDERS = [
10+
AnthonyMaysLoader,
11+
DylanLaffertysLoader,
12+
DavisDLoader,
13+
];
914

1015
@Module({
1116
providers: [

0 commit comments

Comments
 (0)