Skip to content

feat: solved merge conflict and generated loader file #349

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

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
57 changes: 57 additions & 0 deletions lesson_10/libraries/src/loaders/lj_mcwilliams_loader.ts
Original file line number Diff line number Diff line change
@@ -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<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[]> {
// 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<Credit[]> {
//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;
}
}
3 changes: 3 additions & 0 deletions lesson_10/libraries/src/loaders/loaders.module.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Module } from '@nestjs/common';
import { AngelicaCastilloLoader } from './angelica_castillo_loader.js';
import { AnthonyMaysLoader } from './anthony_mays_loader.js';
import { LjMcwilliamsLoader } from './lj_mcwilliams_loader.js';
import { JamesCapparellLoader } from './james_capparell_loader.js';
import { NileJacksonLoader } from './nile_jackson_loader.js';
import { XavierCruzLoader } from './xavier_cruz_loader.js';
Expand All @@ -10,6 +11,8 @@ export const Loaders = Symbol.for('Loaders');
// Add your quiz provider here.
const LOADER_PROVIDERS = [
AnthonyMaysLoader,
XavierCruzLoader,
LjMcwilliamsLoader,
AngelicaCastilloLoader,
JamesCapparellLoader,
NileJacksonLoader,
Expand Down