-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
로그인 된 유저에 따른 Todo List 단순 조회 기능
- Loading branch information
Showing
7 changed files
with
87 additions
and
26 deletions.
There are no files selected for viewing
20 changes: 11 additions & 9 deletions
20
src/main/kotlin/com/namju/simple_todo/todo/controller/TodoController.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
22
src/main/kotlin/com/namju/simple_todo/todo/entity/TodoEntity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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})" | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/kotlin/com/namju/simple_todo/todo/repository/TodoRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
23
src/main/kotlin/com/namju/simple_todo/todo/service/TodoSerivce.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters