Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[POM-80] feat: 고객팀에게 store과 menu 정보 전송 추가 #60

Merged
merged 6 commits into from
Sep 22, 2023
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
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ ext {

dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-security'
// implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.boot:spring-boot-starter-web'
compileOnly 'org.projectlombok:lombok'
runtimeOnly 'com.h2database:h2'
Expand Down
40 changes: 40 additions & 0 deletions src/main/java/com/ray/pominowner/global/vo/InfoSender.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.ray.pominowner.global.vo;

import com.ray.pominowner.menu.domain.Menu;
import com.ray.pominowner.menu.service.vo.MenuInfo;
import com.ray.pominowner.store.domain.Store;
import com.ray.pominowner.store.service.vo.StoreInfo;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

@Component
public class InfoSender {

public void send(Store store) {
RestTemplate restTemplate = new RestTemplate();
StoreInfo body = StoreInfo.from(store);

HttpEntity<StoreInfo> storeInfoHttpEntity = new HttpEntity<>(body);
ResponseEntity<String> response = restTemplate.exchange("http://52.78.144.166:8080/api/v1/stores", HttpMethod.POST, storeInfoHttpEntity, String.class);
validateResponse(response);
}

public void send(Menu menu) {
RestTemplate restTemplate = new RestTemplate();
MenuInfo body = MenuInfo.from(menu);

HttpEntity<MenuInfo> menuInfoHttpEntity = new HttpEntity<>(body);
ResponseEntity<String> response = restTemplate.exchange("http://52.78.144.166:8080/api/v1/stores", HttpMethod.POST, menuInfoHttpEntity, String.class);
validateResponse(response);
}

private void validateResponse(ResponseEntity<String> exchange) {
if (!exchange.getStatusCode().is2xxSuccessful()) {
throw new IllegalArgumentException("상점 정보를 전송하는데 실패했습니다.");
}
}

}
6 changes: 2 additions & 4 deletions src/main/java/com/ray/pominowner/menu/domain/Menu.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import jakarta.persistence.OneToOne;
import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.springframework.util.Assert;

Expand All @@ -18,6 +19,7 @@
import static jakarta.persistence.FetchType.EAGER;
import static lombok.AccessLevel.PROTECTED;

@Getter
@Entity
@EqualsAndHashCode(of = "id")
@NoArgsConstructor(access = PROTECTED)
Expand Down Expand Up @@ -84,8 +86,4 @@ private boolean isNotValidStoreId(Menu menu) {
.equals(this.storeId);
}

public Long getId() {
return id;
}

}
12 changes: 10 additions & 2 deletions src/main/java/com/ray/pominowner/menu/service/MenuService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,34 @@

import com.ray.pominowner.menu.domain.Menu;
import com.ray.pominowner.menu.repository.MenuRepository;
import com.ray.pominowner.global.vo.InfoSender;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
@RequiredArgsConstructor
public class MenuService {

private final MenuRepository menuRepository;
private final InfoSender infoSender;


public Long registerMenu(Menu menu) {
return menuRepository.save(menu).getId();
Long id = menuRepository.save(menu).getId();
infoSender.send(menu);

return id;
}

@Transactional
public void updateMenu(Menu menu, Long menuId) {
Menu findMenu = menuRepository.findById(menuId)
.orElseThrow(() -> new IllegalArgumentException("해당 메뉴가 존재하지 않습니다."));
Menu updatedMenu = findMenu.updateMenu(menu);
menuRepository.save(updatedMenu);

infoSender.send(updatedMenu);
}

}
28 changes: 28 additions & 0 deletions src/main/java/com/ray/pominowner/menu/service/vo/MenuInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.ray.pominowner.menu.service.vo;

import com.ray.pominowner.menu.domain.Menu;

public record MenuInfo(Long id,
String name,
int price,
String imageUrl,
String menuOptionName,
String description,
boolean verifyAge,
Long storeId) {

public static MenuInfo from(Menu menu) {
return new MenuInfo(
menu.getId(),
menu.getName(),
menu.getPrice(),
menu.getImage().getPath(),
"best_seller", //TODO
menu.getInfo(),
false,
menu.getStoreId()
);
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@

public record StoreRegisterRequest(@NotBlank String businessNumber,
@NotBlank String name,
@NotBlank String address,
@NotBlank String mainAddress,
@NotBlank String detailAddress,
@NotBlank String logoImage) {

public Store toEntity() {
RequiredStoreInfo requiredStoreInfo = new RequiredStoreInfo(businessNumber, name, address, logoImage);
RequiredStoreInfo requiredStoreInfo = new RequiredStoreInfo(businessNumber, name, mainAddress, detailAddress, logoImage);
return new Store(requiredStoreInfo);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,41 +9,48 @@

import java.util.regex.Pattern;

@Getter
@Embeddable
@EqualsAndHashCode
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class RequiredStoreInfo {

private static final String BUSINESS_NUMBER_REGEX = "^[0-9]{10}$";

@Getter
private String businessNumber;

private String name;

private String address;
private String mainAddress;

private String logoImage;
private String detailAddress;

private static final String BUSINESS_NUMBER_REGEX = "^[0-9]{10}$";
private String logoImage;

public RequiredStoreInfo(final String businessNumber,
final String name,
final String address,
final String logoImage) {
validateConstructor(businessNumber, name, address, logoImage);
public RequiredStoreInfo(String businessNumber,
String name,
String mainAddress,
String detailAddress,
String logoImage) {
validateConstructor(businessNumber, name, mainAddress, detailAddress,logoImage);

this.businessNumber = businessNumber;
this.name = name;
this.address = address;
this.mainAddress = mainAddress;
this.detailAddress = detailAddress;
this.logoImage = logoImage;
}

private void validateConstructor(final String businessNumber,
final String name,
final String address,
final String logoImage) {
Assert.state(Pattern.matches(BUSINESS_NUMBER_REGEX, businessNumber), "사업자등록번호는 숫자 10자리 입니다.");
private void validateConstructor(String businessNumber,
String name,
String mainAddress,
String detailAddress,
String logoImage) {
Assert.isTrue(Pattern.matches(BUSINESS_NUMBER_REGEX, businessNumber), "사업자등록번호는 숫자 10자리 입니다.");
Assert.hasText(name, "가게 이름은 필수 입니다.");
Assert.hasText(address, "가게 주소는 필수 입니다.");
Assert.hasText(mainAddress, "가게 주소는 필수 입니다.");
Assert.hasText(detailAddress, "가게 상세 주소는 필수 입니다.");
Assert.hasText(logoImage, "가게 로고 이미지는 필수 입니다.");
}

Expand Down
21 changes: 19 additions & 2 deletions src/main/java/com/ray/pominowner/store/domain/Store.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.ray.pominowner.store.domain;

import com.ray.pominowner.global.domain.BaseFileEntity;
import com.ray.pominowner.global.domain.BaseTimeEntity;
import jakarta.persistence.Column;
import jakarta.persistence.Embedded;
Expand Down Expand Up @@ -118,12 +119,28 @@ public PhoneNumber getPhoneNumber() {
return tel;
}

public String getTel() {
return tel.getTel();
}

public Information getInformation() {
return info;
}

public List<StoreImage> getImages() {
return images;
public List<String> getImages() {
return images.stream().map(BaseFileEntity::getPath).toList();
}

public String getName() {
return requiredStoreInfo.getName();
}

public String getMainAddress() {
return requiredStoreInfo.getMainAddress();
}

public String getDetailAddress() {
return requiredStoreInfo.getDetailAddress();
}

}
2 changes: 2 additions & 0 deletions src/main/java/com/ray/pominowner/store/domain/StoreImage.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

import static jakarta.persistence.FetchType.LAZY;
import static lombok.AccessLevel.PROTECTED;

@Entity
@Getter
@NoArgsConstructor(access = PROTECTED)
public class StoreImage extends BaseFileEntity {

Expand Down
25 changes: 22 additions & 3 deletions src/main/java/com/ray/pominowner/store/service/StoreService.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.ray.pominowner.store.service;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.ray.pominowner.global.vo.InfoSender;
import com.ray.pominowner.store.domain.Store;
import com.ray.pominowner.store.repository.StoreRepository;
import lombok.RequiredArgsConstructor;
Expand All @@ -25,39 +26,57 @@ public class StoreService {

private final StoreImageService storeImageService;

private final InfoSender infoSender;

public Long registerStore(Store store) throws JsonProcessingException {
storeServiceValidator.validateBusinessNumber(store.getBusinessNumber());
Long id = storeRepository.save(store).getId();
infoSender.send(store);

return storeRepository.save(store).getId();
return id;
}

public void registerCategory(List<String> categories, Long storeId) {
storeServiceValidator.validateCategory(categories);
storeCategoryService.saveCategories(findStore(storeId), categories);
Store store = findStore(storeId);
storeCategoryService.saveCategories(store, categories);

infoSender.send(store);
}

public void registerPhoneNumber(String phoneNumber, Long storeId) {
Store store = findStore(storeId).retrieveStoreAfterRegisteringPhoneNumber(phoneNumber);
storeRepository.save(store);

infoSender.send(store);
}

public void deletePhoneNumber(Long storeId) {
Store store = findStore(storeId).retrieveStoreAfterDeletingPhoneNumber();
storeRepository.save(store);

infoSender.send(store);
}

public void registerInformation(String information, Long storeId) {
Store store = findStore(storeId).retrieveStoreAfterRegisteringInfo(information);
storeRepository.save(store);

infoSender.send(store);
}

public void deleteInformation(Long storeId) {
Store store = findStore(storeId).retrieveStoreAfterDeletingInfo();
storeRepository.save(store);

infoSender.send(store);
}

public void saveStoreImages(List<MultipartFile> images, Long storeId) {
storeImageService.saveImages(images, findStore(storeId));
Store store = findStore(storeId);
storeImageService.saveImages(images, store);

infoSender.send(store);
}

private Store findStore(Long storeId) {
Expand Down
32 changes: 32 additions & 0 deletions src/main/java/com/ray/pominowner/store/service/vo/StoreInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.ray.pominowner.store.service.vo;

import com.ray.pominowner.store.domain.Store;
import com.ray.pominowner.store.domain.StoreImage;

import java.util.List;

public record StoreInfo(Long id,
String name,
String mainAddress,
String detailAddress,
String phoneNumber,
List<String> images,
String openTime,
String closeTime) {


public static StoreInfo from(Store store) {
return new StoreInfo(
store.getId(),
store.getName(),
store.getMainAddress(),
store.getDetailAddress(),
store.getTel(),
store.getImages(),
"12:00:00",
"22:00:00"
);
}

}

2 changes: 1 addition & 1 deletion src/main/resources/sub
Submodule sub updated from 6e44aa to 6f9075
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static Category category() {
}

public static RequiredStoreInfo requiredStoreInfo() {
return new RequiredStoreInfo("0123456789", "name", "address", "logoImage");
return new RequiredStoreInfo("0123456789", "name", "mainAddress", "detailAddress", "logoImage");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class StoreControllerTest {
@DisplayName("가게 등록에 성공한다")
void successRegisterStore() throws Exception {
// given
StoreRegisterRequest storeRegisterRequest = new StoreRegisterRequest("1234567890", "가게이름", "서울특별시 가게동 주소구", "가게이미지URL.png");
StoreRegisterRequest storeRegisterRequest = new StoreRegisterRequest("1234567890", "가게이름", "서울특별시 가게동 주소구", "100동 108동","가게이미지URL.png");
given(storeService.registerStore(any(Store.class)))
.willReturn(1L);

Expand Down
Loading