Skip to content

Commit b1db5f4

Browse files
author
jjcapparell
committed
feat: added james_capparell_loader.ts and edited loaders.module.ts for lesson_10 Loading the Media Collection assignment.
1 parent 96abdcd commit b1db5f4

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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 JamesCapparellLoader implements Loader {
7+
getLoaderName(): string {
8+
return 'jamescapparell';
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 medias = [];
24+
const readable = fs
25+
.createReadStream('data/media_items.csv', 'utf-8')
26+
.pipe(csv());
27+
for await (const row of readable) {
28+
const {id, title, type, year } = row;
29+
medias.push(new MediaItem(id, title, type, year, []));
30+
}
31+
return medias;
32+
}
33+
34+
async loadCredits(): Promise<Credit[]> {
35+
const credits = [];
36+
const readable = fs
37+
.createReadStream('data/credits.csv', 'utf-8')
38+
.pipe(csv());
39+
for await (const row of readable) {
40+
const { media_item_id, role, name } = row;
41+
credits.push(new Credit(media_item_id, name, role));
42+
}
43+
return credits;
44+
}
45+
}

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 { JamesCapparellLoader } from './james_capparell_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, JamesCapparellLoader];
89

910
@Module({
1011
providers: [

0 commit comments

Comments
 (0)