From 896787fab7ef046bbaaf485e97e916f411f90ee8 Mon Sep 17 00:00:00 2001 From: giwoong Date: Sat, 2 Sep 2023 23:32:53 +0900 Subject: [PATCH] =?UTF-8?q?=ED=8E=98=EC=9D=B4=EC=A7=95=20=EA=B8=B0?= =?UTF-8?q?=EB=8A=A5=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/application/MainService.java | 2 +- .../skhuming/notice/api/NoticeController.java | 25 +++--- .../notice/application/NoticeService.java | 77 +++++++++---------- .../skhuming/notice/domain/Notice.java | 29 +++++-- .../domain/repository/NoticeRepository.java | 7 +- .../ranking/api/RankingController.java | 9 ++- .../ranking/application/RankingService.java | 40 ++++++---- 7 files changed, 108 insertions(+), 81 deletions(-) diff --git a/src/main/java/com/itcontest/skhuming/main/application/MainService.java b/src/main/java/com/itcontest/skhuming/main/application/MainService.java index c751c8b..8e26ee7 100644 --- a/src/main/java/com/itcontest/skhuming/main/application/MainService.java +++ b/src/main/java/com/itcontest/skhuming/main/application/MainService.java @@ -23,7 +23,7 @@ public List mainPageRanking() { List memberRankingList = new ArrayList<>(); for (int i = 0; i < Math.min(3, memberList.size()); i++) { Member member = memberList.get(i); - MemberRankResDto memberRankResDto = new MemberRankResDto(member.getMemberId(), member.getTier(), member.getScore(), member.getNickname(), member.getDepartment()); + MemberRankResDto memberRankResDto = new MemberRankResDto(member.getMemberId(), member.getTier(), member.getScore(), member.getNickname(), member.getDepartment(), 0); memberRankingList.add(memberRankResDto); } diff --git a/src/main/java/com/itcontest/skhuming/notice/api/NoticeController.java b/src/main/java/com/itcontest/skhuming/notice/api/NoticeController.java index 017cfb0..ee06b93 100644 --- a/src/main/java/com/itcontest/skhuming/notice/api/NoticeController.java +++ b/src/main/java/com/itcontest/skhuming/notice/api/NoticeController.java @@ -3,6 +3,7 @@ import com.itcontest.skhuming.notice.api.dto.response.DetailsNoticeResDto; import com.itcontest.skhuming.notice.api.dto.response.NoticeListResDto; import com.itcontest.skhuming.notice.application.NoticeService; +import org.springframework.data.domain.Page; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; @@ -24,26 +25,26 @@ public ResponseEntity detailsNoticeResponse(@RequestParam(" return new ResponseEntity<>(noticeService.detailsNoticeResponse(noticeId), HttpStatus.OK); } - @GetMapping("/api/notice/list") - public ResponseEntity> noticeList() { - return new ResponseEntity<>(noticeService.noticeList(), HttpStatus.OK); - } - @GetMapping("/api/search-notice/list") - public ResponseEntity> noticeSearchList(@RequestParam("searchKeyword") String searchKeyword) { - List noticeSearchList; + public ResponseEntity> noticeSearchList(@RequestParam(value = "searchKeyword", required = false) String searchKeyword, + @RequestParam(value = "page", defaultValue = "0") int page, + @RequestParam(value = "size", defaultValue = "10") int size) { + + Page noticeSearchPage; if (searchKeyword == null) { - noticeSearchList = noticeService.noticeList(); + noticeSearchPage = noticeService.noticeList(page, size); } else { - noticeSearchList = noticeService.noticeSearchList(searchKeyword); + noticeSearchPage = noticeService.noticeSearchList(searchKeyword, page, size); } - return new ResponseEntity<>(noticeSearchList, HttpStatus.OK); + return new ResponseEntity<>(noticeSearchPage, HttpStatus.OK); } @GetMapping("/user/api/scrap/list") - public ResponseEntity> myScrapList(@RequestParam("memberId") Long memberId) { - return new ResponseEntity<>(noticeService.myScrapNoticeList(memberId), HttpStatus.OK); + public ResponseEntity> myScrapList(@RequestParam("memberId") Long memberId, + @RequestParam(value = "page", defaultValue = "0") int page, + @RequestParam(value = "size", defaultValue = "10") int size) { + return new ResponseEntity<>(noticeService.myScrapNoticeList(memberId, page, size), HttpStatus.OK); } @PostMapping("/user/api/notice/scrap") diff --git a/src/main/java/com/itcontest/skhuming/notice/application/NoticeService.java b/src/main/java/com/itcontest/skhuming/notice/application/NoticeService.java index 0b90847..85bc553 100644 --- a/src/main/java/com/itcontest/skhuming/notice/application/NoticeService.java +++ b/src/main/java/com/itcontest/skhuming/notice/application/NoticeService.java @@ -1,14 +1,19 @@ package com.itcontest.skhuming.notice.application; -import com.itcontest.skhuming.jwt.SecurityUtil; +import com.itcontest.skhuming.global.util.SecurityUtil; import com.itcontest.skhuming.member.domain.Member; +import com.itcontest.skhuming.member.domain.MemberScrapNotice; import com.itcontest.skhuming.member.domain.repository.MemberRepository; +import com.itcontest.skhuming.member.domain.repository.MemberScrapNoticeRepository; import com.itcontest.skhuming.member.exception.NotFoundMemberException; import com.itcontest.skhuming.notice.api.dto.response.DetailsNoticeResDto; import com.itcontest.skhuming.notice.api.dto.response.NoticeListResDto; import com.itcontest.skhuming.notice.domain.Notice; import com.itcontest.skhuming.notice.domain.repository.NoticeRepository; import com.itcontest.skhuming.notice.exception.NotFoundNoticeException; +import lombok.extern.slf4j.Slf4j; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Sort; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -19,18 +24,23 @@ import java.util.List; @Service +@Slf4j @Transactional public class NoticeService { + private static final String TILDE = "~"; + private LocalDate now; private DateTimeFormatter formatter; private final NoticeRepository noticeRepository; private final MemberRepository memberRepository; + private final MemberScrapNoticeRepository memberScrapNoticeRepository; - public NoticeService(NoticeRepository noticeRepository, MemberRepository memberRepository) { + public NoticeService(NoticeRepository noticeRepository, MemberRepository memberRepository, MemberScrapNoticeRepository memberScrapNoticeRepository) { this.noticeRepository = noticeRepository; this.memberRepository = memberRepository; + this.memberScrapNoticeRepository = memberScrapNoticeRepository; formatter = DateTimeFormatter.ofPattern("MMdd"); } @@ -50,68 +60,55 @@ public DetailsNoticeResDto detailsNoticeResponse(Long noticeId) { memberIdList); } - public List noticeList() { - List noticeList = noticeRepository.findAll(Sort.by(Sort.Direction.DESC, "noticeId")); + public Page noticeList(int page, int size) { + Page noticeSearchPage = noticeRepository.findAll(PageRequest.of(page, size, Sort.by(Sort.Direction.DESC, "noticeId"))); - return getNoticeListRes(noticeList); + return noticeSearchPage.map(this::mapToNotice); } - public List noticeSearchList(String searchKeyword) { - List noticeSearchList = noticeRepository.findByTitleContaining(searchKeyword, Sort.by(Sort.Direction.DESC, "noticeId")); + public Page noticeSearchList(String searchKeyword, int page, int size) { + Page noticeSearchPage = noticeRepository.findByTitleContaining(searchKeyword, PageRequest.of(page, size, Sort.by(Sort.Direction.DESC, "noticeId"))); - return getNoticeListRes(noticeSearchList); + return noticeSearchPage.map(this::mapToNotice); } - private List getNoticeListRes(List noticeList) { + private NoticeListResDto mapToNotice(Notice notice) { updateLocalDate(); - String SystemTime = now.format(formatter); - - List noticeListResDtoList = new ArrayList<>(); - for (Notice notice : noticeList) { - boolean end = false; - String[] splitDate = notice.getSchedule().split("~"); - String date = getToStringDate(splitDate[1]); + String systemTime = now.format(formatter); - if (Integer.parseInt(date) < Integer.parseInt(SystemTime)) { - end = true; - } + String[] splitDate = notice.getSchedule().split(TILDE); + String date = getToStringDate(splitDate[1]); - NoticeListResDto noticeListResDto = new NoticeListResDto(notice.getNoticeId(), notice.getTitle(), end); - noticeListResDtoList.add(noticeListResDto); - } + boolean end = Integer.parseInt(date) < Integer.parseInt(systemTime); - return noticeListResDtoList; + return new NoticeListResDto(notice.getNoticeId(), notice.getTitle(), end); } - public List myScrapNoticeList(Long memberId) { + public Page myScrapNoticeList(Long memberId, int page, int size) { SecurityUtil.memberTokenMatch(memberId); - Member member = memberRepository.findById(memberId).orElseThrow(NotFoundMemberException::new); - updateLocalDate(); - String SystemTime = now.format(formatter); + Page myScrapNoticePage = memberScrapNoticeRepository.findByMyScrapNotice(member, PageRequest.of(page, size)); - List noticeListResDtoList = new ArrayList<>(); - for (Notice notice : member.getScrapNotices()) { - boolean end = false; - String[] splitDate = notice.getSchedule().split("~"); - String date = getToStringDate(splitDate[1]); + return myScrapNoticePage.map(this::mapToMyScrapNotice); + } - if (Integer.parseInt(date) < Integer.parseInt(SystemTime)) { - end = true; - } + private NoticeListResDto mapToMyScrapNotice(MemberScrapNotice MemberScrapNotice) { + updateLocalDate(); + String systemTime = now.format(formatter); - NoticeListResDto noticeListResDto = new NoticeListResDto(notice.getNoticeId(), notice.getTitle(), end); - noticeListResDtoList.add(noticeListResDto); - } + String[] splitDate = MemberScrapNotice.getNotice().getSchedule().split(TILDE); + String date = getToStringDate(splitDate[1]); + + boolean end = Integer.parseInt(date) < Integer.parseInt(systemTime); - return noticeListResDtoList; + return new NoticeListResDto(MemberScrapNotice.getNotice().getNoticeId(), MemberScrapNotice.getNotice().getTitle(), end); } private String getToStringDate(String schedule) { StringBuilder sb = new StringBuilder(); try { - for (String date : schedule.split("/")) { + for (String date : schedule.substring(5).split("-")) { if (Integer.parseInt(date) < 10) { sb.append("0"); } diff --git a/src/main/java/com/itcontest/skhuming/notice/domain/Notice.java b/src/main/java/com/itcontest/skhuming/notice/domain/Notice.java index da7509d..913a4ee 100644 --- a/src/main/java/com/itcontest/skhuming/notice/domain/Notice.java +++ b/src/main/java/com/itcontest/skhuming/notice/domain/Notice.java @@ -2,12 +2,14 @@ import com.itcontest.skhuming.member.domain.Member; import com.itcontest.skhuming.member.domain.MemberScrapNotice; +import com.itcontest.skhuming.notice.exception.NotScheduleFormatException; import lombok.*; import javax.persistence.*; import java.util.ArrayList; import java.util.List; -import java.util.Objects; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import java.util.stream.Collectors; @Entity @@ -15,6 +17,8 @@ @Getter public class Notice { + private static final Pattern SCHEDULE_PATTERN = Pattern.compile("\\d{4}-\\d{1,2}-\\d{1,2}~\\d{4}-\\d{1,2}-\\d{1,2}"); + @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "notice_id") @@ -36,8 +40,19 @@ protected Notice() { } public Notice(String title, String schedule, String contents, int mileageScore) { + notScheduleFormat(schedule); + + this.title = title; + this.schedule = schedule; + this.contents = contents; + this.mileageScore = mileageScore; + } + + public void update(String title, String schedule, String contents, int mileageScore) { + notScheduleFormat(schedule); + this.title = title; - this.schedule = Objects.requireNonNullElse(schedule, "1/1"); + this.schedule = schedule; this.contents = contents; this.mileageScore = mileageScore; } @@ -48,11 +63,11 @@ public List getScrapMember() { .collect(Collectors.toList()); } - public void update(String title, String schedule, String contents, int mileageScore) { - this.title = title; - this.schedule = schedule; - this.contents = contents; - this.mileageScore = mileageScore; + public void notScheduleFormat(String schedule) { + Matcher matcher = SCHEDULE_PATTERN.matcher(schedule); + if (!matcher.matches()) { + throw new NotScheduleFormatException(); + } } } diff --git a/src/main/java/com/itcontest/skhuming/notice/domain/repository/NoticeRepository.java b/src/main/java/com/itcontest/skhuming/notice/domain/repository/NoticeRepository.java index 0522b98..d503a06 100644 --- a/src/main/java/com/itcontest/skhuming/notice/domain/repository/NoticeRepository.java +++ b/src/main/java/com/itcontest/skhuming/notice/domain/repository/NoticeRepository.java @@ -1,15 +1,14 @@ package com.itcontest.skhuming.notice.domain.repository; import com.itcontest.skhuming.notice.domain.Notice; -import org.springframework.data.domain.Sort; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; -import java.util.List; - @Repository public interface NoticeRepository extends JpaRepository { // 게시글 검색기능 (findBy(컬럼이름)Containing -> 컬럼에서 키워드가 포함된 것을 찾겠다. 글자 한 글자만 검색해도 그 글자가 포함된 것은 다 나옴) - List findByTitleContaining(String searchKeyword, Sort noticeId); + Page findByTitleContaining(String searchKeyword, PageRequest noticeId); } diff --git a/src/main/java/com/itcontest/skhuming/ranking/api/RankingController.java b/src/main/java/com/itcontest/skhuming/ranking/api/RankingController.java index 117ed13..2803f40 100644 --- a/src/main/java/com/itcontest/skhuming/ranking/api/RankingController.java +++ b/src/main/java/com/itcontest/skhuming/ranking/api/RankingController.java @@ -2,12 +2,12 @@ import com.itcontest.skhuming.member.api.dto.response.MemberRankResDto; import com.itcontest.skhuming.ranking.application.RankingService; +import org.springframework.data.domain.Page; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; - -import java.util.List; +import org.springframework.web.bind.annotation.RequestParam; @Controller public class RankingController { @@ -19,7 +19,8 @@ public RankingController(RankingService rankingService) { } @GetMapping("/api/ranking/list") - public ResponseEntity> memberRankingList() { - return new ResponseEntity<>(rankingService.memberRanking(), HttpStatus.OK); + public ResponseEntity> memberRankingList(@RequestParam(value = "page", defaultValue = "0") int page, + @RequestParam(value = "size", defaultValue = "10") int size) { + return new ResponseEntity<>(rankingService.memberRanking(page, size), HttpStatus.OK); } } diff --git a/src/main/java/com/itcontest/skhuming/ranking/application/RankingService.java b/src/main/java/com/itcontest/skhuming/ranking/application/RankingService.java index 38275ab..e1c9830 100644 --- a/src/main/java/com/itcontest/skhuming/ranking/application/RankingService.java +++ b/src/main/java/com/itcontest/skhuming/ranking/application/RankingService.java @@ -3,11 +3,12 @@ import com.itcontest.skhuming.member.api.dto.response.MemberRankResDto; import com.itcontest.skhuming.member.domain.Member; import com.itcontest.skhuming.member.domain.repository.MemberRepository; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Sort; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; -import java.util.ArrayList; import java.util.List; @Service @@ -20,19 +21,32 @@ public RankingService(MemberRepository memberRepository) { this.memberRepository = memberRepository; } - public List memberRanking() { - List memberList = memberRepository.findAll(Sort.by(Sort.Direction.DESC, "score")); - - List memberRankingList = new ArrayList<>(); - for (Member member : memberList) { - MemberRankResDto memberRankResDto = new MemberRankResDto(member.getMemberId(), - member.getTier(), - member.getScore(), - member.getNickname(), - member.getDepartment()); - memberRankingList.add(memberRankResDto); + public Page memberRanking(int page, int size) { + Page members = memberRepository.findAll(PageRequest.of(page, size, Sort.by(Sort.Direction.DESC, "score"))); + + return members.map(this::mapToMember); + } + + private MemberRankResDto mapToMember(Member member) { + return new MemberRankResDto(member.getMemberId(), + member.getTier(), + member.getScore(), + member.getNickname(), + member.getDepartment(), + myRanking(member.getScore())); + } + + private int myRanking(int score) { + List rankedMembers = memberRepository.findAll(Sort.by(Sort.Direction.DESC, "score")); + + for (int i = 0; i < rankedMembers.size(); i++) { + if (rankedMembers.get(i).getScore() == score) { + return i + 1; + } } - return memberRankingList; + return -1; } + + }