Skip to content

Commit 98717b3

Browse files
committed
feat Saunders modified loader files implementing MediaItem
1 parent b01aa8a commit 98717b3

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import csv from 'csv-parser';
2+
import fs from 'fs';
3+
import { Credit, MediaItem } from '../models/index.js';
4+
import { Loader } from './loader.js';
5+
6+
export class KhaylaSaundersLoader implements Loader {
7+
getLoaderName(): string {
8+
return 'khaylasaunders';
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+
//Extra credit was generated with the assistance of chatGPT
19+
const creditsMap: Record<string, Credit[]> = {};
20+
21+
for (const credit of credits) {
22+
const mediaItemId = credit.getMediaItemId();
23+
if (!creditsMap[mediaItemId]) {
24+
creditsMap[mediaItemId] = [];
25+
}
26+
creditsMap[mediaItemId].push(credit);
27+
}
28+
29+
return [...mediaItems.values()];
30+
}
31+
32+
async loadMediaItems(): Promise<MediaItem[]> {
33+
// TODO: Implement this method.
34+
const mediaItems = [];
35+
const readable = fs
36+
.createReadStream('data/media_items.csv', 'utf-8')
37+
.pipe(csv());
38+
for await (const row of readable) {
39+
const { id, title, type, year } = row;
40+
mediaItems.push(new MediaItem(id, title, type, year, []));
41+
}
42+
return mediaItems;
43+
}
44+
45+
async loadCredits(): Promise<Credit[]> {
46+
const credits = [];
47+
const readable = fs
48+
.createReadStream('data/credits.csv', 'utf-8')
49+
.pipe(csv());
50+
for await (const row of readable) {
51+
const { media_item_id, role, name } = row;
52+
credits.push(new Credit(media_item_id, name, role));
53+
}
54+
return credits;
55+
}
56+
}

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

33
import { AnthonyMaysLoader } from './anthony_mays_loader.js';
4+
import { KhaylaSaundersLoader } from './khayla_saunders_loader.js';
45

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

7-
const LOADER_PROVIDERS = [AnthonyMaysLoader];
8+
const LOADER_PROVIDERS = [AnthonyMaysLoader, KhaylaSaundersLoader];
89

910
@Module({
1011
providers: [

0 commit comments

Comments
 (0)