| 1 | package edu.ucsb.cs156.gauchoride.controllers; | |
| 2 | ||
| 3 | import org.springframework.beans.factory.annotation.Autowired; | |
| 4 | import org.springframework.security.access.prepost.PreAuthorize; | |
| 5 | import org.springframework.web.bind.annotation.GetMapping; | |
| 6 | import org.springframework.web.bind.annotation.PutMapping; | |
| 7 | import org.springframework.web.bind.annotation.PostMapping; | |
| 8 | import org.springframework.web.bind.annotation.DeleteMapping; | |
| 9 | import org.springframework.web.bind.annotation.RequestBody; | |
| 10 | import org.springframework.web.bind.annotation.RequestMapping; | |
| 11 | import org.springframework.web.bind.annotation.RequestParam; | |
| 12 | import org.springframework.web.bind.annotation.RestController; | |
| 13 | ||
| 14 | import com.fasterxml.jackson.databind.ObjectMapper; | |
| 15 | import com.fasterxml.jackson.core.JsonProcessingException; | |
| 16 | import org.springframework.data.domain.Pageable; | |
| 17 | import org.springframework.data.domain.PageRequest; | |
| 18 | ||
| 19 | import javax.validation.Valid; | |
| 20 | ||
| 21 | import edu.ucsb.cs156.gauchoride.entities.DriverChat; | |
| 22 | import edu.ucsb.cs156.gauchoride.entities.User; | |
| 23 | import edu.ucsb.cs156.gauchoride.repositories.DriverChatRepository; | |
| 24 | import edu.ucsb.cs156.gauchoride.errors.EntityNotFoundException; | |
| 25 | import io.swagger.annotations.Api; | |
| 26 | import io.swagger.annotations.ApiOperation; | |
| 27 | import io.swagger.annotations.ApiParam; | |
| 28 | import java.time.LocalDateTime; | |
| 29 | ||
| 30 | @Api(description= "Driver chat information") | |
| 31 | @RequestMapping("/api/driverchats") | |
| 32 | @RestController | |
| 33 | public class DriverChatController extends ApiController { | |
| 34 | ||
| 35 | @Autowired | |
| 36 | DriverChatRepository driverChatRepository; | |
| 37 | ||
| 38 | @Autowired | |
| 39 | ObjectMapper mapper; | |
| 40 | ||
| 41 | @ApiOperation(value = "Get a list of all chat message") | |
| 42 | @PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_DRIVER')") | |
| 43 | @GetMapping("/all") | |
| 44 | public Iterable<DriverChat> getAllMessages() { | |
| 45 |
1
1. getAllMessages : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/DriverChatController::getAllMessages → KILLED |
return driverChatRepository.findAll(); |
| 46 | } | |
| 47 | ||
| 48 | @ApiOperation(value = "Get the latest N messages based on query parameters") | |
| 49 | @PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_DRIVER')") | |
| 50 | @GetMapping("/list") | |
| 51 | public Iterable<DriverChat> getRecentMessage( | |
| 52 | @ApiParam(name = "limit", required = true, example ="100", value="an integer, representing how many messages to query", type="Long") @RequestParam int limit | |
| 53 | ) { | |
| 54 | Pageable pageable = PageRequest.of(0, limit); | |
| 55 |
1
1. getRecentMessage : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/DriverChatController::getRecentMessage → KILLED |
return driverChatRepository.findAllByOrderByTimeStampDesc(pageable); |
| 56 | } | |
| 57 | ||
| 58 | @ApiOperation(value = "Get a single chat message") | |
| 59 | @PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_DRIVER')") | |
| 60 | @GetMapping("") | |
| 61 | public DriverChat getMessageById(@ApiParam(name = "id", required = true, example="15", value="id of the message", type="Long") @RequestParam Long id) { | |
| 62 | DriverChat message = driverChatRepository.findById(id) | |
| 63 |
1
1. lambda$getMessageById$0 : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/DriverChatController::lambda$getMessageById$0 → KILLED |
.orElseThrow(()->new EntityNotFoundException(DriverChat.class, id)); |
| 64 | ||
| 65 |
1
1. getMessageById : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/DriverChatController::getMessageById → KILLED |
return message; |
| 66 | } | |
| 67 | ||
| 68 | @ApiOperation(value="Create a new chat message") | |
| 69 | @PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_DRIVER')") | |
| 70 | @PostMapping("/post") | |
| 71 | public DriverChat postNewMessage( | |
| 72 | @RequestBody String messageContent | |
| 73 | ) throws JsonProcessingException { | |
| 74 | ||
| 75 | User currentUser = getCurrentUser().getUser(); | |
| 76 | DriverChat chatMessage = DriverChat.builder() | |
| 77 | .sender(currentUser) | |
| 78 | .messageContent(messageContent) | |
| 79 | .timeStamp(LocalDateTime.now()) | |
| 80 | .build(); | |
| 81 | | |
| 82 |
1
1. postNewMessage : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/DriverChatController::postNewMessage → KILLED |
return driverChatRepository.save(chatMessage); |
| 83 | } | |
| 84 | ||
| 85 | @ApiOperation(value="Update a driver's message") | |
| 86 | @PreAuthorize("hasRole('ROLE_DRIVER')") | |
| 87 | @PutMapping("") | |
| 88 | public DriverChat updateMessageByDriver( | |
| 89 | @ApiParam(name = "id", required = true, example="15", value="id of the message", type="Long") @RequestParam Long id, | |
| 90 | @ApiParam(name = "messageContent", required = true, example="can you pick me up?", value="content of new the message", type="String") @RequestParam String messageContent | |
| 91 | ) { | |
| 92 | User currentUser = getCurrentUser().getUser(); | |
| 93 | DriverChat message = driverChatRepository.findByIdAndSender(id, currentUser) | |
| 94 |
1
1. lambda$updateMessageByDriver$1 : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/DriverChatController::lambda$updateMessageByDriver$1 → KILLED |
.orElseThrow(()->new EntityNotFoundException(DriverChat.class, id)); |
| 95 | | |
| 96 |
1
1. updateMessageByDriver : removed call to edu/ucsb/cs156/gauchoride/entities/DriverChat::setMessageContent → KILLED |
message.setMessageContent(messageContent); |
| 97 | driverChatRepository.save(message); | |
| 98 |
1
1. updateMessageByDriver : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/DriverChatController::updateMessageByDriver → KILLED |
return message; |
| 99 | } | |
| 100 | ||
| 101 | @ApiOperation(value="Update a driver's message(admin can edit anyone's messages)") | |
| 102 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 103 | @PutMapping("admin") | |
| 104 | public DriverChat updateMessageByAdmin( | |
| 105 | @ApiParam(name = "id", required = true, example="15", value="id of the message", type="Long") @RequestParam Long id, | |
| 106 | @ApiParam(name = "messageContent", required = true, example="can you pick me up?", value="content of the message", type="String") @RequestParam String messageContent | |
| 107 | ) { | |
| 108 | DriverChat message = driverChatRepository.findById(id) | |
| 109 |
1
1. lambda$updateMessageByAdmin$2 : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/DriverChatController::lambda$updateMessageByAdmin$2 → KILLED |
.orElseThrow(()->new EntityNotFoundException(DriverChat.class, id)); |
| 110 | | |
| 111 |
1
1. updateMessageByAdmin : removed call to edu/ucsb/cs156/gauchoride/entities/DriverChat::setMessageContent → KILLED |
message.setMessageContent(messageContent); |
| 112 | driverChatRepository.save(message); | |
| 113 |
1
1. updateMessageByAdmin : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/DriverChatController::updateMessageByAdmin → KILLED |
return message; |
| 114 | } | |
| 115 | ||
| 116 | @ApiOperation(value="Delete a chat message if it belongs to you") | |
| 117 | @PreAuthorize("hasRole('ROLE_DRIVER')") | |
| 118 | @DeleteMapping() | |
| 119 | public Object deleteMessageByDriver( | |
| 120 | @ApiParam(name = "id", required = true, example="15", value="id of the message", type="Long") @RequestParam Long id | |
| 121 | ) { | |
| 122 | User currentUser = getCurrentUser().getUser(); | |
| 123 | DriverChat message = driverChatRepository.findByIdAndSender(id, currentUser) | |
| 124 |
1
1. lambda$deleteMessageByDriver$3 : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/DriverChatController::lambda$deleteMessageByDriver$3 → KILLED |
.orElseThrow(()->new EntityNotFoundException(DriverChat.class, id)); |
| 125 | ||
| 126 |
1
1. deleteMessageByDriver : removed call to edu/ucsb/cs156/gauchoride/repositories/DriverChatRepository::delete → KILLED |
driverChatRepository.delete(message); |
| 127 |
1
1. deleteMessageByDriver : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/DriverChatController::deleteMessageByDriver → KILLED |
return genericMessage("Message with id %s deleted.".formatted(id)); |
| 128 | } | |
| 129 | ||
| 130 | @ApiOperation(value="Delete a chat message") | |
| 131 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 132 | @DeleteMapping("/admin") | |
| 133 | public Object deleteMessageByAdmin( | |
| 134 | @ApiParam(name = "id", required = true, example="15", value="id of the message", type="Long") @RequestParam Long id | |
| 135 | ) { | |
| 136 | DriverChat message = driverChatRepository.findById(id) | |
| 137 |
1
1. lambda$deleteMessageByAdmin$4 : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/DriverChatController::lambda$deleteMessageByAdmin$4 → KILLED |
.orElseThrow(()->new EntityNotFoundException(DriverChat.class, id)); |
| 138 | ||
| 139 |
1
1. deleteMessageByAdmin : removed call to edu/ucsb/cs156/gauchoride/repositories/DriverChatRepository::delete → KILLED |
driverChatRepository.delete(message); |
| 140 |
1
1. deleteMessageByAdmin : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/DriverChatController::deleteMessageByAdmin → KILLED |
return genericMessage("Message with id %s deleted.".formatted(id)); |
| 141 | } | |
| 142 | ||
| 143 | @ApiOperation(value="Delete all chat messages") | |
| 144 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 145 | @DeleteMapping("/all") | |
| 146 | public Object deleteAllMessages() { | |
| 147 |
1
1. deleteAllMessages : removed call to edu/ucsb/cs156/gauchoride/repositories/DriverChatRepository::deleteAll → KILLED |
driverChatRepository.deleteAll(); |
| 148 |
1
1. deleteAllMessages : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/DriverChatController::deleteAllMessages → KILLED |
return genericMessage("All messages have been deleted."); |
| 149 | } | |
| 150 | } | |
Mutations | ||
| 45 |
1.1 |
|
| 55 |
1.1 |
|
| 63 |
1.1 |
|
| 65 |
1.1 |
|
| 82 |
1.1 |
|
| 94 |
1.1 |
|
| 96 |
1.1 |
|
| 98 |
1.1 |
|
| 109 |
1.1 |
|
| 111 |
1.1 |
|
| 113 |
1.1 |
|
| 124 |
1.1 |
|
| 126 |
1.1 |
|
| 127 |
1.1 |
|
| 137 |
1.1 |
|
| 139 |
1.1 |
|
| 140 |
1.1 |
|
| 147 |
1.1 |
|
| 148 |
1.1 |