diff --git a/lesson_10/libraries/src/loaders/lj_mcwilliams_loader.ts b/lesson_10/libraries/src/loaders/lj_mcwilliams_loader.ts new file mode 100644 index 00000000..842df07e --- /dev/null +++ b/lesson_10/libraries/src/loaders/lj_mcwilliams_loader.ts @@ -0,0 +1,57 @@ +import csv from 'csv-parser'; +import fs from 'fs'; +import { Credit, MediaItem } from '../models/index.js'; +import { Loader } from './loader.js'; + +export class LjMcwilliamsLoader implements Loader { + getLoaderName(): string { + return 'ljmcwilliams'; + } + + 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 { + // TODO: Implement this method. + const media = []; + const readable = fs + .createReadStream('data/media_items.csv', 'utf-8') + .pipe(csv()); + for await (const row of readable) { + /**This destructures the CSV file rows. */ + const { id, type, title, year } = row; + media.push(new MediaItem(id, title, type, year, [])); + } + return media; + } + + /* + An asyncchronous function named loadCredits + returns a Promise of an array of Credit Objects. + */ + async loadCredits(): Promise { + //The empty credits array will store parsed credit data + const credits = []; + /** + * This var creates a readable stream from the CSV file and + * is piped through the csv function to parse the data. + */ + const readable = fs + .createReadStream('data/credits.csv', 'utf-8') + .pipe(csv()); + //This asynchronously iterates over each row of the parsed CSV data. + 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 cfd693ea..d80a6ccd 100644 --- a/lesson_10/libraries/src/loaders/loaders.module.ts +++ b/lesson_10/libraries/src/loaders/loaders.module.ts @@ -10,6 +10,7 @@ import { HummadTanweerLoader } from './hummad_tanweer_loader.js'; import { JamesCapparellLoader } from './james_capparell_loader.js'; import { JosephCaballeroLoader } from './joseph_caballero_loader.js'; import { KimberleeHaldaneLoader } from './kimberlee_haldane_loader.js'; +import { LjMcwilliamsLoader } from './lj_mcwilliams_loader.js'; import { NileJacksonLoader } from './nile_jackson_loader.js'; import { OyeyemiJimohLoader } from './oyeyemi_jimoh_loader.js'; import { PabloLimonParedesLoader } from './pablo_limon_paredes_loader.js'; @@ -32,6 +33,7 @@ const LOADER_PROVIDERS = [ JamesCapparellLoader, JosephCaballeroLoader, KimberleeHaldaneLoader, + LjMcwilliamsLoader, NileJacksonLoader, OyeyemiJimohLoader, PabloLimonParedesLoader,