1 | package edu.ucsb.cs156.gauchoride.controllers; | |
2 | ||
3 | import java.util.ArrayList; | |
4 | ||
5 | import com.fasterxml.jackson.core.JsonProcessingException; | |
6 | import com.fasterxml.jackson.databind.ObjectMapper; | |
7 | ||
8 | import edu.ucsb.cs156.gauchoride.entities.User; | |
9 | import edu.ucsb.cs156.gauchoride.repositories.UserRepository; | |
10 | ||
11 | import edu.ucsb.cs156.gauchoride.errors.EntityNotFoundException; | |
12 | ||
13 | ||
14 | import org.springframework.beans.factory.annotation.Autowired; | |
15 | import org.springframework.http.ResponseEntity; | |
16 | import org.springframework.security.access.prepost.PreAuthorize; | |
17 | import org.springframework.web.bind.annotation.DeleteMapping; | |
18 | import org.springframework.web.bind.annotation.GetMapping; | |
19 | import org.springframework.web.bind.annotation.PostMapping; | |
20 | import org.springframework.web.bind.annotation.RequestMapping; | |
21 | import org.springframework.web.bind.annotation.RequestParam; | |
22 | import org.springframework.web.bind.annotation.RestController; | |
23 | ||
24 | import io.swagger.v3.oas.annotations.tags.Tag; | |
25 | import io.swagger.v3.oas.annotations.Operation; | |
26 | import io.swagger.v3.oas.annotations.Parameter; | |
27 | ||
28 | ||
29 | @Tag(name = "Driver Controller") | |
30 | @RequestMapping("/api/drivers") | |
31 | @RestController | |
32 | public class DriversController extends ApiController { | |
33 | @Autowired | |
34 | UserRepository userRepository; | |
35 | ||
36 | @Autowired | |
37 | ObjectMapper mapper; | |
38 | ||
39 | @Operation(summary = "Get a list of all drivers") | |
40 | @PreAuthorize("hasRole('ROLE_ADMIN') || hasRole('ROLE_DRIVER') || hasRole('ROLE_RIDER')") | |
41 | @GetMapping("/all") | |
42 | public ResponseEntity<String> drivers() | |
43 | throws JsonProcessingException { | |
44 | ArrayList<User> driverUsers = new ArrayList<User>(); | |
45 | Iterable<User> users = userRepository.findAll(); | |
46 | for (User user : users) { | |
47 |
1
1. drivers : negated conditional → KILLED |
if(user.getDriver()) { |
48 | driverUsers.add(user); | |
49 | } | |
50 | } | |
51 | | |
52 | String body = mapper.writeValueAsString(driverUsers); | |
53 |
1
1. drivers : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/DriversController::drivers → KILLED |
return ResponseEntity.ok().body(body); |
54 | } | |
55 | } | |
Mutations | ||
47 |
1.1 |
|
53 |
1.1 |