Skip to content

Commit dc2ae77

Browse files
authored
feat: adds Rich's Lesson12 CSVDataLoader (#305)
* feat: Lesson12 CSVDataLoader - failing 2 tests. * fix: altered file paths * fix: fixes code to make all tests now work * chore: refactors filepath code to be more succinct
1 parent c5dd081 commit dc2ae77

File tree

1 file changed

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

1 file changed

+121
-0
lines changed
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
package com.codedifferently.lesson12.factory.richhawkins;
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
23+
public class CsvDataLoader implements LibraryCsvDataLoader {
24+
@Override
25+
public LibraryDataModel loadData() {
26+
var model = new LibraryDataModel();
27+
model.mediaItems = readMediaItems(("csv/media_items.csv"));
28+
model.guests = readGuests(("csv/guests.csv"));
29+
Map<String, List<CheckoutModel>> checkoutsByGuestEmail =
30+
getCheckedOutItems(("csv/checked_out_items.csv"));
31+
32+
// Combine checkouts with guests
33+
for (LibraryGuestModel guest : model.guests) {
34+
List<CheckoutModel> checkouts = checkoutsByGuestEmail.get(guest.email);
35+
if (checkouts != null) {
36+
guest.checkedOutItems = checkouts;
37+
} else {
38+
guest.checkedOutItems = new ArrayList<>();
39+
}
40+
}
41+
42+
model.guests.forEach(guest -> {});
43+
44+
return model;
45+
}
46+
47+
private List<MediaItemModel> readMediaItems(String filePath) {
48+
List<MediaItemModel> items = new ArrayList<>();
49+
try (CSVReader reader =
50+
new CSVReader(
51+
new FileReader(new ClassPathResource(filePath).getFile()))) { // Skip the header
52+
String[] header = reader.readNext();
53+
String[] line;
54+
while ((line = reader.readNext()) != null) {
55+
MediaItemModel item = new MediaItemModel();
56+
item.type = line[0];
57+
item.id = UUID.fromString(line[1]);
58+
item.title = line[2];
59+
item.isbn = line[3];
60+
item.authors = Arrays.asList(line[4].split("\\s*,\\s*"));
61+
// checks for empty index and if empty sets to 0
62+
if (line[5].equals("")) item.pages = 0;
63+
else item.pages = Integer.parseInt(line[5]);
64+
// checks for empty index and sets it to 0 if empty
65+
if (line[6].equals("")) item.pages = 0;
66+
else item.pages = Integer.parseInt(line[6]);
67+
item.edition = line[7];
68+
items.add(item);
69+
}
70+
} catch (IOException e) {
71+
throw new RuntimeException("Failed to read CSV file", e);
72+
} catch (CsvException e) {
73+
throw new RuntimeException("Error parsing CSV file", e);
74+
}
75+
return items;
76+
}
77+
78+
private List<LibraryGuestModel> readGuests(String filePath) {
79+
List<LibraryGuestModel> guests = new ArrayList<>();
80+
try (CSVReader reader =
81+
new CSVReader(
82+
new FileReader(new ClassPathResource(filePath).getFile()))) { // Skip the header
83+
String[] header = reader.readNext();
84+
String[] line;
85+
while ((line = reader.readNext()) != null) {
86+
LibraryGuestModel guest = new LibraryGuestModel();
87+
guest.type = line[0];
88+
guest.name = line[1];
89+
guest.email = line[2];
90+
guests.add(guest);
91+
}
92+
} catch (IOException e) {
93+
throw new RuntimeException("Failed to read CSV file", e);
94+
} catch (CsvException e) {
95+
throw new RuntimeException("Error parsing CSV file", e);
96+
}
97+
return guests;
98+
}
99+
100+
private Map<String, List<CheckoutModel>> getCheckedOutItems(String filePath) {
101+
Map<String, List<CheckoutModel>> checkoutsByGuestEmail = new HashMap<>();
102+
try (CSVReader reader =
103+
new CSVReader(new FileReader(new ClassPathResource(filePath).getFile()))) {
104+
// Skip the header
105+
String[] header = reader.readNext();
106+
String[] line;
107+
while ((line = reader.readNext()) != null) {
108+
String email = line[0];
109+
CheckoutModel item = new CheckoutModel();
110+
item.itemId = UUID.fromString(line[1]);
111+
item.dueDate = Instant.parse(line[2]);
112+
checkoutsByGuestEmail.computeIfAbsent(email, k -> new ArrayList<>()).add(item);
113+
}
114+
} catch (IOException e) {
115+
throw new RuntimeException("Failed to read checked out items", e);
116+
} catch (CsvException e) {
117+
throw new RuntimeException("Error parsing CSV file", e);
118+
}
119+
return checkoutsByGuestEmail;
120+
}
121+
}

0 commit comments

Comments
 (0)