Skip to content

Commit

Permalink
[FIX] E7-S3 메뉴 delete 방식 변경 및 테스트 코드 작성 #99
Browse files Browse the repository at this point in the history
  • Loading branch information
yeongunheo committed Nov 15, 2021
1 parent 216e53c commit a5863bd
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public ResponseEntity<Long> updateMenu(@PathVariable Long id, @RequestBody MenuU
return new ResponseEntity<>(menuService.update(id, requestDto), HttpStatus.OK);
}

@DeleteMapping("/menu/{id}")
@PatchMapping("/menu/{id}")
public ResponseEntity<Long> deleteMenu(@PathVariable Long id) {
return new ResponseEntity<>(menuService.delete(id), HttpStatus.OK);
}
Expand Down
4 changes: 4 additions & 0 deletions backend/src/main/java/com/infp/ciat/category/entity/Menu.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,8 @@ public void update(MenuUpdateRequestDto requestDto) {
this.orders = requestDto.getOrders();
this.showYn = requestDto.getShowYn();
}

public void delete() {
this.showYn = "N";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,21 +56,29 @@ public List<MenuDto> getList() {
@Transactional(readOnly = true)
public MenuDto getDetail(Long id) {
return menuRepository.findById(id)
.orElseThrow(()-> new ResponseStatusException(HttpStatus.NOT_FOUND, "해당 게시글이 없습니다."))
.orElseThrow(()-> new ResponseStatusException(HttpStatus.NOT_FOUND, "해당 메뉴가 없습니다."))
.fromEntity();
}

@Transactional
public Long update(Long id, MenuUpdateRequestDto requestDto) {
Menu targetMenu = menuRepository.findById(id).orElseThrow(()-> new ResponseStatusException(HttpStatus.NOT_FOUND, "해당 게시글이 없습니다."));
Menu targetMenu = menuRepository.findById(id).orElseThrow(()-> new ResponseStatusException(HttpStatus.NOT_FOUND, "해당 메뉴가 없습니다."));
targetMenu.update(requestDto);

return targetMenu.getId();
}

@Transactional
public Long delete(Long id) {
menuRepository.deleteById(id);
Menu menu = menuRepository.findById(id)
.orElseThrow(()-> new ResponseStatusException(HttpStatus.NOT_FOUND, "해당 메뉴가 없습니다."));

if (menu.getShowYn().equals("N")) {
throw new ResponseStatusException(HttpStatus.NOT_ACCEPTABLE, "이미 삭제된 메뉴입니다.");
}
if (menu.getShowYn().equals("Y")) {
menu.delete();
}

return id;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.HttpStatus;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.web.server.ResponseStatusException;

import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;

@ActiveProfiles("test")
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
Expand Down Expand Up @@ -46,15 +49,15 @@ public void createMenu() {
String icon = "doc";
String url = "https://www.naver.com";
Long orders = 3L;
String isActivated = "Y";
String showYn = "Y";

MenuSaveRequestDto requestDto = MenuSaveRequestDto.builder()
.uid(uid)
.name(name)
.icon(icon)
.url(url)
.orders(orders)
.isActivated(isActivated)
.showYn(showYn)
.build();

// when
Expand All @@ -69,7 +72,7 @@ public void createMenu() {
assertThat(all.get(0).getIcon()).isEqualTo(icon);
assertThat(all.get(0).getUrl()).isEqualTo(url);
assertThat(all.get(0).getOrders()).isEqualTo(orders);
assertThat(all.get(0).getIsActivated()).isEqualTo(isActivated);
assertThat(all.get(0).getShowYn()).isEqualTo(showYn);
}

@Test
Expand All @@ -82,15 +85,15 @@ public void getMenuList() {
.icon("test")
.url("/test")
.orders(1L)
.isActivated("Y")
.showYn("Y")
.build();
MenuSaveRequestDto requestDto2 = MenuSaveRequestDto.builder()
.uid("M001")
.name("테스트2")
.icon("test2")
.url("/test2")
.orders(2L)
.isActivated("N")
.showYn("N")
.build();

menuService.create(requestDto);
Expand All @@ -112,15 +115,15 @@ public void getOneMenu() {
String icon = "test";
String url = "/test";
Long orders = 1L;
String isActivated = "Y";
String showYn = "Y";

Menu savedMenu = menuRepository.save(Menu.builder()
.uid(uid)
.name(name)
.icon(icon)
.url(url)
.orders(orders)
.isActivated(isActivated)
.showYn(showYn)
.build());

// when
Expand All @@ -132,7 +135,7 @@ public void getOneMenu() {
assertThat(detail.getIcon()).isEqualTo(icon);
assertThat(detail.getUrl()).isEqualTo(url);
assertThat(detail.getOrders()).isEqualTo(orders);
assertThat(detail.getIsActivated()).isEqualTo(isActivated);
assertThat(detail.getShowYn()).isEqualTo(showYn);
}

@Test
Expand All @@ -145,19 +148,19 @@ public void updateMenu() {
.icon("test")
.url("/test")
.orders(1L)
.isActivated("Y")
.showYn("Y")
.build());

Long updateId = savedMenu.getId();
String newName = "test22";
String inactivate = "N";
String showYn = "N";

MenuUpdateRequestDto requestDto = MenuUpdateRequestDto.builder()
.name(newName)
.icon("test")
.url("/test")
.orders(1L)
.isActivated(inactivate)
.showYn(showYn)
.build();

// when
Expand All @@ -166,7 +169,7 @@ public void updateMenu() {
// then
List<Menu> all = menuRepository.findAll();
assertThat(all.get(0).getName()).isEqualTo(newName);
assertThat(all.get(0).getIsActivated()).isEqualTo(inactivate);
assertThat(all.get(0).getShowYn()).isEqualTo(showYn);
}

@Test
Expand All @@ -179,24 +182,26 @@ public void deleteMenu() {
.icon("test")
.url("/test")
.orders(1L)
.isActivated("Y")
.showYn("Y")
.build());
Menu savedMenu2 = menuRepository.save(Menu.builder()
.uid("M002")
.name("테스트2")
.icon("test2")
.url("/test2")
.orders(2L)
.isActivated("Y")
.showYn("N")
.build());

// when
menuService.delete(savedMenu1.getId());

// then
String updatedShowYn = savedMenu1.getShowYn();
List<Menu> all = menuRepository.findAll();
assertThat(all.size()).isEqualTo(1);
assertThat(all.get(0).getUid()).isEqualTo("M002");
assertThat(all.get(0).getShowYn()).isEqualTo("N");
ResponseStatusException exception = assertThrows(ResponseStatusException.class, () -> menuService.delete(savedMenu2.getId()));
assertThat(exception.getStatus()).isEqualTo(HttpStatus.NOT_ACCEPTABLE);
}

@Test
Expand All @@ -209,15 +214,15 @@ public void getMenuAndCatInOrder() {
.icon("icon")
.url("/url")
.orders(2L)
.isActivated("Y")
.showYn("Y")
.build();
MenuSaveRequestDto requestDto2 = MenuSaveRequestDto.builder()
.uid("M002")
.name("hello")
.icon("icon")
.url("/url")
.orders(1L)
.isActivated("Y")
.showYn("Y")
.build();
menuService.create(requestDto);
menuService.create(requestDto2);
Expand All @@ -230,7 +235,7 @@ public void getMenuAndCatInOrder() {
.icon("test")
.url("/test")
.orders(2L)
.isActivated("Y")
.showYn("Y")
.menu(menu)
.build();
CategorySaveRequestDto catRequestDto2 = CategorySaveRequestDto.builder()
Expand All @@ -239,7 +244,7 @@ public void getMenuAndCatInOrder() {
.icon("test2")
.url("/test2")
.orders(3L)
.isActivated("N")
.showYn("N")
.menu(menu)
.build();
CategorySaveRequestDto catRequestDto3 = CategorySaveRequestDto.builder()
Expand All @@ -248,7 +253,7 @@ public void getMenuAndCatInOrder() {
.icon("test2")
.url("/test2")
.orders(1L)
.isActivated("N")
.showYn("N")
.menu(menu)
.build();

Expand Down

0 comments on commit a5863bd

Please sign in to comment.