From 5338d2d30792ef83dfc80c0fbcfbbf243caaf53b Mon Sep 17 00:00:00 2001 From: Sangyoo Date: Sat, 28 Jan 2023 17:55:50 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20responseDto=EC=97=90=20isVoted=20?= =?UTF-8?q?=EC=A0=81=EC=9A=A9=20#78?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../post/controller/PostController.java | 72 +++++++++++++++---- .../domain/post/dto/PostResponseDto.java | 1 + .../pikcha/domain/post/mapper/PostMapper.java | 22 ------ .../batch/RankingCalcConfiguration.java | 46 ++++++++---- 4 files changed, 92 insertions(+), 49 deletions(-) diff --git a/server/src/main/java/com/main36/pikcha/domain/post/controller/PostController.java b/server/src/main/java/com/main36/pikcha/domain/post/controller/PostController.java index ef8d3643..51e6f383 100644 --- a/server/src/main/java/com/main36/pikcha/domain/post/controller/PostController.java +++ b/server/src/main/java/com/main36/pikcha/domain/post/controller/PostController.java @@ -38,6 +38,7 @@ import java.io.IOException; import java.util.*; +import java.util.stream.Collectors; @RestController @Validated @@ -215,40 +216,40 @@ public ResponseEntity> getPost(@PathVariable("post-id") @Posi return ResponseEntity.ok(new DataResponseDto<>(response)); } - @GetMapping("/home") - public ResponseEntity> getHomePosts(@RequestParam(defaultValue = "newest", required = false) String sort, + @GetMapping(value = {"/home","/home/{member-id}"}) + public ResponseEntity> getHomePosts(@PathVariable("member-id") Optional memberId, + @RequestParam(defaultValue = "newest", required = false) String sort, @RequestParam(defaultValue = "1", required = false) @Positive int page, @RequestParam(defaultValue = "8", required = false) @Positive int size) { sort = getString(sort); Page allPostsBySort = postService.findAllPostsBySort(page - 1, size, sort); List content = allPostsBySort.getContent(); - return new ResponseEntity<>(new MultiResponseDto<>( - mapper.postListToPostHomeResponseDtoList(content), allPostsBySort), HttpStatus.OK); - + return responseMethod(content, allPostsBySort, memberId); } - @PostMapping("/filter") - public ResponseEntity> getFilteredPosts(@RequestParam(defaultValue = "newest", required = false) String sort, + @PostMapping(value= {"/filter", "/filter/{member-id}"}) + public ResponseEntity> getFilteredPosts(@PathVariable("member-id") Optional memberId, + @RequestParam(defaultValue = "newest", required = false) String sort, @RequestParam(defaultValue = "1", required = false) @Positive int page, @RequestParam(defaultValue = "9", required = false) @Positive int size, @RequestBody ProvinceFilterDto filterDto) { sort = getString(sort); - List posts; + List content; Page postPage; if(filterDto.getProvinces().size() == 0 ) { postPage = postService.findAllPostsBySort(page - 1, size, sort); }else{ postPage = postService.findAllPostsByProvincesSort(filterDto.getProvinces(), page - 1, size, sort); } - posts = postPage.getContent(); - return new ResponseEntity<>(new MultiResponseDto<>( - mapper.postListToPostHomeResponseDtoList(posts), postPage), HttpStatus.OK); + content = postPage.getContent(); + return responseMethod(content, postPage, memberId); } - @GetMapping("/{attraction-id}") + @GetMapping(value = {"/{attraction-id}","/{attraction-id}/{member-id}"}) public ResponseEntity> getPostsByAttractionDetailsPage(@PathVariable("attraction-id") long attractionId, + @PathVariable("member-id") Optional memberId, @RequestParam(defaultValue = "newest", required = false) String sort, @RequestParam(defaultValue = "1", required = false) @Positive int page, @RequestParam(defaultValue = "8", required = false) @Positive int size) { @@ -256,9 +257,7 @@ public ResponseEntity> getPostsByAttractionDetailsPage(@Path Page allPostsBySort = postService.findAllPostsByAttractionId(attractionId,page - 1, size, sort); List content = allPostsBySort.getContent(); - return new ResponseEntity<>(new MultiResponseDto<>( - mapper.postListToPostHomeResponseDtoList(content), allPostsBySort), HttpStatus.OK); - + return responseMethod(content, allPostsBySort, memberId); } @LoginUser @@ -318,4 +317,47 @@ private Post verifiedById(long clientId, long postId) { } return post; } + + private List loginMapping(List postList, long memberId){ + return postList.stream() + .map(post -> { + return PostResponseDto.Home.builder() + .postId(post.getPostId()) + .memberId(post.getMember().getMemberId()) + .username(post.getMember().getUsername()) + .memberPicture(post.getMember().getPicture()) + .pictureUrl(post.getPostImages().isEmpty() ? "" : post.getPostImages().get(0).getPostImageUrl()) + .views(post.getViews()) + .likes(post.getLikes()) + .isVoted(postService.isVoted(memberId, post.getPostId())) + .postTitle(post.getPostTitle()) + .createdAt(post.getCreatedAt()) + .modifiedAt(post.getModifiedAt()) + .build(); + }).collect(Collectors.toList()); + } + private List guestMapping(List postList){ + return postList.stream() + .map(post -> { + return PostResponseDto.Home.builder() + .postId(post.getPostId()) + .memberId(post.getMember().getMemberId()) + .username(post.getMember().getUsername()) + .memberPicture(post.getMember().getPicture()) + .pictureUrl(post.getPostImages().isEmpty() ? "" : post.getPostImages().get(0).getPostImageUrl()) + .views(post.getViews()) + .likes(post.getLikes()) + .isVoted(false) + .postTitle(post.getPostTitle()) + .createdAt(post.getCreatedAt()) + .modifiedAt(post.getModifiedAt()) + .build(); + }).collect(Collectors.toList()); + } + + private ResponseEntity> responseMethod(List content, Page page, Optional memberId){ + return memberId.>>map(aLong -> new ResponseEntity<>(new MultiResponseDto<>( + loginMapping(content, aLong), page), HttpStatus.OK)).orElseGet(() -> new ResponseEntity<>(new MultiResponseDto<>( + guestMapping(content), page), HttpStatus.OK)); + } } diff --git a/server/src/main/java/com/main36/pikcha/domain/post/dto/PostResponseDto.java b/server/src/main/java/com/main36/pikcha/domain/post/dto/PostResponseDto.java index 8b12881f..9e1bdcf9 100644 --- a/server/src/main/java/com/main36/pikcha/domain/post/dto/PostResponseDto.java +++ b/server/src/main/java/com/main36/pikcha/domain/post/dto/PostResponseDto.java @@ -23,6 +23,7 @@ public static class Home { private String pictureUrl; private int views; private int likes; + private Boolean isVoted; private String postTitle; private LocalDateTime createdAt; private LocalDateTime modifiedAt; diff --git a/server/src/main/java/com/main36/pikcha/domain/post/mapper/PostMapper.java b/server/src/main/java/com/main36/pikcha/domain/post/mapper/PostMapper.java index c77be9e0..50595fe0 100644 --- a/server/src/main/java/com/main36/pikcha/domain/post/mapper/PostMapper.java +++ b/server/src/main/java/com/main36/pikcha/domain/post/mapper/PostMapper.java @@ -54,26 +54,4 @@ default PostResponseDto.Detail postToPostDetailResponseDto(Post post) { .modifiedAt(post.getModifiedAt()) .build(); } - - default List postListToPostHomeResponseDtoList(List postList) { - if (postList == null) { - return null; - } - - return postList.stream() - .map(post -> { - return PostResponseDto.Home.builder() - .postId(post.getPostId()) - .memberId(post.getMember().getMemberId()) - .username(post.getMember().getUsername()) - .memberPicture(post.getMember().getPicture()) - .pictureUrl(post.getPostImages().isEmpty() ? "" : post.getPostImages().get(0).getPostImageUrl()) - .views(post.getViews()) - .likes(post.getLikes()) - .postTitle(post.getPostTitle()) - .createdAt(post.getCreatedAt()) - .modifiedAt(post.getModifiedAt()) - .build(); - }).collect(Collectors.toList()); - } } diff --git a/server/src/main/java/com/main36/pikcha/global/batch/RankingCalcConfiguration.java b/server/src/main/java/com/main36/pikcha/global/batch/RankingCalcConfiguration.java index fd776743..47b03668 100644 --- a/server/src/main/java/com/main36/pikcha/global/batch/RankingCalcConfiguration.java +++ b/server/src/main/java/com/main36/pikcha/global/batch/RankingCalcConfiguration.java @@ -1,6 +1,7 @@ /* package com.main36.pikcha.global.batch; +import com.main36.pikcha.domain.attraction.entity.Attraction; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.batch.core.Job; @@ -8,11 +9,18 @@ import org.springframework.batch.core.configuration.annotation.JobBuilderFactory; import org.springframework.batch.core.configuration.annotation.JobScope; import org.springframework.batch.core.configuration.annotation.StepBuilderFactory; +import org.springframework.batch.core.configuration.annotation.StepScope; +import org.springframework.batch.item.database.JpaPagingItemReader; +import org.springframework.batch.item.database.builder.JpaPagingItemReaderBuilder; import org.springframework.batch.repeat.RepeatStatus; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import javax.persistence.EntityManagerFactory; +import java.util.HashMap; +import java.util.Map; + @Slf4j @RequiredArgsConstructor @Configuration @@ -20,24 +28,38 @@ public class RankingCalcConfiguration { private final JobBuilderFactory jobBuilderFactory; private final StepBuilderFactory stepBuilderFactory; + private final EntityManagerFactory entityManagerFactory; @Bean - public Job simpleJob(){ - return jobBuilderFactory.get("simpleJob") - .start(simpleStep1(null)) - .next(simpleStep2(null)) - .build(); + public Job ExampleJob(){ + + Job exampleJob = jobBuilderFactory.get("exampleJob") + .start() } @Bean @JobScope - public Step simpleStep1(@Value("#{jobParameters[requestDate]}")String requestDate){ - return stepBuilderFactory.get("simpleJob") - .tasklet((contribution, chunkContext)-> { - log.info(">>>>> This is simpleStep 1"); - log.info(">>>>> requestDate = {}", requestDate); - return RepeatStatus.FINISHED; - }) + public Step step(*/ +/*@Value("#{jobParameters[requestDate]}")String requestDate*//* +){ + return stepBuilderFactory.get("step") + .chunk(10) + .reader(reader()) + } + + @Bean + @StepScope + public JpaPagingItemReader reader(){ + */ +/*Map parameterValues = new HashMap<>(); + parameterValues.put("numOfPosts" , 100);*//* + + + return new JpaPagingItemReaderBuilder() + .pageSize(10) + .queryString("SELECT p FROM ATTRACTION p ORDER BY likes DESC") + .entityManagerFactory(entityManagerFactory) + .name("JpaPagingItemREader") .build(); } @Bean