62 lines
2 KiB
Java
62 lines
2 KiB
Java
package de.szut.casino.user;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.http.HttpHeaders;
|
|
import org.springframework.http.HttpStatus;
|
|
import org.springframework.http.ResponseEntity;
|
|
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.RequestHeader;
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
|
import de.szut.casino.user.dto.CreateUserDto;
|
|
import de.szut.casino.user.dto.GetUserDto;
|
|
import jakarta.validation.Valid;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
@Slf4j
|
|
@RestController
|
|
public class UserController {
|
|
|
|
@Autowired
|
|
private UserService userService;
|
|
|
|
@GetMapping("/user/{id}")
|
|
public ResponseEntity<?> getUser(@PathVariable String id) {
|
|
if (id == null || !userService.exists(id)) {
|
|
return ResponseEntity.notFound().build();
|
|
}
|
|
|
|
return ResponseEntity.ok(userService.getUser(id));
|
|
}
|
|
|
|
@PostMapping("/user")
|
|
public ResponseEntity<?> createUser(@RequestBody @Valid CreateUserDto userData) {
|
|
if (userService.exists(userData.getKeycloakId())) {
|
|
|
|
return this.redirect("/user/" + userData.getKeycloakId());
|
|
}
|
|
|
|
return ResponseEntity.ok(userService.createUser(userData));
|
|
}
|
|
|
|
@GetMapping("/user")
|
|
public ResponseEntity<GetUserDto> getCurrentUser(@RequestHeader("Authorization") String token) {
|
|
GetUserDto userData = userService.getCurrentUserAsDto(token);
|
|
|
|
if (userData == null) {
|
|
return ResponseEntity.notFound().build();
|
|
}
|
|
|
|
return ResponseEntity.ok(userData);
|
|
}
|
|
|
|
private ResponseEntity<Object> redirect(String route) {
|
|
HttpHeaders headers = new HttpHeaders();
|
|
headers.add("Location", route);
|
|
|
|
return new ResponseEntity<>(headers, HttpStatus.FOUND);
|
|
}
|
|
}
|