Skip to content

Commit

Permalink
Add new features and fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
tsviz committed Jan 24, 2024
1 parent b3e7610 commit f97330a
Show file tree
Hide file tree
Showing 10 changed files with 122 additions and 15 deletions.
2 changes: 1 addition & 1 deletion build_and_run_app.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ trap cleanup EXIT

mvn clean package -Dmaven.test.skip=true
docker run -d -p 6379:6379 --name redis_container redis
java -jar target/salesmanager-*-SNAPSHOT.jar --spring.redis.host=localhost --spring.redis.port=6379 --spring.redis.mode=standalone --server.port=8086
java -jar target/salesmanager-*-SNAPSHOT.jar --spring.redis.host=localhost --spring.redis.port=6379 --spring.redis.mode=standalone --server.port=8086 --spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.H2Dialect
46 changes: 38 additions & 8 deletions src/main/java/net/codejava/AppController.java
Original file line number Diff line number Diff line change
@@ -1,32 +1,35 @@
package net.codejava;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Pageable;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
// import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import java.security.Principal;

import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.data.domain.PageRequest; // Add this import statement
import org.springframework.data.domain.Page; // Add this import statement
// import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;


@EnableJpaRepositories(basePackages = "net.codejava")
@Controller
@Transactional
public class AppController {

/**
Expand All @@ -38,6 +41,13 @@ public class AppController {
@Autowired
private AuthenticationManager authenticationManager;

// @Autowired
// private final SalesRecordRepository salesRecordRepository;

// public AppController(SalesRecordRepository salesRecordRepository) {
// this.salesRecordRepository = salesRecordRepository;
// }

@Value("${enableSearchFeature}")
private boolean enableSearchFeature;

Expand All @@ -58,13 +68,22 @@ public String viewHomePage(Model model , Principal principal, @RequestParam(defa

return "index";
}


// @GetMapping("/")
// public String showPage(Model model, @RequestParam(defaultValue="0") int page) {
// Pageable pageable = PageRequest.of(page, 5);
// Page<Sale> data = salesRecordRepository.findAll(pageable);
// data = data != null ? data : Page.empty(); // Assign an empty Page if data is null
// model.addAttribute("data", data);
// return "index";
// }

@RequestMapping("/new")
public String showNewForm(Model model) {
Sale sale = new Sale();
model.addAttribute("sale", sale);
return "new_form";
Sale sale = new Sale();
model.addAttribute("sale", sale);
return "new_form";
}

@RequestMapping(value = "/save", method = RequestMethod.POST)
Expand Down Expand Up @@ -133,4 +152,15 @@ public String search(@ModelAttribute("q") String query, Model model) {
model.addAttribute("listSale", listSale);
return "search";
}

@RequestMapping("/export")
public String exportCSV(@ModelAttribute("q") String query, Model model) {
try {
dao.exportCSV(query, "export.csv");
return "redirect:/";
} catch (Exception e) {
model.addAttribute("error", e.getMessage());
return "error";
}
}
}
13 changes: 10 additions & 3 deletions src/main/java/net/codejava/Sale.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
package net.codejava;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "sales")
public class Sale {
private int id;
@Id
private Long id;
private String item;
private int quantity;
private float amount;
Expand All @@ -15,11 +22,11 @@ protected Sale(final String item, final int quantity, final float amount) {
this.amount = amount;
}

public int getId() {
public Long getId() {
return id;
}

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

Expand Down
27 changes: 27 additions & 0 deletions src/main/java/net/codejava/SaleService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package net.codejava;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

@Service
public class SaleService {

private final SalesRecordRepository salesRecordRepository;

@Autowired
public SaleService(SalesRecordRepository salesRecordRepository) {
this.salesRecordRepository = salesRecordRepository;
}

public Page<Sale> getAllSales(Pageable pageable) {
if (pageable == null) {
// Handle the case where pageable is null, for example by throwing an exception
throw new IllegalArgumentException("Pageable must not be null");
}
System.out.println("Pageable: " + pageable); // print out the pageable parameter
Page<Sale> result = salesRecordRepository.findAll(pageable);
System.out.println("Result: " + result); // print out the result
return result;
}
}
27 changes: 27 additions & 0 deletions src/main/java/net/codejava/SalesDAO.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package net.codejava;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -9,10 +12,12 @@
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.data.domain.Page; // Import the Page class from the correct package
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable; // Import the Pageable class from the correct package


@Repository
public class SalesDAO {
@Autowired
Expand Down Expand Up @@ -97,4 +102,26 @@ public Page<Sale> findAll(Pageable pageable) {

return new PageImpl<>(sales, pageable, total);
}

@RequestMapping("/export")
public void exportCSV(String query, String filename) throws IOException {
// create an instance of FileWriter
FileWriter writer = new FileWriter(filename);
// create an instance of BufferedWriter
BufferedWriter bw = new BufferedWriter(writer);
// write the csv header
bw.write("id,item,quantity,amount");
bw.newLine();
// write the csv body
List<Sale> listSale = search(query);
for (Sale sale : listSale) {
bw.write(sale.getId() + "," + sale.getItem() + "," + sale.getQuantity() + "," + sale.getAmount());
bw.newLine();
}
// print a message
System.out.println("CSV file was created successfully.");
// close the BufferedWriter and FileWriter
bw.close();
writer.close();
}
}
12 changes: 12 additions & 0 deletions src/main/java/net/codejava/SalesRecordRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package net.codejava;

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

public interface SalesRecordRepository extends PagingAndSortingRepository<Sale, Long> {
@Override
@NonNull
Page<Sale> findAll(@NonNull Pageable pageable);
}
1 change: 1 addition & 0 deletions src/main/java/net/codejava/UserRepository.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package net.codejava;

import org.springframework.data.jpa.repository.JpaRepository;

public interface UserRepository extends JpaRepository<User, Long> {
Expand Down
6 changes: 4 additions & 2 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ server.port=8086
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
spring.main.allow-bean-definition-overriding=true
spring.jpa.hibernate.ddl-auto=none
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true
# spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.properties.hibernate.temp.use_jdbc_metadata_defaults=false
spring.redis.host=localhost
spring.redis.port=6379
# spring.data.jpa.repositories.enabled=false
# spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
spring.jpa.hibernate.ddl-auto=none
enableSearchFeature=true
1 change: 1 addition & 0 deletions src/main/resources/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
<div align="center" th:if="${listSale}">
<h1>Sales Records</h1>
<a href="/new">Enter New Sale</a>
<a href="/export">Export to CSV</a>
<br><br>
<div id="searchFeature">
<form action="/search" method="get">
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/net/codejava/JUnit5ExampleTest11.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ void testInsert() {
// assert that the latest item on the list is "test item 123"
assertEquals("test item 123", listSale.get(listSale.size()-1).getItem());
// clean up the database
salesDAO.delete(listSale.get(listSale.size()-1).getId());
salesDAO.delete(listSale.get(listSale.size()-1).getId().intValue());
System.out.println("\n\nTest11-1 Successful!\n\n");
}

Expand Down

0 comments on commit f97330a

Please sign in to comment.