Skip to content

Commit

Permalink
#8 fix: 판매글 목록 조회 시 판매중 글 뒤에 판매완료 글 배치
Browse files Browse the repository at this point in the history
  • Loading branch information
JoongHyun-Kim committed Feb 5, 2024
1 parent 77dc301 commit c17dd41
Showing 1 changed file with 25 additions and 11 deletions.
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

0 comments on commit c17dd41

Please sign in to comment.