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
Expand Up @@ -81,7 +81,7 @@ public ResponseEntity getMyCafe(@PathVariable Long cafeId,
}

@PatchMapping("/{cafeId}")
public ResponseEntity updateCafe(@PathVariable long cafeId,
public ResponseEntity updateCafe(@PathVariable Long cafeId,
Copy link
Collaborator

Choose a reason for hiding this comment

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

wrapper type 으로 변경한 이유가 있을까요?

@RequestBody @Validated CafeDTO cafeDTO, BindingResult bindingResult,
HttpSession httpSession) {
Long userId = (Long) httpSession.getAttribute(SessionKeys.USER_ID);
Expand All @@ -94,4 +94,28 @@ public ResponseEntity updateCafe(@PathVariable long cafeId,
cafeService.updateCafe(cafeDTO);
return ResponseEntity.ok(cafeDTO);
}

@PatchMapping("/{cafeId}/open")
Copy link
Collaborator

Choose a reason for hiding this comment

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

restful 한 uri 를 설계한다고 했을때 open 이나 close 와 같은 동사를 uri 상에 표현하는것은 좋지 않습니다.
cafe 의 상태에 open, closed 를 만들고 cafe 를 업데이트 하는 형태의 uri 설계가 더 좋습니다.

public ResponseEntity openCafe (@PathVariable Long cafeId, HttpSession httpSession){
Long userId = (Long) httpSession.getAttribute(SessionKeys.USER_ID);
boolean openCafe = cafeService.openCafe(cafeId);
if (!openCafe) {
LOGGER.info("카페를 오픈할 수 없습니다. userId = {}, cafeId = {}", userId, 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.

앞의 PR의 comment 참고

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

@PatchMapping("/{cafeId}/close")
public ResponseEntity closeCafe (@PathVariable Long cafeId, HttpSession httpSession){
Long userId = (Long) httpSession.getAttribute(SessionKeys.USER_ID);
boolean closeCafe = cafeService.closeCafe(cafeId);
if (!closeCafe) {
LOGGER.info("카페를 마감할 수 없습니다. userId = {}, cafeId = {}", userId, cafeId);
return ResponseEntity.badRequest().build();
} else {
return ResponseEntity.ok().build();
}
}
}
1 change: 0 additions & 1 deletion src/main/java/com/flab/cafeguidebook/domain/Cafe.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.flab.cafeguidebook.enumeration.CafeCondition;
import com.flab.cafeguidebook.enumeration.CafeRegistration;
import java.time.LocalDateTime;
import org.checkerframework.checker.nullness.qual.NonNull;

public class Cafe {

Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/flab/cafeguidebook/mapper/CafeMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.flab.cafeguidebook.domain.Cafe;
import com.flab.cafeguidebook.dto.CafeDTO;
import com.flab.cafeguidebook.enumeration.CafeCondition;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import com.flab.cafeguidebook.enumeration.CafeRegistration;
Expand All @@ -25,4 +26,8 @@ public interface CafeMapper {
public int updateRegistration(@Param("id") Long id,
@Param("registration") CafeRegistration registration);

public int openCafe(@Param("cafeId") long cafeId, @Param("cafeCondition") CafeCondition cafeCondition);

public int closeCafe(@Param("cafeId") long cafeId, @Param("cafeCondition") CafeCondition cafeCondition);

}
11 changes: 11 additions & 0 deletions src/main/java/com/flab/cafeguidebook/service/CafeService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.flab.cafeguidebook.converter.CafeConverter.CafeDTOToCafeConverter;
import com.flab.cafeguidebook.dto.CafeDTO;
import com.flab.cafeguidebook.enumeration.CafeCondition;
import com.flab.cafeguidebook.enumeration.CafeRegistration;
import com.flab.cafeguidebook.mapper.CafeMapper;
import java.util.List;
Expand Down Expand Up @@ -42,4 +43,14 @@ public boolean updateCafe(CafeDTO cafeDTO) {
int updateCafe = cafeMapper.updateCafe(cafeDTOToCafeConverter.convert(cafeDTO));
return updateCafe == 1;
}

public boolean openCafe(Long cafeId) {
int openCafe = cafeMapper.openCafe(cafeId, CafeCondition.OPEN);
return openCafe == 1;
}

public boolean closeCafe(Long cafeId) {
int closeCafe = cafeMapper.closeCafe(cafeId, CafeCondition.CLOSE);
return closeCafe == 1;
}
}
25 changes: 16 additions & 9 deletions src/main/resources/mybatis/mapper/CafeMapper.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,28 +39,27 @@
#{withPet},
#{cafeCondition},
#{cafeRegistration})
<selectKey resultType="long" keyProperty="cafeId" order="AFTER">
SELECT LAST_INSERT_ID()
</selectKey>
</insert>

<select id="selectMyAllCafe" parameterType="long"
resultType="com.flab.cafeguidebook.dto.CafeDTO">
SELECT *
FROM CAFE
WHERE user_id = #{userId}
</select>

<select id="selectMyCafe" resultType="com.flab.cafeguidebook.dto.CafeDTO"
parameterType="map">
SELECT *
FROM CAFE
WHERE cafe_id = #{cafeId}
AND user_id = #{userId}
</select>

<delete id="deleteAllCafe">
DELETE
FROM CAFE;
</delete>

<update id="updateCafe" parameterType="com.flab.cafeguidebook.dto.CafeDTO">
UPDATE CAFE
SET
Expand All @@ -79,11 +78,19 @@
update_date = now()
WHERE cafe_id = #{cafeId}
</update>

<update id="openCafe">
UPDATE CAFE
SET cafe_condition = #{cafeCondition}
WHERE cafe_id = #{cafeId}
</update>
<update id="closeCafe">
UPDATE CAFE
SET cafe_condition = #{cafeCondition}
WHERE cafe_id = #{cafeId}
</update>
Comment on lines +81 to +90
Copy link
Collaborator

Choose a reason for hiding this comment

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

두개의 쿼리가 동일한것으로 보이는데 sql 은 하나로 하고 사용시 condition을 주는 방식으로 구현할수 있을것 같습니다.

<update id="updateRegistration">
UPDATE CAFE SET cafe_registration = #{cafeRegistration}
WHERE cafe_id = #{cafeId}
</update>

UPDATE CAFE SET cafe_registration = #{cafeRegistration}
WHERE cafe_id = #{cafeId}
</update>
</mapper>

3 changes: 3 additions & 0 deletions src/main/resources/mybatis/mapper/OptionMapper.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
#{menuId},
#{optionPrice},
#{optionStatus})
<selectKey resultType="long" keyProperty="optionId" order="AFTER">
SELECT LAST_INSERT_ID()
</selectKey>
</insert>
<update id="updateOption" parameterType="com.flab.cafeguidebook.dto.OptionDTO">
UPDATE MENUOPTION
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public void addCafe(CafeDTO testCafeDTO) throws Exception {

@Test
public void getMyAllCafe(List<CafeDTO> testCafeDTOList) throws Exception {

for (int i = 0; i < testCafeDTOList.size(); i++) {
addCafe(testCafeDTOList.get(i));
}
Expand All @@ -85,12 +86,10 @@ public void getMyAllCafe(List<CafeDTO> testCafeDTOList) throws Exception {
@Test
public void updateCafe(CafeDTO testCafeDTO) throws Exception {

final long CAFEID = 1;
testCafeDTO.setCafeId(CAFEID);
addCafe(testCafeDTO);

mockMvc.perform(patch("/owner/cafe/" + CAFEID)
.sessionAttr("userId", testCafeDTO.getUserId())
mockMvc.perform(patch("/owner/cafe/" + testCafeDTO.getCafeId())
.sessionAttr(SessionKeys.USER_ID, testCafeDTO.getUserId())
.contentType(MediaType.APPLICATION_JSON_UTF8)
.content(objectMapper.writeValueAsString(testCafeDTO))
.accept(MediaType.APPLICATION_JSON_UTF8))
Expand Down
13 changes: 13 additions & 0 deletions src/test/java/com/flab/cafeguidebook/service/CafeServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import com.flab.cafeguidebook.converter.CafeConverter.CafeDTOToCafeConverter;
import com.flab.cafeguidebook.domain.Cafe;
import com.flab.cafeguidebook.enumeration.CafeCondition;
import com.flab.cafeguidebook.fixture.CafeDTOListFixtureProvider;
import com.flab.cafeguidebook.dto.CafeDTO;
import com.flab.cafeguidebook.fixture.CafeDTOFixtureProvider;
Expand Down Expand Up @@ -80,4 +81,16 @@ public void updateCafe(CafeDTO testCafeDTO, Cafe testCafe) {
given(cafeMapper.updateCafe(testCafe)).willReturn(1);
assertThat(cafeService.updateCafe(testCafeDTO)).isEqualTo(true);
}

@Test
public void openCafe(CafeDTO testCafeDTO) {
given(cafeMapper.openCafe(testCafeDTO.getCafeId(), CafeCondition.OPEN)).willReturn(1);
assertThat(cafeService.openCafe(testCafeDTO.getCafeId())).isEqualTo(true);
}

@Test
public void closeCafe(CafeDTO testCafeDTO) {
given(cafeMapper.closeCafe(testCafeDTO.getCafeId(), CafeCondition.CLOSE)).willReturn(1);
assertThat(cafeService.closeCafe(testCafeDTO.getCafeId())).isEqualTo(true);
}
}