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 #314

Closed
wants to merge 2 commits into from
Closed
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,98 @@
package com.codedifferently.lesson12.factory.mohamedibrahim;

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 java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;
import org.springframework.stereotype.Service;

@Service
public class CsvLoader implements LibraryCsvDataLoader {

@Override
public LibraryDataModel loadData() throws IOException {
LibraryDataModel libraryData = new LibraryDataModel();

libraryData.guests = readGuestList("csv/guests.csv");
libraryData.mediaItems = readMediaItems("csv/media_items.csv");
libraryData.checkedOutItems = readCheckedOutByEmail("csv/checked_out_items.csv");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug here. checkedOutItems doesn't exist on LibraryDataModel.


// Gives each guest a list of their checked out books.
libraryData.guests.forEach(
guest ->
guest.checkedOutItems =
libraryData.checkedOutItems.getOrDefault(guest.email, new ArrayList<>()));
anthonydmays marked this conversation as resolved.
Show resolved Hide resolved

return libraryData;
}

private List<MediaItemModel> readMediaItems(String path) throws IOException {
try (BufferedReader reader = new BufferedReader(new FileReader(path))) {
return reader
.lines()
.skip(1)
.map(
line -> {
String[] parts = line.split(",", 8);
MediaItemModel item = new MediaItemModel();
item.type = parts[0];
item.id = UUID.fromString(parts[1]);
item.title = parts[2];
item.isbn = parts[3];
item.authors = List.of(parts[4]);
item.pages = parts[5].isEmpty() ? 0 : Integer.parseInt(parts[5]);
item.runtime = parts[6].isEmpty() ? 0 : Integer.parseInt(parts[6]);
item.edition = parts[7];
return item;
})
.collect(Collectors.toList());
}
}

private List<LibraryGuestModel> readGuestList(String path) throws IOException {
try (BufferedReader reader = new BufferedReader(new FileReader(path))) {
return reader
.lines()
.skip(1)
.map(
line -> {
String[] parts = line.split(",");
LibraryGuestModel guest = new LibraryGuestModel();
guest.type = parts[0];
guest.name = parts[1];
guest.email = parts[2];
return guest;
})
.collect(Collectors.toList());
}
}

private Map<String, List<CheckoutModel>> readCheckedOutByEmail(String path) throws IOException {
try (BufferedReader reader = new BufferedReader(new FileReader(path))) {
return reader
.lines()
.skip(1)
.map(
line -> {
String[] parts = line.split(",");
CheckoutModel item = new CheckoutModel();
item.itemId = UUID.fromString(parts[1]);
item.dueDate = Instant.parse(parts[2]);
return Map.entry(parts[0], item);
})
.collect(
Collectors.groupingBy(
Map.Entry::getKey, Collectors.mapping(Map.Entry::getValue, Collectors.toList())));
}
}
}