Skip to content

Commit

Permalink
[Add] #42 : IndexService를 KeyResultService에서 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
its-sky committed Jan 9, 2024
1 parent 97f109c commit f3ae79f
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
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.objective.dto.request.ModifyIndexRequestDto;
import org.moonshot.server.domain.objective.exception.ObjectiveNotFoundException;
import org.moonshot.server.domain.objective.model.IndexService;
import org.moonshot.server.domain.objective.model.Objective;
import org.moonshot.server.domain.objective.repository.ObjectiveRepository;
import org.moonshot.server.domain.task.dto.request.TaskCreateRequestDto;
Expand All @@ -25,7 +27,7 @@
@Service
@Transactional(readOnly = true)
@RequiredArgsConstructor
public class KeyResultService {
public class KeyResultService implements IndexService {

private static final int ACTIVE_KEY_RESULT_NUMBER = 3;

Expand Down Expand Up @@ -72,7 +74,7 @@ public void createKeyResult(KeyResultCreateRequestDto request, Long userId) {
throw new KeyResultInvalidPositionException();
}

for (short i = request.idx(); i < krList.size(); i++) {
for (int i = request.idx(); i < krList.size(); i++) {
krList.get(i).incrementIdx();
}
keyResultRepository.save(KeyResult.builder()
Expand Down Expand Up @@ -128,4 +130,24 @@ public void modifyKeyResult(KeyResultModifyRequestDto request, Long userId) {
}
}

@Override
@Transactional
public void modifyIdx(ModifyIndexRequestDto request, Long userId) {
KeyResult keyResult = keyResultRepository.findKeyResultAndObjective(request.id())
.orElseThrow(KeyResultNotFoundException::new);
userService.validateUserAuthorization(keyResult.getObjective().getUser(), userId);
Integer prevIdx = keyResult.getIdx();
if (prevIdx.equals(request.idx())) {
return;
}
List<KeyResult> krList = keyResultRepository.findAllByObjectiveOrderByIdx(keyResult.getObjective());

keyResult.modifyIdx(request.idx());
if (prevIdx < request.idx()) {
keyResultRepository.bulkUpdateIdxDecrease(prevIdx + 1, request.idx(), keyResult.getObjective().getId(), keyResult.getId());
} else {
keyResultRepository.bulkUpdateIdxIncrease(request.idx(), prevIdx, keyResult.getObjective().getId(), keyResult.getId());
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.moonshot.server.domain.objective.dto.request;

import jakarta.validation.constraints.NotNull;
import org.hibernate.validator.constraints.Range;
import org.moonshot.server.domain.objective.model.IndexTarget;

public record ModifyIndexRequestDto(
@NotNull(message = "대상의 ID를 입력해주세요.")
Long id,
@NotNull(message = "대상을 지정해주세요.")
IndexTarget target,
@NotNull(message = "대상의 이동할 위치 값을 입력하세요.")
@Range(min = 0, max = 2, message = "순서는 0부터 2까지로 설정할 수 있습니다.")
Integer idx
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public enum SuccessType {
* 204 NO CONTENT
*/
PATCH_KEY_RESULT_SUCCESS(HttpStatus.NO_CONTENT, "KeyResult 수정을 성공하였습니다."),
PATCH_TARGET_INDEX_SUCCESS(HttpStatus.NO_CONTENT, "대상의 순서 수정을 성공하였습니다."),
DELETE_KEY_RESULT_SUCCESS(HttpStatus.NO_CONTENT, "KeyResult 삭제를 성공하였습니다."),
DELETE_OBJECTIVE_SUCCESS(HttpStatus.NO_CONTENT, "Objective 삭제를 성공하였습니다."),
DELETE_USER_SUCCESS(HttpStatus.NO_CONTENT, "회원 탈퇴에 성공하였습니다." );
Expand Down

0 comments on commit f3ae79f

Please sign in to comment.