Skip to content

Commit

Permalink
feat: 게시글 완료 여부 변경 기능
Browse files Browse the repository at this point in the history
  • Loading branch information
cmsong111 committed Sep 10, 2023
1 parent 4079ac9 commit e4eaf81
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 14 deletions.
3 changes: 2 additions & 1 deletion .idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.namju.simple_todo.todo.controller

import com.namju.simple_todo.todo.service.TodoSerivce
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.PatchMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
import java.security.Principal


@RestController
@RequestMapping("/api/todo")
class TodoAPIController(
var todoService: TodoSerivce
) {

@PatchMapping("/{id}")
fun updateTodo(
@PathVariable id: Long,
principal: Principal
): ResponseEntity<String> {
val username = principal.name

todoService.changeDone(id)
return ResponseEntity.ok("업데이트 완료")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,7 @@ import org.springframework.stereotype.Repository
@Repository
interface TodoRepository : JpaRepository<TodoEntity, Long> {
fun findByUser(user: UserEntity): List<TodoEntity>


fun findByUserOrderByIsDoneAsc(user: UserEntity): List<TodoEntity>
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,23 @@ import com.namju.simple_todo.todo.entity.TodoEntity
import com.namju.simple_todo.todo.repository.TodoRepository
import com.namju.simple_todo.user.entity.UserEntity
import com.namju.simple_todo.user.repository.UserRepository
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Service

@Service
class TodoSerivce(
var todoRepository: TodoRepository,
var userRepository: UserRepository
) {
val log = LoggerFactory.getLogger(this::class.java)
/**
* 유저 아이디로 Todo 리스트 조회
* @param userId 유저 아이디
* @return List<TodoEntity> Todo 리스트
*/
fun getTodoListByUsername(username: String): List<TodoEntity> {
val user: UserEntity = userRepository.findByUsername(username)
return todoRepository.findByUser(user)
return todoRepository.findByUserOrderByIsDoneAsc(user)
}

/**
Expand All @@ -41,4 +43,12 @@ class TodoSerivce(
)
)
}

fun changeDone(
todoId: Long
) {
val todo: TodoEntity = todoRepository.findById(todoId).get()
todo.isDone = !todo.isDone
log.info(todoRepository.save(todo).toString());
}
}
18 changes: 14 additions & 4 deletions src/main/resources/data.sql
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
-- email: cmsong111@naver.com , password: 1234
insert into user_entity (username, password, nickname)
values ('cmsong111@naver.com', '$2a$10$PdmeJs3yBaHhy6YfTF0Zze1Pw9GnxGFA7syoe/NhOmnmx1jOw43RK', '김남주');
values ('cmsong111@naver.com', '$2a$10$PdmeJs3yBaHhy6YfTF0Zze1Pw9GnxGFA7syoe/NhOmnmx1jOw43RK', '김남주');

-- todo
insert into todo_entity (title, content, is_done, user_idx)
values ('할일1', '할일1 내용', false, 1),
('할일2', '할일2 내용', false, 1),
('할일3', '할일3 내용', true, 1);
values ('할일1', '할일1 내용', false, 1),
('할일2', '할일2 내용', false, 1),
('할일3', '할일3 내용', true, 1),
('할일3', '할일3 내용', true, 1),
('할일3', '할일3 내용', true, 1),
('할일3', '할일3 내용', true, 1),
('할일3', '할일3 내용', true, 1),
('할일3', '할일3 내용', true, 1),
('할일3', '할일3 내용', true, 1),
('할일3', '할일3 내용', true, 1),
('할일3', '할일3 내용', true, 1),
('할일5', '할일3 내용', true, 1),
('할일101', '할일3 내용', true, 1);
4 changes: 2 additions & 2 deletions src/main/resources/templates/fragments/footer.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<!DOCTYPE html>
<html lang="ko" xmlns:th="http://www.thymeleaf.org">
<footer th:fragment="fragment-footer" class="footer fixed-bottom bg-light">
<div class="container m-3">
<footer th:fragment="fragment-footer" class="footer py-5 bg-light mt-auto">
<div class="container">
<div class="row">
<div class="col">
<p>&copy; Simple Todo Application by Kotlin</p>
Expand Down
24 changes: 20 additions & 4 deletions src/main/resources/templates/todo/todo.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,23 @@
<html lang="ko" xmlns:th="http://www.thymeleaf.org">
<head>
<div th:replace="~{/fragments/header.html :: fragment-header}"></div>

</head>
<body>
<script th:inline="javascript">
function changeState(todoIdx) {
console.log(todoIdx + "변경 신청됨");
$.ajax({
type: "PATCH",
url: "/api/todo/" + todoIdx,
contentType: "application/json; charset=utf-8",
}).done(function (response) {
location.reload();
}).fail(function (error) {
alert(JSON.stringify(error));
});
}
</script>
<body class="d-flex flex-column min-vh-100">
<nav>
<div th:replace="~{/fragments/nav.html :: fragment-nav}"></div>
</nav>
Expand All @@ -15,8 +30,9 @@ <h1 class="text-center m-3" th:text="|${username}의 투두 리스트|"></h1>
</button>
<ul class="list-group">
<li th:each="todo : ${todoList}" class="list-group-item">
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" th:checked="${todo.isDone()}">
<div class="custom-control custom-checkbox" th:onclick="|changeState(${todo.id})|" >
<input type="checkbox" class="custom-control-input" th:id="|TODO_${todo.id}|"
th:checked="${todo.done}">
<label class="custom-control-label">
<h4 th:text="${todo.getTitle()}"></h4>
<p th:text="${todo.getContent()}"></p>
Expand All @@ -29,7 +45,7 @@ <h4 th:text="${todo.getTitle()}"></h4>
<!-- 모달 창 -->
<div class="modal fade" id="addTodoModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel"
aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">새로운 투두 추가</h5>
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/templates/user/account.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<head>
<div th:replace="~{/fragments/header.html :: fragment-header}"></div>
</head>
<body>
<body class="d-flex flex-column min-vh-100">
<nav>
<div th:replace="~{/fragments/nav.html :: fragment-nav}"></div>
</nav>
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/templates/user/register.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
});
}
</script>
<body>
<body class="d-flex flex-column min-vh-100">

<div class="container mt-5">
<div class="row justify-content-center">
Expand Down

0 comments on commit e4eaf81

Please sign in to comment.