Skip to content

Commit

Permalink
Merge pull request #249 from MOONSHOT-Team/feature/#248
Browse files Browse the repository at this point in the history
[Fix] KeyResult, Task 생성, 삭제 시 Index 위치 조정 로직 변경
  • Loading branch information
its-sky authored Mar 12, 2024
2 parents b4ed228 + 52c0baa commit 4a4edd5
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,8 @@ public void createKeyResult(final KeyResultCreateRequestDto request, final Long
validateActiveKRSizeExceeded(krList.size());
validateIndexUnderMaximum(request.idx(), krList.size());

for (int i = request.idx(); i < krList.size(); i++) {
krList.get(i).incrementIdx();
}
keyResultRepository.bulkUpdateIdxIncrease(request.idx(), krList.size(), objective.getId(), -1L);

KeyResult keyResult = keyResultRepository.save(KeyResult.builder()
.objective(objective)
.title(request.title())
Expand All @@ -113,6 +112,7 @@ public void deleteKeyResult(final Long keyResultId, final Long userId) {
logRepository.deleteAllInBatch(logRepository.findAllByKeyResult(keyResult));
taskRepository.deleteAllInBatch(taskRepository.findAllByKeyResult(keyResult));
keyResultRepository.delete(keyResult);
keyResultRepository.bulkUpdateIdxDecrease(keyResult.getIdx(), 3, keyResult.getObjective().getId(), -1L);
}

public void deleteKeyResult(final Objective objective) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ public DashboardResponseDto deleteObjective(final Long userId, final Long object

keyResultService.deleteKeyResult(objective);
objectiveRepository.delete(objective);
objectiveRepository.bulkUpdateIdxDecreaseAfter(objective.getIdx(), userId);

return getObjectiveInDashboard(userId, null);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,8 @@ public void createTask(final TaskSingleCreateRequestDto request, final Long user
validateActiveTaskSizeExceeded(taskList.size());
validateIndexUnderMaximum(request.idx(), taskList.size());

for (int i = request.idx(); i < taskList.size(); i++) {
taskList.get(i).incrementIdx();
}
taskRepository.bulkUpdateTaskIdxIncrease(request.idx(), taskList.size(), keyResult.getId(), -1L);

saveTask(keyResult, request);
}

Expand Down Expand Up @@ -90,6 +89,7 @@ public void deleteTask(final Long userId, Long taskId) {
validateUserAuthorization(task.getKeyResult().getObjective().getUser().getId(), userId);

taskRepository.deleteById(taskId);
taskRepository.bulkUpdateTaskIdxDecrease(task.getIdx(), 3, task.getKeyResult().getId(), -1L);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,15 @@

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

import org.moonshot.keyresult.model.KeyResult;
import org.moonshot.objective.model.Objective;
import org.moonshot.user.model.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.moonshot.user.model.User;

public interface ObjectiveJpaRepository extends JpaRepository<Objective, Long> {

Long countAllByUserAndIsClosed(User user, boolean isClosed);
@Query("select o from Objective o join fetch o.user where o.id = :objective_id")
Optional<Objective> findObjectiveAndUserById(@Param("objective_id") Long objectiveId);
@Query("select distinct o from Objective o left join fetch o.keyResultList kr left join kr.taskList t where o.id = :objectiveId and o.isClosed = false")
Expand All @@ -23,13 +20,16 @@ public interface ObjectiveJpaRepository extends JpaRepository<Objective, Long> {
@Modifying(clearAutomatically = true)
@Query("UPDATE Objective o SET o.idx = o.idx + 1 WHERE o.user.id = :userId")
void bulkUpdateIdxIncrease(@Param("userId") Long userId);
Long countAllByUserId(Long userId);
@Modifying(clearAutomatically = true)
@Query("UPDATE Objective o SET o.idx = o.idx + 1 WHERE o.idx >= :lBound AND o.idx < :uBound AND o.user.id = :userId AND o.id != :targetId")
void bulkUpdateIdxIncrease(@Param("lBound") int lowerBound, @Param("uBound") int upperBound, @Param("userId") Long userId, @Param("targetId") Long targetId);
@Modifying(clearAutomatically = true)
@Query("UPDATE Objective o SET o.idx = o.idx - 1 WHERE o.idx >= :lBound AND o.idx <= :uBound AND o.user.id = :userId AND o.id != :targetId")
void bulkUpdateIdxDecrease(@Param("lBound") int lowerBound, @Param("uBound") int upperBound, @Param("userId") Long userId, @Param("targetId") Long targetId);
@Modifying(clearAutomatically = true)
@Query("UPDATE Objective o SET o.idx = o.idx - 1 WHERE o.idx >= :lBound AND o.user.id = :userId")
void bulkUpdateIdxDecreaseAfter(@Param("lBound") int lowerBound, @Param("userId") Long userId);
List<Objective> findAllByUserIn(List<User> userList);
Long countAllByUserId(Long userId);

}

0 comments on commit 4a4edd5

Please sign in to comment.