Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion customers.csv
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
name,email
Maile Adkins,[email protected]
Lamar Long,[email protected]
Cynthia Harding,[email protected]
Expand Down
1 change: 0 additions & 1 deletion purchases.csv
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
customer_id,date,credit_card,cvv,category
82,2014-09-15T16:43:41,514 41005 05085 229,150,Alcohol
42,2012-11-19T00:57:50,5491248114297104,816,Furniture
31,2014-01-09T10:52:08,5326988945558924,302,Toiletries
Expand Down
49 changes: 49 additions & 0 deletions src/main/java/com/novauc/Customer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.novauc;

import javax.persistence.*;

@Entity
@Table(name = "customers")
public class Customer {
@Id
@GeneratedValue
int id;

@Column(nullable = false)
String name;

@Column(nullable = false)
String email;

public Customer(String name, String email) {
this.name = name;
this.email = email;
}

public Customer() {
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}
}
9 changes: 9 additions & 0 deletions src/main/java/com/novauc/CustomerRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.novauc;


import org.springframework.data.repository.CrudRepository;

public interface CustomerRepository extends CrudRepository<Customer, Integer> {
Customer findFirstById(int id);
}

87 changes: 87 additions & 0 deletions src/main/java/com/novauc/Purchase.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package com.novauc;


import javax.persistence.*;

@Entity
@Table(name = "purchases")
public class Purchase {

@ManyToOne
Customer customer;

@Id
@GeneratedValue
int id;

@Column(nullable = false)
private String date;

@Column(nullable = false)
private String creditCard;

@Column(nullable = false)
private String cvv;

@Column(nullable = false)
private String category;

public Purchase(Customer customer, String date, String creditCard, String cvv, String category) {
this.customer = customer;
this.date = date;
this.creditCard = creditCard;
this.cvv = cvv;
this.category = category;
}

public Purchase() {
}

public Customer getCustomer() {
return customer;
}

public void setCustomer(Customer customer) {
this.customer = customer;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getDate() {
return date;
}

public void setDate(String date) {
this.date = date;
}

public String getCreditCard() {
return creditCard;
}

public void setCreditCard(String creditCard) {
this.creditCard = creditCard;
}

public String getCvv() {
return cvv;
}

public void setCvv(String cvv) {
this.cvv = cvv;
}

public String getCategory() {
return category;
}

public void setCategory(String category) {
this.category = category;
}
}
13 changes: 13 additions & 0 deletions src/main/java/com/novauc/PurchaseRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.novauc;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.repository.PagingAndSortingRepository;


public interface PurchaseRepository extends PagingAndSortingRepository<Purchase, Integer> {
Page<Purchase> findByCategoryOrderByDateDesc(Pageable pageable, String category);
Page<Purchase> findAllByOrderByDateDesc(Pageable pageable);

// findAllByOrderByDateTimeAsc
}
84 changes: 84 additions & 0 deletions src/main/java/com/novauc/WalmartController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package com.novauc;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;


import javax.annotation.PostConstruct;
import java.io.File;
import java.sql.Date;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Scanner;


@Controller
public class WalmartController {
@Autowired
CustomerRepository customers;
@Autowired
PurchaseRepository purchases;

/***********************
* GET routes
***********************/
@RequestMapping(path = "/", method = RequestMethod.GET)
public String home(Model model, String category, Integer page) {
page = (page == null) ? 0 : page;
PageRequest pr = new PageRequest(page, 5);
Page<Purchase> p;
if (category != null) {
p = purchases.findByCategoryOrderByDateDesc(pr, category);
}

else {
p = purchases.findAllByOrderByDateDesc(pr);
}
model.addAttribute("purchases", p);
model.addAttribute("nextPage", page+1);
model.addAttribute("showNext", p.hasNext());
model.addAttribute("category", category);
return "home";
}

/***********************
* PostConstruct
***********************/


@PostConstruct
public void init() {
try {
Scanner scanner;
if (customers.count() == 0 && purchases.count() == 0) {
scanner = new Scanner(new File("customers.csv"));
while(scanner.hasNext()){
String[] data = scanner.nextLine().split(",");
customers.save(new Customer(data[0], data[1]));
}
scanner = new Scanner(new File("purchases.csv"));
while(scanner.hasNext()){
String[] data = scanner.nextLine().split(",");
purchases.save(new Purchase(checkCustomer(data[0]), data[1], data[2], data[3], data[4]));
}
}
} catch (Exception e){
System.out.println("Huston had a problem with scanning @" + e.getMessage());
}
}
public Customer checkCustomer(String id){
Customer customer = customers.findFirstById(Integer.valueOf(id));
if (customer == null){
System.out.println("customer was null");
//TODO TEST STATEMENT.
//Used to catch mismatched ID fields or invalid entries
}
return customer;
}
}
2 changes: 2 additions & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
spring.datasource.url=jdbc:postgresql://localhost:5432/wallyworld
spring.jpa.generate-ddl=true
65 changes: 65 additions & 0 deletions src/main/resources/templates/home.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>wallE-World</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<style>a{ margin: 0 12px; padding: 0 12px; font-weight: 800;}</style>
</head>
<body>
<header>
<div class="nav" style="text-align:center;">
<nav>
<a href="/">All</a>
<a href="/?category=Furniture">Furniture</a>
<a href="/?category=Alcohol">Alcohol</a>
<a href="/?category=Toiletries">Toiletries</a>
<a href="/?category=Shoes">Shoes</a>
<a href="/?category=Food">Food</a>
<a href="/?category=Jewelry">Jewelry</a>
</nav>
</div>
</header>
<div>
<h5 style="float:left;">{{#currentFilter}} Filter by: <strong>{{currentFilter}}</strong> {{/currentFilter}}{{#searchFilter}} Search parameter: <strong>{{searchFilter}}</strong> {{/searchFilter}}</h5>
<form action="/" method="get" style="display:inline; float:right;">
<input type="text" placeholder="Customer name:" name="customerName">
<button type="submit" class="btn btn-success">Search</button>
</form>
</div>
<table class="table table-hover table-striped">
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Category</th>
<th>Credit Card</th>
<th>CVV</th>
<th>Date</th>
</tr>
</thead>
<tbody>
{{#purchases}}
<tr>{{#customer}}
<td>{{name}}</td>
<td>{{email}}</td>
{{/customer}}
<td>{{category}}</td>
<td>{{creditCard}}</td>
<td>{{cvv}}</td>
<td>{{date}}</td>
</tr>
{{/purchases}}
</tbody>
</table>


{{#showNext}}
<a href="/?page={{nextPage}}{{#category}}&category={{.}}{{/category}}">Next</a>
{{/showNext}}
<br><br>



</body>
</html>