Skip to content

Commit

Permalink
[UBS user] Fix controller (#1183)
Browse files Browse the repository at this point in the history
* Remove from AddressDto list of all districts in that adress

* Fixed OrderWithAddressesResponseDto to return list with districts

* changed in UserProfileDto field from AddressDto To AddressWIthDIstrcitsDto

* deleted unused lines
  • Loading branch information
ospodaryk authored Jul 13, 2023
1 parent 63262f9 commit 32ae1ef
Show file tree
Hide file tree
Showing 14 changed files with 196 additions and 103 deletions.
75 changes: 73 additions & 2 deletions core/src/test/java/greencity/ModelUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import greencity.dto.AddNewTariffDto;
import greencity.dto.CreateAddressRequestDto;
import greencity.dto.address.AddressDto;
import greencity.dto.address.AddressWithDistrictsDto;
import greencity.dto.bag.BagDto;
import greencity.dto.bag.BagLimitDto;
import greencity.dto.courier.CourierDto;
Expand All @@ -16,6 +17,7 @@
import greencity.dto.location.AddLocationTranslationDto;
import greencity.dto.location.LocationCreateDto;
import greencity.dto.location.RegionTranslationDto;
import greencity.dto.location.api.DistrictDto;
import greencity.dto.notification.NotificationDto;
import greencity.dto.notification.NotificationPlatformDto;
import greencity.dto.notification.NotificationTemplateDto;
Expand Down Expand Up @@ -74,6 +76,7 @@
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.Arrays;
import java.util.stream.Collectors;

import static greencity.enums.ViolationLevel.MAJOR;
Expand Down Expand Up @@ -144,7 +147,6 @@ public static List<AddressDto> addressDto() {
.region("Регіон")
.cityEn("Lviv")
.city("Львів")
.addressRegionDistrictList(new ArrayList<>())
.actual(false)
.build());
list.add(AddressDto.builder().id(2L)
Expand All @@ -160,9 +162,78 @@ public static List<AddressDto> addressDto() {
.region("Регіон")
.city("Львів")
.cityEn("Lviv")
.addressRegionDistrictList(new ArrayList<>())
.actual(false)
.build());
return list;
}

public static AddressWithDistrictsDto getAddressWithDistrictsDto(long id) {
return AddressWithDistrictsDto.builder().addressDto(AddressDto.builder()
.id(id)
.region("Вінницька")
.city("Вінниця")
.street("Street")
.district("Distinct")
.houseNumber("25")
.houseCorpus("2")
.entranceNumber("7a")
.addressComment("Address Comment")
.actual(false)
.coordinates(Coordinates.builder()
.latitude(50.4459068)
.longitude(30.4477005)
.build())
.regionEn("RegionEng")
.cityEn("CityEng")
.streetEn("StreetEng")
.districtEn("DistinctEng").build())
.addressRegionDistrictList(Arrays.asList(getDistrictDto()))
.build();
}

public static DistrictDto getDistrictDto() {
return DistrictDto.builder()
.nameUa("Вінниця")
.nameEn("Vinnytsa")
.build();
}

public static List<AddressWithDistrictsDto> addressWithDistrictsDtoList() {
List<AddressWithDistrictsDto> list = new ArrayList<>();
list.add(AddressWithDistrictsDto.builder().addressDto(AddressDto.builder()
.id(1L)
.entranceNumber("7a")
.houseCorpus("2")
.houseNumber("7")
.street("Городоцька")
.streetEn("Gorodotska")
.coordinates(Coordinates.builder().latitude(2.3).longitude(5.6).build())
.district("Залізничний")
.districtEn("Zaliznuchnuy")
.regionEn("Region")
.region("Регіон")
.cityEn("Lviv")
.city("Львів")
.actual(false).build())
.addressRegionDistrictList(new ArrayList<>())

.build());
list.add(AddressWithDistrictsDto.builder().addressDto(AddressDto.builder()
.id(2L)

.entranceNumber("9a")
.houseCorpus("2")
.houseNumber("7")
.street("Шевченка")
.streetEn("Shevchenka")
.coordinates(Coordinates.builder().latitude(3.3).longitude(6.6).build())
.district("Залізничний")
.districtEn("Zaliznuchnuy")
.regionEn("Region")
.region("Регіон")
.city("Львів")
.cityEn("Lviv").actual(false).build())
.addressRegionDistrictList(new ArrayList<>())
.build());
return list;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import greencity.constant.AppConstant;
import greencity.converters.UserArgumentResolver;
import greencity.dto.address.AddressDto;
import greencity.dto.address.AddressWithDistrictsDto;
import greencity.dto.user.UserProfileDto;
import greencity.exception.handler.CustomExceptionHandler;
import greencity.service.ubs.UBSClientService;
Expand Down Expand Up @@ -72,7 +73,7 @@ void setup() {
@Test
void saveUserDate() throws Exception {
UserProfileDto userProfileDto = ModelUtils.userProfileDto();
List<AddressDto> addressDto = ModelUtils.addressDto();
List<AddressWithDistrictsDto> addressDto = ModelUtils.userProfileDto().getAddressDto();
userProfileDto.setAddressDto(addressDto);

ObjectMapper objectMapper = new ObjectMapper();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package greencity.dto.address;

import greencity.dto.location.api.DistrictDto;
import greencity.entity.coords.Coordinates;
import lombok.AllArgsConstructor;
import lombok.Builder;
Expand All @@ -13,7 +12,6 @@
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import java.io.Serializable;
import java.util.List;

import static greencity.constant.ValidationConstant.CH_EN;
import static greencity.constant.ValidationConstant.CH_NUM;
Expand Down Expand Up @@ -70,5 +68,4 @@ public class AddressDto implements Serializable {
private String districtEn;

private String placeId;
private List<DistrictDto> addressRegionDistrictList;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package greencity.dto.address;

import greencity.dto.location.api.DistrictDto;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
import java.util.List;

@Builder
@Data
@NoArgsConstructor
@AllArgsConstructor
public class AddressWithDistrictsDto implements Serializable {
private AddressDto addressDto;
private List<DistrictDto> addressRegionDistrictList;
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package greencity.dto.order;

import greencity.dto.address.AddressDto;
import lombok.*;

import greencity.dto.address.AddressWithDistrictsDto;
import lombok.Getter;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Setter;
import lombok.NoArgsConstructor;
import lombok.ToString;
import lombok.EqualsAndHashCode;
import java.util.List;

@Getter
Expand All @@ -13,5 +18,5 @@
@EqualsAndHashCode
@Builder
public class OrderWithAddressesResponseDto {
private List<AddressDto> addressList;
private List<AddressWithDistrictsDto> addressList;
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package greencity.dto.user;

import greencity.annotations.ValidPhoneNumber;
import greencity.dto.address.AddressDto;
import greencity.dto.address.AddressWithDistrictsDto;
import greencity.util.Bot;
import lombok.AllArgsConstructor;
import lombok.Builder;
Expand Down Expand Up @@ -40,7 +40,7 @@ public class UserProfileDto {
@NotBlank
@ValidPhoneNumber
private String recipientPhone;
private List<AddressDto> addressDto;
private List<AddressWithDistrictsDto> addressDto;
private List<Bot> botList;
private Boolean hasPassword;
@NonNull
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package greencity.mapping.user;

import greencity.dto.address.AddressDto;
import greencity.dto.address.AddressWithDistrictsDto;
import greencity.dto.location.api.DistrictDto;
import greencity.dto.location.api.LocationDto;
import greencity.entity.coords.Coordinates;
Expand All @@ -16,24 +17,24 @@

/**
* Class that used by {@link ModelMapper} to map {@link Address} into
* {@link AddressDto}.
* {@link AddressWithDistrictsDto}.
*/
@Component
public class AddressToAddressDtoMapper extends AbstractConverter<Address, AddressDto> {
public class AddressToAddressWithDistrictsDtoMapper extends AbstractConverter<Address, AddressWithDistrictsDto> {
/**
* Service for getting districts in city.
*/
@Autowired
private LocationApiService locationApiService;

/**
* Method convert {@link Address} to {@link AddressDto}.
* Method convert {@link Address} to {@link AddressWithDistrictsDto}.
*
* @return {@link AddressDto}
* @return {@link AddressWithDistrictsDto}
*/
@Override
public AddressDto convert(Address address) {
return AddressDto.builder()
public AddressWithDistrictsDto convert(Address address) {
return AddressWithDistrictsDto.builder().addressDto(AddressDto.builder()
.id(address.getId())
.region(address.getRegion())
.regionEn(address.getRegionEn())
Expand All @@ -51,8 +52,8 @@ public AddressDto convert(Address address) {
.latitude(address.getCoordinates().getLatitude())
.longitude(address.getCoordinates().getLongitude())
.build())
.actual(address.getActual()).build())
.addressRegionDistrictList(getAllDistricts((address.getRegion()), address.getCity()))
.actual(address.getActual())
.build();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,18 @@
package greencity.mapping.user;

import greencity.dto.address.AddressDto;
import greencity.dto.location.api.DistrictDto;
import greencity.dto.location.api.LocationDto;
import greencity.dto.user.UserProfileUpdateDto;
import greencity.entity.coords.Coordinates;
import greencity.entity.user.User;
import greencity.entity.user.ubs.Address;
import greencity.service.locations.LocationApiService;
import org.modelmapper.AbstractConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

@Component
public class UserToUserProfileUpdateDtoMapper extends AbstractConverter<User, UserProfileUpdateDto> {
/**
* Service for getting districts in city.
*/
@Autowired
private LocationApiService locationApiService;

/**
* Method convert {@link User} to {@link UserProfileUpdateDto}.
*
Expand Down Expand Up @@ -65,17 +55,6 @@ private AddressDto createAddressDto(Address address) {
.longitude(address.getCoordinates().getLongitude())
.build())
.actual(address.getActual())
.addressRegionDistrictList(getAllDistricts((address.getRegion()), address.getCity()))
.build();
}

private List<DistrictDto> getAllDistricts(String region, String city) {
List<LocationDto> locationDtos = locationApiService.getAllDistrictsInCityByNames(region, city);
return locationDtos.stream()
.map(locationDto -> DistrictDto.builder()
.nameUa(locationDto.getLocationNameMap().get("name"))
.nameEn(locationDto.getLocationNameMap().get("name_en"))
.build())
.collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import greencity.dto.TariffsForLocationDto;
import greencity.dto.address.AddressDto;
import greencity.dto.address.AddressInfoDto;
import greencity.dto.address.AddressWithDistrictsDto;
import greencity.dto.bag.BagDto;
import greencity.dto.bag.BagForUserDto;
import greencity.dto.bag.BagOrderDto;
Expand Down Expand Up @@ -534,10 +535,10 @@ private void checkIfUserHaveEnoughPoints(Integer i1, Integer i2) {
@Override
public OrderWithAddressesResponseDto findAllAddressesForCurrentOrder(String uuid) {
Long id = userRepository.findByUuid(uuid).getId();
List<AddressDto> addressDtoList = addressRepo.findAllNonDeletedAddressesByUserId(id)
List<AddressWithDistrictsDto> addressDtoList = addressRepo.findAllNonDeletedAddressesByUserId(id)
.stream()
.sorted(Comparator.comparing(Address::getId))
.map(u -> modelMapper.map(u, AddressDto.class))
.map(u -> modelMapper.map(u, AddressWithDistrictsDto.class))
.collect(toList());
return new OrderWithAddressesResponseDto(addressDtoList);
}
Expand Down Expand Up @@ -1310,9 +1311,9 @@ public UserProfileDto getProfileData(String uuid) {
List<Address> allAddress = addressRepo.findAllNonDeletedAddressesByUserId(user.getId());
UserProfileDto userProfileDto = modelMapper.map(user, UserProfileDto.class);
List<Bot> botList = getListOfBots(user.getUuid());
List<AddressDto> addressDto =
List<AddressWithDistrictsDto> addressDto =
allAddress.stream()
.map(a -> modelMapper.map(a, AddressDto.class))
.map(a -> modelMapper.map(a, AddressWithDistrictsDto.class))
.collect(toList());
userProfileDto.setAddressDto(addressDto);
userProfileDto.setBotList(botList);
Expand Down
Loading

0 comments on commit 32ae1ef

Please sign in to comment.