Skip to content

feat: Jason Watson lesson 10 homework on loaders. #374

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Apr 3, 2025
58 changes: 58 additions & 0 deletions lesson_10/libraries/src/loaders/jason_watson_loader.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import csv from 'csv-parser';
import fs from 'fs';
import { Credit, MediaItem } from '../models/index.js';
import { Loader } from './loader.js';

export class JasonWatsonLoader implements Loader {
getLoaderName(): string {
return 'jasonwatson';
}

async loadData(): Promise<MediaItem[]> {
const credits = await this.loadCredits();
const mediaItems = await this.loadMediaItems();

const mediaMap = new Map<string, MediaItem>();

for (const media of mediaItems) {
mediaMap.set(media.getId(), media);
}

for (const credit of credits) {
const mediaItem = mediaMap.get(credit.getMediaItemId());
if (mediaItem) {
mediaItem.addCredit(credit);
}
}

console.log(
`Loaded ${credits.length} credits and ${mediaItems.length} media items`,
);

return Array.from(mediaMap.values());
}

async loadMediaItems(): Promise<MediaItem[]> {
const mediaItems = [];
const readable = fs
.createReadStream('data/media_items.csv', 'utf-8')
.pipe(csv());
for await (const row of readable) {
const { id, title, type, year } = row;
mediaItems.push(new MediaItem(id, title, type, year, []));
}
return mediaItems;
}

async loadCredits(): Promise<Credit[]> {
const credits = [];
const readable = fs
.createReadStream('data/credits.csv', 'utf-8')
.pipe(csv());
for await (const row of readable) {
const { media_item_id, role, name } = row;
credits.push(new Credit(media_item_id, name, role));
}
return credits;
}
}
2 changes: 2 additions & 0 deletions lesson_10/libraries/src/loaders/loaders.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { KarenAlabiLoader } from './karen_alabi_loader.js';
import { MercedesMathewsLoader } from './mercedes_mathews_loader.js';
import { RasheedMillerLoader } from './rasheed_miller_loader.js';
import { MontezBLoader } from './montez_b_loaders.js';
import { JasonWatsonLoader } from './jason_watson_loader.js';

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

Expand All @@ -19,6 +20,7 @@ const LOADER_PROVIDERS = [
ChanelHuttLoader,
JBeyLoader,
MercedesMathewsLoader,
JasonWatsonLoader,
KarenAlabiLoader,
RasheedMillerLoader,
DavidAdenaikeLoader,
Expand Down