Skip to content
This repository has been archived by the owner on Aug 13, 2022. It is now read-only.

#85 배달 가능 지역 업데이트 API #114

Merged
merged 7 commits into from
Aug 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/main/java/com/bluedelivery/api/shop/ShopUpdateController.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,13 @@
import org.springframework.web.bind.annotation.RequestMapping;

import com.bluedelivery.api.shop.dto.BusinessHoursRequest;
import com.bluedelivery.api.shop.dto.UpdateDeliveryAreaRequest;
import com.bluedelivery.common.response.HttpResponse;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = "가게 정보")
@RequestMapping("/shops/{shopId}")
public interface ShopUpdateController {

Expand Down Expand Up @@ -74,4 +79,10 @@ void updateClosingDays(@PathVariable Long shopId,
@PutMapping("/suspend")
void suspendShop(@PathVariable Long shopId,
@RequestBody SuspensionRequest suspension);

@ApiOperation(value = "배달 가능 지역 업데이트", notes = "읍면동 코드를 받아서 가게의 배달 가능 지역으로 설정한다.")
@PutMapping("/delivery-areas")
ResponseEntity<HttpResponse<?>> updateDeliveryArea(
@PathVariable Long shopId, @RequestBody UpdateDeliveryAreaRequest dto);

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.bluedelivery.api.shop.adapter;

import static com.bluedelivery.common.response.HttpResponse.FAIL;
import static com.bluedelivery.common.response.HttpResponse.response;
import static com.bluedelivery.common.response.HttpResponse.*;

import java.util.List;

Expand All @@ -15,12 +14,16 @@
import com.bluedelivery.api.shop.UpdateCategoryRequest;
import com.bluedelivery.api.shop.UpdateClosingDaysRequest;
import com.bluedelivery.api.shop.dto.BusinessHoursRequest;
import com.bluedelivery.api.shop.dto.DeliveryAreaResponse;
import com.bluedelivery.api.shop.dto.UpdateDeliveryAreaRequest;
import com.bluedelivery.application.category.CategoryNotFoundException;
import com.bluedelivery.application.shop.ShopUpdateService;
import com.bluedelivery.application.shop.dto.UpdateDeliveryAreaTarget;
import com.bluedelivery.common.response.ApiException;
import com.bluedelivery.common.response.ErrorCode;
import com.bluedelivery.common.response.HttpResponse;
import com.bluedelivery.domain.shop.BusinessHour;
import com.bluedelivery.domain.shop.DeliveryArea;

@RestController
public class ShopUpdateControllerImpl implements ShopUpdateController {
Expand Down Expand Up @@ -77,4 +80,15 @@ public void suspendShop(Long shopId, SuspensionRequest suspension) {
updateService.suspend(shopId, suspension.toEntity());
}

@Override
public ResponseEntity<HttpResponse<?>> updateDeliveryArea(Long shopId, UpdateDeliveryAreaRequest dto) {
try {
List<DeliveryArea> deliveryAreas =
updateService.updateDeliveryArea(UpdateDeliveryAreaTarget.of(shopId, dto.getTownCodes()));
return ResponseEntity.ok(response(SUCCESS, DeliveryAreaResponse.of(shopId, deliveryAreas)));
} catch (IllegalArgumentException ex) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(response(FAIL, ex.getMessage()));
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.bluedelivery.api.shop.dto;

import java.util.List;

import com.bluedelivery.domain.shop.DeliveryArea;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor
@AllArgsConstructor(staticName = "of")
public class DeliveryAreaResponse {
private Long shopId;
private List<DeliveryArea> deliveryAreas;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.bluedelivery.api.shop.dto;

import java.util.ArrayList;
import java.util.List;

import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor
public class UpdateDeliveryAreaRequest {

private List<String> townCodes = new ArrayList<>();

public UpdateDeliveryAreaRequest(List<String> townCodes) {
this.townCodes.addAll(townCodes);
}
}
39 changes: 39 additions & 0 deletions src/main/java/com/bluedelivery/application/shop/AddressMapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.bluedelivery.application.shop;

import static java.util.stream.Collectors.toList;

import java.util.List;
import java.util.Objects;

import org.springframework.stereotype.Component;

import com.bluedelivery.domain.address.AddressService;
import com.bluedelivery.domain.shop.DeliveryArea;

import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
@Component
public class AddressMapper {

private final AddressService addressService;

public List<DeliveryArea> deliveryAreas(List<String> codes) {
List<DeliveryArea> deliveryAreas = addressService.getTowns(codes)
.stream()
.map(DeliveryArea::of)
.collect(toList());
validate(codes, deliveryAreas);
return deliveryAreas;
}

private void validate(List<String> codes, List<DeliveryArea> deliveryAreas) {
if (!foundAll(codes, deliveryAreas)) {
throw new IllegalArgumentException("존재하지 않는 지역이 포함되어 있습니다.");
}
}

private boolean foundAll(List<String> codes, List<DeliveryArea> deliveryAreas) {
return !Objects.equals(deliveryAreas.size(), codes.size());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@
import com.bluedelivery.application.category.CategoryManagerService;
import com.bluedelivery.application.shop.businesshour.DayOfWeekMapper;
import com.bluedelivery.application.shop.dto.BusinessHoursTarget;
import com.bluedelivery.application.shop.dto.UpdateDeliveryAreaTarget;
import com.bluedelivery.common.response.ApiException;
import com.bluedelivery.domain.closingday.LegalHolidayClosing;
import com.bluedelivery.domain.closingday.Suspension;
import com.bluedelivery.domain.shop.BusinessHour;
import com.bluedelivery.domain.shop.DeliveryArea;
import com.bluedelivery.domain.shop.Shop;
import com.bluedelivery.domain.shop.ShopRepository;

Expand All @@ -28,6 +30,7 @@ public class ShopUpdateService {

private final ShopRepository shopRepository;
private final CategoryManagerService categoryManagerService;
private final AddressMapper addressMapper;

public List<BusinessHour> updateBusinessHour(BusinessHoursTarget target) {
Shop shop = getShop(target.getShopId());
Expand Down Expand Up @@ -70,7 +73,7 @@ public void updateClosingDays(Long id, UpdateClosingDaysRequest closingDays) {
shop.addClosingDayPolicy(new LegalHolidayClosing());
}
temporaries.stream().forEach(
temporary -> shop.addClosingDayPolicy( temporary.toEntity()));
temporary -> shop.addClosingDayPolicy(temporary.toEntity()));
regulars.stream().forEach(
regular -> shop.addClosingDayPolicy(regular.toEntity()));
}
Expand All @@ -85,6 +88,14 @@ public void suspend(Long shopId, Suspension suspension) {
shop.suspend(suspension);
}

@Transactional
public List<DeliveryArea> updateDeliveryArea(UpdateDeliveryAreaTarget target) {
Shop shop = getShop(target.getShopId());
List<DeliveryArea> areas = addressMapper.deliveryAreas(target.getTownCodes());
shop.updateDeliveryArea(areas);
return areas;
}

private Shop getShop(Long id) {
return shopRepository.findById(id).orElseThrow(() -> new ApiException(SHOP_DOES_NOT_EXIST));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.bluedelivery.application.shop.dto;

import java.util.List;

import lombok.Getter;
import lombok.RequiredArgsConstructor;

@Getter
@RequiredArgsConstructor(staticName = "of")
public class UpdateDeliveryAreaTarget {
private final Long shopId;
private final List<String> townCodes;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import static com.bluedelivery.common.response.ErrorCode.ADDRESS_DOES_NOT_EXIST;

import java.util.List;

import org.springframework.stereotype.Service;

import com.bluedelivery.common.response.ApiException;
Expand All @@ -15,6 +17,7 @@ public class AddressService {

private final AddressRepository addressRepository;
private final BuildingInfoRepository buildingInfoRepository;
private final CityToDongRepository cityToDongRepository;

/**
* 주어진 건물관리번호에 해당하는 건물 정보를 조회한다.
Expand All @@ -39,4 +42,8 @@ public Address getAddress(Long addressId, User user) {
return addressRepository.findByIdAndUser(addressId, user)
.orElseThrow(() -> new ApiException(ADDRESS_DOES_NOT_EXIST));
}

public List<CityToDong> getTowns(List<String> townCodes) {
return cityToDongRepository.findAllById(townCodes);
}
}
2 changes: 2 additions & 0 deletions src/main/java/com/bluedelivery/domain/address/CityToDong.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import javax.persistence.Id;

import lombok.Builder;
import lombok.Getter;

@Getter
@Entity
public class CityToDong {
@Id
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.bluedelivery.domain.address;

import org.springframework.data.jpa.repository.JpaRepository;

public interface CityToDongRepository extends JpaRepository<CityToDong, String> {
}
37 changes: 37 additions & 0 deletions src/main/java/com/bluedelivery/domain/shop/DeliveryArea.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.bluedelivery.domain.shop;

import javax.persistence.Column;
import javax.persistence.Embeddable;

import com.bluedelivery.domain.address.CityToDong;

@Embeddable
public class DeliveryArea {
@Column(name = "TOWN_CODE")
private String townCode;

@Column(name = "TOWN_NAME")
private String townName;

public DeliveryArea() {
}

public DeliveryArea(String townCode, String townName) {
this.townCode = townCode;
this.townName = townName;
}

public static DeliveryArea of(CityToDong town) {
return new DeliveryArea(
town.getAddressJurisdictionEupMyonDongCode(),
town.getEupMyonDongName());
}

public String getTownCode() {
return townCode;
}

public String getTownName() {
return townName;
}
}
14 changes: 14 additions & 0 deletions src/main/java/com/bluedelivery/domain/shop/Shop.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -35,6 +36,10 @@ public class Shop {
private String deliveryAreaGuide;
private int minimumOrderAmount;

@ElementCollection
@CollectionTable(name = "DELIVERY_AREA", joinColumns = @JoinColumn(name = "SHOP_ID"))
private List<DeliveryArea> deliveryAreas = new ArrayList<>();

@ElementCollection
@CollectionTable(name = "BUSINESS_HOUR", joinColumns = @JoinColumn(name = "SHOP_ID"))
private List<BusinessHour> businessHours = new ArrayList<>();
Expand Down Expand Up @@ -164,4 +169,13 @@ public int getMinimumOrderAmount() {
public void setMinimumOrderAmount(int minimumOrderAmount) {
this.minimumOrderAmount = minimumOrderAmount;
}

public boolean updateDeliveryArea(List<DeliveryArea> deliveryAreas) {
this.deliveryAreas.clear();
return this.deliveryAreas.addAll(deliveryAreas);
}

public List<DeliveryArea> getDeliveryAreas() {
return Collections.unmodifiableList(deliveryAreas);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.util.List;

import org.apache.ibatis.annotations.Param;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
Expand Down
Loading