Skip to content

Commit

Permalink
Merge pull request #167 from PSR-Co/refactor/166-assember-to-dto
Browse files Browse the repository at this point in the history
[refactor] Assembler 함수 -> DTO, Entity 이전
  • Loading branch information
chaerlo127 authored Sep 9, 2023
2 parents e0e42f1 + 80eed1b commit aae0bce
Show file tree
Hide file tree
Showing 18 changed files with 200 additions and 205 deletions.
50 changes: 0 additions & 50 deletions src/main/kotlin/com/psr/psr/cs/dto/assembler/CsAssembler.kt

This file was deleted.

14 changes: 13 additions & 1 deletion src/main/kotlin/com/psr/psr/cs/dto/response/FaqListRes.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
package com.psr.psr.cs.dto.response

import com.psr.psr.cs.entity.Faq
import java.util.stream.Collectors

data class FaqListRes (
val faqLists: List<FaqRes>?
)
){
companion object{
fun toFaqListRes(faqList: List<Faq>?) : FaqListRes {
if(faqList!!.isEmpty()) return FaqListRes(null)
return FaqListRes(faqList.stream().map {
f -> FaqRes(f.id, f.type.value, f.title)
}.collect(Collectors.toList()))
}
}
}
9 changes: 8 additions & 1 deletion src/main/kotlin/com/psr/psr/cs/dto/response/FaqRes.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
package com.psr.psr.cs.dto.response

import com.fasterxml.jackson.annotation.JsonInclude
import com.psr.psr.cs.entity.Faq

data class FaqRes (
val faqId: Long,
val type: String,
val title: String,
@JsonInclude(JsonInclude.Include.NON_NULL)
val content: String?= null,
)
){
companion object{
fun toFaqRes(faq: Faq): FaqRes {
return FaqRes(faq.id, faq.type.value, faq.title, faq.content)
}
}
}
21 changes: 20 additions & 1 deletion src/main/kotlin/com/psr/psr/cs/dto/response/NoticeListRes.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
package com.psr.psr.cs.dto.response

import com.psr.psr.cs.dto.response.NoticeRes.Companion.toNoticeResHome
import com.psr.psr.cs.entity.Notice
import java.util.stream.Collectors

data class NoticeListRes (
val noticeLists: List<NoticeRes>?
)
){
companion object{
fun toNoticeListRes(noticeList: List<Notice>?): NoticeListRes {
if (noticeList!!.isEmpty()) return NoticeListRes(null)
return NoticeListRes(noticeList.stream().map {
n -> NoticeRes(n.id, n.title, n.createdAt)
}.collect(Collectors.toList()))
}

fun toNoticeListResForHomePage(noticeList: List<Notice>?): NoticeListRes {
return NoticeListRes(
noticeLists = noticeList?.map { n -> toNoticeResHome(n) }!!.toList()
)
}
}
}
13 changes: 12 additions & 1 deletion src/main/kotlin/com/psr/psr/cs/dto/response/NoticeRes.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.psr.psr.cs.dto.response

import com.fasterxml.jackson.annotation.JsonFormat
import com.fasterxml.jackson.annotation.JsonInclude
import com.psr.psr.cs.entity.Notice
import java.time.LocalDateTime

data class NoticeRes (
Expand All @@ -14,4 +15,14 @@ data class NoticeRes (
val imgUrl: String ?= null,
@JsonInclude(JsonInclude.Include.NON_NULL)
val content: String ?= null
)
){
companion object{
fun toNoticeRes(notice: Notice): NoticeRes {
return NoticeRes(notice.id, notice.title, notice.createdAt, notice.imgUrl, notice.content)
}

fun toNoticeResHome(notice: Notice): NoticeRes {
return NoticeRes(noticeId = notice.id, title = notice.title)
}
}
}
17 changes: 7 additions & 10 deletions src/main/kotlin/com/psr/psr/cs/service/CsService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import com.psr.psr.cs.dto.response.FaqListRes
import com.psr.psr.cs.dto.response.FaqRes
import com.psr.psr.cs.dto.response.NoticeListRes
import com.psr.psr.cs.dto.response.NoticeRes
import com.psr.psr.cs.dto.assembler.CsAssembler
import com.psr.psr.cs.entity.FaqType
import com.psr.psr.cs.repository.FaqRepository
import com.psr.psr.cs.repository.NoticeRepository
Expand All @@ -17,37 +16,35 @@ import org.springframework.stereotype.Service
class CsService(
private val faqRepository: FaqRepository,
private val noticeRepository: NoticeRepository,
private val csAssembler: CsAssembler

) {
// 공지사항 메인
fun getNotices() : NoticeListRes {
return csAssembler.toNoticeListRes(noticeRepository.findByStatusOrderByCreatedAtDesc(ACTIVE_STATUS))
return NoticeListRes.toNoticeListRes(noticeRepository.findByStatusOrderByCreatedAtDesc(ACTIVE_STATUS))
}

// 공지사항 상세
fun getNotice(noticeId: Long) : NoticeRes {
val notice = noticeRepository.findByIdAndStatus(noticeId, ACTIVE_STATUS) ?: throw BaseException(BaseResponseCode.NOT_FOUND_NOTICE)
return csAssembler.toNoticeRes(notice)
return NoticeRes.toNoticeRes(notice)
}

// 자주 묻는 질문 메인
fun getFaqs(type: String?): FaqListRes {
return if(type == null) csAssembler.toFaqListRes(faqRepository.findByStatusOrderByCreatedAtDesc(ACTIVE_STATUS))
return if(type == null) FaqListRes.toFaqListRes(faqRepository.findByStatusOrderByCreatedAtDesc(ACTIVE_STATUS))
else{
csAssembler.toFaqListRes(faqRepository.findByTypeAndStatusOrderByCreatedAtDesc(FaqType.getTypeByName(type), ACTIVE_STATUS))
FaqListRes.toFaqListRes(faqRepository.findByTypeAndStatusOrderByCreatedAtDesc(FaqType.getTypeByName(type), ACTIVE_STATUS))
}
}

// 자주 묻는 질문 상세
fun getFaq(faqId: Long): FaqRes {
val faq = faqRepository.findByIdAndStatus(faqId, ACTIVE_STATUS) ?: throw BaseException(BaseResponseCode.NOT_FOUND_FAQ)
return csAssembler.toFaqRes(faq)
return FaqRes.toFaqRes(faq)
}

// 홈 화면 조회 - 공지사항
fun getHomePage(): NoticeListRes {
return csAssembler.toNoticeListResForHomePage(noticeRepository.findTop3ByStatusOrderByCreatedAtDesc(ACTIVE_STATUS))
}

return NoticeListRes.toNoticeListResForHomePage(noticeRepository.findTop3ByStatusOrderByCreatedAtDesc(ACTIVE_STATUS))
}
}
10 changes: 9 additions & 1 deletion src/main/kotlin/com/psr/psr/global/jwt/dto/TokenDto.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.psr.psr.global.jwt.dto

import com.psr.psr.global.Constant
import org.jetbrains.annotations.NotNull

data class TokenDto(
Expand All @@ -8,4 +9,11 @@ data class TokenDto(
@field:NotNull
var refreshToken: String,
val type: String ?= null
)
){
companion object{
fun toTokenDto(tokenDto: TokenDto) {
tokenDto.accessToken.replace(Constant.JWT.BEARER_PREFIX, "").also { tokenDto.accessToken = it }
tokenDto.refreshToken.replace(Constant.JWT.BEARER_PREFIX, "").also { tokenDto.refreshToken = it }
}
}
}
12 changes: 11 additions & 1 deletion src/main/kotlin/com/psr/psr/user/dto/UserInterestListDto.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
package com.psr.psr.user.dto

import com.psr.psr.user.entity.UserInterest

data class UserInterestListDto (
val interestList: List<UserInterestDto>?
)
){
companion object{
fun toUserInterestListDto(interests: List<UserInterest>?): UserInterestListDto {
return UserInterestListDto(
interestList = interests?.map { i -> UserInterestDto(i.category.value) }?.toList()
)
}
}
}
113 changes: 0 additions & 113 deletions src/main/kotlin/com/psr/psr/user/dto/assembler/UserAssembler.kt

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
package com.psr.psr.user.dto.eidReq

import com.psr.psr.user.dto.request.UserEidReq
import jakarta.validation.constraints.NotBlank
import java.util.*

data class BusinessListReq (
@field:NotBlank
val businesses: List<Business>
){

companion object{
fun toUserEidList(userEidReq: UserEidReq): BusinessListReq {
val business = Business(userEidReq.number, userEidReq.companyDate, userEidReq.ownerName, userEidReq.companyName)
return BusinessListReq(Collections.singletonList(business))
}
}
}
Loading

0 comments on commit aae0bce

Please sign in to comment.