Skip to content

Commit

Permalink
Refactor and enhance SalesDAO and AppController classes, adjust depen…
Browse files Browse the repository at this point in the history
…dencies in pom.xml, update SecurityConfig settings, add new elements to index.html, and revise data in sales.csv for better accuracy.
  • Loading branch information
tsviz committed Jan 22, 2024
1 parent df96a72 commit 240f66a
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 39 deletions.
8 changes: 6 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,11 @@
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>5.1.6.RELEASE</version>
</dependency>

<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
<version>5.1.6.RELEASE</version>
</dependency>

<dependency>
Expand Down Expand Up @@ -134,6 +132,12 @@
<artifactId>spring-session-data-redis</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
<!-- <version>2.1.10.RELEASE</version> -->
</dependency>

</dependencies>

<build>
Expand Down
21 changes: 14 additions & 7 deletions src/main/java/net/codejava/AppController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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<Sale> 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<Sale> salePage = dao.findAll(pageable);

model.addAttribute("enableSearchFeature", enableSearchFeature);
model.addAttribute("listSale", salePage.getContent());
model.addAttribute("currentPage", page);
model.addAttribute("totalPages", salePage.getTotalPages());

return "index";
}

Expand Down
39 changes: 37 additions & 2 deletions src/main/java/net/codejava/SalesDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -25,7 +28,16 @@ public List<Sale> 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);

Expand All @@ -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);
}

Expand All @@ -62,4 +84,17 @@ public List<Sale> search(String query) {
List<Sale> listSale = jdbcTemplate.query(sql, new Object[]{"%" + query.toLowerCase() + "%"}, new BeanPropertyRowMapper<>(Sale.class));
return listSale;
}

public Page<Sale> 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<Sale> sales = jdbcTemplate.query(query, new BeanPropertyRowMapper<>(Sale.class), pageable.getPageSize(), pageable.getOffset());

return new PageImpl<>(sales, pageable, total);
}
}
16 changes: 8 additions & 8 deletions src/main/java/net/codejava/SecurityConfig.java
Original file line number Diff line number Diff line change
@@ -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 {

Expand All @@ -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
Expand All @@ -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);
Expand Down
67 changes: 47 additions & 20 deletions src/main/resources/db/changelog/data/sales.csv
Original file line number Diff line number Diff line change
@@ -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
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
6 changes: 6 additions & 0 deletions src/main/resources/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@ <h1>Sales Records</h1>
</form>
</div>
<br />
<div>
<a th:if="${currentPage > 0}" th:href="@{/?(page=${currentPage - 1})}">Previous</a>
<span th:text="${currentPage + 1}"></span> / <span th:text="${totalPages}"></span>
<a th:if="${currentPage < totalPages - 1}" th:href="@{/?(page=${currentPage + 1})}">Next</a>
</div>
<br />
<form th:action="@{/logout}" method="post">
<input type="hidden" th:name="${_csrf.parameterName}" th:value="${_csrf.token}" />
<input type="submit" value="Logout" />
Expand Down

0 comments on commit 240f66a

Please sign in to comment.