source: src/main/java/com/example/autopartz/controller/AdminController.java@ 676144b

main
Last change on this file since 676144b was 676144b, checked in by andrejtodorovski <82031894+andrejtodorovski@…>, 18 months ago

Added admin view of pending roles and approve functionality

  • Property mode set to 100644
File size: 2.1 KB
Line 
1package com.example.autopartz.controller;
2
3import com.example.autopartz.model.Role;
4import com.example.autopartz.model.User;
5import com.example.autopartz.model.Warehouseman;
6import com.example.autopartz.repository.WarehousemanRepository;
7import com.example.autopartz.service.UserService;
8import org.springframework.stereotype.Controller;
9import org.springframework.ui.Model;
10import org.springframework.web.bind.annotation.GetMapping;
11import org.springframework.web.bind.annotation.PathVariable;
12import org.springframework.web.bind.annotation.PostMapping;
13import org.springframework.web.bind.annotation.RequestMapping;
14
15import javax.servlet.http.HttpServletResponse;
16import java.io.IOException;
17import java.time.LocalDate;
18import java.util.List;
19import java.util.Objects;
20
21@Controller
22@RequestMapping("/")
23public 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}
Note: See TracBrowser for help on using the repository browser.