Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ feat: add user level progress api #457

Merged
merged 2 commits into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.depromeet.domains.item.repository;

import com.depromeet.item.Item;
import com.depromeet.user.User;
import org.locationtech.jts.geom.Point;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
Expand All @@ -21,4 +22,5 @@ public interface ItemRepository extends JpaRepository<Item, Long>, QueryDslItemR
@Query("SELECT i FROM Item i JOIN FETCH i.itemLocation JOIN FETCH i.user JOIN FETCH i.song WHERE i.id = :itemId")
Optional<Item> findById(@Param("itemId") Long itemId);

Long countByUser(User user);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,13 @@ public enum LevelPolicy {
private final Long level;
private final Long dropCountStart;
private final Long dropCountEnd;

public static LevelPolicy getNextLevel(LevelPolicy currentLevel){
int nextIndex = currentLevel.ordinal() + 1;
if (nextIndex < LevelPolicy.values().length) {
return LevelPolicy.values()[nextIndex];
} else {
return currentLevel;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.depromeet.common.dto.ResponseDto;
import com.depromeet.domains.user.dto.request.NicknameChangeDto;
import com.depromeet.domains.user.dto.response.UserDistanceResponseDto;
import com.depromeet.domains.user.dto.response.UserLevelProgressDto;
import com.depromeet.domains.user.dto.response.UserLevelResponseDto;
import com.depromeet.domains.user.dto.response.UserResponseDto;
import com.depromeet.domains.user.service.UserLevelService;
Expand Down Expand Up @@ -68,6 +69,15 @@ public ResponseEntity<UserLevelResponseDto> getUserLevel(@ReqUser User user) {
return ResponseDto.ok(response);
}

@Operation(summary = "사용자 레벨 진행사항 조회")
@ApiResponse(responseCode = "200", description = "사용자 레벨 진행사항 조회 성공")
@ApiErrorResponse(errorCode = "COMMON_NOT_FOUND", description = "사용자 유저 레벨이 존재하지 않음")
@GetMapping("/me/level/progress")
public ResponseEntity<UserLevelProgressDto> getUserLevelProgress(@ReqUser User user) {
var response = userLevelService.getUserLevelProgress(user);
return ResponseDto.ok(response);
}

@Operation(summary = "사용자 반경 조회")
@ApiResponse(responseCode = "200", description = "사용자 반경 조회 성공")
@GetMapping("/me/distance")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.depromeet.domains.user.dto.response;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

@NoArgsConstructor
@AllArgsConstructor
@Getter
public class UserLevelProgressDto {
@Schema(description = "노출 여부", example = "true")
private Boolean isShow;
@Schema(description = "남은 횟수를 나타냅니다.", example = "1")
private Long remainCount;
@Schema(description = "드롭된 횟수를 나타냅니다.", example = "4")
private Long dropCount;
@Schema(description = "레벨 업이 되는 횟수를 나타냅니다.", example = "5")
private Long levelUpCount;
@Schema(description = "팁 메시지를 나타냅니다.", example = "레벨업하면 음악을 들을 수 있는 반경이 넓어져요!")
private String tip;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,47 @@

import com.depromeet.common.error.dto.CommonErrorCode;
import com.depromeet.common.error.exception.internal.NotFoundException;
import com.depromeet.domains.item.repository.ItemRepository;
import com.depromeet.domains.level.data.LevelPolicy;
import com.depromeet.domains.user.dto.response.UserLevelProgressDto;
import com.depromeet.domains.user.dto.response.UserLevelResponseDto;
import com.depromeet.domains.user.repository.UserLevelRepository;
import com.depromeet.user.User;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;

import static com.depromeet.domains.level.data.LevelPolicy.getNextLevel;

@RequiredArgsConstructor
@Service
public class UserLevelService {

private final UserLevelRepository userLevelRepository;
private final ItemRepository itemRepository;

@Transactional(readOnly = true)
public UserLevelResponseDto getUserLevel(User user) {
var level = userLevelRepository.findById(user.getUserLevelId())
.orElseThrow(() -> new NotFoundException(CommonErrorCode.NOT_FOUND, user.getUserLevelId()));
return new UserLevelResponseDto(user, level);
}

@Transactional(readOnly = true)
public UserLevelProgressDto getUserLevelProgress(User user) {
Long dropCount = itemRepository.countByUser(user);
var currentLevel = Arrays.stream(LevelPolicy.values())
.filter(policy -> dropCount >= policy.getDropCountStart() && dropCount < policy.getDropCountEnd())
.findFirst()
.orElse(LevelPolicy.LEVEL_1);
LevelPolicy nextLevel = getNextLevel(currentLevel);
var remainCount = nextLevel.getDropCountStart() - dropCount;
var isShow = !(currentLevel == LevelPolicy.LEVEL_3);
var tipMessage = "레벨업하면 음악을 들을 수 있는 반경이 넓어져요!";
return new UserLevelProgressDto(isShow, remainCount, dropCount, nextLevel.getDropCountStart(), tipMessage);
}

}

Loading