Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2차 배포 #33

Merged
merged 9 commits into from
Nov 23, 2024
11 changes: 10 additions & 1 deletion src/main/java/com/univ/sohwakhaeng/auth/api/dto/TokenDto.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
package com.univ.sohwakhaeng.auth.api.dto;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Builder;

@Builder
public record TokenDto(String grantType, String accessToken) {}
@Schema(description = "토큰 DTO")
public record TokenDto(

@Schema(description = "토큰의 인증 타입", example = "Bearer")
String grantType,

@Schema(description = "인증을 위한 액세스 토큰", example = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...")
String accessToken
) {}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,30 @@

import com.fasterxml.jackson.annotation.JsonProperty;
import com.univ.sohwakhaeng.item.api.dto.ItemRequestDto;
import io.swagger.v3.oas.annotations.media.Schema;

import java.util.List;

@Schema(description = "장바구니 요청 DTO")
public record CartRequestDto(

@Schema(description = "기업의 ID", example = "1")
@JsonProperty("enterpriseId")
Long enterpriseId,

@Schema(description = "장바구니에 담긴 상품 목록", example = """
[
{
"productId": 1,
"quantity": 3
},
{
"productId": 2,
"quantity": 2
}
]
""")
@JsonProperty("items")
List<ItemRequestDto> products
) {

}

Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,56 @@
import com.univ.sohwakhaeng.cart.Cart;
import com.univ.sohwakhaeng.enterprise.api.dto.EnterpriseOverviewDto;
import com.univ.sohwakhaeng.item.api.dto.ItemRequestDto;
import com.univ.sohwakhaeng.item.api.dto.ItemResponseDto;
import com.univ.sohwakhaeng.product.Product;
import java.util.List;
import java.util.function.Function;

import io.swagger.v3.oas.annotations.media.Schema;

@Schema(description = "장바구니 응답 DTO")
public record CartResponseDto(

@Schema(description = "기업 개요 정보", example = """
{
"enterprise_id": 1,
"enterprise_image_url": "https://example.com/enterprise-image.jpg",
"enterprise_name": "초록믿음강진",
"category": "AGRICULTURAL_PRODUCTS"
}
""")
@JsonProperty("enterprise")
EnterpriseOverviewDto enterpriseOverviewDto,

@Schema(description = "장바구니에 담긴 상품 목록", example = """
[
{
"product_id": 1,
"product_name": "유기농 쌀",
"unit": "1kg",
"imageUrl": "https://example.com/product-image.jpg",
"quantity": 3
},
{
"product_id": 2,
"product_name": "감자",
"unit": "1kg",
"imageUrl": "https://example.com/product-image2.jpg",
"quantity": 2
}
]
""")
@JsonProperty("items")
List<ItemRequestDto> itemRequestDtoList
List<ItemResponseDto> itemRequestDtoList
) {
public static CartResponseDto fromEntity(Cart cart) {
public static CartResponseDto fromEntity(Cart cart, String enterpriseImageUrl, Function<Product, String> getProductImageUrlFunction) {
return new CartResponseDto(
EnterpriseOverviewDto.fromEntity(cart.getEnterprise()),
EnterpriseOverviewDto.fromEntity(cart.getEnterprise(), enterpriseImageUrl),
cart.getItems().stream()
.map(ItemRequestDto::fromEntity)
.map(item -> {
String productImageUrl = getProductImageUrlFunction.apply(item.getProduct());
return ItemResponseDto.fromEntity(item, productImageUrl);
})
.toList()
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@
import com.univ.sohwakhaeng.item.Item;
import com.univ.sohwakhaeng.item.api.dto.ItemRequestDto;
import com.univ.sohwakhaeng.item.service.ItemService;
import com.univ.sohwakhaeng.product.Product;
import com.univ.sohwakhaeng.product.service.ProductService;
import com.univ.sohwakhaeng.user.User;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
Expand All @@ -24,6 +27,7 @@ public class CartService {
private final CartRepository cartRepository;
private final EnterpriseService enterpriseService;
private final ItemService itemService;
private final ProductService productService;

@Transactional
public Long saveCart(CartRequestDto requestDto, User user) throws EnterpriseNotFoundException {
Expand All @@ -40,7 +44,11 @@ public Long saveCart(CartRequestDto requestDto, User user) throws EnterpriseNotF
public List<CartResponseDto> getMyCarts(User user) {
List<Cart> carts = getAllCartByUserId(user.getId());
return carts.stream()
.map(CartResponseDto::fromEntity)
.map(cart -> {
String enterpriseImageUrl = enterpriseService.getEnterpriseImageUrl(cart.getEnterprise().getName());
Function<Product, String> getProductImageUrlFunction = product -> productService.getProductImageUrl(product.getName());
return CartResponseDto.fromEntity(cart, enterpriseImageUrl, getProductImageUrlFunction);
})
.collect(Collectors.toList());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,52 @@

import java.util.List;

import io.swagger.v3.oas.annotations.media.Schema;

@Schema(description = "계약 상세 정보 DTO")
@Builder
public record ContractDetailDto(

@Schema(description = "기업 ID", example = "1")
@JsonProperty
long enterpriseId,

@Schema(description = "기업 프로필 이미지 URL", example = "https://example.com/profile-image.jpg")
@JsonProperty
String profileImgUrl,

@Schema(description = "기업 이름", example = "초록믿음강진")
@JsonProperty
String enterpriseName,

@Schema(description = "기업 카테고리", example = "AGRICULTURAL_PRODUCTS")
@JsonProperty
String category,

@Schema(description = "정기 배송 정보", example = "주간 배송 가능")
@JsonProperty
String reqularDelivery,
String regularDelivery,

@Schema(description = "계약 요청 기간", example = "3개월")
@JsonProperty
String requestTerm,

@Schema(description = "계약 제품 목록", example = """
[
{
"productId": 1,
"productName": "유기농 쌀",
"unit": "1kg",
"price": 5000
},
{
"productId": 2,
"productName": "감자",
"unit": "2kg",
"price": 4000
}
]
""")
@JsonProperty
List<ContractProducsResDto> products
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Builder;
import io.swagger.v3.oas.annotations.media.Schema;

@Builder
@Schema(description = "계약 상품 응답 DTO")
public record ContractProducsResDto(

@Schema(description = "상품 이름", example = "유기농 쌀")
@JsonProperty
String name,

@Schema(description = "상품 수량", example = "10")
@JsonProperty
int quantity
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,47 @@

import java.util.List;

import io.swagger.v3.oas.annotations.media.Schema;

@Schema(description = "계약 요청 DTO")
public record ContractReqDto(

@Schema(description = "계약할 기업의 ID", example = "1")
long enterpriseId,

@Schema(description = "주당 배송 빈도", example = "2")
int deliveryWeek,

@Schema(description = "선호하는 배송 요일", example = "Monday")
String deliveryDay,

@Schema(description = "배송 수령 방법", example = "Home Delivery")
String takeMethod,

@Schema(description = "계약 요청 기간", example = "6개월")
String requestedTerm,

@Schema(description = "계약 총 가격", example = "150000")
long totalPrice,

@Schema(description = "계약에 포함된 제품 목록", example = """
[
{
"productId": 1,
"productName": "유기농 쌀",
"unit": "1kg",
"price": 5000,
"quantity": 10
},
{
"productId": 2,
"productName": "감자",
"unit": "2kg",
"price": 4000,
"quantity": 5
}
]
""")
List<ProductDto> products
) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,30 @@
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Builder;

import io.swagger.v3.oas.annotations.media.Schema;

@Schema(description = "계약 정보 DTO")
@Builder
public record ContractsInfoDto(

@Schema(description = "계약과 연관된 기업 ID", example = "1")
@JsonProperty
long enterpriseId,

@Schema(description = "계약 ID", example = "101")
@JsonProperty
long contractId,

@Schema(description = "기업 프로필 이미지 URL", example = "https://example.com/profile-image.jpg")
@JsonProperty
String profileImage,

@Schema(description = "기업 이름", example = "초록믿음강진")
@JsonProperty
String enterpriseName,

@Schema(description = "기업 카테고리", example = "AGRICULTURAL_PRODUCTS")
@JsonProperty
String category
) {
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
package com.univ.sohwakhaeng.contract.api.dto;

import io.swagger.v3.oas.annotations.media.Schema;

@Schema(description = "제품 DTO")
public record ProductDto(

@Schema(description = "제품의 ID", example = "1")
long productId,

@Schema(description = "제품 이름", example = "유기농 쌀")
String productName,

@Schema(description = "제품 수량", example = "10")
int quantity
) {}
) {}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.univ.sohwakhaeng.contract.domain.repository.ContractRepository;
import com.univ.sohwakhaeng.enterprise.Enterprise;
import com.univ.sohwakhaeng.enterprise.repository.EnterpriseRepository;
import com.univ.sohwakhaeng.enterprise.service.EnterpriseService;
import com.univ.sohwakhaeng.global.common.dto.PagedResponseDto;
import com.univ.sohwakhaeng.product.Product;
import com.univ.sohwakhaeng.product.repository.ProductRepository;
Expand All @@ -26,6 +27,7 @@
@RequiredArgsConstructor
public class ContractService {

private final EnterpriseService enterpriseService;
private final ContractRepository contractRepository;
private final ContractProductsRepository contractProductsRepository;
private final EnterpriseRepository enterpriseRepository;
Expand Down Expand Up @@ -79,12 +81,14 @@ private Page<Contract> getPagedContractsByUser(Pageable pageable, User user) {
}

private ContractsInfoDto convertToDtoFromContract(Contract contract) {
String enterpriseImageUrl = enterpriseService.getEnterpriseImageUrl(contract.getEnterprise().getName());

return ContractsInfoDto.builder()
.contractId(contract.getId())
.enterpriseId(contract.getEnterprise().getId())
.enterpriseName(contract.getEnterprise().getName())
.category(contract.getEnterprise().getCategory().toString())
.profileImage(contract.getEnterprise().getImageUrl())
.profileImage(enterpriseImageUrl)
.build();
}

Expand All @@ -93,11 +97,13 @@ private ContractsInfoDto convertToDtoFromContract(Contract contract) {
public ContractDetailDto getContractDetail(long contractId) {
Contract contract = contractRepository.findById(contractId)
.orElseThrow(() -> new IllegalArgumentException("해당 계약이 존재하지 않습니다."));

String enterpriseImageUrl = enterpriseService.getEnterpriseImageUrl(contract.getEnterprise().getName());
return ContractDetailDto.builder()
.enterpriseId(contract.getEnterprise().getId())
.requestTerm(contract.getRequestedTerm())
.reqularDelivery(contract.getDeliveryWeek() + " " + contract.getDeliveryDay())
.profileImgUrl(contract.getEnterprise().getImageUrl())
.regularDelivery(contract.getDeliveryWeek() + " " + contract.getDeliveryDay())
.profileImgUrl(enterpriseImageUrl)
.enterpriseName(contract.getEnterprise().getName())
.products(contract.getContractProducts().stream().map(this::convertToDtoFromContractProducts).toList())
.category(contract.getEnterprise().getCategory().toString())
Expand Down
3 changes: 0 additions & 3 deletions src/main/java/com/univ/sohwakhaeng/enterprise/Enterprise.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@ public class Enterprise {
@Column(name = "enterprise_id")
private Long id;

@Column(name = "enterprise_imageUrl")
private String imageUrl;

@Column(name = "enterprise_name")
private String name;

Expand Down
Loading
Loading