Skip to content

Commit a326fda

Browse files
committed
lesson_10_homework
1 parent 37e11bb commit a326fda

File tree

3 files changed

+91
-136
lines changed

3 files changed

+91
-136
lines changed

lesson_10/libraries/package-lock.json

Lines changed: 31 additions & 135 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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 JasonWatsonLoader implements Loader {
7+
getLoaderName(): string {
8+
return 'jasonwatson';
9+
}
10+
11+
async loadData(): Promise<MediaItem[]> {
12+
const credits = await this.loadCredits();
13+
const mediaItems = await this.loadMediaItems();
14+
15+
const mediaMap = new Map<string, MediaItem>();
16+
17+
for (const media of mediaItems) {
18+
mediaMap.set(media.getId(), media);
19+
}
20+
21+
for (const credit of credits) {
22+
const mediaItem = mediaMap.get(credit.getMediaItemId());
23+
if (mediaItem) {
24+
mediaItem.addCredit(credit);
25+
}
26+
}
27+
28+
console.log(
29+
`Loaded ${credits.length} credits and ${mediaItems.length} media items`,
30+
);
31+
32+
return Array.from(mediaMap.values());
33+
}
34+
35+
async loadMediaItems(): Promise<MediaItem[]> {
36+
const mediaItems = [];
37+
const readable = fs
38+
.createReadStream('data/media_items.csv', 'utf-8')
39+
.pipe(csv());
40+
for await (const row of readable) {
41+
const { id, title, type, year } = row;
42+
mediaItems.push(new MediaItem(id, title, type, year, []));
43+
}
44+
return mediaItems;
45+
}
46+
47+
async loadCredits(): Promise<Credit[]> {
48+
const credits = [];
49+
const readable = fs
50+
.createReadStream('data/credits.csv', 'utf-8')
51+
.pipe(csv());
52+
for await (const row of readable) {
53+
const { media_item_id, role, name } = row;
54+
credits.push(new Credit(media_item_id, name, role));
55+
}
56+
return credits;
57+
}
58+
}

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

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

33
import { AnthonyMaysLoader } from './anthony_mays_loader.js';
4+
import { JasonWatsonLoader } from './jason_watson_loader.js';
45

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

7-
const LOADER_PROVIDERS = [AnthonyMaysLoader];
8+
const LOADER_PROVIDERS = [AnthonyMaysLoader, JasonWatsonLoader];
89

910
@Module({
1011
providers: [

0 commit comments

Comments
 (0)