Skip to content
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

Feat:lesson 10 added loader solution for Tommy Tran #351

Merged
merged 4 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lesson_10/libraries/src/loaders/loaders.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { AngelicaCastilloLoader } from './angelica_castillo_loader.js';
import { AnthonyMaysLoader } from './anthony_mays_loader.js';
import { JamesCapparellLoader } from './james_capparell_loader.js';
import { NileJacksonLoader } from './nile_jackson_loader.js';
import { TommyTranLoader } from './tommy_tran_loader.js';
import { XavierCruzLoader } from './xavier_cruz_loader.js';

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

// Add your quiz provider here.
Expand All @@ -14,6 +14,7 @@ const LOADER_PROVIDERS = [
JamesCapparellLoader,
NileJacksonLoader,
XavierCruzLoader,
TommyTranLoader,
];

@Module({
Expand Down
45 changes: 45 additions & 0 deletions lesson_10/libraries/src/loaders/tommy_tran_loader.ts
Original file line number Diff line number Diff line change
@@ -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 TommyTranLoader implements Loader {
getLoaderName(): string {
return 'tommytran';
}

async loadData(): Promise<MediaItem[]> {
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<MediaItem[]> {
const mediaitems: MediaItem[] = [];
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, [])); // help from xavier to instatiate the class
}
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: mediaItemId, role, name } = row;
credits.push(new Credit(mediaItemId, name, role));
}
return credits;
}
}