-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathUserController.java
More file actions
73 lines (61 loc) · 3.06 KB
/
UserController.java
File metadata and controls
73 lines (61 loc) · 3.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package microservice.controllers;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.beans.factory.annotation.Autowired;
import microservice.services.UserService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import javax.validation.Valid;
import org.springframework.http.ResponseEntity;
import java.net.URI;
import java.net.URISyntaxException;
import org.springframework.web.util.UriComponentsBuilder;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import org.springframework.http.MediaType;
import org.springframework.http.HttpStatus;
import microservice.models.Authorization;
import microservice.models.Message;
import microservice.models.User;
@RestController
public class UserController {
@Autowired
private UserService userService;
@RequestMapping(value = "/users",
method = RequestMethod.POST,
produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public ResponseEntity<Message> register(UriComponentsBuilder builder,
@Valid @RequestBody User user)
throws URISyntaxException, InterruptedException, ExecutionException {
CompletableFuture<User> userFuture = userService.register(user);
User storedUser = userFuture.get();
return ResponseEntity
.created(new URI(builder.toUriString() + "/users/" + storedUser.get_id()))
.body(new Message("user " + user.getUsername() + " successfully registered", storedUser.get_id(), true));
}
@RequestMapping(value = "/users/authentication",
method = RequestMethod.POST,
produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public ResponseEntity<?> authenticateUser(@Valid @RequestBody User user)
throws InterruptedException, ExecutionException {
CompletableFuture<?> userFuture = userService.authenticate(user);
Object result = userFuture.get();
if (result.getClass() == Authorization.class)
return ResponseEntity.ok(result);
else
return new ResponseEntity<>(result, HttpStatus.UNAUTHORIZED);
}
@RequestMapping(value = "/users/authorization",
method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public ResponseEntity<Message> authorizeUser(@RequestHeader("Authorization") String accessToken)
throws InterruptedException, ExecutionException {
CompletableFuture<Message> userFuture = userService.authorize(accessToken);
Message msg = userFuture.get();
if (msg.getStatus().equals("success"))
return ResponseEntity.ok(msg);
else
return new ResponseEntity<>(msg, HttpStatus.UNAUTHORIZED);
}
}