Skip to content

Commit

Permalink
✨ feat: 특정 판매자 페이지(판매자 정보와 판매 상품 불러오기) (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
jjin70 committed Jul 25, 2024
1 parent 031edc7 commit 76b0b47
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import umc.unimade.domain.accounts.dto.SellerMyPageResponse;
import umc.unimade.domain.accounts.dto.SellerPageResponse;
import umc.unimade.domain.accounts.service.SellerQueryService;

import org.springframework.data.domain.Pageable;

@RestController
@RequestMapping("/seller")
Expand All @@ -26,4 +29,17 @@ public ResponseEntity<SellerMyPageResponse> getSellerMyPage(@PathVariable Long s
SellerMyPageResponse response = sellerQueryService.getSellerMyPage(sellerId);
return ResponseEntity.ok(response);
}

@Tag(name = "Seller", description = "판매자 관련 API")
@Operation(summary = "특정 판매자 페이지")
@GetMapping("/{sellerId}")
public ResponseEntity<SellerPageResponse> getSellerPage(@PathVariable Long sellerId,
@RequestParam(required = false, defaultValue = "popular") String sort,
@RequestParam(name = "page", defaultValue = "0") int page,
@RequestParam(name = "size", defaultValue = "10") int size) {

Pageable pageable = PageRequest.of(page, size);
SellerPageResponse sellerPage = sellerQueryService.getSellerPage(sellerId, sort, pageable);
return ResponseEntity.ok(sellerPage);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package umc.unimade.domain.accounts.dto;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.springframework.data.domain.Page;
import umc.unimade.domain.accounts.entity.Seller;
import umc.unimade.domain.products.entity.Products;

import java.util.List;

// TODO - md 찜 수, 상품 찜 수, 접속한 buyer가 해당 seller 찜한 여부, + 설명창,인스타 (?)
@Getter
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class SellerPageResponse {
private String profileImage;
private String name;
private String phone;
private Page<ProductsResponse> products;

public static SellerPageResponse of(Seller seller, Page<ProductsResponse> products) {
return SellerPageResponse.builder()
.profileImage(seller.getProfileImage())
.name(seller.getName())
.phone(seller.getPhone())
.products(products)
.build();
}

@Getter
@NoArgsConstructor
@AllArgsConstructor
@Builder
public static class ProductsResponse {
private Long productId;
private String name;
private Long price;
private String imageUrl;

public static ProductsResponse from(Products product) {
return ProductsResponse.builder()
.productId(product.getId())
.name(product.getName())
.price(product.getPrice())
.imageUrl(product.getProductImages().isEmpty() ? null : product.getProductImages().get(0).getImageUrl())
.build();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
package umc.unimade.domain.accounts.service;

import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import umc.unimade.domain.accounts.dto.SellerMyPageResponse;
import umc.unimade.domain.accounts.dto.SellerPageResponse;
import umc.unimade.domain.accounts.entity.Seller;
import umc.unimade.domain.accounts.exception.SellerExceptionHandler;
import umc.unimade.domain.accounts.repository.SellerRepository;
import umc.unimade.domain.products.dto.SellingProductResponse;
import umc.unimade.domain.products.dto.SoldoutProductResponse;
import umc.unimade.domain.products.entity.ProductStatus;
import umc.unimade.domain.products.entity.Products;
import umc.unimade.domain.products.repository.ProductRepository;
import umc.unimade.global.common.ErrorCode;
import org.springframework.data.domain.Pageable;

import java.util.List;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -40,4 +44,30 @@ public SellerMyPageResponse getSellerMyPage(Long sellerId) {

return SellerMyPageResponse.from(seller, sellingProducts, soldoutProducts);
}
}

// 특정 sellerId의 프로필 정보와 selling 상태인 상품 목록을 조회
public SellerPageResponse getSellerPage(Long sellerId, String sort, Pageable pageable) {
Seller seller = sellerRepository.findById(sellerId)
.orElseThrow(() -> new SellerExceptionHandler(ErrorCode.SELLER_NOT_FOUND));

// 정렬
Page<Products> products;
switch (sort) {
case "popular": // 인기순
products = productRepository.findBySellerIdAndStatusOrderByPopularity(sellerId, ProductStatus.SELLING, pageable);
break;
case "latest": // 최신순
products = productRepository.findBySellerIdAndStatusOrderByCreatedAtDesc(sellerId, ProductStatus.SELLING, pageable);
break;
case "deadline": // 마감순
products = productRepository.findBySellerIdAndStatusOrderByDeadline(sellerId, ProductStatus.SELLING, pageable);
break;
default:
products = productRepository.findBySellerIdAndStatusOrderByPopularity(sellerId, ProductStatus.SELLING, pageable);
}

Page<SellerPageResponse.ProductsResponse> productResponses = products.map(SellerPageResponse.ProductsResponse::from);

return SellerPageResponse.of(seller, productResponses);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,27 @@
import org.springframework.stereotype.Repository;
import umc.unimade.domain.products.entity.ProductStatus;
import umc.unimade.domain.products.entity.Products;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Page;

import java.util.List;

@Repository
public interface ProductRepository extends JpaRepository<Products, Long>, ProductsRepositoryCustom {
List<Products> findTop4BySellerIdAndStatusOrderByCreatedAtDesc(Long sellerId, ProductStatus status);

// deadline 지난 selling상품 찾기
@Query("SELECT p FROM Products p WHERE p.status = :status AND p.deadline < CURRENT_DATE")
List<Products> findExpiredProducts(ProductStatus status);

// 인기순
@Query("SELECT p FROM Products p LEFT JOIN p.favoriteProducts f WHERE p.seller.id = :sellerId AND p.status = :status GROUP BY p.id ORDER BY COUNT(f) DESC")
Page<Products> findBySellerIdAndStatusOrderByPopularity(Long sellerId, ProductStatus status, Pageable pageable);

// 최신순
Page<Products> findBySellerIdAndStatusOrderByCreatedAtDesc(Long sellerId, ProductStatus status, Pageable pageable);

// 마감순
@Query("SELECT p FROM Products p WHERE p.seller.id = :sellerId AND p.status = :status ORDER BY p.deadline ASC")
Page<Products> findBySellerIdAndStatusOrderByDeadline(Long sellerId, ProductStatus status, Pageable pageable);
}

0 comments on commit 76b0b47

Please sign in to comment.