Fork your Repository from here.
Let's write a purchase history table! But first, enjoy this gif of two hamsters in a hamster wheel.
customers.csv and purchases.csv are already in the root of your project, parse them, and store them in PostgreSQL via Spring Data. Then display the purchases along with the name and email of the customer who made them using the entity joining capability of Spring Data.
- Create a database in PostgreSQL and add the appropriate settings to your
application.properties - Create an empty
home.htmltemplate and a/route in your controller to serve it - Create
CustomerandPurchaseclasses that can hold all the info from their respective CSVs, along with an id (add the appropriate annotations so it can be used with Spring Data)- The
Purchaseclass should also have aCustomerfield which will serve to join the two tables together - You don't need to store the
customer_idin yourPurchaseclass, because the join system will create that column for you
- The
- Create
CustomerRepositoryandPurchaseRepositoryinterfaces that can act as Spring Data repositories for the aforementioned classes - In your controller, create a
@PostConstructmethod calledinitto parse the CSV files- For each CSV file, you should loop over each line, parse each column into a
CustomerorPurchaseobject, and add it to a repository - Make it so this only happens when the repositories are empty
- For each CSV file, you should loop over each line, parse each column into a
- Edit
home.htmland the/route to make it display the purchases- It should display the results in an HTML table so the columns line up (see screenshot below)
- You should be able to see the name and email from the customer table, and the category, credit card, cvv, and date fields from the purchase table
- Filter the table by category via a query parameter
- In
PurchaseRepository, add a method that findsPurchaseobjects by category - Modify your
/route to take acategoryparameter - If the parameter isn't null, call the method you made instead of
findAll - Displays links at the top that allow you to filter by each category
- In

