Changeset 7d43957 for src/main/java/com
- Timestamp:
- 01/05/23 01:31:58 (23 months ago)
- Branches:
- main
- Children:
- 676144b
- Parents:
- ab952ab
- Location:
- src/main/java/com/example/autopartz
- Files:
-
- 8 added
- 15 edited
Legend:
- Unmodified
- Added
- Removed
-
src/main/java/com/example/autopartz/AutoPartzApplication.java
rab952ab r7d43957 6 6 import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; 7 7 import org.springframework.security.crypto.password.PasswordEncoder; 8 import org.thymeleaf.extras.springsecurity5.dialect.SpringSecurityDialect; 8 9 9 10 @SpringBootApplication … … 17 18 return new BCryptPasswordEncoder(10); 18 19 } 19 20 @Bean 21 public SpringSecurityDialect securityDialect() { 22 return new SpringSecurityDialect(); 23 } 20 24 21 25 } -
src/main/java/com/example/autopartz/config/WebSecurityConfig.java
rab952ab r7d43957 28 28 http.csrf().disable() 29 29 .authorizeRequests() 30 .antMatchers("/", "/products", "/services", "/filtered", "/login", "/register" ).permitAll()31 .antMatchers("/orders","/repairs","/reviews","/part/*" ).hasRole("CLIENT")30 .antMatchers("/", "/products", "/services", "/filtered", "/login", "/register","/registerWarehouseman","/finishRegister","/test/*").permitAll() 31 .antMatchers("/orders","/repairs","/reviews","/part/*","/currentOrder").hasRole("CLIENT") 32 32 .anyRequest() 33 33 .authenticated() -
src/main/java/com/example/autopartz/controller/HomeController.java
rab952ab r7d43957 1 1 package com.example.autopartz.controller; 2 2 3 import com.example.autopartz.model.Order; 3 4 import com.example.autopartz.model.User; 5 import com.example.autopartz.model.Warehouse; 6 import com.example.autopartz.repository.OrderContainsPartRepository; 4 7 import com.example.autopartz.repository.PartsForCarTypeAndCategoryRepository; 5 8 import com.example.autopartz.repository.RepairShopReviewSummaryRepository; 6 import com.example.autopartz.service.CarService; 7 import com.example.autopartz.service.CategoryService; 8 import com.example.autopartz.service.LoginService; 9 import com.example.autopartz.service.PartService; 9 import com.example.autopartz.repository.WarehouseRepository; 10 import com.example.autopartz.service.*; 10 11 import org.springframework.stereotype.Controller; 11 12 import org.springframework.ui.Model; … … 16 17 17 18 import javax.servlet.http.HttpServletRequest; 19 import javax.servlet.http.HttpServletResponse; 20 import javax.servlet.http.HttpSession; 21 import java.io.IOException; 22 import java.util.Objects; 18 23 19 24 @Controller … … 26 31 private final CategoryService categoryService; 27 32 private final RepairShopReviewSummaryRepository repairShopReviewSummaryRepository; 33 private final WarehouseRepository warehouseRepository; 34 private final OrderContainsPartRepository orderContainsPartRepository; 35 private final OrderService orderService; 28 36 29 public HomeController(LoginService loginService, PartService partService, PartsForCarTypeAndCategoryRepository partsForCarTypeAndCategoryRepository, CarService carService, CategoryService categoryService, RepairShopReviewSummaryRepository repairShopReviewSummaryRepository) { 37 public HomeController(LoginService loginService, PartService partService, PartsForCarTypeAndCategoryRepository partsForCarTypeAndCategoryRepository, CarService carService, CategoryService categoryService, RepairShopReviewSummaryRepository repairShopReviewSummaryRepository, WarehouseRepository warehouseRepository, 38 OrderContainsPartRepository orderContainsPartRepository, OrderService orderService) { 30 39 this.loginService = loginService; 31 40 this.partService = partService; … … 34 43 this.categoryService = categoryService; 35 44 this.repairShopReviewSummaryRepository = repairShopReviewSummaryRepository; 45 this.warehouseRepository = warehouseRepository; 46 this.orderContainsPartRepository = orderContainsPartRepository; 47 this.orderService = orderService; 36 48 } 37 49 … … 54 66 model.addAttribute("services",repairShopReviewSummaryRepository.findAll()); 55 67 model.addAttribute("bodyContent","services"); 68 return "master-template"; 69 } 70 @GetMapping("/currentOrder") 71 public String getCurrentOrder(Model model,HttpSession session){ 72 if(session.getAttribute("order")==null){ 73 model.addAttribute("hasError",true); 74 model.addAttribute("error","Нарачката е празна"); 75 } 76 else { 77 Order o = (Order) session.getAttribute("order"); 78 model.addAttribute("hasError",false); 79 model.addAttribute("order",o); 80 model.addAttribute("parts",orderService.findById(o.getID_order()).getPartList()); 81 } 82 model.addAttribute("bodyContent","currentOrder"); 56 83 return "master-template"; 57 84 } … … 79 106 @PostMapping("/register") 80 107 public void handleRegister(@RequestParam String username, @RequestParam String name, 81 @RequestParam String password, @RequestParam String email, 82 @RequestParam String number){ 83 User u = loginService.register(name,username,email,number,password); 108 @RequestParam String password, @RequestParam String rpassword, 109 @RequestParam String email, @RequestParam String number, 110 @RequestParam String role, HttpServletResponse response, HttpSession session){ 111 System.out.println(username + name + password + rpassword + email + number + role); 112 if(Objects.equals(role, "warehouseman")){ 113 session.setAttribute("username", username); 114 session.setAttribute("name", name); 115 session.setAttribute("password", password); 116 session.setAttribute("rpassword", rpassword); 117 session.setAttribute("email", email); 118 session.setAttribute("number", number); 119 try { 120 response.sendRedirect("/registerWarehouseman"); 121 } catch (IOException e) { 122 throw new RuntimeException(e); 123 } 124 } 125 else { 126 loginService.register(name, username, email, number, password, role); 127 try { 128 response.sendRedirect("/login"); 129 } catch (IOException e) { 130 throw new RuntimeException(e); 131 } 132 } 133 } 134 @GetMapping("/registerWarehouseman") 135 public String getSelectPage(Model model){ 136 model.addAttribute("locations",warehouseRepository.findAll()); 137 model.addAttribute("bodyContent","selectWarehouse"); 138 return "master-template"; 139 } 140 @PostMapping("/finishRegister") 141 public void handleWarehousemanRegister(@RequestParam String location,Model model, HttpServletResponse response, HttpSession session){ 142 System.out.println("here?"); 143 String username = (String) session.getAttribute("username"); 144 String name = (String) session.getAttribute("name"); 145 String password = (String) session.getAttribute("password"); 146 String email = (String) session.getAttribute("email"); 147 String number = (String) session.getAttribute("number"); 148 Warehouse warehouse = warehouseRepository.findAllByLocation(location).stream().findFirst().orElseThrow(RuntimeException::new); 149 loginService.registerWarehouseman(name,username,email,number,password,"warehouseman",warehouse); 150 try { 151 response.sendRedirect("/login"); 152 } catch (IOException e) { 153 throw new RuntimeException(e); 154 } 84 155 } 85 156 } -
src/main/java/com/example/autopartz/controller/PartController.java
rab952ab r7d43957 1 1 package com.example.autopartz.controller; 2 2 3 import com.example.autopartz.model.Client; 4 import com.example.autopartz.model.Order; 3 5 import com.example.autopartz.model.Part; 4 import com.example.autopartz.service.PartService; 5 import com.example.autopartz.service.PriceService; 6 import com.example.autopartz.service.RepairShopService; 6 import com.example.autopartz.model.User; 7 import com.example.autopartz.model.manytomany.OrderContainsPart; 8 import com.example.autopartz.repository.OrderContainsPartRepository; 9 import com.example.autopartz.service.*; 7 10 import org.springframework.stereotype.Controller; 8 11 import org.springframework.ui.Model; 9 12 import org.springframework.web.bind.annotation.*; 10 13 14 import javax.servlet.http.HttpServletRequest; 11 15 import javax.servlet.http.HttpServletResponse; 16 import javax.servlet.http.HttpSession; 12 17 import java.io.IOException; 13 18 … … 18 23 private final RepairShopService repairShopService; 19 24 private final PriceService priceService; 20 public PartController(PartService partService, RepairShopService repairShopService, PriceService priceService) { 25 private final OrderService orderService; 26 private final UserService userService; 27 private final OrderContainsPartRepository orderContainsPartRepository; 28 public PartController(PartService partService, RepairShopService repairShopService, PriceService priceService, OrderService orderService, UserService userService, OrderContainsPartRepository orderContainsPartRepository) { 21 29 this.partService = partService; 22 30 this.repairShopService = repairShopService; 23 31 this.priceService = priceService; 32 this.orderService = orderService; 33 this.userService = userService; 34 this.orderContainsPartRepository = orderContainsPartRepository; 24 35 } 25 36 @GetMapping("/{id}") … … 32 43 return "master-template"; 33 44 } 34 @GetMapping("/delivery /{id}")35 public String getDeliveryPage( @PathVariable Integer id,Model model){45 @GetMapping("/delivery") 46 public String getDeliveryPage(Model model){ 36 47 model.addAttribute("repairShops",repairShopService.findAll()); 37 model.addAttribute("partId",id);38 48 model.addAttribute("bodyContent","deliveryForPart"); 39 49 return "master-template"; … … 57 67 } 58 68 } 69 @PostMapping("/addToOrder/{id}") 70 public void addToOrder(@PathVariable Integer id,@RequestParam Integer quantity, HttpSession session, HttpServletResponse response, HttpServletRequest request){ 71 if(session.getAttribute("order")==null){ 72 User u = userService.findByUsername(request.getRemoteUser()); 73 Order newOrder = orderService.create((Client) u); 74 session.setAttribute("order",newOrder); 75 } 76 Order order = (Order) session.getAttribute("order"); 77 orderContainsPartRepository.save(new OrderContainsPart(id,order.getID_order(),quantity)); 78 try { 79 response.sendRedirect("/products"); 80 } catch (IOException e) { 81 throw new RuntimeException(e); 82 } 83 } 59 84 } -
src/main/java/com/example/autopartz/model/Client.java
rab952ab r7d43957 19 19 @Entity 20 20 public class Client extends User{ 21 public Client(String username, String name, String email, String password, String number) { 22 super(username, name, email, password, number); 23 } 24 21 25 @Override 22 26 public boolean equals(Object o) { -
src/main/java/com/example/autopartz/model/Deliveryman.java
rab952ab r7d43957 23 23 public class Deliveryman extends User{ 24 24 LocalDate employed_from; 25 public static final LocalDate defaultEmployedFrom = LocalDate.of(2020,1,1); 26 27 public Deliveryman(String username, String name, String email, String password, String number) { 28 super(username, name, email, password, number); 29 this.employed_from = defaultEmployedFrom; 30 } 25 31 26 32 @Override … … 38 44 @Override 39 45 public Collection<? extends GrantedAuthority> getAuthorities() { 40 return Collections.singletonList(Role.ROLE_DELIVERYMAN); 46 if(Objects.equals(employed_from, defaultEmployedFrom)) 47 return Collections.singletonList(Role.ROLE_PENDING_DELIVERYMAN); 48 else 49 return Collections.singletonList(Role.ROLE_DELIVERYMAN); 41 50 } 42 51 } -
src/main/java/com/example/autopartz/model/Order.java
rab952ab r7d43957 20 20 public class Order { 21 21 @Id 22 @GeneratedValue(strategy = GenerationType.IDENTITY) 22 23 Integer ID_order; 23 24 String order_status; … … 32 33 @ToString.Exclude 33 34 List<Part> partList; 35 36 public Order(Client user) { 37 this.order_status = "created"; 38 this.user = user; 39 this.order_date = LocalDateTime.now(); 40 } 34 41 35 42 @Override -
src/main/java/com/example/autopartz/model/Repair.java
rab952ab r7d43957 7 7 import org.hibernate.Hibernate; 8 8 9 import javax.persistence.Entity; 10 import javax.persistence.Id; 11 import javax.persistence.JoinColumn; 12 import javax.persistence.ManyToOne; 9 import javax.persistence.*; 13 10 import java.util.Objects; 14 11 … … 21 18 @Id 22 19 Integer ID_repair; 23 @ ManyToOne24 @JoinColumn(name = " vin")25 CarSample carSample;20 @OneToOne 21 @JoinColumn(name = "id_order") 22 Order order; 26 23 @ManyToOne 27 24 @JoinColumn(name = "id_repair_shop") -
src/main/java/com/example/autopartz/model/Role.java
rab952ab r7d43957 6 6 public enum Role implements GrantedAuthority { 7 7 8 ROLE_USER,ROLE_CLIENT, ROLE_ADMIN, ROLE_WAREHOUSEMAN, ROLE_DELIVERYMAN ;8 ROLE_USER,ROLE_CLIENT, ROLE_ADMIN, ROLE_WAREHOUSEMAN, ROLE_DELIVERYMAN, ROLE_PENDING_WAREHOUSEMAN,ROLE_PENDING_DELIVERYMAN; 9 9 10 10 @Override -
src/main/java/com/example/autopartz/model/User.java
rab952ab r7d43957 24 24 public class User implements UserDetails { 25 25 @Id 26 @GeneratedValue(strategy = GenerationType.IDENTITY) 26 27 Integer ID_user; 27 28 String username; -
src/main/java/com/example/autopartz/model/Warehouse.java
rab952ab r7d43957 7 7 import org.hibernate.Hibernate; 8 8 9 import javax.persistence.Column; 9 10 import javax.persistence.Entity; 10 11 import javax.persistence.Id; … … 19 20 @Id 20 21 Integer ID_warehouse; 21 String warehouse_location; 22 @Column(name = "warehouse_location") 23 String location; 22 24 23 25 @Override -
src/main/java/com/example/autopartz/model/Warehouseman.java
rab952ab r7d43957 23 23 public class Warehouseman extends User{ 24 24 LocalDate employed_from; 25 public static final LocalDate defaultEmployedFrom = LocalDate.of(2020,1,1); 25 26 @ManyToOne 26 27 @JoinColumn(name = "id_warehouse") 27 28 Warehouse warehouse; 29 30 public Warehouseman(String username, String name, String email, String password, String number, Warehouse warehouse) { 31 super(username, name, email, password, number); 32 this.employed_from=defaultEmployedFrom; 33 this.warehouse= warehouse; 34 } 28 35 29 36 @Override … … 41 48 @Override 42 49 public Collection<? extends GrantedAuthority> getAuthorities() { 43 return Collections.singletonList(Role.ROLE_WAREHOUSEMAN); 50 if(employed_from==defaultEmployedFrom) 51 return Collections.singletonList(Role.ROLE_PENDING_WAREHOUSEMAN); 52 else 53 return Collections.singletonList(Role.ROLE_WAREHOUSEMAN); 44 54 } 45 55 } -
src/main/java/com/example/autopartz/model/views/RepairsForUser.java
rab952ab r7d43957 33 33 @Id 34 34 String partname; 35 Integer vin;35 Integer orderid; 36 36 String rsname; 37 37 } -
src/main/java/com/example/autopartz/service/LoginService.java
rab952ab r7d43957 2 2 3 3 import com.example.autopartz.model.User; 4 import com.example.autopartz.model.Warehouse; 4 5 5 6 public interface LoginService { 6 User register(String name, String username, String email, String number, String password);7 void register(String name, String username, String email, String number, String password, String role); 7 8 User login(String username, String password); 9 void registerWarehouseman(String name, String username, String email, String number, String password, String role, Warehouse warehouse); 8 10 } -
src/main/java/com/example/autopartz/service/impl/LoginServiceImpl.java
rab952ab r7d43957 1 1 package com.example.autopartz.service.impl; 2 2 3 import com.example.autopartz.model. User;3 import com.example.autopartz.model.*; 4 4 import com.example.autopartz.repository.UserRepository; 5 5 import com.example.autopartz.service.LoginService; 6 6 import org.springframework.stereotype.Service; 7 8 import java.util.Objects; 7 9 8 10 @Service … … 15 17 16 18 @Override 17 public User register(String name, String username, String email, String number, String password) { 18 return userRepository.save(new User(username,name,email,password,number)); 19 public void register(String name, String username, String email, String number, String password, String role) { 20 if (Objects.equals(role, "client")) { 21 userRepository.save(new Client(username, name, email, password, number)); 22 } 23 else { 24 userRepository.save(new Deliveryman(username, name, email, password, number)); 25 } 19 26 } 20 27 … … 24 31 } 25 32 33 @Override 34 public void registerWarehouseman(String name, String username, String email, String number, String password, String role, Warehouse warehouse) { 35 userRepository.save(new Warehouseman(username, name, email, password, number, warehouse)); 36 37 } 38 26 39 // @Override 27 40 // public User findByUsername(String username) {
Note:
See TracChangeset
for help on using the changeset viewer.