Skip to content

Commit

Permalink
Added new endpoint to retrieve address for order by order id (#1608)
Browse files Browse the repository at this point in the history
  • Loading branch information
KizerovDmitriy authored Feb 11, 2025
1 parent adc52eb commit 091c5f2
Show file tree
Hide file tree
Showing 9 changed files with 230 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,8 @@ public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Excepti
SUPER_ADMIN_LINK + "/**",
USER_AGREEMENT_LINK,
USER_AGREEMENT_LINK + "/{id}",
UBS_MANAG_LINK + "/locations-details")
UBS_MANAG_LINK + "/locations-details",
UBS_LINK + "/get-address-for-order/{orderId}")
.hasAnyRole(ADMIN, UBS_EMPLOYEE)
.requestMatchers(HttpMethod.POST,
UBS_MANAG_LINK + "/addCertificate",
Expand Down
29 changes: 29 additions & 0 deletions core/src/main/java/greencity/controller/AddressController.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import greencity.dto.order.OrderAddressDtoRequest;
import greencity.dto.order.OrderWithAddressesResponseDto;
import greencity.dto.user.UserVO;
import greencity.service.ubs.AddressService;
import greencity.service.ubs.UBSClientService;
import greencity.service.ubs.UBSManagementService;
import io.swagger.v3.oas.annotations.Operation;
Expand Down Expand Up @@ -46,6 +47,7 @@
public class AddressController {
private final UBSClientService ubsClientService;
private final UBSManagementService ubsManagementService;
private final AddressService addressService;

/**
* Controller for getting all addresses for current order.
Expand Down Expand Up @@ -200,6 +202,14 @@ public ResponseEntity<List<DistrictDto>> getAllDistrictsForKyiv() {
return ResponseEntity.ok(ubsClientService.getAllDistrictsForKyiv());
}

/**
* Update address for current order. This endpoint updates a users address for
* their current order. The address is updated on the big order table.
*
* @param addressDto The updated address information.
* @param principal The user principal.
* @return HTTP status of 200 if the update was successful.
*/
@Operation(summary = "Update address for current order",
description = "Update address for current order on big order table")
@ApiResponses(value = {
Expand All @@ -215,4 +225,23 @@ public ResponseEntity<Void> updateAddress(@RequestBody @Valid UpdateAddressDto a
ubsManagementService.addressUpdate(addressDto, principal.getName());
return ResponseEntity.ok().build();
}

/**
* Retrieves the address for an order with the given id.
*
* @param orderId The id of the order
* @return The address for the order
*/
@Operation(summary = "Get address for order",
description = "Get address for order for given order id")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = HttpStatuses.OK, content = @Content),
@ApiResponse(responseCode = "401", description = HttpStatuses.UNAUTHORIZED, content = @Content),
@ApiResponse(responseCode = "403", description = HttpStatuses.FORBIDDEN, content = @Content),
@ApiResponse(responseCode = "404", description = HttpStatuses.NOT_FOUND, content = @Content)
})
@GetMapping("/get-address-for-order/{orderId}")
public ResponseEntity<UpdateAddressDto> getAddressForOrder(@PathVariable Long orderId) {
return ResponseEntity.ok(addressService.getAddressForOrder(orderId));
}
}
12 changes: 12 additions & 0 deletions core/src/test/java/greencity/controller/AddressControllerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import greencity.dto.CreateAddressRequestDto;
import greencity.dto.location.api.DistrictDto;
import greencity.dto.order.OrderAddressDtoRequest;
import greencity.service.ubs.AddressService;
import greencity.service.ubs.UBSClientService;
import greencity.service.ubs.UBSManagementService;
import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -54,6 +55,9 @@ class AddressControllerTest {
@Mock
private UBSManagementService managementService;

@Mock
private AddressService addressService;

@InjectMocks
private AddressController addressController;

Expand Down Expand Up @@ -171,4 +175,12 @@ void updateAddressTest() throws Exception {
verify(managementService).addressUpdate(any(), eq(principal.getName()));
}

@Test
void getAddressForOrderTest() throws Exception {
mockMvc.perform(get(ubsLink + "/get-address-for-order/{id}", 1L)
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk());

verify(addressService).getAddressForOrder(1L);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package greencity.service.ubs;

import greencity.dto.address.UpdateAddressDto;

public interface AddressService {
/**
* Gets address for given order id.
*
* @param orderId id of order
* @return address with given order id
* @author Dmytro Kizerov
*/
UpdateAddressDto getAddressForOrder(Long orderId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package greencity.mapping.location;

import greencity.dto.address.UpdateAddressDto;
import greencity.dto.order.OrderAddressExportDetailsDtoUpdate;
import greencity.entity.user.ubs.OrderAddress;
import org.modelmapper.AbstractConverter;
import org.springframework.stereotype.Component;

@Component
public class OrderAddressToUpdateAddressDto extends AbstractConverter<OrderAddress, UpdateAddressDto> {
@Override
protected UpdateAddressDto convert(OrderAddress source) {
return UpdateAddressDto.builder()
.orderAddressExportDetails(
OrderAddressExportDetailsDtoUpdate.builder()
.id(source.getId())
.district(source.getDistrict())
.districtEn(source.getDistrictEn())
.street(source.getStreet())
.streetEn(source.getStreetEn())
.houseCorpus(source.getHouseCorpus())
.entranceNumber(source.getEntranceNumber())
.houseNumber(source.getHouseNumber())
.city(source.getCity())
.cityEn(source.getCityEn())
.region(source.getRegion())
.regionEn(source.getRegionEn())
.addressComment(source.getAddressComment())
.build())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package greencity.service.ubs;

import greencity.constant.ErrorMessage;
import greencity.dto.address.UpdateAddressDto;
import greencity.entity.user.ubs.OrderAddress;
import greencity.exceptions.NotFoundException;
import greencity.repository.OrderAddressRepository;
import lombok.RequiredArgsConstructor;
import org.modelmapper.ModelMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
public class AddressServiceImpl implements AddressService {
private final OrderAddressRepository orderAddressRepository;
private final ModelMapper mapper;

/**
* {@inheritDoc}
*/
@Override
@Transactional(readOnly = true)
public UpdateAddressDto getAddressForOrder(Long orderId) {
OrderAddress orderAddress = orderAddressRepository.findByOrderId(orderId)
.orElseThrow(() -> new NotFoundException(ErrorMessage.NOT_FOUND_ADDRESS_BY_ORDER_ID + orderId));
UpdateAddressDto addressDto = mapper.map(orderAddress, UpdateAddressDto.class);
addressDto.setOrderId(orderId);
return addressDto;
}
}
18 changes: 18 additions & 0 deletions service/src/test/java/greencity/ModelUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -5852,4 +5852,22 @@ public static UpdateAddressDto getUpdateAddressDto() {
.orderId(1L)
.build();
}

public static OrderAddress getOrderAddress1() {
return OrderAddress.builder()
.id(1L)
.district("Деснянський район")
.districtEn("Desnyans'kyi District")
.street("вулиця Шевченка")
.streetEn("Shevchenka Street")
.houseCorpus("2")
.entranceNumber("1")
.houseNumber("34")
.city("Київ")
.cityEn("Kyiv")
.region("місто Київ")
.regionEn("Kyiv city")
.addressComment("Test comment for address №1")
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package greencity.mapping.location;

import greencity.ModelUtils;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.junit.jupiter.MockitoExtension;

import static org.junit.jupiter.api.Assertions.assertEquals;

@ExtendWith(MockitoExtension.class)
class OrderAddressToUpdateAddressDtoTest {
@InjectMocks
private OrderAddressToUpdateAddressDto mapper;

@Test
void convertTest() {
var orderAddress = ModelUtils.getOrderAddress();

var result = mapper.convert(orderAddress);

assertEquals(orderAddress.getId(), result.getOrderAddressExportDetails().getId());
assertEquals(orderAddress.getDistrict(), result.getOrderAddressExportDetails().getDistrict());
assertEquals(orderAddress.getDistrictEn(), result.getOrderAddressExportDetails().getDistrictEn());
assertEquals(orderAddress.getStreet(), result.getOrderAddressExportDetails().getStreet());
assertEquals(orderAddress.getStreetEn(), result.getOrderAddressExportDetails().getStreetEn());
assertEquals(orderAddress.getHouseCorpus(), result.getOrderAddressExportDetails().getHouseCorpus());
assertEquals(orderAddress.getEntranceNumber(), result.getOrderAddressExportDetails().getEntranceNumber());
assertEquals(orderAddress.getHouseNumber(), result.getOrderAddressExportDetails().getHouseNumber());
assertEquals(orderAddress.getCity(), result.getOrderAddressExportDetails().getCity());
assertEquals(orderAddress.getCityEn(), result.getOrderAddressExportDetails().getCityEn());
assertEquals(orderAddress.getRegion(), result.getOrderAddressExportDetails().getRegion());
assertEquals(orderAddress.getRegionEn(), result.getOrderAddressExportDetails().getRegionEn());
assertEquals(orderAddress.getAddressComment(), result.getOrderAddressExportDetails().getAddressComment());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package greencity.service.ubs;

import greencity.ModelUtils;
import greencity.dto.address.UpdateAddressDto;
import greencity.entity.user.ubs.OrderAddress;
import greencity.exceptions.NotFoundException;
import greencity.repository.OrderAddressRepository;
import java.util.Optional;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.modelmapper.ModelMapper;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
class AddressServiceTest {
@Mock
private OrderAddressRepository orderAddressRepository;
@Mock
private ModelMapper mapper;
@InjectMocks
private AddressServiceImpl addressService;

@Test
void getAddressForOrderTest() {
Long orderId = 1L;
OrderAddress orderAddress = ModelUtils.getOrderAddress1();

when(orderAddressRepository.findByOrderId(orderId)).thenReturn(Optional.of(orderAddress));
when(mapper.map(orderAddress, UpdateAddressDto.class)).thenReturn(ModelUtils.getUpdateAddressDto());

var result = addressService.getAddressForOrder(orderId);

assertEquals(orderId, result.getOrderId());
assertEquals(orderAddress.getId(), result.getOrderAddressExportDetails().getId());

verify(orderAddressRepository).findByOrderId(orderId);
}

@Test
void getNotExistingAddressForOrderTest() {
Long orderId = -1L;

when(orderAddressRepository.findByOrderId(orderId)).thenReturn(Optional.empty());

assertThrows(NotFoundException.class, () -> addressService.getAddressForOrder(orderId));

verify(orderAddressRepository).findByOrderId(orderId);
}
}

0 comments on commit 091c5f2

Please sign in to comment.