Skip to content

Commit

Permalink
feat: 번역 내역 리스트 조회 기능 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
soyeonii committed Oct 9, 2023
1 parent 674df63 commit e9dbf25
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 14 deletions.
5 changes: 1 addition & 4 deletions src/main/java/com/makive/moumi/model/domain/Request.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,8 @@ public void accept() {
this.status = RequestStatus.진행중;
}

public void complete() {
public void complete(List<String> responsePdfs) {
this.status = RequestStatus.완료;
}

public void uploadResponsePdfs(List<String> responsePdfs) {
this.responsePdfs = responsePdfs;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@
@Getter
public class RequestRequest {
private List<String> category;
private boolean hasReview;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import com.makive.moumi.model.domain.Request;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

@Repository
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.makive.moumi.repository.specification;

import com.makive.moumi.model.domain.Request;
import com.makive.moumi.model.domain.TranslationCategory;
import jakarta.persistence.criteria.Predicate;
import jakarta.persistence.criteria.Root;
import jakarta.persistence.criteria.Subquery;
import org.springframework.data.jpa.domain.Specification;

import java.util.ArrayList;
import java.util.List;

public class RequestSpecifications {
public static Specification<Request> findAllByClientIdAndCategory(Long clientId, List<String> category, boolean hasReview) {
return (root, query, criteriaBuilder) -> {
List<Predicate> predicates = new ArrayList<>();

predicates.add(criteriaBuilder.equal(root.get("client").get("id"), clientId));

if (!category.isEmpty()) {
Subquery<Long> subquery = query.subquery(Long.class);
Root<TranslationCategory> subqueryTranslationCategory = subquery.from(TranslationCategory.class);
subquery.select(subqueryTranslationCategory.get("translation").get("id"));
subquery.groupBy(subqueryTranslationCategory.get("translation").get("id"));
subquery.having(criteriaBuilder.equal(criteriaBuilder.count(subqueryTranslationCategory.get("category").get("name")), category.size()));
subquery.where(subqueryTranslationCategory.get("category").get("name").in(category));

predicates.add(root.get("id").in(subquery));
}

if (hasReview) {
predicates.add(criteriaBuilder.isNotNull(root.get("review")));
} else {
predicates.add(criteriaBuilder.isNull(root.get("review")));
}

return criteriaBuilder.and(predicates.toArray(new Predicate[0]));
};
}
}
17 changes: 8 additions & 9 deletions src/main/java/com/makive/moumi/service/RequestService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.makive.moumi.exception.Code;
import com.makive.moumi.exception.GeneralException;
import com.makive.moumi.model.domain.Client;
import com.makive.moumi.model.domain.Request;
import com.makive.moumi.model.domain.Review;
Expand All @@ -15,18 +17,19 @@
import com.makive.moumi.repository.ClientRepository;
import com.makive.moumi.repository.RequestRepository;
import com.makive.moumi.repository.TranslationRepository;
import com.makive.moumi.repository.specification.RequestSpecifications;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.stream.Collectors;

Expand All @@ -51,9 +54,11 @@ private static double calculateAverageRating(List<Double> reviewRatings) {
return Double.parseDouble(String.format("%.1f", sum / reviewRatings.size()));
}

// TODO: 카테고리 및 리뷰 작성 여부로 조회하기
public RequestsResponse getRequests(RequestRequest requestRequest, Pageable pageable) {
Slice<Request> requestSlice = requestRepository.findAllByClientId(1L, pageable);
List<String> category = requestRequest.getCategory();
boolean hasReview = requestRequest.isHasReview();
Specification<Request> spec = RequestSpecifications.findAllByClientIdAndCategory(1L, category, hasReview);

Slice<Request> requestSlice = requestRepository.findAll(spec, pageable);
List<RequestDTO> requestDTOList = requestSlice.getContent().stream()
.map(RequestDTO::fromRequest)
Expand Down Expand Up @@ -111,7 +116,6 @@ public void addRequest(Long translationId, List<MultipartFile> files) throws IOE
ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentLength(file.getSize());
metadata.setContentType(file.getContentType());

amazonS3.putObject(bucketName, fileName, file.getInputStream(), metadata);
requestPdfs.add(amazonS3.getUrl(bucketName, fileName).toString());
}
Expand All @@ -133,17 +137,12 @@ public void acceptRequest(Long requestId) {

@Transactional
public void completeRequest(Long requestId, List<MultipartFile> files) throws IOException {
Request request = requestRepository.findById(requestId).orElse(null);
assert request != null;
request.complete();

List<String> responsePdfs = new ArrayList<>();
for (MultipartFile file : files) {
String fileName = "request/response/" + UUID.randomUUID() + "-" + file.getOriginalFilename();
ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentLength(file.getSize());
metadata.setContentType(file.getContentType());

amazonS3.putObject(bucketName, fileName, file.getInputStream(), metadata);
responsePdfs.add(amazonS3.getUrl(bucketName, fileName).toString());
}
Expand Down

0 comments on commit e9dbf25

Please sign in to comment.