Skip to content
This repository was archived by the owner on Aug 13, 2022. It is now read-only.
Open
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
@@ -1,16 +1,20 @@
package com.flab.cafeguidebook.controller;

import com.flab.cafeguidebook.domain.Option;
import com.flab.cafeguidebook.dto.MenuDTO;
import com.flab.cafeguidebook.dto.OptionDTO;
import com.flab.cafeguidebook.service.MenuService;
import com.flab.cafeguidebook.service.OptionService;
import java.util.List;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
Expand Down Expand Up @@ -91,4 +95,69 @@ public ResponseEntity updateOption(@PathVariable long menuId, @PathVariable long
return ResponseEntity.ok(optionDTO);
}

@GetMapping(consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity getAllMenu(@PathVariable long cafeId) {
List<MenuDTO> AllMenu = menuService.getAllMenu(cafeId);
if (AllMenu.size() > 0) {
return ResponseEntity.ok().body(AllMenu);
} else {
LOGGER.info("해당 카페의 전체 메뉴를 조회할 수 없습니다. cafeId = {}", cafeId);
return ResponseEntity.badRequest().build();
Copy link
Collaborator

Choose a reason for hiding this comment

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

실제로 메뉴가 없는경우도 있을수 있는데 400에러를 내리는건 안될것 같습니다.

}
}

@GetMapping(value = "/{menuId}/options", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity getAllOption(@PathVariable long cafeId, @PathVariable long menuId) {
List<OptionDTO> AllOption = optionService.getAllOption(menuId);
if (AllOption.size() > 0) {
return ResponseEntity.ok().body(AllOption);
} else {
LOGGER.info("해당 메뉴의 전체 옵션을 조회할 수 없습니다. cafeId = {}, menuId = {}", cafeId, menuId);
return ResponseEntity.badRequest().build();
Copy link
Collaborator

Choose a reason for hiding this comment

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

여기도 위와 동일

}
}

@GetMapping(value = "/{menuId}", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity getMenu(@PathVariable long cafeId, @PathVariable long menuId) {
MenuDTO menuDTO = menuService.getMenu(menuId);
if (menuDTO == null) {
LOGGER.info("해당 메뉴를 조회할 수 없습니다. cafeId = {}, menuId = {}", cafeId, menuId);
return ResponseEntity.badRequest().build();
Copy link
Collaborator

Choose a reason for hiding this comment

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

역시 이곳에서도 메뉴가 없다고 해서 400 에러를 내면 안될것 같습니다.
이런경우는 리소스를 찾을수 없는 404에러가 적합합니다.

} else {
return ResponseEntity.ok(menuDTO);
}
}

@GetMapping(value = "/{menuId}/options/{optionId}", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity getOption(@PathVariable long menuId, @PathVariable long optionId) {
OptionDTO optionDTO = optionService.getOption(optionId);
if (optionDTO == null) {
LOGGER.info("해당 옵션을 조회할 수 없습니다. menuId = {}, optionId = {}", menuId, optionId);
return ResponseEntity.badRequest().build();
} else {
return ResponseEntity.ok(optionDTO);
}
}

@DeleteMapping(value = "/{menuId}", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity deleteMenu(@PathVariable long cafeId, @PathVariable long menuId) {
boolean deleteMenu = menuService.deleteMenu(menuId);
if (!deleteMenu) {
LOGGER.info("해당 메뉴를 삭제할 수 없습니다. cafeId = {}, menuId = {}", cafeId, menuId);
return ResponseEntity.badRequest().build();
} else {
return ResponseEntity.ok().build();
}
}

@DeleteMapping(value = "/{menuId}/options/{optionId}", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity deleteOption(@PathVariable long menuId, @PathVariable long optionId) {
boolean deleteOption = optionService.deleteOption(optionId);
if (!deleteOption) {
LOGGER.info("해당 옵션을 삭제할 수 없습니다. menuId = {}, optionId = {}", menuId, optionId);
return ResponseEntity.badRequest().build();
} else {
return ResponseEntity.ok().build();
}
}
}
10 changes: 10 additions & 0 deletions src/main/java/com/flab/cafeguidebook/mapper/MenuMapper.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.flab.cafeguidebook.mapper;

import com.flab.cafeguidebook.domain.Menu;
import com.flab.cafeguidebook.dto.MenuDTO;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;

@Mapper
Expand All @@ -9,4 +11,12 @@ public interface MenuMapper {
public int insertMenu(Menu menu);

public int updateMenu(Menu menu);

public List<MenuDTO> selectAllMenu(long cafeId);

public MenuDTO selectMenu(long menuId);

public int deleteMenu(long menuId);

public void deleteAllMenu();
}
10 changes: 10 additions & 0 deletions src/main/java/com/flab/cafeguidebook/mapper/OptionMapper.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.flab.cafeguidebook.mapper;

import com.flab.cafeguidebook.domain.Option;
import com.flab.cafeguidebook.dto.OptionDTO;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;

@Mapper
Expand All @@ -9,4 +11,12 @@ public interface OptionMapper {
public int insertOption(Option optionDTO);

public int updateOption(Option optionDTO);

public List<OptionDTO> selectAllOption(long menuId);

public OptionDTO selectOption(long optionId);

public int deleteOption(long optionId);

public void deleteAllOption();
}
25 changes: 25 additions & 0 deletions src/main/java/com/flab/cafeguidebook/service/MenuService.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
package com.flab.cafeguidebook.service;

import com.flab.cafeguidebook.converter.MenuConverter.MenuDTOToMenuConverter;
import com.flab.cafeguidebook.converter.MenuConverter.MenuToMenuDTOConverter;
import com.flab.cafeguidebook.domain.Menu;
import com.flab.cafeguidebook.dto.MenuDTO;
import com.flab.cafeguidebook.mapper.MenuMapper;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

Expand All @@ -12,6 +19,9 @@ public class MenuService {
@Autowired
private MenuDTOToMenuConverter menuDTOToMenuConverter;

@Autowired
MenuToMenuDTOConverter menuToMenuDTOConverter;

@Autowired
private MenuMapper menuMapper;

Expand All @@ -24,4 +34,19 @@ public boolean updateMenu(MenuDTO menuDTO) {
int updateMenu = menuMapper.updateMenu(menuDTOToMenuConverter.convert(menuDTO));
return updateMenu == 1;
}

public List<MenuDTO> getAllMenu(long cafeId) {
List<MenuDTO> selectAllMenu = menuMapper.selectAllMenu(cafeId);
Copy link
Collaborator

Choose a reason for hiding this comment

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

cafeId 의 유효성 검증이 필요해보입니다.
(존재하는 카페인지 등의)

return selectAllMenu;
}

public MenuDTO getMenu(long menuId) {
MenuDTO selectMenu = menuMapper.selectMenu(menuId);
return selectMenu;
}

public boolean deleteMenu(long menuId) {
int deleteMenu = menuMapper.deleteMenu(menuId);
Copy link
Collaborator

Choose a reason for hiding this comment

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

해당 메뉴가 존재하는지 확인후 없다면 404를 내리고 있는데 삭제를 실패한 경우면 500에러를 내리는게 더 좋을것 같습니다.

return deleteMenu == 1;
}
}
16 changes: 16 additions & 0 deletions src/main/java/com/flab/cafeguidebook/service/OptionService.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.flab.cafeguidebook.converter.OptionConverter.OptionDTOToOptionConverter;
import com.flab.cafeguidebook.dto.OptionDTO;
import com.flab.cafeguidebook.mapper.OptionMapper;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

Expand All @@ -24,4 +25,19 @@ public boolean updateOption(OptionDTO optionDTO) {
int updateOption = optionMapper.updateOption(optionDTOToOptionConverter.convert(optionDTO));
return updateOption == 1;
}

public List<OptionDTO> getAllOption(long menuId) {
List<OptionDTO> selectAllOption = optionMapper.selectAllOption(menuId);
Copy link
Collaborator

Choose a reason for hiding this comment

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

menuId에 대한 검증부터 하는것이 좋을것 같습니다.

return selectAllOption;
}

public OptionDTO getOption(long optionId) {
OptionDTO selectOption = optionMapper.selectOption(optionId);
return selectOption;
}

public boolean deleteOption(long optionId) {
int deleteOption = optionMapper.deleteOption(optionId);
return deleteOption == 1;
}
}
19 changes: 19 additions & 0 deletions src/main/resources/mybatis/mapper/MenuMapper.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,23 @@
update_menu_date = now()
WHERE menu_id = #{menuId}
</update>
<select id="selectAllMenu" parameterType="long" resultType="com.flab.cafeguidebook.dto.MenuDTO">
SELECT *
FROM MENU
WHERE cafe_id = #{cafeId}
</select>
<select id="selectMenu" parameterType="long" resultType="com.flab.cafeguidebook.dto.MenuDTO">
SELECT *
FROM MENU
WHERE menu_id = #{menuId}
</select>
<delete id="deleteMenu" parameterType="long">
DELETE
FROM MENU
WHERE menu_id = #{menuId}
</delete>
<delete id="deleteAllMenu">
DELETE
FROM MENU;
</delete>
</mapper>
19 changes: 19 additions & 0 deletions src/main/resources/mybatis/mapper/OptionMapper.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,23 @@
<if test="optionStatus != null">option_status = #{optionStatus}</if>
WHERE option_id = #{optionId}
</update>
<select id="selectAllOption" parameterType="long" resultType="com.flab.cafeguidebook.dto.OptionDTO">
SELECT *
FROM MENUOPTION
WHERE menu_id = #{menuId}
</select>
<select id="selectOption" parameterType="long" resultType="com.flab.cafeguidebook.dto.OptionDTO">
SELECT *
FROM MENUOPTION
WHERE option_id = #{optionId}
</select>
<delete id="deleteOption" parameterType="long">
DELETE
FROM MENUOPTION
WHERE option_id = #{optionId}
</delete>
<delete id="deleteAllOption" parameterType="long">
DELETE
FROM MENUOPTION;
</delete>
</mapper>
Loading