diff --git a/lesson_10/libraries/src/loaders/amiyah_jones.ts b/lesson_10/libraries/src/loaders/amiyah_jones.ts new file mode 100644 index 00000000..9a4372c1 --- /dev/null +++ b/lesson_10/libraries/src/loaders/amiyah_jones.ts @@ -0,0 +1,45 @@ +import csv from 'csv-parser'; +import fs from 'fs'; +import { Credit, MediaItem } from '../models/index.js'; +import { Loader } from './loader.js'; + +export class AmiyahJonesLoader implements Loader { + getLoaderName(): string { + return 'amiyahjones'; + } + + async loadData(): Promise { + const credits = await this.loadCredits(); + const mediaItems = await this.loadMediaItems(); + + console.log( + `Loaded ${credits.length} credits and ${mediaItems.length} media items`, + ); + + return [...mediaItems.values()]; + } + + async loadMediaItems(): Promise { + const allMedia = []; + const readable = fs + .createReadStream('data/media_items.csv', 'utf-8') + .pipe(csv()); + for await (const row of readable) { + const { id, type, title, year } = row; + allMedia.push(new MediaItem(id, title, type, year, [])); + } + return allMedia; + } + + async loadCredits(): Promise { + 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; + } +} diff --git a/lesson_10/libraries/src/loaders/loaders.module.ts b/lesson_10/libraries/src/loaders/loaders.module.ts index 8ae8b53e..6464187f 100644 --- a/lesson_10/libraries/src/loaders/loaders.module.ts +++ b/lesson_10/libraries/src/loaders/loaders.module.ts @@ -1,5 +1,6 @@ import { Module } from '@nestjs/common'; +import { AmiyahJonesLoader } from './amiyah_jones.js'; import { AngelicaCastilloLoader } from './angelica_castillo_loader.js'; import { AnthonyMaysLoader } from './anthony_mays_loader.js'; import { ChelseaOgbonniaLoader } from './chelsea_ogbonnia_loader.js'; @@ -15,12 +16,12 @@ import { TommyTranLoader } from './tommy_tran_loader.js'; import { XavierCruzLoader } from './xavier_cruz_loader.js'; import { ZionBuchananLoader } from './zion_buchanan_loader.js'; - export const Loaders = Symbol.for('Loaders'); // Add your quiz provider here. const LOADER_PROVIDERS = [ + AmiyahJonesLoader, AngelicaCastilloLoader, AnthonyMaysLoader, ChelseaOgbonniaLoader,