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

feat : 카테고리별 조회 #56

Merged
merged 5 commits into from
Nov 27, 2024
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,6 +4,7 @@
import com.ecyce.karma.domain.bookmark.service.BookmarkService;
import com.ecyce.karma.domain.product.dto.request.ModifyOptionRequest;
import com.ecyce.karma.domain.product.dto.request.ModifyProductRequest;
import com.ecyce.karma.domain.product.dto.request.OptionRequest;
import com.ecyce.karma.domain.product.dto.response.OptionResponse;
import com.ecyce.karma.domain.product.dto.response.ProductDetailResponse;
import com.ecyce.karma.domain.product.dto.request.ProductRequest;
Expand Down Expand Up @@ -109,4 +110,24 @@ public ResponseEntity<?> SearchAllByWord(@AuthUser User user ,@RequestParam(name
return productService.Search(user , searchWord);
}

/* 카테고리별 조회 */
@GetMapping("/category")
public ResponseEntity<?> categorySort(@AuthUser User user , @RequestParam(name ="code") Long category){
return productService.categorySort(user , category);
}

/* 옵션 삭제 */
@DeleteMapping("/{productId}/option/{optionId}")
public String deleteOption(@AuthUser User user ,@PathVariable("productId") Long productId , @PathVariable("optionId") Long optionId){
productService.deleteOption(user , productId , optionId);
return "삭제되었습니다.";
}

/* 옵션 추가 */
@PostMapping("/{productId}/option")
public ResponseEntity<OptionResponse> addOption(@AuthUser User user , @PathVariable("productId") Long productId , @RequestBody OptionRequest request){
return productService.addOption(user , productId , request);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.ecyce.karma.domain.order.entity.Orders;
import com.ecyce.karma.domain.product.dto.request.ModifyProductRequest;
import com.ecyce.karma.domain.user.entity.User;
import com.ecyce.karma.global.entity.BaseTimeEntity;
import jakarta.persistence.*;
import lombok.AccessLevel;
import lombok.Builder;
Expand All @@ -16,7 +17,7 @@
@Entity
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Product {
public class Product extends BaseTimeEntity {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,23 @@ public interface ProductRepository extends JpaRepository<Product , Long> {
@Query("select p from Product p where p.productName LIKE %:searchWord%")
List<Product> findByProductName(@Param("searchWord") String word);

/* 모든 제품을 rating 기준으로 내림차순 정렬 (NULLS LAST 포함)*/
@Query("SELECT p FROM Product p ORDER BY p.rating DESC NULLS LAST")
List<Product> findAllOrderByRatingDesc();



/* 모든 제품을 북마크 순으로 정렬 */
@Query("SELECT p " +
"FROM Product p " +
"LEFT JOIN p.bookmarkList b " +
"GROUP BY p " +
"ORDER BY COUNT(b) DESC")
List<Product> findAllOrderByBookmarkCountDesc();


/* 최신순으로 정렬 */
@Query("SELECT p FROM Product p ORDER BY p.createdAt DESC")
List<Product> findAllOrderByCreatedAtDesc();

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand Down Expand Up @@ -167,4 +168,46 @@ public ResponseEntity<?> Search(User user, String searchWord) {

return ResponseEntity.status(HttpStatus.OK).body(productSimpleResponseList);
}

/* 카테고리별 조회*/
public ResponseEntity<?> categorySort(User user, Long category) {

if(category.equals(1L)){ // 후기순
List<Product> sortedProducts = productRepository.findAllOrderByRatingDesc(); // 정렬된 결과 가져오기
// 로그인 여부에 따른 북마크 상태 포함
List<ProductSimpleResponse> productSimpleResponses = getProductSimpleResponses(user, sortedProducts);
return ResponseEntity.ok(productSimpleResponses);
}
else if(category.equals(2L)){ // 북마크 개수 순
List<Product> sortedProducts = productRepository.findAllOrderByBookmarkCountDesc();
// 로그인 여부에 따른 북마크 상태 포함
List<ProductSimpleResponse> productSimpleResponses = getProductSimpleResponses(user, sortedProducts);
return ResponseEntity.ok(productSimpleResponses);
}
else if(category.equals(3L)){ // 최근순
List<Product> sortedProducts = productRepository.findAllOrderByCreatedAtDesc();
List<ProductSimpleResponse> productSimpleResponse = getProductSimpleResponses(user , sortedProducts);
return ResponseEntity.ok(productSimpleResponse);
}
else throw new CustomException(ErrorCode.INVALID_REQUEST);

}

/* 옵션 삭제 */
public void deleteOption(User user , Long productId , Long optionId) {
ProductOption productOption = productOptionRepository.findByProductIdAndOptionId(productId , optionId);
if(!user.getUserId().equals(productOption.getProduct().getUser().getUserId())){
throw new CustomException(ErrorCode.INVALID_ACCESS);
}
productOptionRepository.delete(productOption);
}

/* 옵션 추가 */
public ResponseEntity<OptionResponse> addOption(User user, Long productId, OptionRequest request) {
Product product = findProduct(productId);
ProductOption productOption = OptionRequest.toEntity(product ,request);
productOptionRepository.save(productOption);
OptionResponse response = OptionResponse.from(productOption);
return ResponseEntity.status(HttpStatus.CREATED).body(response);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public enum ErrorCode {
PRODUCT_NOT_FOUND(HttpStatus.NOT_FOUND , "해당하는 제품을 찾을 수 없습니다."),
PRODUCT_OPTION_NOT_FOUND(HttpStatus.NOT_FOUND, "해당하는 옵션을 찾을 수 없습니다."),
INVALID_ACCESS(HttpStatus.BAD_REQUEST , "해당 사용자는 제품 정보를 수정할 수 없습니다."),
INVALID_REQUEST(HttpStatus.BAD_REQUEST , "잘못된 요청입니다."),

// bookmark
BOOKMARK_NOT_FOUND(HttpStatus.NOT_FOUND, "북마크가 존재하지 않습니다. 북마크를 등록한 후 취소할 수 있습니다."),
Expand Down