Skip to content

Commit e775677

Browse files
author
Yafiaha
committed
Feat : Lesson-10 adding Yafiah Loader provider
1 parent 96abdcd commit e775677

File tree

2 files changed

+64
-1
lines changed

2 files changed

+64
-1
lines changed

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

910
@Module({
1011
providers: [
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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 YafiahAbdullahLoader implements Loader {
7+
getLoaderName(): string {
8+
return 'yafiahabdullah';
9+
}
10+
11+
async loadData(): Promise<MediaItem[]> {
12+
const [credits, mediaItems] = await Promise.all([
13+
this.loadCredits(),
14+
this.loadMediaItems(),
15+
]);
16+
// const credits = await this.loadCredits();
17+
// const mediaItems = await this.loadMediaItems();
18+
19+
console.log(
20+
`Loaded ${credits.length} credits and ${mediaItems.length} media items`,
21+
);
22+
23+
return [...mediaItems.values()];
24+
}
25+
26+
async loadMediaItems(): Promise<MediaItem[]> {
27+
// TODO: Implement this method.
28+
const media = [];
29+
const readable = fs
30+
.createReadStream('data/media_items.csv', 'utf-8')
31+
.pipe(csv());
32+
for await (const row of readable) {
33+
const { id, type, title, year } = row;
34+
media.push(new MediaItem(id, title, type, year, []));
35+
}
36+
return media;
37+
}
38+
39+
// //EXTRA CREDIT
40+
// async loadMediaItems(): Promise<MediaItem[]> {
41+
// const mediaItem = fs.readFileSync('data/media_items.csv',{
42+
// encoding: 'utf-8'
43+
// })
44+
// .split('\n')
45+
// .map((row:string): string[] =>{
46+
// return row.split(',');
47+
// })
48+
// return [];
49+
// }
50+
51+
async loadCredits(): Promise<Credit[]> {
52+
const credits = [];
53+
const readable = fs
54+
.createReadStream('data/credits.csv', 'utf-8')
55+
.pipe(csv());
56+
for await (const row of readable) {
57+
const { media_item_id, role, name } = row;
58+
credits.push(new Credit(media_item_id, name, role));
59+
}
60+
return credits;
61+
}
62+
}

0 commit comments

Comments
 (0)