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
Closed
Changes from 8 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
3752afa
feat: created CsvDataLoader
RandyCastr0 ead0361
chore: created a path for each collection
RandyCastr0 56f38d6
Merge branch 'code-differently:main' into Randy-lesson12
RandyCastr0 cccc361
chore: update files
RandyCastr0 ca1d5d3
chore: added .java to my file
RandyCastr0 70d5ba5
chore: update file
RandyCastr0 c078131
Merge branch 'code-differently:main' into Randy-lesson12
RandyCastr0 0da2d39
chore:
RandyCastr0 93a2177
Merge branch 'code-differently:main' into Randy-lesson12
RandyCastr0 a986a3d
Merge branch 'code-differently:main' into Randy-lesson12
RandyCastr0 b4b31d7
Merge branch 'code-differently:main' into Randy-lesson12
RandyCastr0 7649e84
chore: update csvdataloader
RandyCastr0 ed3e9f8
Merge branch 'code-differently:main' into Randy-lesson12
RandyCastr0 ffc39cb
Merge branch 'code-differently:main' into Randy-lesson12
RandyCastr0 4ecd6f3
Merge branch 'code-differently:main' into Randy-lesson12
RandyCastr0 File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
4 changes: 3 additions & 1 deletion
4
...12/io/io_app/src/main/java/com/codedifferently/lesson12/factory/LibraryCsvDataLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
|
97 changes: 97 additions & 0 deletions
97
.../io_app/src/main/java/com/codedifferently/lesson12/factory/randycastro/CsvDataLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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{ | ||
@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; | ||
} | ||
}*/ | ||
|
||
|
||
|
||
|
||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.