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: csv data loader #307

Closed
wants to merge 15 commits into from
Closed
Show file tree
Hide file tree
Changes from 8 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
@@ -1,10 +1,12 @@
package com.codedifferently.lesson12.factory;

import com.codedifferently.lesson12.models.LibraryDataModel;
import java.io.IOException;

import com.codedifferently.lesson12.models.LibraryDataModel;

/** An object that loads data from a CSV and returns a LibraryDataModel object. */
public interface LibraryCsvDataLoader extends LibraryDataLoader {
@Override
public LibraryDataModel loadData() throws IOException;
}

@@ -0,0 +1,97 @@
package com.codedifferently.lesson12.factory.randycastro;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

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;


// create a path for each file
@Service
public class CsvDataLoader implements LibraryCsvDataLoader{
Copy link
Contributor

Choose a reason for hiding this comment

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

I can see this is still in progress, so feel free to continue working it if you'd like to refine.

@Override
public LibraryDataModel loadData() {
var model = new LibraryDataModel();
model.mediaItems = new ArrayList<>(); // You load this from the file
model.guests = new ArrayList<>(); // You also load this from the file.

List<MediaItemModel> mediaItems = readMediaItems("lesson_12/io/io_app/src/main/resources/csv/media_items.csv");
RandyCastr0 marked this conversation as resolved.
Show resolved Hide resolved
List<LibraryGuestModel> guests = readGuests("path/to/guests.csv");
Map<String, List<CheckoutModel>> checkoutsByGuestEmail = getCheckedOutItems("path/to/checked_out_items.csv");

for (var guest : guests) {
var checkouts = checkoutsByGuestEmail.get(guest.email);
if (checkouts != null) {
guest.checkedOutItems = checkouts;
}
}

return model;
}
private List<MediaItemModel> readMediaItems(String filePath) {
try (var reader = new BufferedReader(new FileReader(filePath))) {
var items = new ArrayList<MediaItemModel>();
String line;
while ((line = reader.readLine()) != null) {
var parts = line.split(",");
var item = new MediaItemModel();

item.type = parts[0];
item.id = parts[1];
item.author = parts[2];
item.type = parts[3];
items.add(item);
}
return items;
} catch (IOException e) {
throw new RuntimeException("Failed to read media items", e);
}
}

}










/*List<MediaItemModel> readMediaItems(String filePath) {
List<MediaItemModel> items = new ArrayList<>();
try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
String line;
while ((line = reader.readLine()) != null) {
String[] parts = line.split(",");
MediaItemModel item = new MediaItemModel();
item.setType(parts[0]);
item.setId(parts[1]);
item.setAuthor(parts[2]);
item.setTitle(parts[3]); // Assuming 'title' instead of 'type' again
items.add(item);
}
} catch (IOException e) {
e.printStackTrace();
// Handle the exception appropriately
}
return items;
}
}*/