From c979cc7537cb557dcc87d2a226e7646c6bd9433e Mon Sep 17 00:00:00 2001 From: NelltheWiz Date: Fri, 28 Mar 2025 22:07:57 +0000 Subject: [PATCH 1/3] feat: adds chanel_hutt_loader.ts --- .../src/loaders/chanel_hutt_loader.ts | 44 +++++++++++++++++++ .../libraries/src/loaders/loaders.module.ts | 8 +++- 2 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 lesson_10/libraries/src/loaders/chanel_hutt_loader.ts diff --git a/lesson_10/libraries/src/loaders/chanel_hutt_loader.ts b/lesson_10/libraries/src/loaders/chanel_hutt_loader.ts new file mode 100644 index 000000000..a00847ee2 --- /dev/null +++ b/lesson_10/libraries/src/loaders/chanel_hutt_loader.ts @@ -0,0 +1,44 @@ +import csv from 'csv-parser'; +import fs from 'fs'; +import { Credit, MediaItem } from '../models/index.js'; +import { Loader } from './loader.js'; + +export class ChanelHuttLoader implements Loader { + getLoaderName(): string { + return 'chanelhutt'; + } + + 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 items = []; + const readable = fs + .createReadStream('data/items.csv', 'utf-8') + .pipe(csv()); + for await (const row of readable) { + const { id, title, type, year } = row; + items.push(new MediaItem(id, title, type, year, [])); + return items; + } + + 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; + } +} \ No newline at end of file diff --git a/lesson_10/libraries/src/loaders/loaders.module.ts b/lesson_10/libraries/src/loaders/loaders.module.ts index 0b2db8214..d9077d48a 100644 --- a/lesson_10/libraries/src/loaders/loaders.module.ts +++ b/lesson_10/libraries/src/loaders/loaders.module.ts @@ -1,11 +1,15 @@ import { Module } from '@nestjs/common'; import { AnthonyMaysLoader } from './anthony_mays_loader.js'; +import { ChanelHuttLoader } from './chanel_hutt_loader.js'; import { DylanLaffertysLoader } from './dylan_lafferty_loaders.js'; - export const Loaders = Symbol.for('Loaders'); -const LOADER_PROVIDERS = [AnthonyMaysLoader, DylanLaffertysLoader]; +const LOADER_PROVIDERS = [ + AnthonyMaysLoader, + DylanLaffertysLoader, + ChanelHuttLoader, +]; @Module({ providers: [ From 0fe510944344e1f06c0974e9ee8f5351459f1a5d Mon Sep 17 00:00:00 2001 From: NelltheWiz Date: Mon, 31 Mar 2025 13:55:01 +0000 Subject: [PATCH 2/3] feat: completed loader.ts file --- lesson_10/libraries/src/loaders/chanel_hutt_loader.ts | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/lesson_10/libraries/src/loaders/chanel_hutt_loader.ts b/lesson_10/libraries/src/loaders/chanel_hutt_loader.ts index a00847ee2..c635187ec 100644 --- a/lesson_10/libraries/src/loaders/chanel_hutt_loader.ts +++ b/lesson_10/libraries/src/loaders/chanel_hutt_loader.ts @@ -20,14 +20,15 @@ export class ChanelHuttLoader implements Loader { } async loadMediaItems(): Promise { - const items = []; + const mediaItems = []; const readable = fs - .createReadStream('data/items.csv', 'utf-8') + .createReadStream('data/media_items.csv', 'utf-8') .pipe(csv()); for await (const row of readable) { const { id, title, type, year } = row; - items.push(new MediaItem(id, title, type, year, [])); - return items; + mediaItems.push(new MediaItem(id, title, type, year, [])); + } + return mediaItems; } async loadCredits(): Promise { @@ -41,4 +42,4 @@ export class ChanelHuttLoader implements Loader { } return credits; } -} \ No newline at end of file +} From 774386ef1699014da30cea05a991745bd80b6b99 Mon Sep 17 00:00:00 2001 From: NelltheWiz Date: Mon, 31 Mar 2025 15:51:19 +0000 Subject: [PATCH 3/3] feat: completes loader and extra credit for lesson10 --- .../src/loaders/chanel_hutt_loader.ts | 24 ++++++++++++++----- 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/lesson_10/libraries/src/loaders/chanel_hutt_loader.ts b/lesson_10/libraries/src/loaders/chanel_hutt_loader.ts index c635187ec..9b21fb652 100644 --- a/lesson_10/libraries/src/loaders/chanel_hutt_loader.ts +++ b/lesson_10/libraries/src/loaders/chanel_hutt_loader.ts @@ -11,12 +11,24 @@ export class ChanelHuttLoader implements Loader { 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()]; + // Create a Hashmap to where the key is a string(MediaItem ID) and the value is the MediaItem object. + const hashMapIndex = new Map(); + // Loops through the mediaItems and adds them to the map by their ID. + for (const mediaItem of mediaItems) { + hashMapIndex.set(mediaItem.getId(), mediaItem); + } + // Loops through the credits and adds them to the mediaItem by getting the mediaItem ID. + for (const credit of credits) { + const mediaItem = hashMapIndex.get(credit.getMediaItemId()); + if (mediaItem) { + mediaItem.addCredit(credit); + } + console.log( + `Loaded ${credits.length} credits and ${mediaItems.length} media items`, + ); + } + // Returns an array of the values from the hashmap. + return Array.from(hashMapIndex.values()); } async loadMediaItems(): Promise {