1 | package com.example.autopartz.controller;
|
---|
2 |
|
---|
3 | import com.example.autopartz.model.Role;
|
---|
4 | import com.example.autopartz.model.User;
|
---|
5 | import com.example.autopartz.model.Warehouseman;
|
---|
6 | import com.example.autopartz.repository.WarehousemanRepository;
|
---|
7 | import com.example.autopartz.service.UserService;
|
---|
8 | import org.springframework.stereotype.Controller;
|
---|
9 | import org.springframework.ui.Model;
|
---|
10 | import org.springframework.web.bind.annotation.GetMapping;
|
---|
11 | import org.springframework.web.bind.annotation.PathVariable;
|
---|
12 | import org.springframework.web.bind.annotation.PostMapping;
|
---|
13 | import org.springframework.web.bind.annotation.RequestMapping;
|
---|
14 |
|
---|
15 | import javax.servlet.http.HttpServletResponse;
|
---|
16 | import java.io.IOException;
|
---|
17 | import java.time.LocalDate;
|
---|
18 | import java.util.List;
|
---|
19 | import java.util.Objects;
|
---|
20 |
|
---|
21 | @Controller
|
---|
22 | @RequestMapping("/")
|
---|
23 | public class AdminController {
|
---|
24 | private final UserService userService;
|
---|
25 | private final WarehousemanRepository warehousemanRepository;
|
---|
26 |
|
---|
27 | public AdminController(UserService userService, WarehousemanRepository warehousemanRepository) {
|
---|
28 | this.userService = userService;
|
---|
29 | this.warehousemanRepository = warehousemanRepository;
|
---|
30 | }
|
---|
31 |
|
---|
32 | @GetMapping("/viewUsers")
|
---|
33 | public String getAllUsers(Model model){
|
---|
34 | List<User> pendingList = userService.findAllUsers().stream().filter(u->u.getAuthorities().contains(Role.ROLE_PENDING_DELIVERYMAN) || u.getAuthorities().contains(Role.ROLE_PENDING_WAREHOUSEMAN)).toList();
|
---|
35 | model.addAttribute("users", pendingList);
|
---|
36 | model.addAttribute("bodyContent", "viewUsers");
|
---|
37 | return "master-template";
|
---|
38 | }
|
---|
39 | @PostMapping("/approve/{id}")
|
---|
40 | public void approve(@PathVariable Integer id, HttpServletResponse response){
|
---|
41 | if(Objects.equals(userService.findById(id).getAuthorities().stream().findFirst().get(),Role.ROLE_PENDING_WAREHOUSEMAN)){
|
---|
42 | Warehouseman wh = (Warehouseman) userService.findById(id);
|
---|
43 | wh.setEmployed_from(LocalDate.now());
|
---|
44 | warehousemanRepository.save(wh);
|
---|
45 | try {
|
---|
46 | response.sendRedirect("/viewUsers");
|
---|
47 | } catch (IOException e) {
|
---|
48 | throw new RuntimeException(e);
|
---|
49 | }
|
---|
50 | }
|
---|
51 | }
|
---|
52 | }
|
---|