Skip to content

Commit

Permalink
Merge pull request #245 from leeminju/feature/exception
Browse files Browse the repository at this point in the history
Feature/exception 정렬 오류 수정
  • Loading branch information
leeminju authored Feb 6, 2024
2 parents 8522add + 4a570ac commit bc5b9d7
Show file tree
Hide file tree
Showing 17 changed files with 171 additions and 103 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;

Expand Down Expand Up @@ -51,6 +53,9 @@ public Page<LikeResponseDto> viewMyLikeProducts(Long userId, User loginUser, Pag
if (!user.getId().equals(loginUser.getId())) {
throw new ApiException("본인의 좋아요 목록만 조회 가능힙니다.", HttpStatus.BAD_REQUEST);
}
pageable = PageRequest.of(pageable.getPageNumber(), pageable.getPageSize(),
pageable.getSort().and(Sort.by("id")));

Page<Like> likeList = likeRepository.findAllByUser(user, pageable);

return likeList.map(LikeResponseDto::new);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public ResponseEntity<ApiResponse> createProduct(@PathVariable Long categoryId,
*/
@GetMapping("/products")
public ResponseEntity<ApiResponse<Page<ProductResponseDto>>> getProducts(
@PageableDefault(size = 10, sort = "createdAt", direction = Sort.Direction.DESC) Pageable pageable) {
@PageableDefault Pageable pageable) {
Page<ProductResponseDto> page = productService.getProducts(pageable);
return ResponseEntity.ok()
.body(new ApiResponse<>("상품 전체 조회에 성공하였습니다.", HttpStatus.OK.value(), page));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand Down Expand Up @@ -56,17 +58,20 @@ public ProductResponseDto createProduct(Long categoryId, ProductRequestDto reque
// 상품 전체 조회 + 페이징 정렬(상품 등록순)
@Transactional(readOnly = true)
public Page<ProductResponseDto> getProducts(Pageable pageable) {
pageable = PageRequest.of(pageable.getPageNumber(), pageable.getPageSize(),
pageable.getSort().and(Sort.by("id")));
Page<Product> products = productRepository.findAll(pageable);
// "Page" 인터페이스가 제공하는 'map' 메서드 활용
return products.map(ProductResponseDto::new);
}

// 카테고리별 상품 조회 + 페이징 정렬(상품 등록순)
public Page<ProductResponseDto> getProductsByCategory(Long categoryId, Pageable pageable) {

if (!categoryRepository.existsById(categoryId)) {
throw new ApiException("해당 카테고리가 존재하지 않습니다.", HttpStatus.NOT_FOUND);
}
pageable = PageRequest.of(pageable.getPageNumber(), pageable.getPageSize(),
pageable.getSort().and(Sort.by("id")));

Page<Product> products = productRepository.findByCategoryId(categoryId, pageable);
return products.map(ProductResponseDto::new);
Expand All @@ -89,7 +94,8 @@ public Page<ProductResponseDto> getProductsBySearch(String keyword, Pageable pag
if (!StringUtils.hasText(keyword)) {
throw new ApiException("검색어를 입력해주세요.", HttpStatus.BAD_REQUEST);
}

pageable = PageRequest.of(pageable.getPageNumber(), pageable.getPageSize(),
pageable.getSort().and(Sort.by("id")));
Page<Product> products = productRepository.findByKeyword(keyword, pageable);
return products.map(ProductResponseDto::new);
}
Expand Down Expand Up @@ -127,7 +133,7 @@ public void deleteProduct(Long productId) {

// 제품에 해당하는 모든 리뷰 삭제
List<Review> reviews = reviewRepository.findAllByProduct_Id(productId);
if (reviews!=null) {
if (reviews != null) {
reviews.stream().forEach(review -> {
reviewService.deleteProductsReview(
review.getProduct().getCategory().getId(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.example.jujuassembly.domain.category.entity.Category;
import com.example.jujuassembly.domain.category.repository.CategoryRepository;
import com.example.jujuassembly.domain.notification.repository.NotificationRepository;
import com.example.jujuassembly.domain.notification.service.NotificationService;
import com.example.jujuassembly.domain.report.dto.ReportPatchRequestDto;
import com.example.jujuassembly.domain.report.dto.ReportRequestDto;
Expand All @@ -16,7 +15,9 @@
import java.io.IOException;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand Down Expand Up @@ -59,25 +60,32 @@ public ReportResponseDto postReport(Long categoryId, MultipartFile image,
//조회
//전체 제보상품 조회
public Page<ReportResponseDto> getAllReports(Pageable pageable) {
pageable = PageRequest.of(pageable.getPageNumber(), pageable.getPageSize(),
pageable.getSort().and(Sort.by("id")));
Page<Report> allReports = reportRepository.findAll(pageable);
return allReports.map(ReportResponseDto::new);
}

//유저별 제보상품 조회
public Page<ReportResponseDto> getUserReports(Long userId, Pageable pageable) {
pageable = PageRequest.of(pageable.getPageNumber(), pageable.getPageSize(),
pageable.getSort().and(Sort.by("id")));
Page<Report> reports = reportRepository.findAllByUserId(userId, pageable);
return reports.map(ReportResponseDto::new);
}

//카테고리별 제보상품 조회
public Page<ReportResponseDto> getReportsByCategoryId(Long categoryId, Pageable pageable) {
pageable = PageRequest.of(pageable.getPageNumber(), pageable.getPageSize(),
pageable.getSort().and(Sort.by("id")));
Page<Report> reports = reportRepository.findAllByCategoryId(categoryId, pageable);
return reports.map(ReportResponseDto::new);
}

//수정
@Transactional
public ReportResponseDto patchReport(Long categoryId, Long reportId, MultipartFile image, Boolean original,
public ReportResponseDto patchReport(Long categoryId, Long reportId, MultipartFile image,
Boolean original,
ReportPatchRequestDto requestDto)
throws IOException {

Expand All @@ -87,17 +95,16 @@ public ReportResponseDto patchReport(Long categoryId, Long reportId, MultipartFi
throw new ApiException("현재 카테고리가 아닙니다.", HttpStatus.BAD_REQUEST);
}

Category ModifiedCategory = categoryRepository.findCategoryByIdOrElseThrow(requestDto.getModifiedCategoryId());
Category ModifiedCategory = categoryRepository.findCategoryByIdOrElseThrow(
requestDto.getModifiedCategoryId());

report.updateName(requestDto.getName());
report.updateCategory(ModifiedCategory);

if (image == null || image.getContentType() == null) {
if (report.getImage() != null) {
if (original) {
String originalImage = report.getImage();
System.out.println("orignalImage"+originalImage);
report.updateImage(originalImage);//원래 이미지 유지
report.updateImage(report.getImage());//원래 이미지 유지
} else {
report.updateImage(null);//원래 이미지 삭제
s3Manager.deleteAllImageFiles(reportId.toString(), S3Manager.REPORT_DIRECTORY_NAME);
Expand All @@ -106,7 +113,7 @@ public ReportResponseDto patchReport(Long categoryId, Long reportId, MultipartFi
report.updateImage(null);
s3Manager.deleteAllImageFiles(reportId.toString(), S3Manager.REPORT_DIRECTORY_NAME);
}
}else {
} else {
if (!image.getContentType().startsWith("image")) {
throw new ApiException("이미지 파일 형식이 아닙니다.", HttpStatus.BAD_REQUEST);
}
Expand All @@ -117,7 +124,8 @@ public ReportResponseDto patchReport(Long categoryId, Long reportId, MultipartFi
}

@Transactional
public ReportResponseDto patchReportStatus(Long categoryId, Long reportId, ReportStatusRequestDto requestDto, User user) {
public ReportResponseDto patchReportStatus(Long categoryId, Long reportId,
ReportStatusRequestDto requestDto, User user) {
categoryRepository.findCategoryByIdOrElseThrow(categoryId);
Report report = reportRepository.findReportByIdOrElseThrow(reportId);
report.updateStatus(requestDto.getStatus());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@
import jakarta.persistence.EntityManager;
import java.util.List;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand Down Expand Up @@ -64,6 +65,8 @@ public ReviewResponseDto createProductsReview(Long categoryId, Long productId,

public Page<ReviewResponseDto> getProductsReview(Long categoryId, Long productId,
Pageable pageable) {
pageable = PageRequest.of(pageable.getPageNumber(), pageable.getPageSize(),
pageable.getSort().and(Sort.by("id")));
categoryRepository.findCategoryByIdOrElseThrow(categoryId);
Product product = productRepository.findProductByIdOrElseThrow(productId);
checkProductCategoryAndCategoryIdEquality(product, categoryId);
Expand Down Expand Up @@ -146,6 +149,8 @@ public void deleteProductsReview(Long categoryId, Long productId, Long reviewId)
}

public Page<ReviewResponseDto> getMyReviews(Long userId, Pageable pageable) {
pageable = PageRequest.of(pageable.getPageNumber(), pageable.getPageSize(),
pageable.getSort().and(Sort.by("id")));
User user = userRepository.findUserByIdOrElseThrow(userId);

Page<Review> reviews = reviewRepository.findAllByUser(user, pageable);
Expand All @@ -155,6 +160,8 @@ public Page<ReviewResponseDto> getMyReviews(Long userId, Pageable pageable) {
@Transactional(readOnly = true)
public Page<ReviewResponseDto> getVerifiedReviews(Long userId, Pageable pageable,
boolean isVerified) {
pageable = PageRequest.of(pageable.getPageNumber(), pageable.getPageSize(),
pageable.getSort().and(Sort.by("id")));
User user = userRepository.findUserByIdOrElseThrow(userId);

Page<Review> reviews;
Expand Down
9 changes: 5 additions & 4 deletions src/main/resources/templates/admin.html
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,12 @@ <h6 class="border-bottom pb-2 mb-0">전체 회원 목록</h6>
<div class="pagination">
정렬:
<select id="sorting" onchange="getAllUsers()" style="margin-right: 10px">
<option value="createdAt">등록일</option>
<option value="modifiedAt">수정일</option>
<option value="nickname">닉네임 순</option>
<option value="loginId">아이디 순</option>
<option value="createdAt">가입 순</option>
</select>
<input type="radio" name="isAsc" value="asc" onchange="getAllUsers"/> 오름차순
<input type="radio" name="isAsc" value="desc" onchange="getAllUsers()" checked/> 내림차순
<input type="radio" name="isAsc" value="asc" onchange="getAllUsers()" checked/> 오름차순
<input type="radio" name="isAsc" value="desc" onchange="getAllUsers()"/> 내림차순
</div>
</div>
<div class="my-3 p-3 bg-body rounded shadow-sm"
Expand Down
9 changes: 5 additions & 4 deletions src/main/resources/templates/chat.html
Original file line number Diff line number Diff line change
Expand Up @@ -491,11 +491,12 @@ <h1 style=" width: fit-content; margin: 10px auto 10px auto">고객과의 채
<div class="pagination">
정렬:
<select id="sorting" onchange="getAllUsers()" style="margin-right: 10px">
<option value="createdAt">등록일</option>
<option value="modifiedAt">수정일</option>
<option value="nickname">닉네임 순</option>
<option value="loginId">아이디 순</option>
<option value="createdAt">가입 순</option>
</select>
<input type="radio" name="isAsc" value="asc" onchange="getAllUsers()"/> 오름차순
<input type="radio" name="isAsc" value="desc" onchange="getAllUsers()" checked/> 내림차순
<input type="radio" name="isAsc" value="asc" onchange="getAllUsers()" checked/> 오름차순
<input type="radio" name="isAsc" value="desc" onchange="getAllUsers()"/> 내림차순
</div>
</div>

Expand Down
11 changes: 11 additions & 0 deletions src/main/resources/templates/mainProduct.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@
</head>
<script>
function getLike(categoryId, id) {
if (myId == null) {
$('#emptyHeart-' + id).show();
$('#fullHeart-' + id).hide();
return;
}
;
$.ajax({
type: "GET",
url: `/v1/categories/${categoryId}/products/${id}/like`,
Expand All @@ -46,6 +52,10 @@
}

function likeProduct(categoryId, id) {
if (myId == null) {
alert("로그인한 사용자만 찜할 수 있습니다.");
return;
}
$.ajax({
type: "POST",
url: `/v1/categories/${categoryId}/products/${id}/like`,
Expand Down Expand Up @@ -462,6 +472,7 @@ <h5 class="card-title">${name}</h5>
<select id="sorting" onchange="getAllProducts()" style="margin-right: 10px">
<option value="reviewCount">리뷰 많은 순</option>
<option value="likesCount">찜 많은 순</option>
<option value="name">이름 순</option>
<option value="createdAt">등록일 순</option>
<option value="modifiedAt">수정일 순</option>

Expand Down
1 change: 1 addition & 0 deletions src/main/resources/templates/myLike.html
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ <h1 style="padding: 10px">내가 찜한 주류</h1>
정렬:
<select id="sorting" onchange="getUserLikeList()" style="margin-right: 10px">
<option value="createdAt">찜한 순</option>
<option value="product.name">이름 순</option>
</select>
<input type="radio" name="isAsc" value="asc" onchange="getUserLikeList()"/> 오름차순
<input type="radio" name="isAsc" value="desc" onchange="getUserLikeList()" checked/> 내림차순
Expand Down
11 changes: 10 additions & 1 deletion src/main/resources/templates/productByCategory.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
<script src="/js/notification.js"></script>
<script>
function getLike(categoryId, id) {
if (myId == null) {
$('#emptyHeart-' + id).show();
$('#fullHeart-' + id).hide();
return;
}
$.ajax({
type: "GET",
url: `/v1/categories/${categoryId}/products/${id}/like`,
Expand All @@ -38,12 +43,15 @@
}, error: function (error) {
$('#emptyHeart-' + id).show();
$('#fullHeart-' + id).hide();
alert(error['responseJSON']['msg']);
}
});
}

function likeProduct(categoryId, id) {
if (myId == null) {
alert("로그인한 사용자만 찜할 수 있습니다.");
return;
}
$.ajax({
type: "POST",
url: `/v1/categories/${categoryId}/products/${id}/like`,
Expand Down Expand Up @@ -460,6 +468,7 @@ <h5 class="card-title">${name}</h5>
<select id="sorting" onchange="getProductByCategory()" style="margin-right: 10px">
<option value="reviewCount">리뷰 많은 순</option>
<option value="likesCount">찜 많은 순</option>
<option value="name">이름 순</option>
<option value="createdAt">등록일 순</option>
<option value="modifiedAt">수정일 순</option>

Expand Down
20 changes: 13 additions & 7 deletions src/main/resources/templates/productDetails.html
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,10 @@
}

function likeReview(categoryId, productId, reviewId) {
if (myId == null) {
alert("로그인한 사용자만 추천할 수 있습니다.");
return;
}
$.ajax({
url: `/v1/categories/${categoryId}/products/${productId}/reviews/${reviewId}/like`,
type: 'POST',
Expand All @@ -382,6 +386,10 @@
}

function dislikeReview(categoryId, productId, reviewId) {
if (myId == null) {
alert("로그인한 사용자만 비추천할 수 있습니다.");
return;
}
$.ajax({
url: `/v1/categories/${categoryId}/products/${productId}/reviews/${reviewId}/dislike`,
type: 'POST',
Expand Down Expand Up @@ -523,13 +531,7 @@
}

function getLike() {
const auth = getToken();

if (auth !== undefined && auth !== '') {
$.ajaxPrefilter(function (options, originalOptions, jqXHR) {
jqXHR.setRequestHeader('Authorization', auth);
});
} else {
if (myId == null) {
$('#emptyHeart').show();
$('#fullHeart').hide();
return;
Expand Down Expand Up @@ -557,6 +559,10 @@
}

function likeProduct() {
if (myId == null) {
alert("로그인한 사용자만 찜할 수 있습니다.");
return;
}
$.ajax({
type: "POST",
url: `/v1/categories/${categoryId}/products/${productId}/like`,
Expand Down
Loading

0 comments on commit bc5b9d7

Please sign in to comment.