Skip to content

Commit 3be6600

Browse files
feat: adds BryanaSingletonBarnhartLoader for loading media items and credits (#372)
Co-authored-by: Anthony D. Mays <[email protected]>
1 parent bd80d81 commit 3be6600

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-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 BryanaSingletonBarnhartLoader implements Loader {
7+
getLoaderName(): string {
8+
return 'bryanasingletonbarnhart';
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+
// TODO: Implement this method.g
24+
const mediaItems = [];
25+
const readable = fs
26+
.createReadStream('data/media_items.csv', 'utf-8')
27+
.pipe(csv());
28+
for await (const row of readable) {
29+
const { id, title, year, type } = row;
30+
mediaItems.push(new MediaItem(id, title, type, year, []));
31+
}
32+
return mediaItems;
33+
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+
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Module } from '@nestjs/common';
22
import { AnthonyMaysLoader } from './anthony_mays_loader.js';
3+
import { BryanaSingletonBarnhartLoader } from './bryana_singleton_barnhart_loader.js';
34
import { ChanelHuttLoader } from './chanel_hutt_loader.js';
45
import { DavidAdenaikeLoader } from './david_adenaike_loader.js';
56
import { DavisDLoader } from './davis_d_loader.js';
@@ -15,6 +16,7 @@ export const Loaders = Symbol.for('Loaders');
1516

1617
const LOADER_PROVIDERS = [
1718
AnthonyMaysLoader,
19+
BryanaSingletonBarnhartLoader,
1820
DylanLaffertysLoader,
1921
DavisDLoader,
2022
ChanelHuttLoader,

0 commit comments

Comments
 (0)