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

[Fix] #58 - KeyResult 엔티티 변경에 따른 LogService, KeyResultService 변경 #59

Merged
merged 6 commits into from
Jan 10, 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
Expand Up @@ -2,6 +2,7 @@

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

import lombok.RequiredArgsConstructor;
import org.moonshot.server.domain.keyresult.dto.request.KeyResultCreateRequestDto;
Expand All @@ -13,6 +14,8 @@
import org.moonshot.server.domain.keyresult.exception.KeyResultNumberExceededException;
import org.moonshot.server.domain.keyresult.model.KeyResult;
import org.moonshot.server.domain.keyresult.repository.KeyResultRepository;
import org.moonshot.server.domain.log.exception.InvalidLogValueException;
import org.moonshot.server.domain.log.exception.LogNotFoundException;
import org.moonshot.server.domain.log.model.Log;
import org.moonshot.server.domain.log.model.LogState;
import org.moonshot.server.domain.objective.dto.request.ModifyIndexRequestDto;
Expand Down Expand Up @@ -135,11 +138,15 @@ public void modifyKeyResult(KeyResultModifyRequestDto request, Long userId) {
LocalDateTime newExpireAt = (request.expireAt() != null) ? request.expireAt() : keyResult.getPeriod().getExpireAt();
keyResult.modifyPeriod(Period.of(newStartAt, newExpireAt));
}
if (request.target() != null) {
if (request.logContent() != null) {
logService.createUpdateLog(request, keyResult.getId());
if (request.target() != null && request.logContent() != null) {
Log updateLog = logService.createUpdateLog(request, keyResult.getId());
if (request.target().equals(updateLog.getKeyResult().getTarget())) {
throw new InvalidLogValueException();
}
Log prevLog = logRepository.findLatestLogByKeyResultId(LogState.RECORD, request.keyResultId())
.orElseThrow(LogNotFoundException::new);
keyResult.modifyTarget(request.target());
keyResult.modifyProgress(logService.calculateProgressBar(prevLog, keyResult));
}
if (request.state() != null) {
keyResult.modifyState(request.state());
Expand Down Expand Up @@ -178,15 +185,11 @@ public KRDetailResponseDto getKRDetails(Long userId, Long keyResultId) {
}
}
return KRDetailResponseDto.of(keyResult.getTitle(),
calculateProgressBar(target, keyResult),
logService.calculateProgressBar(target, keyResult),
keyResult.getState().getValue(),
keyResult.getPeriod().getStartAt(),
keyResult.getPeriod().getExpireAt(),
logService.getLogResponseDto(logList, keyResult));
}

public int calculateProgressBar(Log log, KeyResult keyResult) {
return (log != null) ? (int) (Math.round(log.getCurrNum() / (double) keyResult.getTarget() * 100)) : 0;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@

public class InvalidLogValueException extends MoonshotException {

public InvalidLogValueException() { super(ErrorType.INVALID_LOG_VALUE); }
public InvalidLogValueException() {
super(ErrorType.INVALID_LOG_VALUE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

public class InvalidRecordException extends MoonshotException {

public InvalidRecordException() { super(ErrorType.INVALID_RECORD_VALUE); }
public InvalidRecordException() {
super(ErrorType.INVALID_RECORD_VALUE);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.moonshot.server.domain.log.exception;

import org.moonshot.server.global.common.exception.MoonshotException;
import org.moonshot.server.global.common.response.ErrorType;

public class LogNotFoundException extends MoonshotException {

public LogNotFoundException() {
super(ErrorType.NOT_FOUND_LOG_ERROR);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.moonshot.server.domain.keyresult.exception.KeyResultNotFoundException;
import org.moonshot.server.domain.keyresult.model.KeyResult;
import org.moonshot.server.domain.keyresult.repository.KeyResultRepository;
import org.moonshot.server.domain.keyresult.service.KeyResultService;
import org.moonshot.server.domain.log.dto.request.LogCreateRequestDto;
import org.moonshot.server.domain.log.dto.response.LogResponseDto;
import org.moonshot.server.domain.log.exception.InvalidLogValueException;
Expand All @@ -16,6 +17,8 @@
import org.moonshot.server.domain.log.repository.LogRepository;
import org.moonshot.server.domain.user.repository.UserRepository;
import org.moonshot.server.global.auth.exception.AccessDeniedException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -28,7 +31,6 @@
@RequiredArgsConstructor
public class LogService {

private final UserRepository userRepository;
private final KeyResultRepository keyResultRepository;
private final LogRepository logRepository;

Expand All @@ -46,26 +48,26 @@ public void createRecordLog(Long userId, LogCreateRequestDto request) {
long prevNum = -1;
if (!prevLog.isEmpty()) {
prevNum = prevLog.get().getCurrNum();
if(request.logNum() < prevNum) {
if(request.logNum() == prevNum) {
throw new InvalidLogValueException();
}
}
logRepository.save(Log.builder()
Log log = logRepository.save(Log.builder()
.date(LocalDateTime.now())
.state(LogState.RECORD)
.currNum(request.logNum())
.prevNum(prevNum)
.content(request.logContent())
.keyResult(keyResult)
.build());
keyResult.modifyProgress(calculateProgressBar(log, keyResult));
}

@Transactional
public void createUpdateLog(KeyResultModifyRequestDto request, Long keyResultId) {
public Log createUpdateLog(KeyResultModifyRequestDto request, Long keyResultId) {
KeyResult keyResult = keyResultRepository.findById(keyResultId)
.orElseThrow(KeyResultNotFoundException::new);

logRepository.save(Log.builder()
return logRepository.save(Log.builder()
.date(LocalDateTime.now())
.state(LogState.UPDATE)
.currNum(request.target())
Expand Down Expand Up @@ -124,4 +126,8 @@ public String setTitle(long prevNum, long currNum, Log log, KeyResult keyResult)
}
}

public short calculateProgressBar(Log log, KeyResult keyResult) {
return (log != null) ? (short) (Math.round(log.getCurrNum() / (double) keyResult.getTarget() * 100)) : 0;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public enum ErrorType {
ACTIVE_TASK_NUMBER_EXCEEDED(HttpStatus.BAD_REQUEST, "허용된 Task 개수를 초과하였습니다."),
INVALID_KEY_RESULT_ORDER(HttpStatus.BAD_REQUEST, "정상적이지 않은 KeyResult 위치입니다."),
INVALID_TASK_ORDER(HttpStatus.BAD_REQUEST, "정상적이지 않은 Task 위치입니다."),
INVALID_LOG_VALUE(HttpStatus.BAD_REQUEST, "진척 정도는 이전 값보다 큰 값이 입력되어야합니다."),
INVALID_LOG_VALUE(HttpStatus.BAD_REQUEST, "Log 입력값은 이전 값과 동일할 수 없습니다."),
INVALID_EXPIRE_AT(HttpStatus.BAD_REQUEST, "Objective 종료 기간은 오늘보다 이전 날짜일 수 없습니다."),
INVALID_RECORD_VALUE(HttpStatus.BAD_REQUEST, "진척 정도는 목표값보다 작은 값이 입력되어야 합니다."),

Expand All @@ -47,6 +47,7 @@ public enum ErrorType {
NOT_FOUND_OBJECTIVE_ERROR(HttpStatus.NOT_FOUND, "존재하지 않는 Objective입니다."),
NOT_FOUND_KEY_RESULT_ERROR(HttpStatus.NOT_FOUND, "존재하지 않는 KeyResult입니다."),
NOT_FOUND_TASK_ERROR(HttpStatus.NOT_FOUND, "존재하지 않는 Task입니다."),
NOT_FOUND_LOG_ERROR(HttpStatus.NOT_FOUND, "존재하지 않는 Log입니다."),

/**
* 500 INTERNAL SERVER ERROR
Expand Down