Skip to content

Commit

Permalink
Merge pull request #5 from giwoong01/feature/ranking-refactor
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
giwoong01 authored Sep 8, 2023
2 parents 4df037a + 601fc30 commit c26aba9
Show file tree
Hide file tree
Showing 10 changed files with 238 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
package com.itcontest.skhuming.error;
package com.itcontest.skhuming.global.error;

import com.itcontest.skhuming.email.exception.InvalidEmailAddressException;
import com.itcontest.skhuming.error.dto.ErrorResponse;
import com.itcontest.skhuming.jwt.exception.NotMatchTokenException;
import com.itcontest.skhuming.global.error.dto.ErrorResponse;
import com.itcontest.skhuming.global.jwt.exception.NotMatchTokenException;
import com.itcontest.skhuming.member.exception.InvalidMemberException;
import com.itcontest.skhuming.member.exception.NotFoundMemberException;
import com.itcontest.skhuming.mileage.exception.ExistsMemberHistoryMileageException;
import com.itcontest.skhuming.mileage.exception.NotFoundMileageException;
import com.itcontest.skhuming.notice.exception.NotFoundNoticeException;
import com.itcontest.skhuming.notice.exception.NotScheduleFormatException;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.AuthenticationException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RestControllerAdvice
public class ControllerAdvice {

Expand All @@ -25,10 +22,11 @@ public class ControllerAdvice {
NotFoundMemberException.class,
NotFoundNoticeException.class,
NotFoundMileageException.class,
NotScheduleFormatException.class,
NotMatchTokenException.class,
ExistsMemberHistoryMileageException.class
})
public ResponseEntity<ErrorResponse> handleInvalidate(RuntimeException e) {
public ResponseEntity<ErrorResponse> handleInvalidData(RuntimeException e) {
ErrorResponse errorResponse = new ErrorResponse(e.getMessage());
return ResponseEntity.badRequest().body(errorResponse);
}
Expand Down
17 changes: 15 additions & 2 deletions src/main/java/com/itcontest/skhuming/main/api/MainController.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.itcontest.skhuming.main.api;

import com.itcontest.skhuming.main.application.MainService;
import com.itcontest.skhuming.member.api.dto.response.DepartmentRankResDto;
import com.itcontest.skhuming.member.api.dto.response.MemberRankResDto;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
Expand All @@ -19,8 +21,19 @@ public MainController(MainService mainService) {
}

@GetMapping("/api/main")
public ResponseEntity<List<MemberRankResDto>> mainRankingList() {
return new ResponseEntity<>(mainService.mainPageRanking(), HttpStatus.OK);
public ResponseEntity<List<MemberRankResDto>> mainRankingList(@RequestParam("departmentNumber") int departmentNumber) {
List<MemberRankResDto> mainPageRanking;
if (departmentNumber == 0) {
mainPageRanking = mainService.mainAllRanking();
} else {
mainPageRanking = mainService.mainDepartmentRanking(departmentNumber);
}

return new ResponseEntity<>(mainPageRanking, HttpStatus.OK);
}

@GetMapping("/api/main/department")
public ResponseEntity<List<DepartmentRankResDto>> departmentByRankingList() {
return new ResponseEntity<>(mainService.departmentByRanking(), HttpStatus.OK);
}
}
Original file line number Diff line number Diff line change
@@ -1,33 +1,107 @@
package com.itcontest.skhuming.main.application;

import com.itcontest.skhuming.global.util.ChangeDepartmentUtil;
import com.itcontest.skhuming.member.api.dto.response.DepartmentRankResDto;
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.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Service
@Transactional
public class MainService {
private final MemberRepository memberRepository;

public MainService(MemberRepository memberRepository) {
this.memberRepository = memberRepository;
}

public List<MemberRankResDto> mainPageRanking() {
List<Member> memberList = memberRepository.findAll(Sort.by(Sort.Direction.DESC, "score"));
/**
* 메인페이지 전체 중 3명 랭킹
*/
public List<MemberRankResDto> mainAllRanking() {
List<Member> memberList = memberRepository.findAll(
Sort.by(Sort.Direction.DESC, "score"));

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(), 0);
MemberRankResDto memberRankResDto = new MemberRankResDto(
member.getMemberId(),
member.getTier(),
member.getScore(),
member.getNickname(),
member.getDepartment(),
0);
memberRankingList.add(memberRankResDto);
}

return memberRankingList;
}

/**
* 메인페이지 학부별 3명 랭킹
*/
public List<MemberRankResDto> mainDepartmentRanking(int departmentNumber) {
List<Member> memberList = memberRepository.findByDepartment(
ChangeDepartmentUtil.departmentNumber(departmentNumber), Sort.by(Sort.Direction.DESC, "score"));

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(),
0);

memberRankingList.add(memberRankResDto);
}

return memberRankingList;
}

/**
* 학부별 총합 점수 랭킹
*/
public List<DepartmentRankResDto> departmentByRanking() {
HashMap<Integer, Integer> scoreMap = new HashMap<>();
for (int i = 1; i < 5; i++) {
int sum = 0;
List<Member> memberList = memberRepository.findByDepartment(
ChangeDepartmentUtil.departmentNumber(i), Sort.by(Sort.Direction.DESC, "score"));
for (Member member : memberList) {
sum += member.getScore();
}

scoreMap.put(i, sum);
}

List<Integer> scoreList = new ArrayList<>(scoreMap.keySet());
scoreList.sort((o1, o2) -> scoreMap.get(o2).compareTo(scoreMap.get(o1)));

Long idx = 1L;
List<DepartmentRankResDto> departmentRankList = new ArrayList<>();
for (Integer i : scoreList) {
DepartmentRankResDto departmentRankResDto = new DepartmentRankResDto(
idx,
ChangeDepartmentUtil.departmentNumber(i),
scoreMap.get(i)
);

idx++;
departmentRankList.add(departmentRankResDto);
}

return departmentRankList;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ public MileageController(MileageService mileageService) {
this.mileageService = mileageService;
}


@GetMapping("/api/display-board")
public ResponseEntity<List<String>> displayBoardList() {
return new ResponseEntity<>(mileageService.messageListResponse(), HttpStatus.OK);
}

@GetMapping("/api/mileage/select-box")
public ResponseEntity<List<MileageResDto>> mileageSelectBoxResponse() {
return new ResponseEntity<>(mileageService.mileageSelectBoxResponse(), HttpStatus.OK);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@ public class MileageResDto {

private int mileageScore;

public MileageResDto(Long mileageId, String title, int mileageScore) {
private String endSchedule;

public MileageResDto(Long mileageId, String title, int mileageScore, String endSchedule) {
this.mileageId = mileageId;
this.title = title;
this.mileageScore = mileageScore;
this.endSchedule = endSchedule;
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package com.itcontest.skhuming.mileage.application;

import com.itcontest.skhuming.jwt.SecurityUtil;
import com.itcontest.skhuming.global.util.SecurityUtil;
import com.itcontest.skhuming.member.domain.MemberHistoryMileage;
import com.itcontest.skhuming.member.domain.repository.MemberHistoryMileageRepository;
import com.itcontest.skhuming.member.exception.InvalidMemberException;
import com.itcontest.skhuming.member.exception.NotFoundMemberException;
import com.itcontest.skhuming.mileage.api.response.MemberMileageResDto;
import com.itcontest.skhuming.member.domain.Member;
Expand All @@ -15,17 +14,23 @@
import com.itcontest.skhuming.mileage.domain.repository.MileageRepository;
import com.itcontest.skhuming.mileage.exception.ExistsMemberHistoryMileageException;
import com.itcontest.skhuming.mileage.exception.NotFoundMileageException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
@Transactional
public class MileageService {

private final List<String> messageList = new ArrayList<>();

private final MemberRepository memberRepository;
private final MileageRepository mileageRepository;
private final MemberHistoryMileageRepository memberHistoryMileageRepository;
Expand All @@ -36,18 +41,44 @@ public MileageService(MemberRepository memberRepository, MileageRepository milea
this.memberHistoryMileageRepository = memberHistoryMileageRepository;
}

/**
* 디스플레이 보드 (전광판)
*/
public List<String> messageListResponse() {
if (messageList.size() > 3) {
messageList.remove(0);
}

return messageList;
}

/**
* 마일리지 셀렉트 박스
*/
public List<MileageResDto> mileageSelectBoxResponse() {
List<Mileage> mileageList = mileageRepository.findAll();
final LocalDate now = LocalDate.now();

List<MileageResDto> mileageResDtoList = new ArrayList<>();
for (Mileage mileage : mileageList) {
MileageResDto mileageResDto = new MileageResDto(mileage.getMileageId(), mileage.getTitle(), mileage.getMileageScore());
mileageResDtoList.add(mileageResDto);
MileageResDto mileageResDto = new MileageResDto(
mileage.getMileageId(),
mileage.getTitle(),
mileage.getMileageScore(),
mileage.getEndSchedule());
long daysDiff = ChronoUnit.DAYS.between(now, LocalDate.parse(mileage.getEndSchedule()));

if (mileage.getMileageId() == 1 || daysDiff <= 14) {
mileageResDtoList.add(mileageResDto);
}
}

return mileageResDtoList;
}

/**
* 마일리지 추가
*/
public void memberMileageRequest(MemberMileageReqDto memberMileageReqDto) {
SecurityUtil.memberTokenMatch(memberMileageReqDto.getMemberId());

Expand All @@ -57,9 +88,42 @@ public void memberMileageRequest(MemberMileageReqDto memberMileageReqDto) {
validateDuplicateMemberHistoryMileage(member, mileage);
String systemDate = String.valueOf(LocalDate.now());

int historyRanking = myRanking(member.getScore());
member.plusMyScore(mileage.getMileageScore());
int curRanking = myRanking(member.getScore());

member.addMileageHistory(mileage, systemDate);
memberRepository.save(member);

// 추월 메시지 리스트 추가
this.messageList.add(String.valueOf(message(member, historyRanking, curRanking)));
}

/**
* 추월 메시지 생성
*/
private StringBuilder message(Member member, int historyRanking, int curRanking) {
List<Member> memberList = memberRepository.findAll(
Sort.by(Sort.Direction.DESC, "score"));
StringBuilder message = new StringBuilder();

for (int i = 0; i < memberList.size(); i++) {
if (memberList.get(i).getNickname().equals(member.getNickname())) {
Member downMember = memberList.get(i + 1);

if (historyRanking > curRanking) {
return message.append(member.getNickname())
.append("님이 ")
.append(downMember.getNickname())
.append("님을 추월했습니다! (")
.append(historyRanking)
.append("등 → ")
.append(curRanking)
.append("등)🎉ㅤㅤㅤㅤㅤ🎉");
}
}
}
return message;
}

private void validateDuplicateMemberHistoryMileage(Member member, Mileage mileage) {
Expand All @@ -68,6 +132,22 @@ private void validateDuplicateMemberHistoryMileage(Member member, Mileage mileag
}
}

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 -1;
}

/**
* 마일리지 추가한 것 취소
*/
public void mileageHistoryCancel(Long memberId, Long mileageId) {
SecurityUtil.memberTokenMatch(memberId);

Expand All @@ -79,6 +159,9 @@ public void mileageHistoryCancel(Long memberId, Long mileageId) {
memberRepository.save(member);
}

/**
* 마일리지 추가 목록
*/
public List<MileageHistoryResDto> mileageHistory(Long memberId) {
SecurityUtil.memberTokenMatch(memberId);

Expand All @@ -92,15 +175,17 @@ public List<MileageHistoryResDto> mileageHistory(Long memberId) {
mileage.getMileageId(),
mileage.getTitle(),
mileage.getMileageScore(),
memberHistoryMileage.getSystemDate()
);
memberHistoryMileage.getSystemDate());

mileageHistoryResDtoList.add(mileageHistoryResDto);
}

return mileageHistoryResDtoList;
}

/**
* 유저 정보 응답
*/
public MemberMileageResDto memberMileageResponse(Long memberId) {
SecurityUtil.memberTokenMatch(memberId);

Expand Down
Loading

0 comments on commit c26aba9

Please sign in to comment.