Skip to content

Commit

Permalink
feat: added Xavier's CSV loader (code-differently#331)
Browse files Browse the repository at this point in the history
* feat: started work on Xavier's lesson_10 parse CSV files/finished extra credit 1

* feat: manually parsed CSV for credits

* chore: removed extra variable

* chore: removed hardcoded 100 and replaced with half of media items length

* chore: removed out of context/resolved comments

* feat: updated credits to be a class, parsed with new class

* fix: fixed credit class

* feat: fixed connecting of credits to mediaItems

* Revert "feat: updated credits to be a class, parsed with new class"

This reverts commit 9abb8d6.

* feat: credit.ts matches credit.ts in main

* feat: reworked credit adding to each media item

* chore: formatting/organizing imports loaders.modules
  • Loading branch information
XavierCruz5106 authored Oct 17, 2024
1 parent bbf5a7b commit 6a45e46
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 1 deletion.
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 { 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`,
);

credits.forEach((credit) => {
const mediaItem = mediaItems.find(
(media) => media.getId() === credit.getMediaItemId(),
);

if (mediaItem) {
mediaItem.addCredit(credit);
}
});

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

0 comments on commit 6a45e46

Please sign in to comment.