Skip to content

Commit 5ad4219

Browse files
committed
feat: adding Dasias loader file to lesson_10
1 parent 96abdcd commit 5ad4219

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import csv from 'csv-parser';
2+
import fs from 'fs';
3+
4+
import { Credit, MediaItem } from '../models/index.js';
5+
import { Loader } from './loader.js';
6+
7+
export class DasiaEnglishLoader implements Loader {
8+
getLoaderName(): string {
9+
return 'anthonymays';
10+
}
11+
12+
async loadData(): Promise<MediaItem[]> {
13+
const credits = await this.loadCredits();
14+
const mediaItems = await this.loadMediaItems();
15+
16+
console.log(
17+
`Loaded ${credits.length} credits and ${mediaItems.length} media items`,
18+
);
19+
20+
return [...mediaItems.values()];
21+
}
22+
23+
async loadMediaItems(): Promise<MediaItem[]> {
24+
const matches = fs
25+
.readFileSync('data/media_items.csv', {
26+
encoding: 'utf-8',
27+
})
28+
.split('\n')
29+
.map((row: string): string[] => {
30+
return row.split(',');
31+
});
32+
console.log(matches);
33+
// I used this youtube video to help me understand the above code: https://www.youtube.com/watch?v=bbvECy0ICuo
34+
return [];
35+
}
36+
37+
async loadCredits(): Promise<Credit[]> {
38+
const credits = [];
39+
const readable = fs
40+
.createReadStream('data/credits.csv', 'utf-8')
41+
.pipe(csv());
42+
for await (const row of readable) {
43+
const { media_item_id: mediaItemId, role, name } = row;
44+
credits.push({ mediaItemId, name, role });
45+
}
46+
return credits;
47+
}
48+
}

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

910
@Module({
1011
providers: [

0 commit comments

Comments
 (0)