Skip to content

Commit 6daaa6d

Browse files
authored
feat: adds Aaron's CsvLoader Implementation (#335)
1 parent 7ce2abb commit 6daaa6d

File tree

1 file changed

+104
-0
lines changed
  • lesson_12/io/io_app/src/main/java/com/codedifferently/lesson12/factory/aaronsantiago

1 file changed

+104
-0
lines changed
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package com.codedifferently.lesson12.factory.aaronsantiago;
2+
3+
import com.codedifferently.lesson12.factory.LibraryCsvDataLoader;
4+
import com.codedifferently.lesson12.models.CheckoutModel;
5+
import com.codedifferently.lesson12.models.LibraryDataModel;
6+
import com.codedifferently.lesson12.models.LibraryGuestModel;
7+
import com.codedifferently.lesson12.models.MediaItemModel;
8+
import com.opencsv.CSVReader;
9+
import com.opencsv.exceptions.CsvException;
10+
import java.io.FileReader;
11+
import java.io.IOException;
12+
import java.time.Instant;
13+
import java.util.ArrayList;
14+
import java.util.Arrays;
15+
import java.util.HashMap;
16+
import java.util.List;
17+
import java.util.Map;
18+
import java.util.UUID;
19+
import org.springframework.core.io.ClassPathResource;
20+
import org.springframework.stereotype.Service;
21+
22+
@Service("AaronLibraryCsvDataLoader")
23+
public class CsvDataLoader implements LibraryCsvDataLoader {
24+
25+
@Override
26+
public LibraryDataModel loadData() {
27+
LibraryDataModel model = new LibraryDataModel();
28+
model.mediaItems = readMediaItems("csv/media_items.csv");
29+
model.guests = readGuests("csv/guests.csv");
30+
Map<String, List<CheckoutModel>> checkoutsByGuestEmail =
31+
getCheckedOutItems("csv/checked_out_items.csv");
32+
33+
// Combine checkouts with guests
34+
for (LibraryGuestModel guest : model.guests) {
35+
List<CheckoutModel> checkouts =
36+
checkoutsByGuestEmail.getOrDefault(guest.email, new ArrayList<>());
37+
guest.checkedOutItems = checkouts;
38+
}
39+
40+
return model;
41+
}
42+
43+
private List<MediaItemModel> readMediaItems(String filePath) {
44+
List<MediaItemModel> items = new ArrayList<>();
45+
try (CSVReader reader =
46+
new CSVReader(new FileReader(new ClassPathResource(filePath).getFile()))) {
47+
String[] header = reader.readNext(); // Skip the header
48+
String[] line;
49+
while ((line = reader.readNext()) != null) {
50+
MediaItemModel item = new MediaItemModel();
51+
item.type = line[0];
52+
item.id = UUID.fromString(line[1]);
53+
item.title = line[2];
54+
item.isbn = line[3];
55+
item.authors = Arrays.asList(line[4].split("\\s*,\\s*"));
56+
item.pages = line[5].isEmpty() ? 0 : Integer.parseInt(line[5]);
57+
item.runtime = line[6].isEmpty() ? 0 : Integer.parseInt(line[6]);
58+
item.edition = line[7];
59+
items.add(item);
60+
}
61+
} catch (IOException | CsvException e) {
62+
throw new RuntimeException("Failed to read media items", e);
63+
}
64+
return items;
65+
}
66+
67+
private List<LibraryGuestModel> readGuests(String filePath) {
68+
List<LibraryGuestModel> guests = new ArrayList<>();
69+
try (CSVReader reader =
70+
new CSVReader(new FileReader(new ClassPathResource(filePath).getFile()))) {
71+
String[] header = reader.readNext(); // Skip the header
72+
String[] line;
73+
while ((line = reader.readNext()) != null) {
74+
LibraryGuestModel guest = new LibraryGuestModel();
75+
guest.type = line[0];
76+
guest.name = line[1];
77+
guest.email = line[2];
78+
guests.add(guest);
79+
}
80+
} catch (IOException | CsvException e) {
81+
throw new RuntimeException("Failed to read guests", e);
82+
}
83+
return guests;
84+
}
85+
86+
private Map<String, List<CheckoutModel>> getCheckedOutItems(String filePath) {
87+
Map<String, List<CheckoutModel>> checkoutsByGuestEmail = new HashMap<>();
88+
try (CSVReader reader =
89+
new CSVReader(new FileReader(new ClassPathResource(filePath).getFile()))) {
90+
String[] header = reader.readNext(); // Skip the header
91+
String[] line;
92+
while ((line = reader.readNext()) != null) {
93+
String email = line[0];
94+
CheckoutModel item = new CheckoutModel();
95+
item.itemId = UUID.fromString(line[1]);
96+
item.dueDate = Instant.parse(line[2]);
97+
checkoutsByGuestEmail.computeIfAbsent(email, k -> new ArrayList<>()).add(item);
98+
}
99+
} catch (IOException | CsvException e) {
100+
throw new RuntimeException("Failed to read checked out items", e);
101+
}
102+
return checkoutsByGuestEmail;
103+
}
104+
}

0 commit comments

Comments
 (0)