Skip to content

Commit

Permalink
페이징 기능 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
giwoong01 committed Sep 2, 2023
1 parent 0dae848 commit 896787f
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public List<MemberRankResDto> mainPageRanking() {
List<MemberRankResDto> 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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand All @@ -24,26 +25,26 @@ public ResponseEntity<DetailsNoticeResDto> detailsNoticeResponse(@RequestParam("
return new ResponseEntity<>(noticeService.detailsNoticeResponse(noticeId), HttpStatus.OK);
}

@GetMapping("/api/notice/list")
public ResponseEntity<List<NoticeListResDto>> noticeList() {
return new ResponseEntity<>(noticeService.noticeList(), HttpStatus.OK);
}

@GetMapping("/api/search-notice/list")
public ResponseEntity<List<NoticeListResDto>> noticeSearchList(@RequestParam("searchKeyword") String searchKeyword) {
List<NoticeListResDto> noticeSearchList;
public ResponseEntity<Page<NoticeListResDto>> noticeSearchList(@RequestParam(value = "searchKeyword", required = false) String searchKeyword,
@RequestParam(value = "page", defaultValue = "0") int page,
@RequestParam(value = "size", defaultValue = "10") int size) {

Page<NoticeListResDto> 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<List<NoticeListResDto>> myScrapList(@RequestParam("memberId") Long memberId) {
return new ResponseEntity<>(noticeService.myScrapNoticeList(memberId), HttpStatus.OK);
public ResponseEntity<Page<NoticeListResDto>> 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")
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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");
}

Expand All @@ -50,68 +60,55 @@ public DetailsNoticeResDto detailsNoticeResponse(Long noticeId) {
memberIdList);
}

public List<NoticeListResDto> noticeList() {
List<Notice> noticeList = noticeRepository.findAll(Sort.by(Sort.Direction.DESC, "noticeId"));
public Page<NoticeListResDto> noticeList(int page, int size) {
Page<Notice> noticeSearchPage = noticeRepository.findAll(PageRequest.of(page, size, Sort.by(Sort.Direction.DESC, "noticeId")));

return getNoticeListRes(noticeList);
return noticeSearchPage.map(this::mapToNotice);
}

public List<NoticeListResDto> noticeSearchList(String searchKeyword) {
List<Notice> noticeSearchList = noticeRepository.findByTitleContaining(searchKeyword, Sort.by(Sort.Direction.DESC, "noticeId"));
public Page<NoticeListResDto> noticeSearchList(String searchKeyword, int page, int size) {
Page<Notice> noticeSearchPage = noticeRepository.findByTitleContaining(searchKeyword, PageRequest.of(page, size, Sort.by(Sort.Direction.DESC, "noticeId")));

return getNoticeListRes(noticeSearchList);
return noticeSearchPage.map(this::mapToNotice);
}

private List<NoticeListResDto> getNoticeListRes(List<Notice> noticeList) {
private NoticeListResDto mapToNotice(Notice notice) {
updateLocalDate();
String SystemTime = now.format(formatter);

List<NoticeListResDto> 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<NoticeListResDto> myScrapNoticeList(Long memberId) {
public Page<NoticeListResDto> 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<MemberScrapNotice> myScrapNoticePage = memberScrapNoticeRepository.findByMyScrapNotice(member, PageRequest.of(page, size));

List<NoticeListResDto> 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");
}
Expand Down
29 changes: 22 additions & 7 deletions src/main/java/com/itcontest/skhuming/notice/domain/Notice.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,23 @@

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
@Table(name = "Notice")
@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")
Expand All @@ -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;
}
Expand All @@ -48,11 +63,11 @@ public List<Member> 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();
}
}

}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Notice, Long> {

// 게시글 검색기능 (findBy(컬럼이름)Containing -> 컬럼에서 키워드가 포함된 것을 찾겠다. 글자 한 글자만 검색해도 그 글자가 포함된 것은 다 나옴)
List<Notice> findByTitleContaining(String searchKeyword, Sort noticeId);
Page<Notice> findByTitleContaining(String searchKeyword, PageRequest noticeId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -19,7 +19,8 @@ public RankingController(RankingService rankingService) {
}

@GetMapping("/api/ranking/list")
public ResponseEntity<List<MemberRankResDto>> memberRankingList() {
return new ResponseEntity<>(rankingService.memberRanking(), HttpStatus.OK);
public ResponseEntity<Page<MemberRankResDto>> memberRankingList(@RequestParam(value = "page", defaultValue = "0") int page,
@RequestParam(value = "size", defaultValue = "10") int size) {
return new ResponseEntity<>(rankingService.memberRanking(page, size), HttpStatus.OK);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -20,19 +21,32 @@ public RankingService(MemberRepository memberRepository) {
this.memberRepository = memberRepository;
}

public List<MemberRankResDto> memberRanking() {
List<Member> memberList = memberRepository.findAll(Sort.by(Sort.Direction.DESC, "score"));

List<MemberRankResDto> 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<MemberRankResDto> memberRanking(int page, int size) {
Page<Member> 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<Member> 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;
}


}

0 comments on commit 896787f

Please sign in to comment.