Implemented picking method in Client and refactored picking in server
This commit is contained in:
parent
087c4fc517
commit
1bb4902134
@ -76,6 +76,10 @@ public class NodeRepositoryImpl implements NodeRepository {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public @NotNull PickedRoomResponse pickRoom(@NotNull NodeIdentifier identifier, @NotNull Set<UserIdentifier> users) {
|
public @NotNull PickedRoomResponse pickRoom(@NotNull NodeIdentifier identifier, @NotNull Set<UserIdentifier> users) {
|
||||||
throw new UnsupportedOperationException("Not implemented");
|
return rest.queryPostWithBody(
|
||||||
|
"/nodes/" + identifier.getValue() + "/pick",
|
||||||
|
PickedRoomResponse.class,
|
||||||
|
params -> {}, String.join(",", users.stream().map(user -> user.getValue()).toList())
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,9 +3,7 @@ package ru.dragonestia.picker.api.impl.util;
|
|||||||
import com.fasterxml.jackson.annotation.JsonAutoDetect;
|
import com.fasterxml.jackson.annotation.JsonAutoDetect;
|
||||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import okhttp3.FormBody;
|
import okhttp3.*;
|
||||||
import okhttp3.OkHttpClient;
|
|
||||||
import okhttp3.Response;
|
|
||||||
import org.jetbrains.annotations.ApiStatus.Internal;
|
import org.jetbrains.annotations.ApiStatus.Internal;
|
||||||
import ru.dragonestia.picker.api.exception.ExceptionFactory;
|
import ru.dragonestia.picker.api.exception.ExceptionFactory;
|
||||||
import ru.dragonestia.picker.api.impl.RoomPickerClient;
|
import ru.dragonestia.picker.api.impl.RoomPickerClient;
|
||||||
@ -78,6 +76,22 @@ public class RestTemplate {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public <T> T queryPostWithBody(String uri, Class<T> clazz, ParamsConsumer paramsConsumer, String body) {
|
||||||
|
var request = client.prepareRequestBuilder(uri + queryEncode(paramsConsumer))
|
||||||
|
.post(RequestBody.create(body, MediaType.get("text/plain")))
|
||||||
|
.build();
|
||||||
|
|
||||||
|
try (var response = httpClient.newCall(request).execute()) {
|
||||||
|
checkResponseForErrors(response);
|
||||||
|
|
||||||
|
return json.readValue(new String(Objects.requireNonNull(response.body()).bytes(), StandardCharsets.UTF_8), clazz);
|
||||||
|
} catch (JsonProcessingException ex) {
|
||||||
|
throw new RuntimeException("Json processing error", ex);
|
||||||
|
} catch (IOException ex) {
|
||||||
|
throw new RuntimeException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private String queryEncode(ParamsConsumer paramsConsumer) {
|
private String queryEncode(ParamsConsumer paramsConsumer) {
|
||||||
var params = new HashMap<String, String>();
|
var params = new HashMap<String, String>();
|
||||||
paramsConsumer.accept(params);
|
paramsConsumer.accept(params);
|
||||||
|
|||||||
@ -77,7 +77,7 @@ public class NodeController {
|
|||||||
@PostMapping("/{nodeId}/pick")
|
@PostMapping("/{nodeId}/pick")
|
||||||
ResponseEntity<PickedRoomResponse> pickRoom(
|
ResponseEntity<PickedRoomResponse> pickRoom(
|
||||||
@Parameter(description = "Node identifier") @PathVariable("nodeId") String nodeId,
|
@Parameter(description = "Node identifier") @PathVariable("nodeId") String nodeId,
|
||||||
@Parameter(description = "Users to add", example = "user1,user3,user3") @RequestParam(name = "userIds") String userIds
|
@RequestBody String userIds
|
||||||
) {
|
) {
|
||||||
namingValidator.validateNodeId(nodeId);
|
namingValidator.validateNodeId(nodeId);
|
||||||
|
|
||||||
|
|||||||
@ -80,7 +80,7 @@ public class RoomServiceImpl implements RoomService {
|
|||||||
@Override
|
@Override
|
||||||
public PickedRoomResponse pickAvailable(Node node, List<User> users) {
|
public PickedRoomResponse pickAvailable(Node node, List<User> users) {
|
||||||
var room = roomRepository.pickFree(node, users)
|
var room = roomRepository.pickFree(node, users)
|
||||||
.orElseThrow(() -> new RuntimeException("There are no rooms available"));
|
.orElseThrow(() -> new RuntimeException("There are no rooms available. Given users count: " + users.size()));
|
||||||
var roomUsers = userRepository.usersOf(room);
|
var roomUsers = userRepository.usersOf(room);
|
||||||
|
|
||||||
return new PickedRoomResponse(
|
return new PickedRoomResponse(
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user