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-94] 가게 전화번호 삭제 기능 추가 #32

Merged
merged 4 commits into from
Sep 14, 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
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
Expand Down Expand Up @@ -40,4 +41,9 @@ public void registerPhoneNumber(@RequestBody @Valid PhoneNumberRequest phoneNumb
storeService.registerPhoneNumber(phoneNumberRequest.phoneNumber(), storeId);
}

@DeleteMapping("/{storeId}/phone-numbers")
public void deletePhoneNumber(@PathVariable Long storeId) {
storeService.deletePhoneNumber(storeId);
}

}
16 changes: 13 additions & 3 deletions src/main/java/com/ray/pominowner/store/domain/Store.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public String getBusinessNumber() {
return requiredStoreInfo.getBusinessNumber();
}

public Store afterRegisterPhoneNumber(String phoneNumber) {
public Store retrieveStoreAfterRegisteringPhoneNumber(String phoneNumber) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

메소드명에 retrieveStoreAfter가 필요한가요?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

메서드 명을 어떻게 작성하는게 좋을까요?
단순히 register만 하는게 아니라 return type이 Store라서.. 이럴 경우 어떻게 메서드 명을 작성하는게 좋은지 저도 궁금하긴 하네요

return Store.builder()
.id(this.id)
.requiredStoreInfo(this.requiredStoreInfo)
Expand All @@ -67,12 +67,22 @@ public Store afterRegisterPhoneNumber(String phoneNumber) {
.build();
}

public Store retrieveStoreAfterDeletingPhoneNumber() {
return Store.builder()
.id(this.id)
.requiredStoreInfo(this.requiredStoreInfo)
.info(this.info)
.tel(new PhoneNumber())
.ownerId(this.ownerId)
.build();
}

public Long getId() {
return id;
}

public String getTel() {
return tel.getTel();
public PhoneNumber getPhoneNumber() {
return tel;
Comment on lines +84 to +85
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

지금처럼 쓰실거면 tel의 getter니까 getTel 로 메소드 이름을 지어도 괜찮을 것 같아요

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tel이 db에서는 사용해도 괜찮은데 뭔가 어플리케이션단에서는 쓰기 좀 그래서 PhoneNumber라 이름을 지었어요

}

}
12 changes: 9 additions & 3 deletions src/main/java/com/ray/pominowner/store/service/StoreService.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,24 @@ public Long registerStore(Store store) throws JsonProcessingException {
}

@Transactional
public void registerCategory(final List<String> categories, Long storeId) {
public void registerCategory(List<String> categories, Long storeId) {
storeServiceValidator.validateCategory(categories);
storeCategoryService.saveCategories(findStore(storeId), categories);
}

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

private Store findStore(final Long storeId) {
@Transactional
public void deletePhoneNumber(Long storeId) {
Store store = findStore(storeId).retrieveStoreAfterDeletingPhoneNumber();
storeRepository.save(store);
}

private Store findStore(Long storeId) {
return storeRepository.findById(storeId)
.orElseThrow(() -> new IllegalArgumentException("존재하지 않는 가게입니다."));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.doNothing;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

Expand Down Expand Up @@ -85,4 +85,18 @@ void successRegisterPhoneNumber() throws Exception {
.andExpect(status().isOk());
}

@Test
@WithMockUser
@DisplayName("전화번호 삭제에 성공한다")
void successRemovePhoneNumber() throws Exception {
// given
doNothing().when(storeService).deletePhoneNumber(any(Long.class));

// when, then
mvc.perform(delete("/api/v1/stores/1/phone-numbers")
.with(csrf())
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk());
}

}
24 changes: 20 additions & 4 deletions src/test/java/com/ray/pominowner/store/domain/StoreTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,30 @@ void successWhenRequiredInfoIsNotNull() {
void successRegisterPhoneNumber() {
// given
Store store = StoreTestFixture.store();
String validPhoneNumber = "01012345678";
PhoneNumber validPhoneNumber = new PhoneNumber("010-1234-5678");

// when
Store storeAfterRegisteredPhoneNumber = store.afterRegisterPhoneNumber(validPhoneNumber);
Store storeAfterRegisteredPhoneNumber = store.retrieveStoreAfterRegisteringPhoneNumber(validPhoneNumber.getTel());

// then
assertThat(store.getTel()).isEqualTo("NOT_SELECTED");
assertThat(storeAfterRegisteredPhoneNumber.getTel()).isEqualTo(validPhoneNumber);
assertThat(store.getPhoneNumber()).isEqualTo(new PhoneNumber());
assertThat(storeAfterRegisteredPhoneNumber.getPhoneNumber()).isEqualTo(validPhoneNumber);
}

@Test
@DisplayName("정상적으로 전화번호가 삭제된다")
void successDeletingPhoneNumber() {
// given
String validPhoneNumber = "010-1234-5678";
Store store = StoreTestFixture.store()
.retrieveStoreAfterRegisteringPhoneNumber(validPhoneNumber);

// when
Store deletedPhoneNumberStore = store.retrieveStoreAfterDeletingPhoneNumber();

// then
assertThat(deletedPhoneNumberStore.getPhoneNumber()).isEqualTo(new PhoneNumber());
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,15 @@ void successRegisterPhoneNumber() {
.isThrownBy(() -> storeService.registerPhoneNumber(validPhoneNumber, store.getId()));
}

@Test
@DisplayName("전화번호를 정상적으로 삭제된다")
void successDeletePhoneNumber() {
// given
given(storeRepository.findById(store.getId())).willReturn(Optional.of(store));

// when, then
assertThatNoException()
.isThrownBy(() -> storeService.deletePhoneNumber(store.getId()));
}

}