From 240f66a15aa11ed782d41869971cae66edbc4db7 Mon Sep 17 00:00:00 2001 From: Tsvi Zandany Date: Sun, 21 Jan 2024 20:38:04 -0600 Subject: [PATCH] Refactor and enhance SalesDAO and AppController classes, adjust dependencies in pom.xml, update SecurityConfig settings, add new elements to index.html, and revise data in sales.csv for better accuracy. --- pom.xml | 8 ++- src/main/java/net/codejava/AppController.java | 21 ++++-- src/main/java/net/codejava/SalesDAO.java | 39 ++++++++++- .../java/net/codejava/SecurityConfig.java | 16 ++--- .../resources/db/changelog/data/sales.csv | 67 +++++++++++++------ src/main/resources/templates/index.html | 6 ++ 6 files changed, 118 insertions(+), 39 deletions(-) diff --git a/pom.xml b/pom.xml index 25f53e7..6cb6185 100644 --- a/pom.xml +++ b/pom.xml @@ -92,13 +92,11 @@ org.springframework.security spring-security-config - 5.1.6.RELEASE org.springframework.security spring-security-core - 5.1.6.RELEASE @@ -134,6 +132,12 @@ spring-session-data-redis + + org.springframework.data + spring-data-commons + + + diff --git a/src/main/java/net/codejava/AppController.java b/src/main/java/net/codejava/AppController.java index 1862d07..69ed7c4 100755 --- a/src/main/java/net/codejava/AppController.java +++ b/src/main/java/net/codejava/AppController.java @@ -6,6 +6,7 @@ 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; @@ -20,6 +21,9 @@ 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 @Controller @@ -42,13 +46,16 @@ public boolean getEnableSearchFeature() { } @RequestMapping("/") - public String viewHomePage(Model model , Principal principal) { - // if (principal != null) { - // User is logged in, add the data to the model - List listSale = dao.list(); - model.addAttribute("enableSearchFeature", enableSearchFeature); - model.addAttribute("listSale", listSale); - // } + public String viewHomePage(Model model , Principal principal, @RequestParam(defaultValue = "0") int page) { + int pageSize = 20; // number of records per page + Pageable pageable = PageRequest.of(page, pageSize); + Page salePage = dao.findAll(pageable); + + model.addAttribute("enableSearchFeature", enableSearchFeature); + model.addAttribute("listSale", salePage.getContent()); + model.addAttribute("currentPage", page); + model.addAttribute("totalPages", salePage.getTotalPages()); + return "index"; } diff --git a/src/main/java/net/codejava/SalesDAO.java b/src/main/java/net/codejava/SalesDAO.java index d6dd910..2d304ca 100755 --- a/src/main/java/net/codejava/SalesDAO.java +++ b/src/main/java/net/codejava/SalesDAO.java @@ -9,6 +9,9 @@ import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; import org.springframework.jdbc.core.simple.SimpleJdbcInsert; import org.springframework.stereotype.Repository; +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 { @@ -25,7 +28,16 @@ public List list() { } public void save(Sale sale) { - SimpleJdbcInsert insertActor = new SimpleJdbcInsert(jdbcTemplate); + if (sale == null) { + throw new IllegalArgumentException("Sale object cannot be null"); + } + + if (jdbcTemplate == null) { + throw new IllegalStateException("JdbcTemplate cannot be null"); + } + + SimpleJdbcInsert insertActor = + new SimpleJdbcInsert(jdbcTemplate != null ? jdbcTemplate : new JdbcTemplate()); insertActor.withTableName("sales").usingColumns("item", "quantity", "amount"); BeanPropertySqlParameterSource param = new BeanPropertySqlParameterSource(sale); @@ -40,9 +52,19 @@ public Sale get(int id) { } public void update(Sale sale) { + if (sale == null) { + throw new IllegalArgumentException("Sale object cannot be null"); + } + String sql = "UPDATE SALES SET item=:item, quantity=:quantity, amount=:amount WHERE id=:id"; BeanPropertySqlParameterSource param = new BeanPropertySqlParameterSource(sale); - NamedParameterJdbcTemplate template = new NamedParameterJdbcTemplate(jdbcTemplate); + + if (jdbcTemplate == null) { + throw new IllegalStateException("JdbcTemplate cannot be null"); + } + + NamedParameterJdbcTemplate template = + new NamedParameterJdbcTemplate(jdbcTemplate != null ? jdbcTemplate : new JdbcTemplate()); template.update(sql, param); } @@ -62,4 +84,17 @@ public List search(String query) { List listSale = jdbcTemplate.query(sql, new Object[]{"%" + query.toLowerCase() + "%"}, new BeanPropertyRowMapper<>(Sale.class)); return listSale; } + + public Page findAll(Pageable pageable) { + String countQuery = "SELECT count(*) FROM sales"; + Integer totalInteger = jdbcTemplate.queryForObject(countQuery, Integer.class); + + // Check if totalInteger is null + int total = (totalInteger != null) ? totalInteger : 0; + + String query = "SELECT * FROM sales ORDER BY id ASC LIMIT ? OFFSET ?"; + List sales = jdbcTemplate.query(query, new BeanPropertyRowMapper<>(Sale.class), pageable.getPageSize(), pageable.getOffset()); + + return new PageImpl<>(sales, pageable, total); + } } diff --git a/src/main/java/net/codejava/SecurityConfig.java b/src/main/java/net/codejava/SecurityConfig.java index a887839..6f3d10b 100644 --- a/src/main/java/net/codejava/SecurityConfig.java +++ b/src/main/java/net/codejava/SecurityConfig.java @@ -1,17 +1,17 @@ package net.codejava; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; + import org.springframework.security.authentication.AuthenticationManager; import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; +// import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; import org.springframework.security.crypto.password.PasswordEncoder; -@Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @@ -21,11 +21,6 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter { @Autowired private PasswordEncoder passwordEncoder; - @Bean - public AuthenticationManager authenticationManagerBean() throws Exception { - return super.authenticationManagerBean(); - } - @Override protected void configure(HttpSecurity http) throws Exception { http @@ -49,6 +44,11 @@ protected void configure(HttpSecurity http) throws Exception { .permitAll(); } + @Bean + public AuthenticationManager customAuthenticationManager() throws Exception { + return authenticationManager(); + } + @Autowired public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder); diff --git a/src/main/resources/db/changelog/data/sales.csv b/src/main/resources/db/changelog/data/sales.csv index 8bc5a30..12de216 100644 --- a/src/main/resources/db/changelog/data/sales.csv +++ b/src/main/resources/db/changelog/data/sales.csv @@ -1,21 +1,48 @@ item;amount;quantity;date -Laptop;1000.00;5;2020-01-01 -Desktop;800.00;3;2020-01-02 -Printer;500.00;10;2020-01-03 -Keyboard;50.00;20;2020-01-04 -Mouse;20.00;50;2020-01-05 -Monitor;300.00;2;2020-01-06 -Headphones;80.00;10;2020-01-07 -Speakers;150.00;5;2020-01-08 -Tablet;600.00;4;2020-01-09 -Smartphone;800.00;3;2020-01-10 -Camera;400.00;6;2020-01-11 -Printer Ink;30.00;50;2020-01-12 -External Hard Drive;120.00;8;2020-01-13 -USB Flash Drive;20.00;100;2020-01-14 -Ethernet Cable;10.00;50;2020-01-15 -HDMI Cable;15.00;30;2020-01-16 -VGA Cable;10.00;30;2020-01-17 -Power Strip;25.00;20;2020-01-18 -Extension Cord;15.00;20;2020-01-19 -Wireless Router;80.00;5;2020-01-20 \ No newline at end of file +Laptop;1500.00;5;2020-01-01 +Desktop;1200.00;3;2020-01-02 +Printer;300.00;10;2020-01-03 +Keyboard;70.00;20;2020-01-04 +Mouse;30.00;50;2020-01-05 +Monitor;200.00;2;2020-01-06 +Headphones;100.00;10;2020-01-07 +Speakers;120.00;5;2020-01-08 +Tablet;500.00;4;2020-01-09 +Smartphone;700.00;3;2020-01-10 +Camera;500.00;6;2020-01-11 +Printer Ink;40.00;50;2020-01-12 +External Hard Drive;150.00;8;2020-01-13 +USB Flash Drive;25.00;100;2020-01-14 +Ethernet Cable;15.00;50;2020-01-15 +HDMI Cable;20.00;30;2020-01-16 +VGA Cable;15.00;30;2020-01-17 +Power Strip;30.00;20;2020-01-18 +Extension Cord;20.00;20;2020-01-19 +Wireless Router;90.00;5;2020-01-20 +Laptop Charger;60.00;15;2020-01-21 +Smartphone Charger;25.00;30;2020-01-22 +Camera Charger;30.00;20;2020-01-23 +Printer Cable;15.00;40;2020-01-24 +External Hard Drive Case;20.00;25;2020-01-25 +USB Flash Drive Case;10.00;50;2020-01-26 +Ethernet Cable Organizer;15.00;30;2020-01-27 +HDMI Cable Adapter;20.00;20;2020-01-28 +VGA Cable Adapter;15.00;20;2020-01-29 +Power Strip with USB Ports;35.00;10;2020-01-30 +USB-C Cable;17.00;35;2020-01-31 +Wireless Mouse;30.00;25;2020-02-01 +Wireless Keyboard;40.00;20;2020-02-02 +Laptop Stand;50.00;15;2020-02-03 +Monitor Stand;45.00;15;2020-02-04 +Webcam Cover;7.00;50;2020-02-05 +Screen Cleaner Kit;15.00;30;2020-02-06 +Laptop Cooling Pad;30.00;20;2020-02-07 +USB Hub;20.00;20;2020-02-08 +Monitor Light Bar;50.00;10;2020-02-09 +Bluetooth Headset;35.00;20;2020-02-10 +Wireless Earbuds;60.00;15;2020-02-11 +Portable Speaker;45.00;15;2020-02-12 +Smart Watch;80.00;10;2020-02-13 +Fitness Tracker;70.00;15;2020-02-14 +Tablet Stand;25.00;25;2020-02-15 +Phone Holder;15.00;30;2020-02-16 diff --git a/src/main/resources/templates/index.html b/src/main/resources/templates/index.html index e81c5c2..078599d 100755 --- a/src/main/resources/templates/index.html +++ b/src/main/resources/templates/index.html @@ -83,6 +83,12 @@

Sales Records


+
+ Previous + / + Next +
+