implemented client room repository
This commit is contained in:
parent
e1975532b3
commit
7a2c0fb1a4
@ -1,55 +1,71 @@
|
||||
package ru.dragonestia.picker.api.impl.repository;
|
||||
|
||||
import ru.dragonestia.picker.api.impl.util.RestTemplate;
|
||||
import ru.dragonestia.picker.api.impl.util.type.HttpMethod;
|
||||
import ru.dragonestia.picker.api.model.instance.InstanceId;
|
||||
import ru.dragonestia.picker.api.model.room.Room;
|
||||
import ru.dragonestia.picker.api.model.room.RoomId;
|
||||
import ru.dragonestia.picker.api.repository.RoomRepository;
|
||||
import ru.dragonestia.picker.api.repository.response.ResponseObject;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.*;
|
||||
|
||||
public class RoomRepositoryImpl implements RoomRepository {
|
||||
|
||||
private final RestTemplate restTemplate;
|
||||
private final RestTemplate rest;
|
||||
|
||||
public RoomRepositoryImpl(RestTemplate restTemplate) {
|
||||
this.restTemplate = restTemplate;
|
||||
public RoomRepositoryImpl(RestTemplate rest) {
|
||||
this.rest = rest;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<RoomId> allRoomsIds(InstanceId instanceId) {
|
||||
throw new UnsupportedOperationException("Not implemented");
|
||||
return Arrays.stream(rest.query("/instances/target/%s/rooms".formatted(instanceId.getValue()), HttpMethod.GET, String[].class))
|
||||
.map(RoomId::of)
|
||||
.toList();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Room getRoom(InstanceId instanceId, RoomId roomId) {
|
||||
throw new UnsupportedOperationException("Not implemented");
|
||||
return rest.query("/instances/target/%s/rooms/target/%s".formatted(instanceId.getValue(), roomId.getValue()), HttpMethod.GET, ResponseObject.RRoom.class).covert();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<RoomId, Room> getRooms(InstanceId instanceId, Collection<RoomId> rooms) {
|
||||
throw new UnsupportedOperationException("Not implemented");
|
||||
var map = new HashMap<RoomId, Room>();
|
||||
Arrays.stream(rest.query("/instances/target/%s/rooms/list".formatted(instanceId.getValue()), HttpMethod.GET, ResponseObject.RRoom[].class, params -> {
|
||||
params.put("id", String.join(",", rooms.stream().map(RoomId::getValue).toList()));
|
||||
})).map(ResponseObject.RRoom::covert).forEach(room -> map.put(room.id(), room));
|
||||
return map;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void createRoom(InstanceId instanceId, RoomId roomId, int slots, String payload, boolean locked, boolean persist) {
|
||||
throw new UnsupportedOperationException("Not implemented");
|
||||
rest.queryPostWithBody("/instances/target/%s/rooms".formatted(instanceId.getValue()), params -> {
|
||||
params.put("instanceId", instanceId.getValue());
|
||||
params.put("id", roomId.getValue());
|
||||
params.put("slots", Integer.toString(slots));
|
||||
params.put("locked", Boolean.toString(locked));
|
||||
params.put("persist", Boolean.toString(persist));
|
||||
}, payload);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteRoom(InstanceId instanceId, RoomId roomId) {
|
||||
throw new UnsupportedOperationException("Not implemented");
|
||||
rest.query("/instances/target/%s/rooms/target/%s".formatted(instanceId.getValue(), roomId.getValue()), HttpMethod.DELETE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteRooms(InstanceId instanceId, Collection<RoomId> rooms) {
|
||||
throw new UnsupportedOperationException("Not implemented");
|
||||
rest.query("/instances/target/%s/rooms/list".formatted(instanceId.getValue()), HttpMethod.DELETE, params -> {
|
||||
params.put("id", String.join(",", rooms.stream().map(RoomId::getValue).toList()));
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void lockRoom(InstanceId instanceId, RoomId roomId, boolean newState) {
|
||||
throw new UnsupportedOperationException("Not implemented");
|
||||
rest.query("/instances/target/%s/rooms/target/%s".formatted(instanceId.getValue(), roomId.getValue()), HttpMethod.PUT, param -> {
|
||||
param.put("newState", Boolean.toString(newState));
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@ -97,6 +97,20 @@ public class RestTemplate {
|
||||
}
|
||||
}
|
||||
|
||||
public void queryPostWithBody(String uri, 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);
|
||||
} catch (JsonProcessingException ex) {
|
||||
throw new RuntimeException("Json processing error", ex);
|
||||
} catch (IOException ex) {
|
||||
throw new RuntimeException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
private String queryEncode(ParamsConsumer paramsConsumer) {
|
||||
var params = new HashMap<String, String>();
|
||||
paramsConsumer.accept(params);
|
||||
|
||||
@ -43,9 +43,9 @@ public class RoomController {
|
||||
ResponseEntity<Void> createRoom(@PathVariable String instanceId,
|
||||
@RequestParam String id,
|
||||
@RequestParam int slots,
|
||||
@RequestParam String payload,
|
||||
@RequestParam(defaultValue = "false") boolean locked,
|
||||
@RequestParam(defaultValue = "false") boolean persist) {
|
||||
@RequestParam(defaultValue = "false") boolean persist,
|
||||
@RequestBody String payload) {
|
||||
var instance = instanceService.find(InstanceId.of(instanceId))
|
||||
.orElseThrow(() -> DoesNotExistsException.forInstance(InstanceId.of(instanceId)));
|
||||
roomService.create(roomFactory.create(RoomId.of(id), instance, slots, payload, persist, locked));
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user