diff --git a/pom.xml b/pom.xml
index 00045c4..0fa474a 100644
--- a/pom.xml
+++ b/pom.xml
@@ -67,9 +67,13 @@
org.springframework
spring-tx
-
-
-
+
+
+
+ org.springframework.boot
+ spring-boot-starter-security
+
+
diff --git a/src/main/java/com/chantha/springdemo/Configuration/WebSecurityConfiguration.java b/src/main/java/com/chantha/springdemo/Configuration/WebSecurityConfiguration.java
new file mode 100644
index 0000000..1158fa6
--- /dev/null
+++ b/src/main/java/com/chantha/springdemo/Configuration/WebSecurityConfiguration.java
@@ -0,0 +1,38 @@
+package com.chantha.springdemo.Configuration;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+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.security.crypto.factory.PasswordEncoderFactories;
+import org.springframework.security.crypto.password.PasswordEncoder;
+import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
+
+@Configuration
+@EnableWebSecurity
+public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {
+
+ @Override
+ protected void configure(AuthenticationManagerBuilder auth) throws Exception {
+ auth.inMemoryAuthentication().withUser("admin").password("{noop}admin").roles("ADMIN")
+ .and().withUser("dba").password("{noop}dba").roles("DBA")
+ .and().withUser("user").password("{noop}user").roles("USER");
+ }
+
+ @Override
+ protected void configure(HttpSecurity http) throws Exception {
+ http.formLogin();
+ http.logout().logoutRequestMatcher(new AntPathRequestMatcher("/logout"));
+ http.authorizeRequests()
+ .antMatchers("/admin/**").hasAnyRole("ADMIN")
+ .antMatchers("/dba/**").hasAnyRole("ADMIN","DBA")
+ .antMatchers("/user/**").hasAnyRole("ADMIN","DBA","USER");
+ http.csrf().disable();
+ }
+
+
+}
diff --git a/src/main/java/com/chantha/springdemo/controller/HomeController.java b/src/main/java/com/chantha/springdemo/controller/HomeController.java
deleted file mode 100644
index e842f75..0000000
--- a/src/main/java/com/chantha/springdemo/controller/HomeController.java
+++ /dev/null
@@ -1,45 +0,0 @@
-package com.chantha.springdemo.controller;
-
-import java.util.List;
-
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.stereotype.Controller;
-import org.springframework.ui.Model;
-import org.springframework.web.bind.annotation.*;
-import com.chantha.springdemo.model.Food;
-import com.chantha.springdemo.service.FoodService;
-
-
-@Controller
-public class HomeController {
-
- @Autowired
- private final FoodService foodService;
-
- public HomeController(FoodService foodService) {
- super();
- this.foodService = foodService;
- }
- @RequestMapping("/home")
- public String show(Model mod) {
- List food=foodService.findFoodLimit();
- mod.addAttribute("foods", food);
- return "index";
- }
- @PostMapping("/food/update")
- public String updateProduct(@ModelAttribute(name="food") Food food) {
- foodService.saveFood(food);
- return "redirect:/home";
- }
-
- @RequestMapping("/food/delete/{id}")
- public String deleteFoodById(@PathVariable("id") int id) {
- foodService.deleteFoodById(id);
- return "redirect:/home";
- }
- @RequestMapping(value="/food/add",method=RequestMethod.POST)
- public String addProduct(@ModelAttribute(name="food") Food food) {
- foodService.saveFood(food);
- return "redirect:/home";
- }
-}
diff --git a/src/main/java/com/chantha/springdemo/controller/MainController.java b/src/main/java/com/chantha/springdemo/controller/MainController.java
new file mode 100644
index 0000000..19a7a0d
--- /dev/null
+++ b/src/main/java/com/chantha/springdemo/controller/MainController.java
@@ -0,0 +1,43 @@
+package com.chantha.springdemo.controller;
+
+import com.chantha.springdemo.model.Product;
+import com.chantha.springdemo.service.ProductService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Controller;
+import org.springframework.ui.Model;
+import org.springframework.web.bind.annotation.RequestMapping;
+
+import java.util.List;
+
+@Controller
+public class MainController {
+
+ @Autowired
+ private final ProductService productService;
+
+ public MainController(ProductService productService) {
+ this.productService=productService;
+ }
+ @RequestMapping("/")
+ public String indexPage(Model mod){
+ mod.addAttribute("products",productService.findAllProduct());
+ return "index";
+ }
+
+ @RequestMapping("/admin")
+ public String adminPage(Model model){
+ List products=productService.findAllProduct();
+ model.addAttribute("products",products);
+ return "admin/admin";
+ }
+
+ @RequestMapping("/dba")
+ public String dbaPage(){
+ return "dba/dba";
+ }
+
+ @RequestMapping("/user")
+ public String userPage(){
+ return "user/user";
+ }
+}
diff --git a/src/main/java/com/chantha/springdemo/controller/ProductController.java b/src/main/java/com/chantha/springdemo/controller/ProductController.java
new file mode 100644
index 0000000..66c2769
--- /dev/null
+++ b/src/main/java/com/chantha/springdemo/controller/ProductController.java
@@ -0,0 +1,101 @@
+package com.chantha.springdemo.controller;
+
+import com.chantha.springdemo.model.Product;
+import com.chantha.springdemo.service.ProductService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Controller;
+import org.springframework.util.StringUtils;
+import org.springframework.web.bind.annotation.*;
+import org.springframework.web.multipart.MultipartFile;
+import org.springframework.web.servlet.mvc.support.RedirectAttributes;
+
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.nio.file.StandardCopyOption;
+import java.time.LocalDate;
+
+
+@Controller
+public class ProductController {
+ @Autowired
+ private final ProductService productService;
+
+ public ProductController(ProductService productService) {
+ this.productService=productService;
+ }
+
+ public void uploadFile(MultipartFile file, RedirectAttributes attributes) {
+ final String UPLOAD_DIR="E:/Interview/springdemo/src/main/resources/static/img/";
+ // normalize the file path
+ String fileName = StringUtils.cleanPath(file.getOriginalFilename());
+
+ // save the file on the local file system
+ try {
+ Path path = Paths.get(UPLOAD_DIR + LocalDate.now()+fileName);
+ Files.copy(file.getInputStream(), path, StandardCopyOption.REPLACE_EXISTING);
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ @PostMapping("/admin/product/add")
+ public String addProduct(@RequestParam("file") MultipartFile file, @ModelAttribute("product") Product product, RedirectAttributes attributes){
+ try {
+ if(product.getName().equals("")||product.getColor().equals("")) {
+ attributes.addFlashAttribute("message", "Something wrong with your information");
+ return "redirect:/admin";
+ }
+ if(!file.isEmpty()){
+ uploadFile(file,attributes);
+ product.setImg(StringUtils.cleanPath(LocalDate.now()+file.getOriginalFilename()));
+ }
+ else{
+ product.setImg("default.jpg");
+ }
+ productService.save(product);
+ return "redirect:/admin";
+ }catch (Exception ex){
+ attributes.addFlashAttribute("message", ex.getMessage());
+ return "redirect:/admin";
+ }
+ }
+ @PostMapping("/admin/product/update")
+ public String updateProduct(@RequestParam("file") MultipartFile file, @ModelAttribute("product") Product product, RedirectAttributes attributes){
+ try {
+ if(product.getName().equals("")||product.getColor().equals("")) {
+ attributes.addFlashAttribute("message", "Something wrong with your information");
+ return "redirect:/admin";
+ }
+ if(!file.isEmpty()){
+ uploadFile(file,attributes);
+ product.setImg(StringUtils.cleanPath(LocalDate.now()+file.getOriginalFilename()));
+ }
+ else{
+ Product p=productService.findById(product.getId());
+ product.setImg(p.getImg());
+ }
+ attributes.addFlashAttribute("message", "Update Successfully");
+ productService.save(product);
+ return "redirect:/admin";
+ }catch (Exception ex){
+ attributes.addFlashAttribute("message", ex.getMessage());
+ return "redirect:/admin";
+ }
+ }
+ @RequestMapping("/admin/product/delete/{id}")
+ public String deleteProduct(@PathVariable Long id, RedirectAttributes attributes)
+ {
+ try{
+ Product pro=productService.findById(id);
+ productService.deleteById(id);
+ attributes.addFlashAttribute("message","Product name "+pro.getName()+" has delete successfully");
+ return "redirect:/admin";
+ }catch (Exception ex){
+ attributes.addFlashAttribute("message", "Sorry your product not register yet");
+ return "redirect:/admin";
+ }
+ }
+
+}
+
diff --git a/src/main/java/com/chantha/springdemo/controller/ProductRestController.java b/src/main/java/com/chantha/springdemo/controller/ProductRestController.java
new file mode 100644
index 0000000..fff4f10
--- /dev/null
+++ b/src/main/java/com/chantha/springdemo/controller/ProductRestController.java
@@ -0,0 +1,136 @@
+package com.chantha.springdemo.controller;
+
+import com.chantha.springdemo.model.Product;
+import com.chantha.springdemo.service.ProductService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.ResponseEntity;
+import org.springframework.validation.annotation.Validated;
+import org.springframework.web.bind.annotation.*;
+import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.validation.Valid;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+@RestController
+@RequestMapping("/api/product")
+public class ProductRestController {
+
+ private ProductService productService;
+ private Map map=null;
+ private HttpStatus status=null;
+
+ @Autowired
+ public ProductRestController(ProductService productService) {
+ this.productService = productService;
+ }
+
+ @GetMapping(value = "/findAll",headers = "Accept=application/json")
+ public ResponseEntity