Skip to content

Commit

Permalink
Merge pull request #14 from TEAM-ALOM/10-feat-랭킹-기능-구현
Browse files Browse the repository at this point in the history
rank
  • Loading branch information
xaxeon authored Nov 21, 2024
2 parents e6f7909 + 299a04f commit 3a19501
Show file tree
Hide file tree
Showing 16 changed files with 405 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.alom.dorundorunbe.domain.ranking.controller;

import com.alom.dorundorunbe.domain.ranking.domain.Ranking;
import com.alom.dorundorunbe.domain.ranking.dto.*;
import com.alom.dorundorunbe.domain.ranking.service.RankingService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/ranking")
@RequiredArgsConstructor
public class RankingController {
private final RankingService rankingService;

@PostMapping("/join")
public ResponseEntity<JoiningRankingResponseDto> joinRanking(@RequestBody JoiningRankingRequestDto requestDto){
Long rankId = rankingService.joinRanking(requestDto.userId());
Ranking joinRank = rankingService.findOne(rankId);
JoiningRankingResponseDto responseDto = JoiningRankingResponseDto.of(joinRank.getUser().getId(), joinRank.getCreatedAt());
return ResponseEntity.ok(responseDto);
}


@PutMapping
public ResponseEntity<UpdateRankingResponseDto>updateRanking(@RequestBody UpdateRankingRequestDto requestDto){
Long rankId = rankingService.updateRanking(
requestDto.userId(),
requestDto.tier(),
requestDto.distance(),
requestDto.time(),
requestDto.cadence(),
requestDto.grade()
);
Ranking updateRank = rankingService.findOne(rankId);


UpdateRankingResponseDto responseDto = UpdateRankingResponseDto.of(updateRank);
return ResponseEntity.ok(responseDto);

}


@GetMapping
public ResponseEntity<List<RankingDto>> fetchRankings() {
List<Ranking> rankings = rankingService.fetchAllRankings();
// if (rankings.isEmpty()) {
// throw new RankingNotFoundException("랭킹 데이터가 없습니다.");
// }
//빈 리스트로 반환이 되었을 때 이를 오류처리할것인가에 대한 처리
List<RankingDto> list = rankings.stream()
.map(RankingDto::of)
.toList();
return ResponseEntity.ok(list);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,43 @@
import lombok.Getter;
import lombok.NoArgsConstructor;

import static jakarta.persistence.FetchType.LAZY;

@Entity @Getter
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Table(name = "ranking")
public class Ranking extends BaseEntity {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "rank_id")
private Long id;

@OneToOne
@OneToOne(fetch = LAZY)
@JoinColumn(name = "user_id", nullable = false)
private User user;

@Column(nullable = false)
private Rank rank;
@Column(name = "rank_level", nullable = true)
@Enumerated(EnumType.STRING)
private Tier tier; //user에도 rank핗드가 있어야 될 것 같습니다 user.getRank로..일단 true로 테스트
//이름 변경->티어

private long time;

private long distance;

private int cadence;

private int grade;

public void updateRanking(Tier tier, long distance, long time, int cadence, int grade){
this.tier = tier;
this.distance = distance;
this.time = time;
this.cadence = cadence;
this.grade = grade;
}

}
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package com.alom.dorundorunbe.domain.ranking.domain;

public enum Rank {
public enum Tier {
STARTER("스타터"),
BEGINNER("비기너"),
AMATEUR("아마추어"),
PRO("프로");

private final String rank;

Rank(String rank) {
Tier(String rank) {
this.rank = rank;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.alom.dorundorunbe.domain.ranking.dto;

public record JoiningRankingRequestDto(
Long userId
) {
public static JoiningRankingRequestDto of(Long userId){
return new JoiningRankingRequestDto(userId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.alom.dorundorunbe.domain.ranking.dto;

import java.time.LocalDateTime;

public record JoiningRankingResponseDto(
Long userId,
LocalDateTime createdAt
) {

public static JoiningRankingResponseDto of(Long userId, LocalDateTime createdAt){
return new JoiningRankingResponseDto(userId,createdAt);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.alom.dorundorunbe.domain.ranking.dto;

import com.alom.dorundorunbe.domain.ranking.domain.Tier;
import com.alom.dorundorunbe.domain.ranking.domain.Ranking;

import java.time.LocalDateTime;

public record RankingDto(
Long userId,
Tier tier,
long distance,
long time,
int cadence,

int grade,
LocalDateTime createdAt,
LocalDateTime modifiedAt
) {
public static RankingDto of(Ranking ranking) {
return new RankingDto(
ranking.getUser().getId(),
ranking.getTier(),
ranking.getDistance(),
ranking.getTime(),
ranking.getCadence(),
ranking.getGrade(),
ranking.getCreatedAt(),
ranking.getModifiedAt()
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.alom.dorundorunbe.domain.ranking.dto;

import com.alom.dorundorunbe.domain.ranking.domain.Tier;

public record UpdateRankingRequestDto(
Long userId,
Tier tier,
long distance,
long time,
int cadence,
int grade

) {
public static UpdateRankingRequestDto of(Long userId, Tier tier, long distance, long time, int cadence, int grade) {
return new UpdateRankingRequestDto(userId, tier, distance, time, cadence, grade);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.alom.dorundorunbe.domain.ranking.dto;

import com.alom.dorundorunbe.domain.ranking.domain.Tier;
import com.alom.dorundorunbe.domain.ranking.domain.Ranking;

import java.time.LocalDateTime;

public record UpdateRankingResponseDto(
Long userId,
Tier tier,
long distance,
long time,
int cadence,

int grade,
LocalDateTime createdAt,
LocalDateTime modifiedAt
) {
public static UpdateRankingResponseDto of(Ranking ranking) {
return new UpdateRankingResponseDto(
ranking.getUser().getId(),
ranking.getTier(),
ranking.getDistance(),
ranking.getTime(),
ranking.getCadence(),
ranking.getGrade(),
ranking.getCreatedAt(),
ranking.getModifiedAt()
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.alom.dorundorunbe.domain.ranking.exception;

public class RankingNotFoundException extends RuntimeException{
public RankingNotFoundException() {
super();
}

public RankingNotFoundException(String message) {
super(message);
}

public RankingNotFoundException(String message, Throwable cause) {
super(message, cause);
}

public RankingNotFoundException(Throwable cause) {
super(cause);
}

protected RankingNotFoundException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.alom.dorundorunbe.domain.ranking.exception;

public class UserAlreadyJoinedRankingException extends RuntimeException{
public UserAlreadyJoinedRankingException() {
super();
}

public UserAlreadyJoinedRankingException(String message) {
super(message);
}

public UserAlreadyJoinedRankingException(String message, Throwable cause) {
super(message, cause);
}

public UserAlreadyJoinedRankingException(Throwable cause) {
super(cause);
}

protected UserAlreadyJoinedRankingException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.alom.dorundorunbe.domain.ranking.exception;

public class UserNotFoundException extends RuntimeException{
public UserNotFoundException() {
super();
}

public UserNotFoundException(String message) {
super(message);
}

public UserNotFoundException(String message, Throwable cause) {
super(message, cause);
}

public UserNotFoundException(Throwable cause) {
super(cause);
}

protected UserNotFoundException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.alom.dorundorunbe.domain.ranking.exception.advice;

import com.alom.dorundorunbe.domain.ranking.exception.RankingNotFoundException;
import com.alom.dorundorunbe.domain.ranking.exception.UserAlreadyJoinedRankingException;
import com.alom.dorundorunbe.domain.ranking.exception.UserNotFoundException;
import com.alom.dorundorunbe.domain.ranking.exception.common.ErrorResult;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class ExRankingHandlerAdvice {
@ExceptionHandler(UserNotFoundException.class)
public ResponseEntity<ErrorResult> handleUserNotFoundException(UserNotFoundException e) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(ErrorResult.of("404", e.getMessage()));
}

@ExceptionHandler(UserAlreadyJoinedRankingException.class)
public ResponseEntity<ErrorResult> handleUserAlreadyJoinedRankingException(UserAlreadyJoinedRankingException e) {
return ResponseEntity.status(HttpStatus.CONFLICT).body(ErrorResult.of("409", e.getMessage()));
}

@ExceptionHandler(RankingNotFoundException.class)
public ResponseEntity<ErrorResult> handleRankingNotFoundException(RankingNotFoundException e) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(ErrorResult.of("404", e.getMessage()));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.alom.dorundorunbe.domain.ranking.exception.common;

public class ErrorMessages {
public static final String USER_NOT_FOUND = "사용자를 찾을 수 없습니다.";
public static final String USER_ALREADY_JOINED = "이미 참여 중입니다.";
public static final String RANKING_NOT_FOUND = "랭킹을 찾을 수 없습니다.";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.alom.dorundorunbe.domain.ranking.exception.common;

public record ErrorResult(
String code,
String message) {
public static ErrorResult of(String code, String message) {
return new ErrorResult(code, message);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.alom.dorundorunbe.domain.ranking.repository;

import com.alom.dorundorunbe.domain.ranking.domain.Ranking;
import com.alom.dorundorunbe.domain.user.domain.User;
import org.springframework.data.jpa.repository.EntityGraph;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;
import java.util.Optional;

public interface RankingRepository extends JpaRepository<Ranking, Long> {
Optional<Ranking> findByUser(User user);
boolean existsByUser(User user);

@Override
@EntityGraph(attributePaths = {"user"})
List<Ranking>findAll();




}
Loading

0 comments on commit 3a19501

Please sign in to comment.