Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: adds chukwumaibezim folder with CsvLoader class #315

Merged
merged 2 commits into from Mar 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1,101 @@
package com.codedifferently.lesson12.factory.chukwumaibezim;

import com.codedifferently.lesson12.factory.LibraryCsvDataLoader;
import com.codedifferently.lesson12.models.CheckoutModel;
import com.codedifferently.lesson12.models.LibraryDataModel;
import com.codedifferently.lesson12.models.LibraryGuestModel;
import com.codedifferently.lesson12.models.MediaItemModel;
import com.opencsv.CSVReader;
import com.opencsv.exceptions.CsvValidationException;
import java.io.FileReader;
import java.io.IOException;
import java.time.Instant;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Service;

@Service
public class CsvLoader implements LibraryCsvDataLoader {

@Override
public LibraryDataModel loadData() {
LibraryDataModel libraryDataModel = new LibraryDataModel();
try {
libraryDataModel.mediaItems = readMediaItemsFromCsv("csv/media_items.csv");
libraryDataModel.guests = readGuestsFromCsv("csv/guests.csv");
populateGuestsWithCheckouts("csv/checked_out_items.csv", libraryDataModel.guests);
} catch (IOException e) {
throw new RuntimeException("Failed to load data from CSV files", e);
} catch (CsvValidationException ex) {
}
return libraryDataModel;
}

private List<MediaItemModel> readMediaItemsFromCsv(String filePath)
throws IOException, CsvValidationException {
List<MediaItemModel> mediaItems = new ArrayList<>();
try (CSVReader reader =
new CSVReader(new FileReader(new ClassPathResource(filePath).getFile()))) {
reader.skip(1); // Skip header
String[] line;
while ((line = reader.readNext()) != null) {
MediaItemModel mediaItem = new MediaItemModel();
mediaItem.type = line[0];
mediaItem.id = UUID.fromString(line[1]);
mediaItem.title = line[2];
mediaItem.isbn = line[3];
mediaItem.authors = List.of(line[4].split("\\s*,\\s*"));
mediaItem.pages = line[5].isEmpty() ? 0 : Integer.parseInt(line[5]);
mediaItem.runtime = line[6].isEmpty() ? 0 : Integer.parseInt(line[6]);
mediaItem.edition = line[7];
mediaItems.add(mediaItem);
}
}
return mediaItems;
}

private List<LibraryGuestModel> readGuestsFromCsv(String filePath)
throws IOException, CsvValidationException {
List<LibraryGuestModel> guests = new ArrayList<>();
try (CSVReader reader =
new CSVReader(new FileReader(new ClassPathResource(filePath).getFile()))) {
reader.skip(1); // Skip header
String[] line;
while ((line = reader.readNext()) != null) {
LibraryGuestModel guest = new LibraryGuestModel();
guest.type = line[0];
guest.name = line[1];
guest.email = line[2];
guests.add(guest);
}
}
return guests;
}

private void populateGuestsWithCheckouts(String filePath, List<LibraryGuestModel> guests)
throws IOException, CsvValidationException {
Map<String, List<CheckoutModel>> checkoutsByGuestEmail = new HashMap<>();
try (CSVReader reader =
new CSVReader(new FileReader(new ClassPathResource(filePath).getFile()))) {
reader.skip(1); // Skip header
String[] line;
while ((line = reader.readNext()) != null) {
String email = line[0];
CheckoutModel checkout = new CheckoutModel();
checkout.itemId = UUID.fromString(line[1]);
checkout.dueDate = Instant.parse(line[2]);
checkoutsByGuestEmail.computeIfAbsent(email, k -> new ArrayList<>()).add(checkout);
}
}
// Assign checked-out items to respective guests
for (LibraryGuestModel guest : guests) {
List<CheckoutModel> checkouts =
checkoutsByGuestEmail.getOrDefault(guest.email, new ArrayList<>());
guest.checkedOutItems = checkouts;
}
}
}