Skip to content
This repository has been archived by the owner on Aug 13, 2022. It is now read-only.

#66 식당 insert 기능 - 2차 리뷰 반영 #71

Merged
merged 9 commits into from
Mar 5, 2022
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class CheckLoginAspect {
private final HttpSession httpSession;

@Before("@annotation(com.flab.doorrush.domain.authentication.annotation.CheckLogin)")
public void checkLogin() throws AuthenticationCredentialsNotFoundException {
public void checkLogin(){
String currentId = (String) httpSession.getAttribute(AuthenticationService.LOGIN_SESSION);
if (!StringUtils.hasLength(currentId)) {
throw new AuthenticationCredentialsNotFoundException("로그인이 필요합니다.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ public ResponseEntity<BasicResponse<Void>> addRestaurant(@PathVariable Long owne
restaurantService.addRestaurant(addRestaurantRequest, ownerSeq);
return ResponseEntity.status(HttpStatus.OK).build();
}

}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,8 @@
@Mapper
public interface RestaurantMapper {

Restaurant selectRestaurantBySeq(long restaurantSeq);

int insertRestaurant(Restaurant restaurant);

Restaurant selectRestaurantByRestaurantSeq(Long restaurantSeq);

Long selectRestaurantSeq(Restaurant restaurant);
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ public class Restaurant {
private String introduction;
private Long minimumOrderAmount;
private Long addressSeq;

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.flab.doorrush.domain.restaurant.dto.request;

import com.flab.doorrush.domain.restaurant.domain.Restaurant;
import com.flab.doorrush.domain.restaurant.restaurantEnum.RestaurantCategory;
import com.flab.doorrush.domain.user.domain.YnStatus;
import javax.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
Expand All @@ -16,7 +17,7 @@ public class AddRestaurantRequest {
private RestaurantAddressRequest restaurantAddressRequest;

@NotNull
private String category;
private RestaurantCategory category;

@NotNull
private YnStatus openYn;
Expand All @@ -34,7 +35,7 @@ public class AddRestaurantRequest {
public Restaurant toEntity(Long addressSeq, Long ownerSeq) {
return Restaurant.builder()
.ownerSeq(ownerSeq)
.category(this.category)
.category(this.category.categoryValue)
.openYn(this.openYn)
.restaurantName(this.restaurantName)
.introduction(this.introduction)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

import com.flab.doorrush.domain.user.domain.Address;
import javax.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;

@Getter
@Builder
@AllArgsConstructor
public class RestaurantAddressRequest {

@NotNull
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.flab.doorrush.domain.restaurant.dto.response;

import com.flab.doorrush.domain.restaurant.domain.Restaurant;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;

@Getter
@Builder
@AllArgsConstructor
public class AddRestaurantResponse {

private Restaurant restaurant;

public static AddRestaurantResponse from(Restaurant restaurant) {

return AddRestaurantResponse.builder()
.restaurant(restaurant)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package com.flab.doorrush.domain.restaurant.restaurantEnum;


import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;

public enum RestaurantCategory {

WESTERN("양식"),
Expand All @@ -12,16 +9,12 @@ public enum RestaurantCategory {
SOUTHEAST_ASIAN("동남아"),
SNACK("분식");

public final String category;
public final String categoryValue;

RestaurantCategory(String category) {
this.category = category;
RestaurantCategory(String categoryValue) {
this.categoryValue = categoryValue;
}

@JsonCreator
public static RestaurantCategory from(@JsonProperty("category") String category) {
return valueOf(category);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.flab.doorrush.domain.restaurant.dao.RestaurantMapper;
import com.flab.doorrush.domain.restaurant.domain.Restaurant;
import com.flab.doorrush.domain.restaurant.dto.request.AddRestaurantRequest;
import com.flab.doorrush.domain.restaurant.dto.response.AddRestaurantResponse;
import com.flab.doorrush.domain.restaurant.exception.AddRestaurantException;
import com.flab.doorrush.domain.user.dao.UserAddressMapper;
import com.flab.doorrush.domain.user.domain.Address;
Expand All @@ -19,15 +20,19 @@ public class RestaurantService {


@Transactional
public void addRestaurant(AddRestaurantRequest addRestaurantRequest, Long ownerSeq) {
public AddRestaurantResponse addRestaurant(AddRestaurantRequest addRestaurantRequest,
Long ownerSeq) {
Address address = addRestaurantRequest.getRestaurantAddressRequest().toEntity();
try {
userAddressMapper.insertAddress(address);
Long addressSeq = userAddressMapper.selectAddressSeq(address);
Long addressSeq = address.getAddressSeq();
Restaurant restaurant = addRestaurantRequest.toEntity(addressSeq, ownerSeq);
restaurantMapper.insertRestaurant(restaurant);
return AddRestaurantResponse.from(restaurant);

} catch (Exception e) {
throw new AddRestaurantException(e, "식당 insert 처리 중 예외 발생");
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,4 @@ public interface UserAddressMapper {

Optional<Address> selectAddressBySeq(Long addressSeq);

Long selectAddressSeq(Address address);
}
10 changes: 0 additions & 10 deletions src/main/resources/mybatis/mapper/RestaurantMapper.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,4 @@
WHERE RESTAURANT_SEQ = #{restaurantSeq}
</select>

<select id="selectRestaurantSeq"
parameterType="com.flab.doorrush.domain.restaurant.domain.Restaurant"
resultType="Long">
SELECT RESTAURANT_SEQ
FROM RESTAURANT
WHERE OWNER_SEQ = #{ownerSeq}
AND CATEGORY = #{category}
AND RESTAURANT_NAME = #{restaurantName}
AND ADDRESS_SEQ = #{addressSeq};
</select>
</mapper>
10 changes: 0 additions & 10 deletions src/main/resources/mybatis/mapper/UserAddressMapper.xml
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,4 @@
WHERE ADDRESS_SEQ = #{addressSeq}
</select>

<select id="selectAddressSeq"
parameterType="com.flab.doorrush.domain.restaurant.dto.request.RestaurantAddressRequest"
resultType="Long">
SELECT ADDRESS_SEQ
FROM ADDRESS
WHERE POST = #{post}
AND SPOT_Y = #{spotY}
AND SPOT_X = #{spotX}
AND ADDRESS_DETAIL = #{addressDetail}
</select>
</mapper>
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class AuthenticationServiceTest {


@Test
@DisplayName("로그인 성공 테스트 login메소드 실행 후 세션의 loginId 속성 값을 아이디 값과 비교한다.")
@DisplayName("로그인 성공 테스트 login 메소드 실행 후 세션의 loginId 속성 값을 아이디 값과 비교한다.")
public void loginSuccessTest() {
// Given
MockHttpSession session = new MockHttpSession();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public void addRestaurantSuccessTest() throws Exception {

AddRestaurantRequest addRestaurantRequest = AddRestaurantRequest.builder()
.restaurantAddressRequest(restaurantAddressRequest)
.category(RestaurantCategory.CHINESE.category)
.category(RestaurantCategory.CHINESE)
.openYn(YnStatus.N)
.restaurantName("맛맛집")
.introduction("아주 맛있습니다")
Expand All @@ -60,7 +60,7 @@ public void addRestaurantSuccessTest() throws Exception {
}

@Test
@DisplayName("addRestaurant 실패 테스트 기존에 저장된 address 정보로 요청 시 AddRestaurantException 발생, HttpStatus.BAD_REQUEST 를 반환한다.")
@DisplayName("addRestaurant 실패 테스트 존재하지 않는 사장님 정보로 요청 시 AddRestaurantException 발생, HttpStatus.BAD_REQUEST 를 반환한다.")
public void addRestaurantFailTest() throws Exception {
// Given
RestaurantAddressRequest restaurantAddressRequest = RestaurantAddressRequest.builder()
Expand All @@ -71,7 +71,7 @@ public void addRestaurantFailTest() throws Exception {

AddRestaurantRequest addRestaurantRequest = AddRestaurantRequest.builder()
.restaurantAddressRequest(restaurantAddressRequest)
.category(RestaurantCategory.CHINESE.category)
.category(RestaurantCategory.CHINESE)
.openYn(YnStatus.N)
.restaurantName("맛맛집")
.introduction("아주 맛있습니다")
Expand All @@ -80,7 +80,7 @@ public void addRestaurantFailTest() throws Exception {
String content = objectMapper.writeValueAsString(addRestaurantRequest);

// When
mockMvc.perform(post("/restaurants/1/addRestaurant").content(content)
mockMvc.perform(post("/restaurants/0/addRestaurant").content(content)
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON))
.andDo(print())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static org.junit.jupiter.api.Assertions.*;

import com.flab.doorrush.domain.restaurant.domain.Restaurant;
import com.flab.doorrush.domain.restaurant.restaurantEnum.RestaurantCategory;
import com.flab.doorrush.domain.user.domain.YnStatus;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
Expand All @@ -23,7 +24,7 @@ public void insertRestaurantSuccessTest() {
// Given
Restaurant restaurant = Restaurant.builder()
.ownerSeq(5L)
.category("중식")
.category(RestaurantCategory.CHINESE.categoryValue)
.openYn(YnStatus.Y)
.restaurantName("냠냠식당")
.introduction("맛좋습니다.")
Expand Down Expand Up @@ -53,23 +54,4 @@ public void selectRestaurantByRestaurantSeqTest() {
assertEquals(5, restaurant.getAddressSeq());
}


@Test
@DisplayName("selectRestaurantSeq 성공 테스트 select 결과 restaurantSeq 값 반환 한다.")
public void selectRestaurantSeqTest() {
// Given
Restaurant restaurant = Restaurant.builder()
.ownerSeq(1L)
.category("중식")
.openYn(YnStatus.Y)
.restaurantName("중식중 최고집")
.introduction("증식집 중 최고를 자랑합니다.")
.minimumOrderAmount(12000L)
.addressSeq(5L)
.build();

// Then When
Long restaurantSeq = restaurantMapper.selectRestaurantSeq(restaurant);
assertEquals(1, restaurantSeq);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ class RestaurantCategoryTest {
@Test
public void confirmRestaurantCategoryTest() {

assertEquals("양식", RestaurantCategory.WESTERN.category);
assertEquals("중식", RestaurantCategory.CHINESE.category);
assertEquals("일식", RestaurantCategory.JAPANESE.category);
assertEquals("동남아", RestaurantCategory.SOUTHEAST_ASIAN.category);
assertEquals("분식", RestaurantCategory.SNACK.category);
assertEquals("양식", RestaurantCategory.WESTERN.categoryValue);
assertEquals("중식", RestaurantCategory.CHINESE.categoryValue);
assertEquals("일식", RestaurantCategory.JAPANESE.categoryValue);
assertEquals("동남아", RestaurantCategory.SOUTHEAST_ASIAN.categoryValue);
assertEquals("분식", RestaurantCategory.SNACK.categoryValue);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertEquals;

import com.flab.doorrush.domain.restaurant.dao.RestaurantMapper;
import com.flab.doorrush.domain.restaurant.domain.Restaurant;
import com.flab.doorrush.domain.restaurant.dto.request.AddRestaurantRequest;
import com.flab.doorrush.domain.restaurant.dto.request.RestaurantAddressRequest;
import com.flab.doorrush.domain.restaurant.dto.response.AddRestaurantResponse;
import com.flab.doorrush.domain.restaurant.exception.AddRestaurantException;
import com.flab.doorrush.domain.restaurant.restaurantEnum.RestaurantCategory;
import com.flab.doorrush.domain.user.dao.UserAddressMapper;
import com.flab.doorrush.domain.user.domain.YnStatus;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
Expand All @@ -25,13 +23,8 @@ class RestaurantServiceTest {
@Autowired
private RestaurantService restaurantService;

@Autowired
private RestaurantMapper restaurantMapper;

@Autowired
private UserAddressMapper userAddressMapper;

@Test
@DisplayName("addRestaurant 성공 테스트 식당 정보 insert 결과 확인")
public void addRestaurantSuccessTest() {
// Given
RestaurantAddressRequest restaurantAddressRequest = RestaurantAddressRequest.builder()
Expand All @@ -42,33 +35,29 @@ public void addRestaurantSuccessTest() {

AddRestaurantRequest addRestaurantRequest = AddRestaurantRequest.builder()
.restaurantAddressRequest(restaurantAddressRequest)
.category(RestaurantCategory.CHINESE.category)
.category(RestaurantCategory.CHINESE)
.openYn(YnStatus.N)
.restaurantName("맛맛집")
.introduction("아주 맛있습니다")
.minimumOrderAmount(0L).build();

Long ownerSeq = 1L;
// When
restaurantService.addRestaurant(addRestaurantRequest, ownerSeq);
AddRestaurantResponse restaurantResponse = restaurantService.addRestaurant(addRestaurantRequest,
ownerSeq);

// Then
Long addressSeq = userAddressMapper.selectAddressSeq(restaurantAddressRequest.toEntity());
Long restaurantSeq = restaurantMapper.selectRestaurantSeq(
addRestaurantRequest.toEntity(addressSeq, ownerSeq));
Restaurant restaurant = restaurantMapper.selectRestaurantByRestaurantSeq(restaurantSeq);
assertEquals(restaurantSeq, restaurant.getRestaurantSeq());
assertEquals(1L, restaurant.getOwnerSeq());
assertEquals(RestaurantCategory.CHINESE.category, restaurant.getCategory());
assertEquals(YnStatus.N, restaurant.getOpenYn());
assertEquals("맛맛집", restaurant.getRestaurantName());
assertEquals("아주 맛있습니다", restaurant.getIntroduction());
assertEquals(0L, restaurant.getMinimumOrderAmount());
assertEquals(addressSeq, restaurant.getAddressSeq());
assertEquals(1L, restaurantResponse.getRestaurant().getOwnerSeq());
assertEquals(RestaurantCategory.CHINESE.categoryValue,
restaurantResponse.getRestaurant().getCategory());
assertEquals(YnStatus.N, restaurantResponse.getRestaurant().getOpenYn());
assertEquals("맛맛집", restaurantResponse.getRestaurant().getRestaurantName());
assertEquals("아주 맛있습니다", restaurantResponse.getRestaurant().getIntroduction());

}

@Test
@DisplayName("기존에 저장된 address 정보로 식당 insert 시 AddRestaurantException 발생")
@DisplayName("addRestaurant 실패 테스트 존재하지 않는 사장님 정보로 식당 insert 시 AddRestaurantException 발생")
public void addRestaurantFailTest() {
// Given
RestaurantAddressRequest restaurantAddressRequest = RestaurantAddressRequest.builder()
Expand All @@ -79,12 +68,12 @@ public void addRestaurantFailTest() {

AddRestaurantRequest addRestaurantRequest = AddRestaurantRequest.builder()
.restaurantAddressRequest(restaurantAddressRequest)
.category(RestaurantCategory.CHINESE.category)
.category(RestaurantCategory.CHINESE)
.openYn(YnStatus.N)
.restaurantName("맛맛집")
.introduction("아주 맛있습니다")
.minimumOrderAmount(0L).build();
Long ownerSeq = 1L;
Long ownerSeq = 0L;

// Then
assertThrows(AddRestaurantException.class, () ->
Expand Down
Loading