Skip to content

Commit 1f3b8e0

Browse files
committed
Feat/Combines credit and media items.
1 parent aeba25b commit 1f3b8e0

File tree

1 file changed

+27
-8
lines changed

1 file changed

+27
-8
lines changed

lesson_10/libraries/src/loaders/dylan_lafferty_loaders.ts

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,37 @@
11
import csv from 'csv-parser';
22
import fs from 'fs';
33
import { Credit, MediaItem } from '../models/index.js';
4+
import { Loader } from './loader.js';
45

5-
export class DylanLaffertysLoader {
6-
getLoaderName() {
6+
export class DylanLaffertysLoader implements Loader {
7+
getLoaderName(): string {
78
return 'dylanlafferty';
89
}
910
async loadData(): Promise<MediaItem[]> {
10-
const credits = await this.loadCredits();
11-
const mediaItems = await this.loadMediaItems();
11+
const credits = await this.loadCredits(); //loads credits from the csv file
12+
const mediaItems = await this.loadMediaItems(); //loads media items from csv file
13+
14+
//Creates a map where the key is a string and the value is MediaItem Object
15+
const mapIndex = new Map<string, MediaItem>();
16+
17+
//Loops through the CSV file and gets the id of the mediaItem that is specified.
18+
for (const mediaItem of mediaItems) {
19+
mapIndex.set(mediaItem.getId(), mediaItem);
20+
21+
}
22+
23+
for (const credit of credits) {
24+
const mediaItem = mapIndex.get(credit.getMediaItemId()); //Finds the media item by getting media ID
25+
if (mediaItem) {
26+
mediaItem.addCredit(credit);
27+
}
28+
}
29+
1230
console.log(
1331
`Loaded ${credits.length} credits and ${mediaItems.length} media items`,
1432
);
15-
return [...mediaItems.values()];
33+
//Returns a newly created array by converting the map values
34+
return Array.from(mapIndex.values());
1635
}
1736

1837
async loadMediaItems(): Promise<MediaItem[]> {
@@ -22,8 +41,8 @@ export class DylanLaffertysLoader {
2241
.pipe(csv());
2342

2443
for await (const row of readable) {
25-
const { id, title, type, releaseYear } = row;
26-
mediaItem.push(new MediaItem(id, title, type, parseInt(releaseYear), []));
44+
const { id, title, type, year } = row;
45+
mediaItem.push(new MediaItem(id, title, type, year, []));
2746
}
2847
return mediaItem;
2948
}
@@ -40,4 +59,4 @@ export class DylanLaffertysLoader {
4059
return credits;
4160
}
4261
}
43-
//# sourceMappingURL=dylan_lafferty_loaders.js.map
62+
//# sourceMappingURL=dylan_lafferty_loaders.js.map

0 commit comments

Comments
 (0)