Skip to content

Commit 840aecb

Browse files
committed
Feat: Adds Shawn Dunsmore Lesson010 Provider Class.
1 parent 96abdcd commit 840aecb

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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 ShawnDunsmoreLoader implements Loader {
7+
getLoaderName(): string {
8+
return 'shawndunsmore';
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+
return [...mediaItems.values()];
20+
}
21+
22+
async loadMediaItems(): Promise<MediaItem[]> {
23+
const mediaItems: MediaItem[] = [];
24+
const readable = fs
25+
.createReadStream('data/media_items.csv', 'utf-8')
26+
.pipe(csv());
27+
28+
for await (const row of readable) {
29+
const { id, title, type, year } = row; // Adjust the fields based on your CSV structure
30+
mediaItems.push(new MediaItem(id, title, type, year, [])); // Assuming MediaItem constructor matches this
31+
}
32+
33+
return mediaItems;
34+
}
35+
36+
async loadCredits(): Promise<Credit[]> {
37+
const credits = [];
38+
const readable = fs
39+
.createReadStream('data/credits.csv', 'utf-8')
40+
.pipe(csv());
41+
for await (const row of readable) {
42+
const { media_item_id, role, name } = row;
43+
credits.push(new Credit(media_item_id, name, role));
44+
}
45+
return credits;
46+
}
47+
}

0 commit comments

Comments
 (0)