-
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.
- Loading branch information
Showing
10 changed files
with
241 additions
and
6 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
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
74 changes: 74 additions & 0 deletions
74
src/main/kotlin/me/gistory/newbies_server_24/controllers/PostController.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,74 @@ | ||
package me.gistory.newbies_server_24.controllers | ||
|
||
import io.swagger.v3.oas.annotations.security.SecurityRequirement | ||
import io.swagger.v3.oas.annotations.tags.Tag | ||
import me.gistory.newbies_server_24.dto.CreatePostDto | ||
import me.gistory.newbies_server_24.dto.PostDto | ||
import me.gistory.newbies_server_24.dto.PostListDto | ||
import me.gistory.newbies_server_24.dto.UpdatePostDto | ||
import me.gistory.newbies_server_24.entities.Post | ||
import me.gistory.newbies_server_24.services.PostService | ||
import org.springframework.security.core.Authentication | ||
import org.springframework.web.bind.annotation.* | ||
import java.util.* | ||
|
||
@Tag(name = "Post") | ||
@RestController | ||
@RequestMapping("/posts") | ||
class PostController ( | ||
private val postService: PostService | ||
) { | ||
|
||
@GetMapping("") | ||
fun getPosts( | ||
@RequestParam(value = "tag", required = false) tag: String?, | ||
@RequestParam(value = "boardUuid", required = false) boardUuid: String?, | ||
): PostListDto { | ||
val boardId = boardUuid?.let { UUID.fromString(it) } | ||
val posts = postService.getPosts(boardId = boardId, tagId = tag).toList() | ||
return PostListDto( | ||
count = posts.size, | ||
list = posts.map(Post::toPostDto) | ||
) | ||
} | ||
|
||
@GetMapping("/search") | ||
fun searchPosts( | ||
@RequestParam(value = "keyword") keyword: String | ||
): PostListDto { | ||
val posts = postService.searchPost(keyword).toList() | ||
return PostListDto( | ||
count = posts.size, | ||
list = posts.map(Post::toPostDto) | ||
) | ||
} | ||
|
||
@SecurityRequirement(name = "Bearer Authorization") | ||
@PostMapping("") | ||
fun createPost( | ||
@RequestBody post: CreatePostDto, | ||
@RequestParam(value = "boardUuid") boardUuid: String, | ||
authentication: Authentication | ||
): PostDto { | ||
return postService.createPost(post, UUID.fromString(boardUuid), authentication.name).toPostDto() | ||
} | ||
|
||
@SecurityRequirement(name = "Bearer Authorization") | ||
@PatchMapping("/{uuid}") | ||
fun updatePost( | ||
@RequestBody post: UpdatePostDto, | ||
@PathVariable("uuid") uuid: String, | ||
authentication: Authentication | ||
): PostDto { | ||
return postService.updatePost(post, UUID.fromString(uuid), authentication.name).toPostDto() | ||
} | ||
|
||
@SecurityRequirement(name = "Bearer Authorization") | ||
@DeleteMapping("/{uuid}") | ||
fun deletePost( | ||
@PathVariable("uuid") uuid: String, | ||
authentication: Authentication | ||
): Unit { | ||
return postService.deletePost(UUID.fromString(uuid), authentication.name) | ||
} | ||
} |
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
31 changes: 31 additions & 0 deletions
31
src/main/kotlin/me/gistory/newbies_server_24/dto/PostDto.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,31 @@ | ||
package me.gistory.newbies_server_24.dto | ||
|
||
import me.gistory.newbies_server_24.entities.Board | ||
import java.util.* | ||
|
||
data class CreatePostDto( | ||
val title: String, | ||
val body: String, | ||
val tags: List<String> | ||
) | ||
|
||
data class UpdatePostDto( | ||
val title: String?, | ||
val body: String?, | ||
val tags: List<String>?, | ||
) | ||
|
||
data class PostDto ( | ||
val id: UUID, | ||
val title: String, | ||
val body: String, | ||
val tags: List<String>, | ||
val board: BoardSummaryDto, | ||
val createdAt: Date, | ||
val createdBy: UserDto | ||
) | ||
|
||
data class PostListDto ( | ||
val count: Int, | ||
val list: List<PostDto>, | ||
) |
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
7 changes: 7 additions & 0 deletions
7
src/main/kotlin/me/gistory/newbies_server_24/exceptions/PostNotFoundException.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,7 @@ | ||
package me.gistory.newbies_server_24.exceptions | ||
|
||
import org.springframework.http.HttpStatus | ||
import org.springframework.web.bind.annotation.ResponseStatus | ||
|
||
@ResponseStatus(value = HttpStatus.NOT_FOUND, reason = "Post Not Found") | ||
class PostNotFoundException : RuntimeException() |
7 changes: 7 additions & 0 deletions
7
src/main/kotlin/me/gistory/newbies_server_24/exceptions/TagNotFoundException.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,7 @@ | ||
package me.gistory.newbies_server_24.exceptions | ||
|
||
import org.springframework.http.HttpStatus | ||
import org.springframework.web.bind.annotation.ResponseStatus | ||
|
||
@ResponseStatus(HttpStatus.NOT_FOUND, reason = "Tag Not Found") | ||
class TagNotFoundException : RuntimeException() |
9 changes: 8 additions & 1 deletion
9
src/main/kotlin/me/gistory/newbies_server_24/repositories/PostRepository.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,7 +1,14 @@ | ||
package me.gistory.newbies_server_24.repositories | ||
|
||
import me.gistory.newbies_server_24.entities.Board | ||
import me.gistory.newbies_server_24.entities.Post | ||
import me.gistory.newbies_server_24.entities.Tag | ||
import org.springframework.data.repository.CrudRepository | ||
import java.util.UUID | ||
|
||
interface PostRepository : CrudRepository<Post, UUID> | ||
interface PostRepository : CrudRepository<Post, UUID> { | ||
fun findByTags(tag: Tag): List<Post> | ||
fun findByBoard(board: Board): List<Post> | ||
fun findByTagsAndBoard(tag: Tag, board: Board): List<Post> | ||
fun findByTitleContainingIgnoreCaseOrBodyContainingIgnoreCase(titleContainingIgnoreCase: String, bodyContainingIgnoreCase: String): List<Post> | ||
} |
93 changes: 93 additions & 0 deletions
93
src/main/kotlin/me/gistory/newbies_server_24/services/PostService.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,93 @@ | ||
package me.gistory.newbies_server_24.services | ||
|
||
import jakarta.transaction.Transactional | ||
import me.gistory.newbies_server_24.dto.CreatePostDto | ||
import me.gistory.newbies_server_24.dto.UpdatePostDto | ||
import me.gistory.newbies_server_24.entities.Post | ||
import me.gistory.newbies_server_24.exceptions.* | ||
import me.gistory.newbies_server_24.repositories.AuthRepository | ||
import me.gistory.newbies_server_24.repositories.BoardRepository | ||
import me.gistory.newbies_server_24.repositories.PostRepository | ||
import me.gistory.newbies_server_24.repositories.TagRepository | ||
import org.slf4j.LoggerFactory | ||
import org.springframework.data.repository.findByIdOrNull | ||
import org.springframework.stereotype.Service | ||
import java.util.UUID | ||
|
||
@Service | ||
@Transactional | ||
class PostService ( | ||
private val postRepository: PostRepository, | ||
private val tagRepository: TagRepository, | ||
private val boardRepository: BoardRepository, | ||
private val authRepository: AuthRepository, | ||
) { | ||
private val logger = LoggerFactory.getLogger(AuthService::class.java) | ||
|
||
fun getPosts(boardId: UUID?, tagId: String?): List<Post> { | ||
if (boardId == null && tagId == null) { | ||
return postRepository.findAll().toList() | ||
} else if (boardId == null && tagId != null) { | ||
val tag = tagRepository.findByIdOrNull(tagId) ?: return postRepository.findAll().toList() | ||
return postRepository.findByTags(tag) | ||
} else if (tagId == null && boardId != null) { | ||
val board = boardRepository.findByIdOrNull(boardId) ?: return postRepository.findAll().toList() | ||
return postRepository.findByBoard(board) | ||
} else if (tagId != null && boardId != null) { | ||
val tag = tagRepository.findByIdOrNull(tagId) ?: return postRepository.findAll().toList() | ||
val board = boardRepository.findByIdOrNull(boardId) ?: return postRepository.findAll().toList() | ||
return postRepository.findByTagsAndBoard(tag, board) | ||
} | ||
throw ForbiddenException() | ||
} | ||
|
||
fun searchPost(keyword: String): List<Post> { | ||
return postRepository.findByTitleContainingIgnoreCaseOrBodyContainingIgnoreCase(keyword, keyword) | ||
} | ||
|
||
fun createPost(dto: CreatePostDto, boardId: UUID, userEmail: String): Post { | ||
val board = boardRepository.findByIdOrNull(boardId) ?: throw BoardNotFoundException() | ||
val user = authRepository.findByEmail(userEmail) ?: throw UserNotFoundException() | ||
val tags = try { | ||
tagRepository.findAllById(dto.tags).toMutableSet() | ||
} catch(e: Error) { | ||
throw TagNotFoundException() | ||
} | ||
return postRepository.save(Post( | ||
title = dto.title, | ||
body = dto.body, | ||
tags = tags, | ||
createdBy = user, | ||
board = board, | ||
)) | ||
} | ||
|
||
fun updatePost(dto: UpdatePostDto, postId: UUID, userEmail: String): Post { | ||
val post = postRepository.findByIdOrNull(postId) ?: throw PostNotFoundException() | ||
val user = authRepository.findByEmail(userEmail) ?: throw UserNotFoundException() | ||
if (post.createdBy.id != user.id) { | ||
throw ForbiddenException() | ||
} | ||
post.title = dto.title ?: post.title | ||
post.body = dto.body ?: post.body | ||
if (dto.tags != null) { | ||
val tags = try { | ||
tagRepository.findAllById(dto.tags).toMutableSet() | ||
} catch(e: Error) { | ||
throw TagNotFoundException() | ||
} | ||
post.tags = tags | ||
} | ||
return postRepository.save(post) | ||
} | ||
|
||
fun deletePost(postId: UUID, userEmail: String): Unit { | ||
val user = authRepository.findByEmail(userEmail) ?: throw UserNotFoundException() | ||
val post = postRepository.findByIdOrNull(postId) | ||
?: throw PostNotFoundException() | ||
if (post.createdBy.id != user.id) { | ||
throw ForbiddenException() | ||
} | ||
postRepository.delete(post) | ||
} | ||
} |