1
1
import csv from 'csv-parser' ;
2
2
import fs from 'fs' ;
3
3
import { Credit , MediaItem } from '../models/index.js' ;
4
+ import { Loader } from './loader.js' ;
4
5
5
- export class DylanLaffertysLoader {
6
- getLoaderName ( ) {
6
+ export class DylanLaffertysLoader implements Loader {
7
+ getLoaderName ( ) : string {
7
8
return 'dylanlafferty' ;
8
9
}
9
10
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
+
12
30
console . log (
13
31
`Loaded ${ credits . length } credits and ${ mediaItems . length } media items` ,
14
32
) ;
15
- return [ ...mediaItems . values ( ) ] ;
33
+ //Returns a newly created array by converting the map values
34
+ return Array . from ( mapIndex . values ( ) ) ;
16
35
}
17
36
18
37
async loadMediaItems ( ) : Promise < MediaItem [ ] > {
@@ -22,8 +41,8 @@ export class DylanLaffertysLoader {
22
41
. pipe ( csv ( ) ) ;
23
42
24
43
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 , [ ] ) ) ;
27
46
}
28
47
return mediaItem ;
29
48
}
@@ -40,4 +59,4 @@ export class DylanLaffertysLoader {
40
59
return credits ;
41
60
}
42
61
}
43
- //# sourceMappingURL=dylan_lafferty_loaders.js.map
62
+ //# sourceMappingURL=dylan_lafferty_loaders.js.map
0 commit comments