-
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.
Browse files
Browse the repository at this point in the history
#199 TODO feature
- Loading branch information
Showing
15 changed files
with
1,029 additions
and
4 deletions.
There are no files selected for viewing
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
58 changes: 58 additions & 0 deletions
58
src/main/kotlin/com/tistory/shanepark/dutypark/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 |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package com.tistory.shanepark.dutypark.todo.controller | ||
|
||
import com.tistory.shanepark.dutypark.member.domain.annotation.Login | ||
import com.tistory.shanepark.dutypark.security.domain.dto.LoginMember | ||
import com.tistory.shanepark.dutypark.todo.domain.dto.TodoRequest | ||
import com.tistory.shanepark.dutypark.todo.domain.dto.TodoResponse | ||
import com.tistory.shanepark.dutypark.todo.service.TodoService | ||
import org.springframework.validation.annotation.Validated | ||
import org.springframework.web.bind.annotation.* | ||
import java.util.* | ||
|
||
@RestController | ||
@RequestMapping("/api/todos") | ||
class TodoController( | ||
private val todoService: TodoService | ||
) { | ||
|
||
@GetMapping | ||
fun todoList( | ||
@Login loginMember: LoginMember | ||
): List<TodoResponse> { | ||
return todoService.todoList(loginMember) | ||
} | ||
|
||
@PostMapping | ||
fun addTodo( | ||
@Login loginMember: LoginMember, | ||
@RequestBody @Validated todoRequest: TodoRequest | ||
): TodoResponse { | ||
return todoService.addTodo(loginMember, todoRequest.title, todoRequest.content) | ||
} | ||
|
||
@PutMapping("/{id}") | ||
fun editTodo( | ||
@Login loginMember: LoginMember, | ||
@PathVariable id: UUID, | ||
@RequestBody @Validated todoRequest: TodoRequest | ||
): TodoResponse { | ||
return todoService.editTodo(loginMember, id, todoRequest.title, todoRequest.content) | ||
} | ||
|
||
@PatchMapping("/position") | ||
fun updatePosition( | ||
@Login loginMember: LoginMember, | ||
@RequestBody ids: List<UUID> | ||
) { | ||
todoService.updatePosition(loginMember, ids) | ||
} | ||
|
||
@DeleteMapping("/{id}") | ||
fun deleteTodo( | ||
@Login loginMember: LoginMember, | ||
@PathVariable id: UUID | ||
) { | ||
todoService.deleteTodo(loginMember, id) | ||
} | ||
|
||
} |
15 changes: 15 additions & 0 deletions
15
src/main/kotlin/com/tistory/shanepark/dutypark/todo/domain/dto/TodoRequest.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,15 @@ | ||
package com.tistory.shanepark.dutypark.todo.domain.dto | ||
|
||
import jakarta.validation.constraints.NotBlank | ||
import org.hibernate.validator.constraints.Length | ||
import org.springframework.validation.annotation.Validated | ||
|
||
@Validated | ||
data class TodoRequest( | ||
|
||
@field: NotBlank | ||
@field: Length(min = 1, max = 100) | ||
val title: String, | ||
|
||
val content: String | ||
) |
26 changes: 26 additions & 0 deletions
26
src/main/kotlin/com/tistory/shanepark/dutypark/todo/domain/dto/TodoResponse.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,26 @@ | ||
package com.tistory.shanepark.dutypark.todo.domain.dto | ||
|
||
import com.tistory.shanepark.dutypark.todo.domain.entity.Todo | ||
import java.time.LocalDateTime | ||
|
||
data class TodoResponse( | ||
val id: String, | ||
val title: String, | ||
val content: String, | ||
val position: Int, | ||
val createdDate: LocalDateTime, | ||
) { | ||
|
||
companion object { | ||
fun from(todo: Todo): TodoResponse { | ||
return TodoResponse( | ||
id = todo.id.toString(), | ||
title = todo.title, | ||
content = todo.content, | ||
position = todo.position, | ||
createdDate = todo.createdDate | ||
) | ||
} | ||
} | ||
|
||
} |
39 changes: 39 additions & 0 deletions
39
src/main/kotlin/com/tistory/shanepark/dutypark/todo/domain/entity/Todo.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,39 @@ | ||
package com.tistory.shanepark.dutypark.todo.domain.entity | ||
|
||
import com.tistory.shanepark.dutypark.common.domain.entity.EntityBase | ||
import com.tistory.shanepark.dutypark.member.domain.entity.Member | ||
import jakarta.persistence.* | ||
import org.springframework.data.jpa.domain.support.AuditingEntityListener | ||
import java.time.LocalDateTime | ||
|
||
@Entity | ||
@EntityListeners(AuditingEntityListener::class) | ||
class Todo( | ||
@ManyToOne(fetch = FetchType.LAZY) | ||
@JoinColumn(name = "member_id", nullable = false) | ||
val member: Member, | ||
|
||
@Column(name = "title", nullable = false, length = 50) | ||
var title: String, | ||
|
||
@Column(name = "content", nullable = false, length = 50) | ||
var content: String, | ||
|
||
@Column(name = "position", nullable = false) | ||
var position: Int, | ||
|
||
) : EntityBase() { | ||
|
||
fun update(title: String, content: String) { | ||
this.title = title | ||
this.content = content | ||
this.modifiedDate = LocalDateTime.now() | ||
} | ||
|
||
@Column(name = "modified_date", updatable = true) | ||
var modifiedDate: LocalDateTime = LocalDateTime.now() | ||
|
||
@Column(name = "created_date", updatable = false) | ||
val createdDate: LocalDateTime = LocalDateTime.now() | ||
|
||
} |
21 changes: 21 additions & 0 deletions
21
src/main/kotlin/com/tistory/shanepark/dutypark/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,21 @@ | ||
package com.tistory.shanepark.dutypark.todo.repository | ||
|
||
import com.tistory.shanepark.dutypark.member.domain.entity.Member | ||
import com.tistory.shanepark.dutypark.todo.domain.entity.Todo | ||
import org.springframework.data.jpa.repository.JpaRepository | ||
import org.springframework.data.jpa.repository.Query | ||
import org.springframework.data.repository.query.Param | ||
import java.util.* | ||
|
||
interface TodoRepository : JpaRepository<Todo, UUID> { | ||
|
||
@Query( | ||
"SELECT COALESCE(MAX(t.position), 0) " + | ||
"FROM Todo t " + | ||
"WHERE t.member = :member" | ||
) | ||
fun findMaxPositionByMember(@Param("member") member: Member): Int | ||
|
||
fun findAllByMemberOrderByPosition(member: Member): List<Todo> | ||
|
||
} |
79 changes: 79 additions & 0 deletions
79
src/main/kotlin/com/tistory/shanepark/dutypark/todo/service/TodoService.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,79 @@ | ||
package com.tistory.shanepark.dutypark.todo.service | ||
|
||
import com.tistory.shanepark.dutypark.member.domain.entity.Member | ||
import com.tistory.shanepark.dutypark.member.repository.MemberRepository | ||
import com.tistory.shanepark.dutypark.security.domain.dto.LoginMember | ||
import com.tistory.shanepark.dutypark.todo.domain.dto.TodoResponse | ||
import com.tistory.shanepark.dutypark.todo.domain.entity.Todo | ||
import com.tistory.shanepark.dutypark.todo.repository.TodoRepository | ||
import org.springframework.stereotype.Service | ||
import org.springframework.transaction.annotation.Transactional | ||
import java.util.* | ||
|
||
@Service | ||
@Transactional | ||
class TodoService( | ||
private val memberRepository: MemberRepository, | ||
private val todoRepository: TodoRepository | ||
) { | ||
|
||
@Transactional(readOnly = true) | ||
fun todoList(loginMember: LoginMember): List<TodoResponse> { | ||
val member = findMember(loginMember) | ||
return todoRepository.findAllByMemberOrderByPosition(member) | ||
.map { TodoResponse.from(it) } | ||
} | ||
|
||
fun addTodo(loginMember: LoginMember, title: String, content: String): TodoResponse { | ||
val member = findMember(loginMember) | ||
val todoLastPosition = todoRepository.findMaxPositionByMember(member) | ||
|
||
val todo = Todo(member = member, title = title, content = content, position = todoLastPosition + 1) | ||
todoRepository.save(todo) | ||
|
||
return TodoResponse.from(todo) | ||
} | ||
|
||
fun editTodo(loginMember: LoginMember, id: UUID, title: String, content: String): TodoResponse { | ||
val member = findMember(loginMember) | ||
|
||
val todo = todoRepository.findById(id) | ||
.orElseThrow { IllegalArgumentException("Todo not found") } | ||
|
||
verifyOwnership(todo, member) | ||
|
||
todo.update(title, content) | ||
return TodoResponse.from(todo) | ||
} | ||
|
||
fun updatePosition(loginMember: LoginMember, ids: List<UUID>) { | ||
val member = findMember(loginMember) | ||
|
||
val indexMap = ids.mapIndexed { index, id -> id to index }.toMap() | ||
val todos = todoRepository.findAllById(ids).sortedBy { indexMap.getValue(it.id) } | ||
|
||
todos.forEachIndexed { index, todo -> | ||
verifyOwnership(todo, member) | ||
todo.position = index | ||
} | ||
} | ||
|
||
fun deleteTodo(loginMember: LoginMember, id: UUID) { | ||
val member = findMember(loginMember) | ||
val todo = todoRepository.findById(id).orElseThrow { IllegalArgumentException("Todo not found") } | ||
verifyOwnership(todo, member) | ||
|
||
todoRepository.delete(todo) | ||
} | ||
|
||
private fun verifyOwnership(todo: Todo, member: Member) { | ||
if (todo.member.id != member.id) { | ||
throw IllegalArgumentException("Todo is not yours") | ||
} | ||
} | ||
|
||
private fun findMember(loginMember: LoginMember): Member = | ||
memberRepository.findById(loginMember.id) | ||
.orElseThrow { IllegalArgumentException("Member not found") } | ||
|
||
} |
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,12 @@ | ||
CREATE TABLE `todo` | ||
( | ||
`id` char(36) PRIMARY KEY, | ||
`member_id` bigint NOT NULL, | ||
`title` varchar(50) NOT NULL, | ||
`content` varchar(50) NOT NULL, | ||
`position` int NOT NULL, | ||
`created_date` datetime NOT NULL, | ||
`modified_date` datetime NOT NULL, | ||
|
||
index `idx_member_id` (`member_id`) | ||
); |
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
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
Large diffs are not rendered by default.
Oops, something went wrong.
Oops, something went wrong.