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

#21 [feat] 투두 토글 변경 #22

Merged
merged 1 commit into from
Sep 1, 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 @@ -8,8 +8,8 @@
@AllArgsConstructor
public enum ErrorCode {
// 400
INVALID_TOKEN_EXCEPTION(HttpStatus.BAD_REQUEST, "유효하지 않은 토큰을 입력했습니다."),
INVALID_VALUE_TYPE_EXCEPTION(HttpStatus.BAD_REQUEST, "유효하지 않은 타입 값을 입력했습니다."),
INVALID_USER_EXCEPTION(HttpStatus.OK, "유효하지 않은 사용자입니다"),
INVALID_ENUM_TYPE_EXCEPTION(HttpStatus.BAD_REQUEST, "요청한 상수 값이 유효하지 않습니다."),
INVALID_EMPTY_TYPE_EXCEPTION(HttpStatus.BAD_REQUEST, "해당 값은 null 또 상수 값이 유효하지 않습니다."),
VALIDATION_REQUEST_MISSING_EXCEPTION(HttpStatus.BAD_REQUEST, "요청값이 유효하지 않습니다."),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public enum SuccessCode {
TODO_SUCCESS(HttpStatus.OK, "투두 찾기 성공입니다."),
TODO_SAVE_SUCCESS(HttpStatus.OK, "투두 저장 성공입니다"),
TODO_DELETE_SUCCESS(HttpStatus.OK, "투두 삭제 성공입니다"),
TODO_TOGGLE_SUCCESS(HttpStatus.OK, "투두 토글 변경 성공입니다"),
SAVE_CAREER_SUCCESS(HttpStatus.CREATED, "커리어 등록 성공"),
GET_CAREER_DETAIL_SUCCESS(HttpStatus.OK, "커리어 상세 조회 성공");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,16 @@ public ApiResponse deleteTodo(
return ErrorResponse.error(e.getErrorCode());
}
}
@PatchMapping("/{todoId}")
public ApiResponse toggleTodoChecked(
@RequestHeader("userId") Long userId,
@PathVariable Long todoId
) {
try {
todoService.changeTodoChecked(userId, todoId);
return SuccessNonDataResponse.success(SuccessCode.TODO_TOGGLE_SUCCESS);
} catch (NotFoundException | BadRequestException e) {
return ErrorResponse.error(e.getErrorCode());
}
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/example/letscareer/todo/domain/Todo.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

@Entity
@Getter
@Setter
@Setter //필요
@Builder
@NoArgsConstructor
@AllArgsConstructor
Expand Down
20 changes: 18 additions & 2 deletions src/main/java/com/example/letscareer/todo/service/TodoService.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.example.letscareer.todo.service;

import com.example.letscareer.common.exception.model.BadRequestException;
import com.example.letscareer.common.exception.model.NotFoundException;
import com.example.letscareer.todo.domain.Todo;
import com.example.letscareer.todo.dto.TodoDTO;
Expand All @@ -17,8 +18,7 @@
import java.util.OptionalInt;
import java.util.stream.Collectors;

import static com.example.letscareer.common.exception.enums.ErrorCode.TODO_NOT_FOUND_EXCEPTION;
import static com.example.letscareer.common.exception.enums.ErrorCode.USER_NOT_FOUND_EXCEPTION;
import static com.example.letscareer.common.exception.enums.ErrorCode.*;

@Service
@RequiredArgsConstructor
Expand Down Expand Up @@ -53,4 +53,20 @@ public void deleteTodo(final Long userId, final Long todoId){
todoRepository.deleteByTodoId(todo.get().getTodoId());
}
}

@Transactional
public void changeTodoChecked(Long userId, Long todoId) {
Todo todo = todoRepository.findById(todoId)
.orElseThrow(() -> new NotFoundException(TODO_NOT_FOUND_EXCEPTION));

// User ID가 일치하는지 확인
if (!todo.getUser().getUserId().equals(userId)) {
throw new BadRequestException(INVALID_USER_EXCEPTION);
}

// isChecked 필드를 반전
todo.setChecked(!todo.isChecked());

todoRepository.save(todo);
}
}
Loading