Skip to content

Commit

Permalink
feat: todo 추가 기능
Browse files Browse the repository at this point in the history
  • Loading branch information
cmsong111 committed Sep 10, 2023
1 parent f82479e commit 4079ac9
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.namju.simple_todo.todo.controller

import com.namju.simple_todo.todo.dto.TodoForm
import com.namju.simple_todo.todo.service.TodoSerivce
import org.springframework.security.access.prepost.PreAuthorize
import org.springframework.stereotype.Controller
import org.springframework.ui.Model
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestMapping
import java.security.Principal

Expand All @@ -22,6 +24,21 @@ class TodoController(
todoService.getTodoListByUsername(principal.name).apply {
model.addAttribute("todoList", this)
}
model.addAttribute("username", principal.name)
return "todo/todo"
}

@PostMapping
@PreAuthorize("hasRole('ROLE_USER')")
fun addTodo(
principal: Principal,
todoForm: TodoForm
) : String {
todoService.addTodo(
username = principal.name,
title = todoForm.title,
content = todoForm.content
)
return "redirect:/todo"
}
}
27 changes: 24 additions & 3 deletions src/main/kotlin/com/namju/simple_todo/todo/service/TodoSerivce.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,33 @@ class TodoSerivce(
var userRepository: UserRepository
) {
/**
* 유저 아이디로 Todo 리스트 조회
* @param userId 유저 아이디
* @return List<TodoEntity> Todo 리스트
* 유저 아이디로 Todo 리스트 조회
* @param userId 유저 아이디
* @return List<TodoEntity> Todo 리스트
*/
fun getTodoListByUsername(username: String): List<TodoEntity> {
val user: UserEntity = userRepository.findByUsername(username)
return todoRepository.findByUser(user)
}

/**
* Todo 추가
* @param username 유저 아이디
* @param title Todo 제목
* @param content Todo 내용
*/
fun addTodo(
username: String,
title: String,
content: String
) {
val user: UserEntity = userRepository.findByUsername(username)
todoRepository.save(
TodoEntity(
title = title,
content = content,
user = user
)
)
}
}
39 changes: 35 additions & 4 deletions src/main/resources/templates/todo/todo.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@
<div th:replace="~{/fragments/nav.html :: fragment-nav}"></div>
</nav>

<div class="container mt-5">
<h1 class="text-center m-3">투두 리스트</h1>
<div class="container mtb-8">
<h1 class="text-center m-3" th:text="|${username}의 투두 리스트|"></h1>
<button type="button" class="btn btn-primary m-3" data-toggle="modal" data-target="#addTodoModal">
New Todo
</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()}">
<label class="custom-control-label" >
<input type="checkbox" class="custom-control-input" th:checked="${todo.isDone()}">
<label class="custom-control-label">
<h4 th:text="${todo.getTitle()}"></h4>
<p th:text="${todo.getContent()}"></p>
</label>
Expand All @@ -23,6 +26,34 @@ <h4 th:text="${todo.getTitle()}"></h4>
</ul>
</div>

<!-- 모달 창 -->
<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-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">새로운 투두 추가</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<div class="modal-body">
<form th:action="@{/todo}" method="post">
<div class="form-group">
<label for="title">제목</label>
<input type="text" class="form-control" id="title" name="title" required>
</div>
<div class="form-group">
<label for="content">내용</label>
<textarea class="form-control" id="content" name="content" rows="3"></textarea>
</div>
<button type="submit" class="btn btn-primary">추가</button>
</form>
</div>
</div>
</div>
</div>

<footer>
<div th:replace="~{/fragments/footer.html :: fragment-footer}"></div>
</footer>
Expand Down

0 comments on commit 4079ac9

Please sign in to comment.