Skip to content

Commit

Permalink
feat: adds Nile_Jackson_loader.ts and Modifies loader.ts -lesson10Hw (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
nilejack authored Oct 17, 2024
1 parent 6a45e46 commit d5bc1a4
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lesson_10/libraries/src/loaders/loaders.module.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import { Module } from '@nestjs/common';
import { AnthonyMaysLoader } from './anthony_mays_loader.js';
import { NileJacksonLoader } from './nile_jackson_loader.js';
import { XavierCruzLoader } from './xavier_cruz_loader.js';

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

// Add your quiz provider here.
const LOADER_PROVIDERS = [AnthonyMaysLoader, XavierCruzLoader];
const LOADER_PROVIDERS = [
AnthonyMaysLoader,
XavierCruzLoader,
NileJacksonLoader,
];

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

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 = [];
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;
}
}

0 comments on commit d5bc1a4

Please sign in to comment.