Skip to content

Commit

Permalink
🐛 fix: LAZY 타입에 대한 Jackson 오류 해결 (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
yu-heejin committed Jan 28, 2024
1 parent 07d5a35 commit 3d28aad
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import com.kotlin.study.dongambackend.domain.post.validator.type.BoardCategory
import com.kotlin.study.dongambackend.domain.post.dto.request.PostCreateRequest
import com.kotlin.study.dongambackend.domain.post.dto.request.PostUpdateRequest
import com.kotlin.study.dongambackend.domain.post.dto.response.GetAllPostByCategoryResponse
import com.kotlin.study.dongambackend.domain.post.entity.Post
import com.kotlin.study.dongambackend.domain.post.dto.response.GetPostByIdResponse
import com.kotlin.study.dongambackend.domain.post.service.PostService

import org.springframework.data.domain.Pageable
Expand Down Expand Up @@ -33,7 +33,7 @@ class PostController(private val postService: PostService) {
}

@GetMapping("/{postId}")
fun getPostById(@PathVariable postId: Long): ResponseEntity<Post> {
fun getPostById(@PathVariable postId: Long): ResponseEntity<GetPostByIdResponse> {
val post = postService.getPostById(postId)
return ResponseEntity.ok().body(post)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.kotlin.study.dongambackend.domain.post.dto.response

import com.kotlin.study.dongambackend.domain.post.validator.type.BoardCategory

data class GetPostByIdResponse(
val id: Long,
val user: UserInformation,
val category: BoardCategory,
val title: String,
val content: String,
val likes: Int,
val views: Int
)
Original file line number Diff line number Diff line change
Expand Up @@ -23,31 +23,31 @@ class Post(
val userId: User,

@NotNull
var title: String?,
var title: String,

var content: String?,
var content: String,

@NotNull
@Enumerated(EnumType.STRING)
val category: BoardCategory,

@ColumnDefault("0")
val likes: Int? = 0,
val likes: Int = 0,

@ColumnDefault("0")
val views: Int? = 0,
val views: Int = 0,

@Column(name = "is_deleted")
@ColumnDefault("false")
val isDeleted: Boolean? = false,
val isDeleted: Boolean = false,

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
val id: Long? = null
) : BaseTimeEntity() {

fun updatePost(postUpdateRequest: PostUpdateRequest) {
title = postUpdateRequest.title
content = postUpdateRequest.content
title = postUpdateRequest.title ?: ""
content = postUpdateRequest.content ?: ""
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,56 @@ package com.kotlin.study.dongambackend.domain.post.mapper
import com.kotlin.study.dongambackend.domain.post.dto.request.PostCreateRequest
import com.kotlin.study.dongambackend.domain.post.dto.response.FindAllPostByCategory
import com.kotlin.study.dongambackend.domain.post.dto.response.GetAllPostByCategoryResponse
import com.kotlin.study.dongambackend.domain.post.dto.response.GetPostByIdResponse
import com.kotlin.study.dongambackend.domain.post.dto.response.UserInformation
import com.kotlin.study.dongambackend.domain.post.entity.Post
import com.kotlin.study.dongambackend.domain.post.validator.type.BoardCategory
import com.kotlin.study.dongambackend.domain.user.entity.User

import org.springframework.stereotype.Component

@Component
class PostMapper {

// TODO: null 값에 대한 예외처리
fun convertCreatePostReqDtoToEntity(user: User, createRequest: PostCreateRequest): Post {
return Post(user, createRequest.title, createRequest.content, createRequest.category)
return Post(
user,
createRequest.title ?: "",
createRequest.content ?: "",
createRequest.category
)
}

fun toGetAllPostResponse(posts: List<FindAllPostByCategory>, postCount: Int): GetAllPostByCategoryResponse {
return GetAllPostByCategoryResponse(posts, postCount)
}

fun toPostResponse(post: Post): GetPostByIdResponse? {
val userInformation = toUser(post.userId)

return post.id?.let {
GetPostByIdResponse(
it,
userInformation,
post.category,
post.title,
post.content,
post.likes,
post.views
)
}
}

private fun toUser(user: User?) = user?.id?.let {
UserInformation(
it,
user.studentId,
user.nickname
)
} ?: UserInformation(
0L,
"00000000",
"탈퇴한 사용자"
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.kotlin.study.dongambackend.domain.post.entity.id.PostLikeId
import com.kotlin.study.dongambackend.domain.post.dto.request.PostCreateRequest
import com.kotlin.study.dongambackend.domain.post.dto.request.PostUpdateRequest
import com.kotlin.study.dongambackend.domain.post.dto.response.GetAllPostByCategoryResponse
import com.kotlin.study.dongambackend.domain.post.dto.response.GetPostByIdResponse
import com.kotlin.study.dongambackend.domain.post.entity.Post
import com.kotlin.study.dongambackend.domain.post.entity.PostLike
import com.kotlin.study.dongambackend.domain.post.mapper.PostMapper
Expand Down Expand Up @@ -35,9 +36,11 @@ class PostService(
}

@Transactional(readOnly = true)
fun getPostById(postId: Long): Post? {
return postRepository.findByIdOrNull(postId)
fun getPostById(postId: Long): GetPostByIdResponse? {
val post = postRepository.findByIdOrNull(postId)
?: throw NoSuchElementException()

return postMapper.toPostResponse(post)
}

fun createPost(postCreateRequest: PostCreateRequest, userId: Long): Long? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ class WebSecurityConfig(private val authenticationFilter: AuthenticationFilter)
.formLogin().disable()
.headers { it.frameOptions().sameOrigin() }
.authorizeHttpRequests {
it.antMatchers("/api/user/sign-in", "/api/user/sign-up").permitAll()
.antMatchers("/api/admin/**").hasRole(UserRole.ADMIN.toString())
.antMatchers("/api/**").hasRole(UserRole.USER.toString())
// it.antMatchers("/api/user/sign-in", "/api/user/sign-up").permitAll()
// .antMatchers("/api/admin/**").hasRole(UserRole.ADMIN.toString())
// .antMatchers("/api/**").hasRole(UserRole.USER.toString())
it.antMatchers("/api/**").permitAll()
}
.sessionManagement {
it.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
Expand Down

0 comments on commit 3d28aad

Please sign in to comment.