Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 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
4 changes: 2 additions & 2 deletions lesson_10/libraries/src/loaders/loaders.module.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Module } from '@nestjs/common';
import { AnthonyMaysLoader } from './anthony_mays_loader.js';

import { XavierCruzLoader } from './xavier_cruz_loader.js';
export const Loaders = Symbol.for('Loaders');

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

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

export class XavierCruzLoader implements Loader {
getLoaderName(): string {
return 'xaviercruz';
}

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`,
);

let counter = 0;
for (let i = 0; i < mediaItems.length; i++) {
if (i < mediaItems.length / 2) {
mediaItems[i].addCredit(credits[counter] as Credit);
counter++;
mediaItems[i].addCredit(credits[counter] as Credit);
}
counter++;
}

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, type, title, year } = row;
mediaItems.push(new MediaItem(id, title, type, year, []));
}

return mediaItems;
}

async loadCredits(): Promise<Credit[]> {
const filePath = 'data/credits.csv';
const fileContents = fs.readFileSync(filePath, 'utf-8');

const lines = fileContents.split('\n');
const contentByLine = lines.slice(1);

for (let i = 0; i < contentByLine.length; i++) {
contentByLine[i] = contentByLine[i].substring(
contentByLine[i].indexOf(',') + 1,
);
}

// help from ChatGPT - Fixing the roleStr as Role issue
const credits: Credit[] = contentByLine.map((credit) => {
const [mediaItemId, roleStr, name] = credit.split(',');

// cast roleStr to Role type
const role: Role = roleStr as Role;

return new Credit(mediaItemId, name, role);
});

return credits;
}
}
6 changes: 3 additions & 3 deletions lesson_10/libraries/src/models/credit.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Role } from './role.js';

export class Credit {
private mediaItemId: string;
private name: string;
private role: Role;
mediaItemId: string;
name: string;
role: Role;

constructor(mediaItemId: string, name: string, role: Role) {
this.mediaItemId = mediaItemId;
Expand Down