Skip to content

Commit

Permalink
Replace services with feign clients
Browse files Browse the repository at this point in the history
  • Loading branch information
Fagorym committed Feb 28, 2024
1 parent ca0ff0a commit e769e5d
Show file tree
Hide file tree
Showing 12 changed files with 95 additions and 254 deletions.
3 changes: 2 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,13 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-security:3.0.2'
implementation 'org.flywaydb:flyway-core'
implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'
implementation 'org.springframework.cloud:spring-cloud-starter-openfeign'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
implementation 'org.modelmapper:modelmapper:3.1.1'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'io.projectreactor:reactor-test'
implementation 'org.postgresql:postgresql:42.5.4'
implementation 'org.postgresql:postgresql:42.7.0'
implementation 'io.micrometer:micrometer-tracing-bridge-brave'
implementation 'io.zipkin.reporter2:zipkin-reporter-brave:2.16.4'
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.1.0'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
package ru.nsu.fit.directors.userservice;

import io.swagger.v3.oas.annotations.OpenAPIDefinition;
import io.swagger.v3.oas.annotations.servers.Server;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@OpenAPIDefinition(servers = {
@Server(
description = "This is the localhost",
url = "localhost:8080/user"
),
@Server(
description = "This is the real server",
url = "80.87.200.185/user"
)
})
@EnableDiscoveryClient
@EnableFeignClients
@OpenAPIDefinition
public class UserServiceApplication {

public static void main(String[] args) {
Expand Down
106 changes: 0 additions & 106 deletions src/main/java/ru/nsu/fit/directors/userservice/api/AbstractApi.java

This file was deleted.

33 changes: 0 additions & 33 deletions src/main/java/ru/nsu/fit/directors/userservice/api/DefaultApi.java

This file was deleted.

This file was deleted.

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

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import ru.nsu.fit.directors.userservice.dto.CompanyDto;
import ru.nsu.fit.directors.userservice.dto.request.ReviewCreationDto;
import ru.nsu.fit.directors.userservice.dto.response.EstablishmentListDto;
import ru.nsu.fit.directors.userservice.dto.response.ResponseReviewDto;
import ru.nsu.fit.directors.userservice.model.Company;

@FeignClient("establishment-service")
public interface EstablishmentServiceClient {
@RequestMapping(method = RequestMethod.GET, value = "/establishment", produces = MediaType.APPLICATION_JSON_VALUE)
Company getCompanyById(@RequestParam Long establishmentId);

@RequestMapping(method = RequestMethod.GET, value = "/internal/establishment", produces = MediaType.APPLICATION_JSON_VALUE)
List<CompanyDto> getCompaniesByIds(@RequestParam List<Long> ids);

@RequestMapping(method = RequestMethod.GET, value = "/establishment/all", produces = MediaType.APPLICATION_JSON_VALUE)
EstablishmentListDto searchEstablishments(
@RequestParam Optional<String> name,
@RequestParam Optional<Boolean> hasMap,
@RequestParam Optional<Boolean> hasCardPayment,
@RequestParam Optional<String> category
);

@RequestMapping(method = RequestMethod.GET, value = "/internal/review", produces = MediaType.APPLICATION_JSON_VALUE)
List<ResponseReviewDto> getReviewsByExternalIds(@RequestParam List<Long> ids);

@RequestMapping(method = RequestMethod.POST, value = "/internal/review", produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
Long createReview(@RequestBody ReviewCreationDto reviewCreationDto);

@RequestMapping(method = RequestMethod.GET, value = "/establishment/internal/time", produces = MediaType.APPLICATION_JSON_VALUE)
List<LocalDateTime> getValidTimes(@RequestParam Long establishmentId);
}
13 changes: 0 additions & 13 deletions src/main/java/ru/nsu/fit/directors/userservice/api/OrderApi.java

This file was deleted.

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

import java.util.List;
import java.util.Optional;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import ru.nsu.fit.directors.userservice.dto.response.ResponseOrderDto;

@FeignClient("order-service")
public interface OrderServiceClient {
@RequestMapping(method = RequestMethod.GET, value = "/order", produces = MediaType.APPLICATION_JSON_VALUE)
List<ResponseOrderDto> getUserOrders(@RequestParam Long userId, @RequestParam Optional<Integer> status);

@RequestMapping(method = RequestMethod.GET, value = "/order/id", produces = MediaType.APPLICATION_JSON_VALUE)
ResponseOrderDto getOrderById(@RequestParam Long id);
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package ru.nsu.fit.directors.userservice.repository;

import lombok.RequiredArgsConstructor;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.stereotype.Component;
import ru.nsu.fit.directors.userservice.api.EstablishmentApi;
import ru.nsu.fit.directors.userservice.api.EstablishmentServiceClient;
import ru.nsu.fit.directors.userservice.dto.CompanyDto;
import ru.nsu.fit.directors.userservice.model.Company;

Expand All @@ -12,20 +11,13 @@
@Component
@RequiredArgsConstructor
public class CompanyService {
private final EstablishmentApi establishmentApi;
private final EstablishmentServiceClient establishmentServiceClient;

public Company getCompanyById(Long establishmentId) {
return establishmentApi.syncGetWithParams(
uriBuilder -> uriBuilder.path("/establishment").queryParam("establishmentId", establishmentId).build(),
new ParameterizedTypeReference<>() {
}
);
return establishmentServiceClient.getCompanyById(establishmentId);
}

public List<CompanyDto> getCompaniesByIds(List<Long> ids) {
return establishmentApi.syncListGetWithParams(
uriBuilder -> uriBuilder.path("/internal/establishment").queryParam("ids", ids).build(),
new ParameterizedTypeReference<>() {}
);
return establishmentServiceClient.getCompaniesByIds(ids);
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package ru.nsu.fit.directors.userservice.service;

import lombok.RequiredArgsConstructor;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.stereotype.Component;
import ru.nsu.fit.directors.userservice.api.EstablishmentApi;
import ru.nsu.fit.directors.userservice.api.EstablishmentServiceClient;
import ru.nsu.fit.directors.userservice.dto.request.RequestGetEstablishmentParameters;
import ru.nsu.fit.directors.userservice.dto.request.ReviewCreationDto;
import ru.nsu.fit.directors.userservice.dto.response.EstablishmentListDto;
Expand All @@ -19,21 +18,17 @@
@RequiredArgsConstructor
@ParametersAreNonnullByDefault
public class EstablishmentServiceImpl implements EstablishmentService {
private final EstablishmentApi establishmentApi;
private final EstablishmentServiceClient establishmentClient;
private final FavouritesService favouritesService;

@Nonnull
@Override
public EstablishmentListDto getEstablishmentByParams(RequestGetEstablishmentParameters parameters) {
EstablishmentListDto establishmentInfoList = establishmentApi.syncGetWithParams(
uriBuilder -> uriBuilder.path("/establishment/all")
.queryParamIfPresent("name", Optional.ofNullable(parameters.name()))
.queryParamIfPresent("hasMap", Optional.ofNullable(parameters.hasMap()))
.queryParamIfPresent("hasCardPayment", Optional.ofNullable(parameters.hasCardPayment()))
.queryParamIfPresent("category", Optional.ofNullable(parameters.category()))
.build(),
new ParameterizedTypeReference<>() {
}
EstablishmentListDto establishmentInfoList = establishmentClient.searchEstablishments(
Optional.ofNullable(parameters.name()),
Optional.ofNullable(parameters.hasMap()),
Optional.ofNullable(parameters.hasCardPayment()),
Optional.ofNullable(parameters.category())
);
establishmentInfoList.establishments().forEach(
info -> info.setFavourite(favouritesService.getFavouritesIds().contains(info.getId()))
Expand All @@ -44,26 +39,12 @@ public EstablishmentListDto getEstablishmentByParams(RequestGetEstablishmentPara
@Nonnull
@Override
public List<ResponseReviewDto> getReviewsByExternalIds(List<Long> externalIds) {
return establishmentApi.syncListGetWithParams(
uriBuilder -> uriBuilder.path("/internal/review")
.queryParam("ids", externalIds)
.build(),
new ParameterizedTypeReference<>() {
}
);
return establishmentClient.getReviewsByExternalIds(externalIds);
}

@Nonnull
@Override
public Long createReview(ReviewCreationDto reviewCreationDto) {
return establishmentApi.syncPostWithParams(
uriBuilder -> uriBuilder.path("/internal/review").build(),
reviewCreationDto,
new ParameterizedTypeReference<>() {
},
new ParameterizedTypeReference<>() {
}
);

return establishmentClient.createReview(reviewCreationDto);
}
}
Loading

0 comments on commit e769e5d

Please sign in to comment.