Skip to content

Commit

Permalink
Create api implementation for couple of requests
Browse files Browse the repository at this point in the history
  • Loading branch information
Fagorym committed Oct 24, 2023
1 parent 280fb05 commit 517ede0
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 108 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package ru.nsu.fit.directors.userservice.api;

import brave.internal.Nullable;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.util.UriBuilder;
import ru.nsu.fit.directors.userservice.dto.response.BaseResponse;
import ru.nsu.fit.directors.userservice.exception.ClientException;
import ru.nsu.fit.directors.userservice.exception.ServerNotAvailableException;

import java.net.URI;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;

public class AbstractApi implements DefaultApi {
private final WebClient.Builder webClient;

public AbstractApi(WebClient.Builder webClient) {
this.webClient = webClient;
}

@Nullable
public <T> T syncGetWithParams(String path, Map<String, ?> params, Class<T> type) {
ParameterizedTypeReference<BaseResponse<T>> reference = new ParameterizedTypeReference<>() {
};
return Optional.ofNullable(webClient.build()
.get()
.uri(path, params)
.accept(MediaType.APPLICATION_JSON)
.retrieve()
.onStatus(HttpStatusCode::is5xxServerError, response -> response.bodyToMono(String.class).map(ServerNotAvailableException::new))
.toEntity(reference)
.block())
.map(ResponseEntity::getBody)
.map(BaseResponse::getResult)
.orElse(null);
}

@Nullable
public <T> List<T> syncListGetWithParams(
Function<UriBuilder, URI> uriBuilder,
ParameterizedTypeReference<BaseResponse<List<T>>> reference
) {
return Optional.ofNullable(webClient.build()
.get()
.uri(uriBuilder)
.accept(MediaType.APPLICATION_JSON)
.retrieve()
.onStatus(
HttpStatusCode::is4xxClientError,
response -> response.bodyToMono(BaseResponse.class)
.map(resp -> new ClientException(resp.getException().getMessage()))
)
.onStatus(
HttpStatusCode::is5xxServerError,
response -> response.bodyToMono(String.class).map(ServerNotAvailableException::new)
)
.toEntity(reference)
.block())
.map(ResponseEntity::getBody)
.map(BaseResponse::getResult)
.orElse(null);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package ru.nsu.fit.directors.userservice.api;

import brave.internal.Nullable;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.web.util.UriBuilder;
import ru.nsu.fit.directors.userservice.dto.response.BaseResponse;

import java.net.URI;
import java.util.List;
Expand All @@ -14,6 +16,6 @@ public interface DefaultApi {
<T> T syncGetWithParams(String path, Map<String, ?> params, Class<T> type);

@Nullable
<T> List<T> syncListGetWithParams(Function<UriBuilder, URI> uriBuilder, Class<T> type);
<T> List<T> syncListGetWithParams(Function<UriBuilder, URI> uriBuilder, ParameterizedTypeReference<BaseResponse<List<T>>> reference);

}
Original file line number Diff line number Diff line change
@@ -1,52 +1,15 @@
package ru.nsu.fit.directors.userservice.api;

import brave.internal.Nullable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.util.UriBuilder;
import ru.nsu.fit.directors.userservice.exception.ServerNotAvailableException;

import java.net.URI;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;

@Component
@RequiredArgsConstructor
public class EstablishmentApi implements DefaultApi {
private final WebClient.Builder establishmentClientBuilder;

@Nullable
public <T> T syncGetWithParams(String path, Map<String, ?> params, Class<T> type) {
return Optional.ofNullable(establishmentClientBuilder.build()
.get()
.uri(path, params)
.accept(MediaType.APPLICATION_JSON)
.retrieve()
.onStatus(HttpStatusCode::is5xxServerError, response -> response.bodyToMono(String.class).map(ServerNotAvailableException::new))
.toEntity(type)
.block())
.map(ResponseEntity::getBody)
.orElse(null);
}
public class EstablishmentApi extends AbstractApi {

@Nullable
public <T> List<T> syncListGetWithParams(Function<UriBuilder, URI> uriBuilder, Class<T> type) {
return Optional.ofNullable(establishmentClientBuilder.build()
.get()
.uri(uriBuilder)
.accept(MediaType.APPLICATION_JSON)
.retrieve()
.onStatus(HttpStatusCode::is5xxServerError, response -> response.bodyToMono(String.class).map(ServerNotAvailableException::new))
.toEntityList(type)
.block())
.map(ResponseEntity::getBody)
.orElse(null);
@Autowired
public EstablishmentApi(WebClient.Builder establishmentClientBuilder) {
super(establishmentClientBuilder);
}

}
58 changes: 5 additions & 53 deletions src/main/java/ru/nsu/fit/directors/userservice/api/OrderApi.java
Original file line number Diff line number Diff line change
@@ -1,61 +1,13 @@
package ru.nsu.fit.directors.userservice.api;

import brave.internal.Nullable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.util.UriBuilder;
import ru.nsu.fit.directors.userservice.dto.response.BaseResponse;
import ru.nsu.fit.directors.userservice.exception.ClientException;
import ru.nsu.fit.directors.userservice.exception.ServerNotAvailableException;

import java.net.URI;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;

@Component
@RequiredArgsConstructor
public class OrderApi implements DefaultApi {
private final WebClient.Builder orderClientBuilder;

@Nullable
public <T> T syncGetWithParams(String path, Map<String, ?> params, Class<T> type) {
return Optional.ofNullable(orderClientBuilder.build()
.get()
.uri(path, params)
.accept(MediaType.APPLICATION_JSON)
.retrieve()
.onStatus(HttpStatusCode::is5xxServerError, response -> response.bodyToMono(String.class).map(ServerNotAvailableException::new))
.toEntity(type)
.block())
.map(ResponseEntity::getBody)
.orElse(null);
public class OrderApi extends AbstractApi {
@Autowired
public OrderApi(WebClient.Builder orderClientBuilder) {
super(orderClientBuilder);
}

@Nullable
public <T> List<T> syncListGetWithParams(Function<UriBuilder, URI> uriBuilder, Class<T> type) {
return Optional.ofNullable(orderClientBuilder.build()
.get()
.uri(uriBuilder)
.accept(MediaType.APPLICATION_JSON)
.retrieve()
.onStatus(
HttpStatusCode::is4xxClientError,
response -> response.bodyToMono(BaseResponse.class).map(resp -> new ClientException(resp.getException().getMessage()))
)
.onStatus(
HttpStatusCode::is5xxServerError,
response -> response.bodyToMono(String.class).map(ServerNotAvailableException::new)
)
.toEntityList(type)
.block())
.map(ResponseEntity::getBody)
.orElse(null);
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ru.nsu.fit.directors.userservice.service;

import lombok.RequiredArgsConstructor;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;
import ru.nsu.fit.directors.userservice.api.EstablishmentApi;
Expand All @@ -13,8 +14,10 @@
import ru.nsu.fit.directors.userservice.mapper.OrderMapper;
import ru.nsu.fit.directors.userservice.model.User;

import javax.annotation.Nonnull;
import javax.annotation.ParametersAreNonnullByDefault;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

Expand Down Expand Up @@ -43,14 +46,15 @@ public void cancelOrder(Long orderId) {
}

@Override
@Nonnull
public List<ResponseOrderDto> getOrders(Integer status) {
User loggedUser = securityService.getLoggedInUser();
return orderApi.syncListGetWithParams(
uriBuilder -> uriBuilder.path("/order")
.queryParam("userId", loggedUser.getId())
.queryParam("status", status)
.build(),
ResponseOrderDto.class
new ParameterizedTypeReference<>() {}
);

}
Expand All @@ -67,19 +71,22 @@ private void validateUserOrder(Long orderId) {
}

private void validateOrderTime(RequestOrderDto requestOrderDto) {
boolean isOrderTimeNotValid = Optional.ofNullable(
establishmentApi.syncListGetWithParams(
uriBuilder -> uriBuilder.path("/establishment/internal/time")
.queryParam("establishmentId", requestOrderDto.getEstablishmentId())
.build(),
LocalDateTime.class
)
)
.map(times -> !times.contains(requestOrderDto.getDate().atTime(requestOrderDto.getTime())))
.orElse(true);
if (isOrderTimeNotValid) {
LocalDateTime bookingTime = requestOrderDto.getDate().atTime(requestOrderDto.getTime());
List<LocalDateTime> validTimes = Optional.of(getValidTimes(requestOrderDto))
.orElse(Collections.emptyList());
if (!validTimes.contains(bookingTime)) {
throw new OrderBookingTimeException();
}

}

@Nonnull
private List<LocalDateTime> getValidTimes(RequestOrderDto requestOrderDto) {
return establishmentApi.syncListGetWithParams(
uriBuilder -> uriBuilder.path("/establishment/internal/time")
.queryParam("establishmentId", requestOrderDto.getEstablishmentId())
.build(),
new ParameterizedTypeReference<>() {}
);
}
}

0 comments on commit 517ede0

Please sign in to comment.