Skip to content

Commit

Permalink
feat: Todo List 조회 기능
Browse files Browse the repository at this point in the history
로그인 된 유저에 따른 Todo List 단순 조회 기능
  • Loading branch information
cmsong111 committed Sep 9, 2023
1 parent 51f7588 commit 61e59a5
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
package com.namju.simple_todo.todo.controller

import jakarta.servlet.http.HttpServletRequest
import org.slf4j.LoggerFactory
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.RequestMapping
import java.security.Principal

@Controller
@RequestMapping("/todo")
class TodoController {

val log = LoggerFactory.getLogger(this::class.java)
class TodoController(
var todoService: TodoSerivce
) {
@GetMapping
@PreAuthorize("hasRole('ROLE_USER')")
fun todo(
httpServletRequest: HttpServletRequest
principal: Principal,
model: Model
): String {
log.info("user: ${httpServletRequest.session.getAttribute("user")}")
todoService.getTodoListByUsername(principal.name).apply {
model.addAttribute("todoList", this)
}
return "todo/todo"
}


}
6 changes: 6 additions & 0 deletions src/main/kotlin/com/namju/simple_todo/todo/dto/TodoForm.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.namju.simple_todo.todo.dto

data class TodoForm (
var title: String,
var content: String,
)
22 changes: 22 additions & 0 deletions src/main/kotlin/com/namju/simple_todo/todo/entity/TodoEntity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.namju.simple_todo.todo.entity

import com.namju.simple_todo.user.entity.UserEntity
import jakarta.persistence.*

@Entity
class TodoEntity(
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
var id: Long = 0,

var title: String,
var content: String,
var isDone: Boolean = false,

@ManyToOne(cascade = [CascadeType.ALL])
var user: UserEntity,
) {
override fun toString(): String {
return "TodoEntity(id=$id, title='$title', content='$content', isDone=$isDone, user=${user.username})"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.namju.simple_todo.todo.repository

import com.namju.simple_todo.todo.entity.TodoEntity
import com.namju.simple_todo.user.entity.UserEntity
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.stereotype.Repository

@Repository
interface TodoRepository : JpaRepository<TodoEntity, Long> {
fun findByUser(user: UserEntity): List<TodoEntity>
}
23 changes: 23 additions & 0 deletions src/main/kotlin/com/namju/simple_todo/todo/service/TodoSerivce.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.namju.simple_todo.todo.service

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.springframework.stereotype.Service

@Service
class TodoSerivce(
var todoRepository: TodoRepository,
var userRepository: UserRepository
) {
/**
* 유저 아이디로 Todo 리스트 조회
* @param userId 유저 아이디
* @return List<TodoEntity> Todo 리스트
*/
fun getTodoListByUsername(username: String): List<TodoEntity> {
val user: UserEntity = userRepository.findByUsername(username)
return todoRepository.findByUser(user)
}
}
6 changes: 6 additions & 0 deletions src/main/resources/data.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
-- email: cmsong111@naver.com , password: 1234
insert into user_entity (username, password, nickname)
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);
25 changes: 8 additions & 17 deletions src/main/resources/templates/todo/todo.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,15 @@
</nav>

<div class="container mt-5">
<h1 class="text-center">투두 리스트</h1>
<h1 class="text-center m-3">투두 리스트</h1>
<ul class="list-group">
<li class="list-group-item">
<div class="form-check">
<input type="checkbox" class="form-check-input" id="todo1">
<label class="form-check-label" for="todo1">할 일 1</label>
</div>
</li>
<li class="list-group-item">
<div class="form-check">
<input type="checkbox" class="form-check-input" id="todo2">
<label class="form-check-label" for="todo2">할 일 2</label>
</div>
</li>
<li class="list-group-item">
<div class="form-check">
<input type="checkbox" class="form-check-input" id="todo3">
<label class="form-check-label" for="todo3">할 일 3</label>
<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" >
<h4 th:text="${todo.getTitle()}"></h4>
<p th:text="${todo.getContent()}"></p>
</label>
</div>
</li>
</ul>
Expand Down

0 comments on commit 61e59a5

Please sign in to comment.