Skip to content
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 @@ -4,7 +4,10 @@

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import in.koreatech.koin.domain.ownershop.dto.OwnerShopsRequest;
import in.koreatech.koin.domain.ownershop.dto.OwnerShopsResponse;
import in.koreatech.koin.global.auth.Auth;
import io.swagger.v3.oas.annotations.Operation;
Expand Down Expand Up @@ -33,4 +36,19 @@ ResponseEntity<OwnerShopsResponse> getOwnerShops(
@Auth(permit = {OWNER}) Long userId
);

@ApiResponses(
value = {
@ApiResponse(responseCode = "201"),
@ApiResponse(responseCode = "401", content = @Content(schema = @Schema(hidden = true))),
@ApiResponse(responseCode = "403", content = @Content(schema = @Schema(hidden = true))),
@ApiResponse(responseCode = "404", content = @Content(schema = @Schema(hidden = true))),
}
)
@Operation(summary = "상점 생성")
@SecurityRequirement(name = "Jwt Authentication")
@PostMapping("/owner/shops")
ResponseEntity<Void> createOwnerShops(
@Auth(permit = {OWNER}) Long userId,
@RequestBody OwnerShopsRequest ownerShopsRequest
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

import static in.koreatech.koin.domain.user.model.UserType.OWNER;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;

import in.koreatech.koin.domain.ownershop.dto.OwnerShopsRequest;
import in.koreatech.koin.domain.ownershop.dto.OwnerShopsResponse;
import in.koreatech.koin.domain.ownershop.service.OwnerShopService;
import in.koreatech.koin.global.auth.Auth;
Expand All @@ -23,4 +26,13 @@ public ResponseEntity<OwnerShopsResponse> getOwnerShops(
OwnerShopsResponse ownerShopsResponses = ownerShopService.getOwnerShops(ownerId);
return ResponseEntity.ok().body(ownerShopsResponses);
}

@Override
public ResponseEntity<Void> createOwnerShops(
@Auth(permit = {OWNER}) Long ownerId,
@RequestBody OwnerShopsRequest ownerShopsRequest
) {
ownerShopService.createOwnerShops(ownerId, ownerShopsRequest);
return ResponseEntity.status(HttpStatus.CREATED).build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package in.koreatech.koin.domain.ownershop.dto;

import java.time.LocalTime;
import java.util.List;

import in.koreatech.koin.domain.owner.model.Owner;
import in.koreatech.koin.domain.shop.model.Shop;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;

public record OwnerShopsRequest(
@Schema(description = "주소", example = "충청남도 천안시 동남구 병천면 충절로 1600")
@NotBlank(message = "주소를 입력해주세요.")
String address,

@Schema(description = "상점 카테고리 고유 id 리스트", example = "[1]")
List<Long> categoryIds,

@Schema(description = "배달 가능 여부", example = "false")
Boolean delivery,

@Schema(description = "배달 금액", example = "1000")
Long deliveryPrice,

@Schema(description = "기타정보", example = "이번주 전 메뉴 10% 할인 이벤트합니다.")
String description,

@Schema(description = "이미지 URL 리스트", example = "[\"string\"]")
List<String> imageUrls,

@Schema(description = "가게명", example = "써니 숯불 도시락")
@NotBlank(message = "상점 이름을 입력해주세요.")
String name,

@Schema(description = "요일별 운영 시간과 휴무 여부")
List<InnerOpenRequest> open,

@Schema(description = "계좌 이체 가능 여부", example = "true")
Boolean payBank,

@Schema(description = "카드 가능 여부", example = "true")
Boolean payCard,

@Schema(description = "전화번호", example = "041-123-4567")
@NotBlank(message = "전화번호를 입력해주세요.")
String phone
) {

public Shop toEntity(Owner owner) {
return Shop.builder()
.owner(owner)
.address(address)
.deliveryPrice(deliveryPrice)
.delivery(delivery)
.description(description)
.payBank(payBank)
.payCard(payCard)
.phone(phone)
.name(name)
.internalName(name)
.chosung(name().substring(0, 1))
.isDeleted(false)
.isEvent(false)
.remarks("")
.hit(0L)
.build();
}

public record InnerOpenRequest(
@Schema(description = "닫는 시간", example = "22:30")
LocalTime closeTime,

@Schema(description = "휴무 여부", example = "false")
Boolean closed,

@Schema(description = "요일", example = "MONDAY")
@NotBlank(message = "영업 요일을 입력해주세요.")
String dayOfWeek,

@Schema(description = "여는 시간", example = "10:00")
LocalTime openTime
) {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,19 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import in.koreatech.koin.domain.owner.model.Owner;
import in.koreatech.koin.domain.owner.repository.OwnerRepository;
import in.koreatech.koin.domain.ownershop.dto.OwnerShopsRequest;
import in.koreatech.koin.domain.ownershop.dto.OwnerShopsResponse;
import in.koreatech.koin.domain.shop.model.Shop;
import in.koreatech.koin.domain.shop.model.ShopCategory;
import in.koreatech.koin.domain.shop.model.ShopCategoryMap;
import in.koreatech.koin.domain.shop.model.ShopImage;
import in.koreatech.koin.domain.shop.model.ShopOpen;
import in.koreatech.koin.domain.shop.repository.ShopCategoryMapRepository;
import in.koreatech.koin.domain.shop.repository.ShopCategoryRepository;
import in.koreatech.koin.domain.shop.repository.ShopImageRepository;
import in.koreatech.koin.domain.shop.repository.ShopOpenRepository;
import in.koreatech.koin.domain.shop.repository.ShopRepository;
import lombok.RequiredArgsConstructor;

Expand All @@ -16,9 +27,46 @@
public class OwnerShopService {

private final ShopRepository shopRepository;
private final OwnerRepository ownerRepository;
private final ShopOpenRepository shopOpenRepository;
private final ShopCategoryMapRepository shopCategoryMapRepository;
private final ShopCategoryRepository shopCategoryRepository;
private final ShopImageRepository shopImageRepository;
Comment on lines +30 to +34
Copy link
Collaborator

Choose a reason for hiding this comment

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

A

뭐가 많네요.. ㅇㅁㅇ;;


public OwnerShopsResponse getOwnerShops(Long ownerId) {
List<Shop> shops = shopRepository.findAllByOwnerId(ownerId);
return OwnerShopsResponse.from(shops);
}

@Transactional
public void createOwnerShops(Long ownerId, OwnerShopsRequest ownerShopsRequest) {
Owner owner = ownerRepository.getById(ownerId);
Shop newShop = ownerShopsRequest.toEntity(owner);
shopRepository.save(newShop);
for (String imageUrl : ownerShopsRequest.imageUrls()) {
ShopImage shopImage = ShopImage.builder()
.shop(newShop)
.imageUrl(imageUrl)
.build();
shopImageRepository.save(shopImage);
}
Comment on lines +46 to +52
Copy link
Collaborator

Choose a reason for hiding this comment

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

R

Suggested change
for (String imageUrl : ownerShopsRequest.imageUrls()) {
ShopImage shopImage = ShopImage.builder()
.shop(newShop)
.imageUrl(imageUrl)
.build();
shopImageRepository.save(shopImage);
}
ownerShopsRequest.imageUrls().stream()
.map(imageUrl -> ShopImage.builder()
.shop(newShop)
.imageUrl(imageUrl)
.build())
.forEach(shopImageRepository::save);

코드를 조금 손보면 이런 식으로도 쓸 수 있지 않을까요?
for문은 최대한 지양하는 방향으로 코딩해보시는 걸 추천드립니다

Copy link
Member

Choose a reason for hiding this comment

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

이거는 stream 내부에서 예외 발생하면 안멈추고 계속 돌아버려서
이런 경우에는 for문이 더 나은 선택입니다 👍

for (OwnerShopsRequest.InnerOpenRequest open : ownerShopsRequest.open()) {
ShopOpen shopOpen = ShopOpen.builder()
.shop(newShop)
.openTime(open.openTime())
.closeTime(open.closeTime())
.dayOfWeek(open.dayOfWeek())
.closed(open.closed())
.build();
shopOpenRepository.save(shopOpen);
}
List<ShopCategory> shopCategories = shopCategoryRepository.findAllByIdIn(ownerShopsRequest.categoryIds());
for (ShopCategory shopCategory : shopCategories) {
ShopCategoryMap shopCategoryMap = ShopCategoryMap.builder()
.shopCategory(shopCategory)
.shop(newShop)
.build();
shopCategoryMapRepository.save(shopCategoryMap);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@

import in.koreatech.koin.domain.shop.dto.MenuCategoriesResponse;
import in.koreatech.koin.domain.shop.dto.ShopMenuResponse;
import in.koreatech.koin.domain.shop.dto.ShopResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;

import static io.swagger.v3.oas.annotations.enums.ParameterIn.PATH;

import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
Expand Down Expand Up @@ -39,7 +42,21 @@ ResponseEntity<ShopMenuResponse> findMenu(
)
@Operation(summary = "메뉴 카테고리 목록 조회")
@GetMapping("/shops/{shopId}/menus/categories")
ResponseEntity<MenuCategoriesResponse> findMenuCategories(
ResponseEntity<MenuCategoriesResponse> getMenuCategories(
@Parameter(in = PATH) @PathVariable Long shopId
);

@ApiResponses(
value = {
@ApiResponse(responseCode = "200"),
@ApiResponse(responseCode = "401", content = @Content(schema = @Schema(hidden = true))),
@ApiResponse(responseCode = "403", content = @Content(schema = @Schema(hidden = true))),
@ApiResponse(responseCode = "404", content = @Content(schema = @Schema(hidden = true))),
}
)
@Operation(summary = "특정 상점 조회")
@GetMapping("/shops/{id}")
ResponseEntity<ShopResponse> getShopById(
@Parameter(in = PATH) @PathVariable Long id
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import in.koreatech.koin.domain.shop.dto.MenuCategoriesResponse;
import in.koreatech.koin.domain.shop.dto.ShopMenuResponse;
import in.koreatech.koin.domain.shop.dto.ShopResponse;
import in.koreatech.koin.domain.shop.service.ShopService;
import lombok.RequiredArgsConstructor;

Expand All @@ -26,10 +27,18 @@ public ResponseEntity<ShopMenuResponse> findMenu(
}

@GetMapping("/shops/{shopId}/menus/categories")
public ResponseEntity<MenuCategoriesResponse> findMenuCategories(
public ResponseEntity<MenuCategoriesResponse> getMenuCategories(
@PathVariable Long shopId
) {
MenuCategoriesResponse menuCategories = shopService.getMenuCategories(shopId);
return ResponseEntity.ok(menuCategories);
}

@GetMapping("/shops/{id}")
public ResponseEntity<ShopResponse> getShopById(
@PathVariable Long id
) {
ShopResponse shopResponse = shopService.getShop(id);
return ResponseEntity.ok(shopResponse);
}
}
Loading