Skip to content

Commit

Permalink
Merge pull request #13 from wanted-fork-fork/paging
Browse files Browse the repository at this point in the history
fix: paging 역순으로 나오게 수정
  • Loading branch information
HyoTaek-Jang authored Nov 16, 2021
2 parents 92e1a7e + f99f256 commit 0f4418b
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 84 deletions.
5 changes: 5 additions & 0 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
## 제목

## 작업 내용

## 주의 사항
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

Expand All @@ -34,38 +35,18 @@ public ResponseEntity<Object> findAllStudy(HttpServletRequest request, @RequestP
String email = jwtTokenUtil.getSubject(request);
User userByEmail = userService.findUserByEmail(email);

PageRequest pageRequest = PageRequest.of(page, 20);
PageRequest pageRequest = PageRequest.of(page, 20, Sort.by("id").descending());
Page<Study> studySlice = studyService.findStudyAll(pageRequest);

List<PagingResponse> pagingResponseList = new LinkedList<>();
for (Study study:
studySlice) {
// 1. 시간지남, 2. 취소됨 3. 조기마감
if ( !study.getUser().getUniversity().getName().equals(userByEmail.getUniversity().getName())) {
if ( study.getCancellation() || !study.getUser().getUniversity().getName().equals(userByEmail.getUniversity().getName())) {
continue;
}

List<StudyCategory> studyCategories = studyCategoryService.findStudyCategoryByStudy(study);
List<String> give = new LinkedList<>();
List<String> take = new LinkedList<>();
String img = null;

for (StudyCategory studyCategory:
studyCategories) {
if (studyCategory.getType() == 1) {
//give
give.add(studyCategory.getCategory().getName());
} else {
take.add(studyCategory.getCategory().getName());
img = studyCategory.getCategory().getImg();
}
}

List<StudyFile> studyFileByStudy = studyFileService.findStudyFileByStudy(study);
if (!studyFileByStudy.isEmpty()) {
img = s3Service.getFileUrl(studyFileByStudy.get(0).getFile());
}
PagingResponse pagingResponse = new PagingResponse(study, img, give, take);
PagingResponse pagingResponse = studyService.makePagingResponseByStudy(study);
pagingResponseList.add(pagingResponse);
}

Expand All @@ -81,39 +62,18 @@ public ResponseEntity<Object> findFilterStudy(HttpServletRequest request, @Reque
String email = jwtTokenUtil.getSubject(request);
User userByEmail = userService.findUserByEmail(email);

PageRequest pageRequest = PageRequest.of(page, 20);
PageRequest pageRequest = PageRequest.of(page, 20, Sort.by("id").descending());
Page<StudyCategory> studyCategoryByCategory = studyCategoryService.findStudyByfiltering(give, take, pageRequest);

List<PagingResponse> pagingResponseList = new LinkedList<>();
for (StudyCategory curStudy:
studyCategoryByCategory) {
Study study = curStudy.getStudy();

if ( study.getType() != type || !study.getUser().getUniversity().getName().equals(userByEmail.getUniversity().getName())) {
if ( study.getCancellation() || study.getType() != type || !study.getUser().getUniversity().getName().equals(userByEmail.getUniversity().getName())) {
continue;
}

List<StudyCategory> studyCategories = studyCategoryService.findStudyCategoryByStudy(study);
List<String> giveCategory = new LinkedList<>();
List<String> takeCategory = new LinkedList<>();
String img = null;

for (StudyCategory studyCategory:
studyCategories) {
if (studyCategory.getType() == 1) {
//give
giveCategory.add(studyCategory.getCategory().getName());
} else {
takeCategory.add(studyCategory.getCategory().getName());
img = studyCategory.getCategory().getImg();
}
}

List<StudyFile> studyFileByStudy = studyFileService.findStudyFileByStudy(study);
if (!studyFileByStudy.isEmpty()) {
img = s3Service.getFileUrl(studyFileByStudy.get(0).getFile());
}
PagingResponse pagingResponse = new PagingResponse(study, img, giveCategory, takeCategory);
PagingResponse pagingResponse = studyService.makePagingResponseByStudy(study);
pagingResponseList.add(pagingResponse);
}

Expand All @@ -124,13 +84,4 @@ public ResponseEntity<Object> findFilterStudy(HttpServletRequest request, @Reque
return ResFormat.response(true, 200, res);
}

@PostMapping("/test")
public String test() {
for (int i = 0; i < 50; i++) {
Study build = Study.builder().title(i + "번째 테스트 글").build();
studyService.saveStudy(build);
}

return "good";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,20 +90,7 @@ public ResponseEntity<Object> findStudyDetail(HttpServletRequest request, @Reque
profileUrl = s3Service.getFileUrl(user.getImg());
}

Double rate = null;
List<Review> allReviewByToUser = reviewService.findAllReviewByToUser(user);

Long cnt = 0L;
Double point = 0D;
for (Review review :
allReviewByToUser) {
cnt += 1;
point += review.getReviewPoint();
}

if (cnt >= 5) {
rate = point / cnt;
}
Double rate = reviewService.findUserRate(user);

UserInformationDto userInformation = new UserInformationDto(user, profileUrl, rate);

Expand Down Expand Up @@ -257,20 +244,8 @@ public ResponseEntity<Object> findOtherStudy(HttpServletRequest request) {
if (ownerUser.getImg() != null) {
profileUrl = s3Service.getFileUrl(ownerUser.getImg());
}
Double rate = null;
List<Review> allReviewByToUser = reviewService.findAllReviewByToUser(ownerUser);

Long cnt = 0L;
Double point = 0D;
for (Review review :
allReviewByToUser) {
cnt += 1;
point += review.getReviewPoint();
}

if (cnt >= 5) {
rate = point / cnt;
}
Double rate = reviewService.findUserRate(ownerUser);

UserInformationDto userInformationDto = new UserInformationDto(ownerUser, profileUrl, rate);

Expand Down
18 changes: 18 additions & 0 deletions src/main/java/com/forkfork/cake/service/ReviewService.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,22 @@ public class ReviewService {
public List<Review> findAllReviewByToUser(User user) {
return reviewRepository.findAllByToUser(user);
}

public Double findUserRate(User user) {
Double rate = null;
List<Review> allByToUser = reviewRepository.findAllByToUser(user);
Long cnt = 0L;
Double point = 0D;
for (Review review :
allByToUser) {
cnt += 1;
point += review.getReviewPoint();
}

if (cnt >= 5) {
rate = point / cnt;
}

return rate;
}
}
34 changes: 34 additions & 0 deletions src/main/java/com/forkfork/cake/service/StudyService.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
package com.forkfork.cake.service;

import com.forkfork.cake.domain.Study;
import com.forkfork.cake.domain.StudyCategory;
import com.forkfork.cake.domain.StudyFile;
import com.forkfork.cake.dto.paging.response.PagingResponse;
import com.forkfork.cake.repository.StudyRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Slice;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;

@Service
@RequiredArgsConstructor
public class StudyService {

private final StudyRepository studyRepository;
private final StudyCategoryService studyCategoryService;
private final StudyFileService studyFileService;
private final S3Service s3Service;

public Study saveStudy(Study study) {
return studyRepository.save(study);
Expand All @@ -25,4 +34,29 @@ public Page<Study> findStudyAll(PageRequest pageRequest) {
public Study findStudyById(Long id) {
return studyRepository.findById(id).orElse(null);
}

public PagingResponse makePagingResponseByStudy(Study study) {
List<StudyCategory> studyCategories = studyCategoryService.findStudyCategoryByStudy(study);
List<String> give = new LinkedList<>();
List<String> take = new LinkedList<>();
String img = null;

for (StudyCategory studyCategory:
studyCategories) {
if (studyCategory.getType() == 1) {
//give
give.add(studyCategory.getCategory().getName());
} else {
take.add(studyCategory.getCategory().getName());
img = studyCategory.getCategory().getImg();
}
}

List<StudyFile> studyFileByStudy = studyFileService.findStudyFileByStudy(study);
if (!studyFileByStudy.isEmpty()) {
img = s3Service.getFileUrl(studyFileByStudy.get(0).getFile());
}

return new PagingResponse(study, img, give, take);
}
}

0 comments on commit 0f4418b

Please sign in to comment.