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

[fix/8-getProduceList2] 판매글 목록 조회 시 판매중 글 뒤에 판매완료 글 배치 #48

Merged
merged 1 commit into from
Feb 5, 2024
Merged
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 @@ -15,11 +15,12 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import static com.kkobugi.puremarket.common.constants.Constant.INACTIVE;
import static com.kkobugi.puremarket.common.constants.Constant.Produce.FOR_SALE;
import static com.kkobugi.puremarket.common.constants.Constant.Produce.SOLD_OUT;
import static com.kkobugi.puremarket.common.enums.BaseResponseStatus.*;

@Service
Expand All @@ -34,21 +35,37 @@ public class ProduceService {
private String bucketName;

// 판매글 목록 조회
public ProduceListResponse getProduceList() throws BaseException { // TODO: status=SOLD_OUT인 것도 조회
public ProduceListResponse getProduceList() throws BaseException {
try {
// 판매 상태인 글 최신순으로 조회
List<ProduceListResponse.ProduceDto> produceList = produceRepository.findByStatusEqualsOrderByCreatedDateDesc(FOR_SALE).stream()
List<ProduceListResponse.ProduceDto> forSaleList = produceRepository.findByStatusEqualsOrderByCreatedDateDesc(FOR_SALE).stream()
.map(produce -> new ProduceListResponse.ProduceDto(
produce.getProduceIdx(),
produce.getTitle(),
produce.getPrice(),
produce.getProduceImage(),
produce.getStatus()))
.collect(Collectors.toList());
if (produceList.isEmpty()) throw new BaseException(NULL_PRODUCE_LIST);
produce.getStatus())).toList();

// 판매완료 상태인 글 최신순으로 조회
List<ProduceListResponse.ProduceDto> soldOutList = produceRepository.findByStatusEqualsOrderByCreatedDateDesc(SOLD_OUT).stream()
.map(produce -> new ProduceListResponse.ProduceDto(
produce.getProduceIdx(),
produce.getTitle(),
produce.getPrice(),
produce.getProduceImage(),
produce.getStatus())).toList();

// validation
//if (forSaleList.isEmpty() && soldOutList.isEmpty()) throw new BaseException(NULL_PRODUCE_LIST);

//forSaleList.addAll(soldOutList);
List<ProduceListResponse.ProduceDto> produceList = new ArrayList<>();
produceList.addAll(forSaleList);
produceList.addAll(soldOutList);

return new ProduceListResponse(produceList);
} catch (BaseException e) {
throw e;
// } catch (BaseException e) {
// throw e;
} catch (Exception e) {
throw new BaseException(DATABASE_ERROR);
}
Expand Down Expand Up @@ -80,9 +97,6 @@ public void postProduce(ProducePostRequest producePostRequest) throws BaseExcept
try {
User writer = userRepository.findByUserIdx(authService.getUserIdxFromToken()).orElseThrow(() -> new BaseException(INVALID_USER_IDX));

// validation
if (producePostRequest.title().length() > 32) throw new BaseException(TITLE_EXCEEDED_MAX_LIMIT);

// upload image
String fullPath = gcsService.uploadImage("produce", producePostRequest.produceImage());
String produceImageUrl = "https://storage.googleapis.com/"+bucketName+"/"+fullPath;
Expand Down