Skip to content

Commit 4adab3c

Browse files
author
marleomac3
committed
feat: solved merge conflict and generated loader file
1 parent 6a45e46 commit 4adab3c

File tree

2 files changed

+63
-1
lines changed

2 files changed

+63
-1
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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 LjMcwilliamsLoader implements Loader {
7+
getLoaderName(): string {
8+
return 'ljmcwilliams';
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.
24+
const media = [];
25+
const readable = fs
26+
.createReadStream('data/media_items.csv', 'utf-8')
27+
.pipe(csv());
28+
for await (const row of readable) {
29+
/**this destructures the CSV file rows */
30+
const { id, type, title, year } = row;
31+
media.push(new MediaItem(id, title, type, year, []));
32+
}
33+
return media;
34+
}
35+
36+
/*
37+
an asyncchronous function named loadCredits
38+
returns a Promise of an array of Credit Objects
39+
*/
40+
async loadCredits(): Promise<Credit[]> {
41+
//the empty credits array will store parsed credit data
42+
const credits = [];
43+
/**
44+
* this var creates a readable stream from the CSV file and
45+
* is piped through the csv function to parse the data
46+
*/
47+
const readable = fs
48+
.createReadStream('data/credits.csv', 'utf-8')
49+
.pipe(csv());
50+
//this asynchronously iterates over each row of the parsed CSV data
51+
for await (const row of readable) {
52+
const { media_item_id, role, name } = row;
53+
credits.push(new Credit(media_item_id, name, role));
54+
}
55+
return credits;
56+
}
57+
}

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
import { Module } from '@nestjs/common';
22
import { AnthonyMaysLoader } from './anthony_mays_loader.js';
3+
import { LjMcwilliamsLoader } from './lj_mcwilliams_loader.js';
34
import { XavierCruzLoader } from './xavier_cruz_loader.js';
45

56
export const Loaders = Symbol.for('Loaders');
67

78
// Add your quiz provider here.
8-
const LOADER_PROVIDERS = [AnthonyMaysLoader, XavierCruzLoader];
9+
const LOADER_PROVIDERS = [
10+
AnthonyMaysLoader,
11+
XavierCruzLoader,
12+
LjMcwilliamsLoader,
13+
];
914

1015
@Module({
1116
providers: [

0 commit comments

Comments
 (0)