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

[Refactor/66] 항목 순서변경 API 수정 #67

Merged
merged 1 commit into from
Jul 29, 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 @@ -50,18 +50,20 @@ public enum ErrorStatus implements BaseErrorCode {

MAX_STUDY_ERROR(HttpStatus.BAD_REQUEST, "STUDY4010", "최대 등록 개수를 초과했습니다."),

// 항목 관련
NO_SUBJECT_FOUND(HttpStatus.NOT_FOUND, "SUBJECT4040", "해당 항목을 찾지 못하였습니다."),
NO_ACCESS_SUBJECT_PERMISSION(HttpStatus.FORBIDDEN, "SUBJECT4030", "해당 항목을 접근할 권한이 없습니다."),
NO_DELETE_SUBJECT_PERMISSION(HttpStatus.FORBIDDEN, "SUBJECT4030", "해당 항목을 삭제할 권한이 없습니다."),
NO_CHANGE_SUBJECT_PERMISSION(HttpStatus.FORBIDDEN, "SUBJECT4030", "해당 항목을 변경할 권한이 없습니다."),
INVALID_ORDER_NUMBER(HttpStatus.BAD_REQUEST, "ORDER400", "유효하지 않은 순서 번호입니다."),
NOT_CHANGE_ORDER_NUMBER(HttpStatus.BAD_REQUEST, "ORDER400", "기존과 동일한 순서입니다."),
INVALID_REQUEST(HttpStatus.BAD_REQUEST, "INVALID_REQUEST", "잘못된 요청입니다."),
MAX_SUBJECT_ERROR(HttpStatus.BAD_REQUEST, "SUBJECT4000", "최대 항목 개수를 초과했습니다."),
DUPLICATE_SUBJECT_TITLE(HttpStatus.BAD_REQUEST, "SUBJECT4001", "항목 제목이 중복되었습니다."),
NOT_CHANGE_STATUS(HttpStatus.BAD_REQUEST, "SUBJECT4002", "항목의 상태를 변경할 수 없습니다."),
INVALID_STATUS(HttpStatus.BAD_REQUEST, "SUBJECT4003", "올바르지 않은 항목의 상태입니다."),

// 항목 관련
// 할 일 관련
NO_TODO_FOUND(HttpStatus.NOT_FOUND, "TODO4040", "해당 할 일을 찾지 못하였습니다."),
MOVED_SUBJECT_RESTRICTION(HttpStatus.BAD_REQUEST, "TODO4040", "보관함에 이동된 항목은 삭제 및 조회만 가능합니다."),
NOT_TODO_OWNER(HttpStatus.BAD_REQUEST, "TODO4009", "해당 회원의 할 일이 아닙니다.");
Expand Down
20 changes: 11 additions & 9 deletions src/main/java/timify/com/subject/SubjectService.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import jakarta.validation.Valid;
import java.util.Arrays;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -74,21 +75,22 @@ public Subject updateOrder(Member member, Long subjectId, int newOrderNum) {

validateMember(member, subject);

List<Subject> subjects = subjectRepository.findAllByMemberAndStatus(member,
SubjectStatus.ACTIVE);

if (subject.getStatus() != SubjectStatus.ACTIVE) {
throw new SubjectHandler(ErrorStatus.NO_CHANGE_SUBJECT_PERMISSION);
}
List<Subject> subjects = subjectRepository.findAllByMemberAndStatus(member, subject.getStatus());

if (newOrderNum > subjects.size() || newOrderNum < 1) {
throw new SubjectHandler(ErrorStatus.INVALID_ORDER_NUMBER);
}

subjects.stream().filter(s -> s.getOrderNum() == newOrderNum)
.forEach(s -> s.updateOrderNum(subject.getOrderNum()));
if (subject.getOrderNum() == newOrderNum) {
throw new SubjectHandler(ErrorStatus.NOT_CHANGE_ORDER_NUMBER);
}

subject.updateOrderNum(newOrderNum);
LinkedList<Subject> subjectList = new LinkedList<>(subjects);
subjectList.remove(subject);
subjectList.add(newOrderNum - 1, subject);

AtomicInteger index = new AtomicInteger(1);
subjectList.forEach(subj -> subj.updateOrderNum(index.getAndIncrement()));

return subject;
}
Expand Down
Loading