first commit
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
package com.gameplatform.server.controller;
|
||||
|
||||
import com.gameplatform.server.model.User;
|
||||
import com.gameplatform.server.service.UserService;
|
||||
import jakarta.validation.Valid;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/users")
|
||||
public class UserController {
|
||||
private final UserService userService;
|
||||
|
||||
public UserController(UserService userService) {
|
||||
this.userService = userService;
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
public Mono<User> getById(@PathVariable Long id) {
|
||||
return userService.getById(id);
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public Flux<User> listAll() {
|
||||
return userService.listAll();
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
@ResponseStatus(HttpStatus.CREATED)
|
||||
public Mono<User> create(@Valid @RequestBody User user) {
|
||||
return userService.create(user);
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
@ResponseStatus(HttpStatus.NO_CONTENT)
|
||||
public Mono<Void> delete(@PathVariable Long id) {
|
||||
return userService.deleteById(id)
|
||||
.filter(Boolean::booleanValue)
|
||||
.then();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
package com.gameplatform.server.controller.auth;
|
||||
|
||||
import com.gameplatform.server.model.dto.auth.LoginRequest;
|
||||
import com.gameplatform.server.model.dto.auth.LoginResponse;
|
||||
import com.gameplatform.server.security.JwtService;
|
||||
import io.jsonwebtoken.Claims;
|
||||
import jakarta.validation.Valid;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/auth")
|
||||
public class AuthController {
|
||||
|
||||
private final com.gameplatform.server.service.auth.AuthService authService;
|
||||
private final JwtService jwtService;
|
||||
|
||||
public AuthController(com.gameplatform.server.service.auth.AuthService authService, JwtService jwtService) {
|
||||
this.authService = authService;
|
||||
this.jwtService = jwtService;
|
||||
}
|
||||
|
||||
@PostMapping("/login")
|
||||
@ResponseStatus(HttpStatus.OK)
|
||||
public Mono<LoginResponse> login(@Valid @RequestBody LoginRequest req) {
|
||||
return authService.login(req);
|
||||
}
|
||||
|
||||
@GetMapping("/me")
|
||||
public Mono<Object> me(@RequestHeader(HttpHeaders.AUTHORIZATION) String authorization) {
|
||||
String token = authorization != null && authorization.startsWith("Bearer ") ? authorization.substring(7) : authorization;
|
||||
return Mono.fromCallable(() -> jwtService.parse(token))
|
||||
.map(this::claimsToMe);
|
||||
}
|
||||
|
||||
private Object claimsToMe(Claims c) {
|
||||
return new java.util.LinkedHashMap<>() {{
|
||||
put("userType", c.get("userType"));
|
||||
put("userId", c.get("userId"));
|
||||
put("username", c.get("username"));
|
||||
put("role", c.get("role"));
|
||||
put("exp", c.getExpiration());
|
||||
}};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user