Skip to content

Commit 6a45e46

Browse files
feat: added Xavier's CSV loader (#331)
* 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
1 parent bbf5a7b commit 6a45e46

File tree

2 files changed

+73
-1
lines changed

2 files changed

+73
-1
lines changed

lesson_10/libraries/src/loaders/loaders.module.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import { Module } from '@nestjs/common';
22
import { AnthonyMaysLoader } from './anthony_mays_loader.js';
3+
import { XavierCruzLoader } from './xavier_cruz_loader.js';
34

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

67
// Add your quiz provider here.
7-
const LOADER_PROVIDERS = [AnthonyMaysLoader];
8+
const LOADER_PROVIDERS = [AnthonyMaysLoader, XavierCruzLoader];
89

910
@Module({
1011
providers: [
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import csv from 'csv-parser';
2+
import fs from 'fs';
3+
import { Credit, MediaItem, Role } from '../models/index.js';
4+
import { Loader } from './loader.js';
5+
6+
export class XavierCruzLoader implements Loader {
7+
getLoaderName(): string {
8+
return 'xaviercruz';
9+
}
10+
11+
async loadData(): Promise<MediaItem[]> {
12+
const credits = await this.loadCredits();
13+
const mediaItems = await this.loadMediaItems();
14+
15+
console.log(
16+
`Loaded ${credits.length} credits and ${mediaItems.length} media items`,
17+
);
18+
19+
credits.forEach((credit) => {
20+
const mediaItem = mediaItems.find(
21+
(media) => media.getId() === credit.getMediaItemId(),
22+
);
23+
24+
if (mediaItem) {
25+
mediaItem.addCredit(credit);
26+
}
27+
});
28+
29+
return [...mediaItems.values()];
30+
}
31+
32+
async loadMediaItems(): Promise<MediaItem[]> {
33+
const mediaItems = [];
34+
const readable = fs
35+
.createReadStream('data/media_items.csv', 'utf-8')
36+
.pipe(csv());
37+
38+
for await (const row of readable) {
39+
const { id, type, title, year } = row;
40+
mediaItems.push(new MediaItem(id, title, type, year, []));
41+
}
42+
43+
return mediaItems;
44+
}
45+
46+
async loadCredits(): Promise<Credit[]> {
47+
const filePath = 'data/credits.csv';
48+
const fileContents = fs.readFileSync(filePath, 'utf-8');
49+
50+
const lines = fileContents.split('\n');
51+
const contentByLine = lines.slice(1);
52+
53+
for (let i = 0; i < contentByLine.length; i++) {
54+
contentByLine[i] = contentByLine[i].substring(
55+
contentByLine[i].indexOf(',') + 1,
56+
);
57+
}
58+
59+
// help from ChatGPT - Fixing the roleStr as Role issue
60+
const credits: Credit[] = contentByLine.map((credit) => {
61+
const [mediaItemId, roleStr, name] = credit.split(',');
62+
63+
// cast roleStr to Role type
64+
const role: Role = roleStr as Role;
65+
66+
return new Credit(mediaItemId, name, role);
67+
});
68+
69+
return credits;
70+
}
71+
}

0 commit comments

Comments
 (0)