57 lines
1.9 KiB
Java
57 lines
1.9 KiB
Java
package pl.edu.amu.wmi.bookapi.api;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.http.ResponseEntity;
|
|
import org.springframework.security.core.context.SecurityContextHolder;
|
|
import org.springframework.web.bind.annotation.GetMapping;
|
|
import org.springframework.web.bind.annotation.PathVariable;
|
|
import org.springframework.web.bind.annotation.PostMapping;
|
|
import org.springframework.web.bind.annotation.RequestBody;
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
import pl.edu.amu.wmi.bookapi.api.dto.MessageDto;
|
|
import pl.edu.amu.wmi.bookapi.service.MessageService;
|
|
|
|
@RestController
|
|
@RequestMapping("/api/messages")
|
|
public class MessageController {
|
|
|
|
private MessageService messageService;
|
|
|
|
@Autowired
|
|
public MessageController(MessageService messageService) {
|
|
this.messageService = messageService;
|
|
}
|
|
|
|
@GetMapping
|
|
public ResponseEntity listThreads() {
|
|
return ResponseEntity.ok(
|
|
messageService.getThreads(
|
|
getUsername()
|
|
)
|
|
);
|
|
}
|
|
|
|
@PostMapping
|
|
public ResponseEntity createMessage(@RequestBody MessageDto messageDto) {
|
|
messageService.createMessage(
|
|
messageDto.getContent(),
|
|
getUsername(),
|
|
messageDto.getRecipient()
|
|
);
|
|
|
|
return ResponseEntity.ok().build();
|
|
}
|
|
|
|
@GetMapping("/{threadId}")
|
|
public ResponseEntity getMessagesInThread(@PathVariable String threadId) {
|
|
return ResponseEntity.ok(messageService.getMessagesInThread(threadId, getUsername()));
|
|
}
|
|
|
|
private String getUsername() {
|
|
String username = SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString();
|
|
System.out.println("Username - " + username);
|
|
return username;
|
|
}
|
|
}
|