diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..c574c3e --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..63e9001 --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/jarRepositories.xml b/.idea/jarRepositories.xml new file mode 100644 index 0000000..712ab9d --- /dev/null +++ b/.idea/jarRepositories.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/jpa-buddy.xml b/.idea/jpa-buddy.xml new file mode 100644 index 0000000..d08f400 --- /dev/null +++ b/.idea/jpa-buddy.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..12ea664 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,15 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/src/main/java/net/javaguides/springboot/config/SecurityConfiguration.java b/src/main/java/net/javaguides/springboot/config/SecurityConfiguration.java index 1417029..35b1788 100644 --- a/src/main/java/net/javaguides/springboot/config/SecurityConfiguration.java +++ b/src/main/java/net/javaguides/springboot/config/SecurityConfiguration.java @@ -41,7 +41,12 @@ public class SecurityConfiguration extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() - .antMatchers("/admin").hasRole("ADMIN") + .antMatchers( + "/admin", + "/deleteEmployee", + "/showFormForUpdate", + "/saveUser") + .hasRole("ADMIN") .antMatchers( "/registration**", "/js/**", diff --git a/src/main/java/net/javaguides/springboot/service/UserService.java b/src/main/java/net/javaguides/springboot/service/UserService.java index 28cbb4b..2c7b786 100644 --- a/src/main/java/net/javaguides/springboot/service/UserService.java +++ b/src/main/java/net/javaguides/springboot/service/UserService.java @@ -1,10 +1,19 @@ package net.javaguides.springboot.service; +import org.springframework.data.domain.Page; import org.springframework.security.core.userdetails.UserDetailsService; import net.javaguides.springboot.model.User; import net.javaguides.springboot.web.dto.UserRegistrationDto; +import java.util.List; + public interface UserService extends UserDetailsService{ User save(UserRegistrationDto registrationDto); + List getAllUsers(); + Page findPaginated(int pageNo, int pageSize, String sortField, String sortDirection); + User getUserById(long id); + void deleteUserById(long id); + void saveUser(User user); + } diff --git a/src/main/java/net/javaguides/springboot/service/UserServiceImpl.java b/src/main/java/net/javaguides/springboot/service/UserServiceImpl.java index f271014..a9f2927 100644 --- a/src/main/java/net/javaguides/springboot/service/UserServiceImpl.java +++ b/src/main/java/net/javaguides/springboot/service/UserServiceImpl.java @@ -2,9 +2,15 @@ package net.javaguides.springboot.service; import java.util.Arrays; import java.util.Collection; +import java.util.List; import java.util.stream.Collectors; +import java.util.Optional; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Pageable; +import org.springframework.data.domain.Sort; import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.authority.SimpleGrantedAuthority; import org.springframework.security.core.userdetails.UserDetails; @@ -52,5 +58,42 @@ public class UserServiceImpl implements UserService{ private Collection mapRolesToAuthorities(Collection roles){ return roles.stream().map(role -> new SimpleGrantedAuthority(role.getName())).collect(Collectors.toList()); } + + @Override + public Page findPaginated(int pageNo, int pageSize, String sortField, String sortDirection) { + Sort sort = sortDirection.equalsIgnoreCase(Sort.Direction.ASC.name()) ? Sort.by(sortField).ascending() : + Sort.by(sortField).descending(); + + Pageable pageable = PageRequest.of(pageNo - 1, pageSize, sort); + return this.userRepository.findAll(pageable); + } + + @Override + public List getAllUsers() { + return userRepository.findAll(); + } + + @Override + public User getUserById(long id) { + Optional optional = userRepository.findById(id); + User user = null; + if (optional.isPresent()) { + user = optional.get(); + } else { + throw new RuntimeException(" Employee not found for id :: " + id); + } + return user; + } + + @Override + public void deleteUserById(long id) { + this.userRepository.deleteById(id); + } + + @Override + public void saveUser(User user) { + this.userRepository.save(user); + } + } diff --git a/src/main/java/net/javaguides/springboot/web/MainController.java b/src/main/java/net/javaguides/springboot/web/MainController.java index d7cc127..97448a8 100644 --- a/src/main/java/net/javaguides/springboot/web/MainController.java +++ b/src/main/java/net/javaguides/springboot/web/MainController.java @@ -1,10 +1,28 @@ package net.javaguides.springboot.web; +import net.javaguides.springboot.service.UserService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Slice; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; +import java.util.List; +import net.javaguides.springboot.model.User; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestParam; + + +import org.springframework.web.bind.annotation.ModelAttribute; +import org.springframework.web.bind.annotation.PostMapping; + +import net.javaguides.springboot.service.UserService; + @Controller public class MainController { + @Autowired + private UserService userService; @GetMapping("/login") public String login() { @@ -13,11 +31,43 @@ public class MainController { @GetMapping("/") public String home() { + return "index"; } + + @GetMapping("/admin") - public String admin() { + public String admin(Model model) { + List listUsers = userService.getAllUsers(); + model.addAttribute("listUsers", listUsers); + return "admin"; } + @GetMapping("/deleteEmployee/{id}") + public String deleteEmployee(@PathVariable (value = "id") long id) { + + // call delete employee method + this.userService.deleteUserById(id); + return "redirect:/admin"; + } + + @GetMapping("/showFormForUpdate/{id}") + public String showFormForUpdate(@PathVariable ( value = "id") long id, Model model) { + + User user = userService.getUserById(id); + + model.addAttribute("user",user); + return "update_user"; + } + @PostMapping("/saveUser") + public String saveUser(@ModelAttribute("employee") User user) { + // save employee to database + userService.saveUser(user); + return "redirect:/"; + } + + + + } diff --git a/src/main/resources/templates/admin.html b/src/main/resources/templates/admin.html index c96d10d..7e2b6fe 100644 --- a/src/main/resources/templates/admin.html +++ b/src/main/resources/templates/admin.html @@ -37,9 +37,26 @@

-
-

The good shit

- Welcome Admin -
+ + + + + + + + + + + + + + + + + + +
#FirstNameLastNameEmail
Update + Delete +
diff --git a/src/main/resources/templates/index.html b/src/main/resources/templates/index.html index 6bfc30f..e2e4b14 100644 --- a/src/main/resources/templates/index.html +++ b/src/main/resources/templates/index.html @@ -29,6 +29,7 @@ @@ -41,8 +42,6 @@

Registration and Login with Spring Boot, Spring Security, Thymeleaf, Hibernate and MySQL

Welcome User - diff --git a/src/main/resources/templates/update_user.html b/src/main/resources/templates/update_user.html new file mode 100644 index 0000000..1ec3857 --- /dev/null +++ b/src/main/resources/templates/update_user.html @@ -0,0 +1,39 @@ + + + + +Employee Management System + + + + +
+

Employee Management System

+
+

Update Employee

+ +
+ + + + + + + + + + + + + + +
+ +
+ + Back to User List +
+ + \ No newline at end of file diff --git a/target/classes/net/javaguides/springboot/RegistrationLoginSpringBootSecurityThymeleafApplication.class b/target/classes/net/javaguides/springboot/RegistrationLoginSpringBootSecurityThymeleafApplication.class index 7d874a8..2f799b9 100644 Binary files a/target/classes/net/javaguides/springboot/RegistrationLoginSpringBootSecurityThymeleafApplication.class and b/target/classes/net/javaguides/springboot/RegistrationLoginSpringBootSecurityThymeleafApplication.class differ diff --git a/target/classes/net/javaguides/springboot/config/SecurityConfiguration.class b/target/classes/net/javaguides/springboot/config/SecurityConfiguration.class index 8e7539f..9d6c9d3 100644 Binary files a/target/classes/net/javaguides/springboot/config/SecurityConfiguration.class and b/target/classes/net/javaguides/springboot/config/SecurityConfiguration.class differ diff --git a/target/classes/net/javaguides/springboot/model/Role.class b/target/classes/net/javaguides/springboot/model/Role.class index 1f7cf0f..e5f98a2 100644 Binary files a/target/classes/net/javaguides/springboot/model/Role.class and b/target/classes/net/javaguides/springboot/model/Role.class differ diff --git a/target/classes/net/javaguides/springboot/model/User.class b/target/classes/net/javaguides/springboot/model/User.class index 57b7ce2..74ab5fa 100644 Binary files a/target/classes/net/javaguides/springboot/model/User.class and b/target/classes/net/javaguides/springboot/model/User.class differ diff --git a/target/classes/net/javaguides/springboot/repository/UserRepository.class b/target/classes/net/javaguides/springboot/repository/UserRepository.class index d92141a..5cb5193 100644 Binary files a/target/classes/net/javaguides/springboot/repository/UserRepository.class and b/target/classes/net/javaguides/springboot/repository/UserRepository.class differ diff --git a/target/classes/net/javaguides/springboot/service/UserService.class b/target/classes/net/javaguides/springboot/service/UserService.class index 30aa8bd..a2da106 100644 Binary files a/target/classes/net/javaguides/springboot/service/UserService.class and b/target/classes/net/javaguides/springboot/service/UserService.class differ diff --git a/target/classes/net/javaguides/springboot/service/UserServiceImpl.class b/target/classes/net/javaguides/springboot/service/UserServiceImpl.class index 35e18b2..cdf442f 100644 Binary files a/target/classes/net/javaguides/springboot/service/UserServiceImpl.class and b/target/classes/net/javaguides/springboot/service/UserServiceImpl.class differ diff --git a/target/classes/net/javaguides/springboot/web/MainController.class b/target/classes/net/javaguides/springboot/web/MainController.class index 62bd005..31c5907 100644 Binary files a/target/classes/net/javaguides/springboot/web/MainController.class and b/target/classes/net/javaguides/springboot/web/MainController.class differ diff --git a/target/classes/net/javaguides/springboot/web/UserRegistrationController.class b/target/classes/net/javaguides/springboot/web/UserRegistrationController.class index 2498eba..c737d78 100644 Binary files a/target/classes/net/javaguides/springboot/web/UserRegistrationController.class and b/target/classes/net/javaguides/springboot/web/UserRegistrationController.class differ diff --git a/target/classes/net/javaguides/springboot/web/dto/UserRegistrationDto.class b/target/classes/net/javaguides/springboot/web/dto/UserRegistrationDto.class index d6c9444..b43d61c 100644 Binary files a/target/classes/net/javaguides/springboot/web/dto/UserRegistrationDto.class and b/target/classes/net/javaguides/springboot/web/dto/UserRegistrationDto.class differ diff --git a/target/classes/templates/admin.html b/target/classes/templates/admin.html new file mode 100644 index 0000000..7e2b6fe --- /dev/null +++ b/target/classes/templates/admin.html @@ -0,0 +1,62 @@ + + + + +Registration and Login App + + + + + + + + +
+
+ + + + + + + + + + + + + + + + + + + +
#FirstNameLastNameEmail
Update + Delete +
+ + diff --git a/target/classes/templates/index.html b/target/classes/templates/index.html index 6bfc30f..e2e4b14 100644 --- a/target/classes/templates/index.html +++ b/target/classes/templates/index.html @@ -29,6 +29,7 @@ @@ -41,8 +42,6 @@

Registration and Login with Spring Boot, Spring Security, Thymeleaf, Hibernate and MySQL

Welcome User - diff --git a/target/classes/templates/update_user.html b/target/classes/templates/update_user.html new file mode 100644 index 0000000..1ec3857 --- /dev/null +++ b/target/classes/templates/update_user.html @@ -0,0 +1,39 @@ + + + + +Employee Management System + + + + +
+

Employee Management System

+
+

Update Employee

+ +
+ + + + + + + + + + + + + + +
+ +
+ + Back to User List +
+ + \ No newline at end of file diff --git a/target/test-classes/net/javaguides/springboot/RegistrationLoginSpringBootSecurityThymeleafApplicationTests.class b/target/test-classes/net/javaguides/springboot/RegistrationLoginSpringBootSecurityThymeleafApplicationTests.class index 44426f2..e6cb60c 100644 Binary files a/target/test-classes/net/javaguides/springboot/RegistrationLoginSpringBootSecurityThymeleafApplicationTests.class and b/target/test-classes/net/javaguides/springboot/RegistrationLoginSpringBootSecurityThymeleafApplicationTests.class differ