Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
@@ -1,10 +1,11 @@
import { Module } from '@nestjs/common';
import { AnthonyMaysLoader } from './anthony_mays_loader.js';
import {YafiahAbdullahLoader} from './yafiah_abdullah_loader.js';

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

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

@Module({
providers: [
Expand Down
50 changes: 50 additions & 0 deletions lesson_10/libraries/src/loaders/yafiah_abdullah_loader.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import csv from 'csv-parser';
import fs from 'fs';
import { Credit, MediaItem } from '../models/index.js';
import { Loader } from './loader.js';

export class YafiahAbdullahLoader implements Loader {
getLoaderName(): string {
return 'yafiahabdullah';
}

async loadData(): Promise<MediaItem[]> {
const [credits, mediaItems] = await Promise.all([
this.loadCredits(),
this.loadMediaItems(),
]);
// 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[]> {
// TODO: Implement this method.
const media = [];
const readable = fs
.createReadStream('data/media_items.csv', 'utf-8')
.pipe(csv());
for await (const row of readable) {
const { id, type, title, year } = row;
media.push(new MediaItem(id, title, type, year, []));
}
return media;
}

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;
}
}