Skip to content

Commit

Permalink
Merge pull request #51 from PSR-Co/feat/#50-getFaqs
Browse files Browse the repository at this point in the history
  • Loading branch information
chaerlo127 committed Aug 5, 2023
2 parents 80ce7df + a9006c9 commit 3868a33
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 8 deletions.
30 changes: 23 additions & 7 deletions src/main/kotlin/com/psr/psr/cs/controller/CsController.kt
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package com.psr.psr.cs.controller

import com.psr.psr.cs.dto.FaqListRes
import com.psr.psr.cs.dto.FaqRes
import com.psr.psr.cs.dto.NoticeListRes
import com.psr.psr.cs.dto.NoticeRes
import com.psr.psr.cs.service.CsService
import com.psr.psr.global.dto.BaseResponse
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.ResponseBody
import org.springframework.web.bind.annotation.RestController
import org.springframework.web.bind.annotation.*

@RestController
@RequestMapping("/cs")
Expand All @@ -27,9 +25,27 @@ class CsController(
/**
* 공지사항 상세
*/
@GetMapping("/notices/{noticeIdx}")
@GetMapping("/notices/{noticeId}")
@ResponseBody
fun getNotice(@PathVariable(name = "noticeIdx") noticeId: Long): BaseResponse<NoticeRes>{
fun getNotice(@PathVariable(name = "noticeId") noticeId: Long): BaseResponse<NoticeRes>{
return BaseResponse(csService.getNotice(noticeId))
}

/**
* 자주 묻는 질문 메인
*/
@GetMapping("/faqs")
@ResponseBody
fun getFaqs(@RequestParam(value = "type", required = false) type: String?): BaseResponse<FaqListRes>{
return BaseResponse(csService.getFaqs(type))
}

/**
* 자주 묻는 질문 상세
*/
@GetMapping("/faqs/{faqId}")
@ResponseBody
fun getFaq(@PathVariable(name = "faqId") faqId: Long): BaseResponse<FaqRes>{
return BaseResponse(csService.getFaq(faqId))
}
}
5 changes: 5 additions & 0 deletions src/main/kotlin/com/psr/psr/cs/dto/FaqListRes.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.psr.psr.cs.dto

data class FaqListRes (
val faqLists: List<FaqRes>?
)
11 changes: 11 additions & 0 deletions src/main/kotlin/com/psr/psr/cs/dto/FaqRes.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.psr.psr.cs.dto

import com.fasterxml.jackson.annotation.JsonInclude

data class FaqRes (
val faqId: Long,
val type: String,
val title: String,
@JsonInclude(JsonInclude.Include.NON_NULL)
val content: String?= null,
)
16 changes: 16 additions & 0 deletions src/main/kotlin/com/psr/psr/cs/dto/assembler/CsAssembler.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.psr.psr.cs.dto.assembler

import com.psr.psr.cs.dto.FaqListRes
import com.psr.psr.cs.dto.FaqRes
import com.psr.psr.cs.dto.NoticeListRes
import com.psr.psr.cs.dto.NoticeRes
import com.psr.psr.cs.entity.Faq
import com.psr.psr.cs.entity.Notice
import org.springframework.stereotype.Component
import java.util.stream.Collectors
Expand All @@ -21,4 +24,17 @@ class CsAssembler {
fun toNoticeRes(notice: Notice): NoticeRes {
return NoticeRes(notice.id, notice.title, notice.createdAt, notice.imgKey)
}

// 자주 묻는 질문 메인
fun toFaqListRes(faqList: List<Faq>?) : FaqListRes {
if(faqList == null) return FaqListRes(null)
return FaqListRes(faqList.stream().map {
f -> FaqRes(f.id, f.type.value, f.title)
}.collect(Collectors.toList()))
}

// 자주 묻는 질문 상세
fun toFaqRes(faq: Faq): FaqRes {
return FaqRes(faq.id, faq.type.value, faq.title, faq.content)
}
}
12 changes: 11 additions & 1 deletion src/main/kotlin/com/psr/psr/cs/entity/FaqType.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
package com.psr.psr.cs.entity

import com.psr.psr.global.exception.BaseException
import com.psr.psr.global.exception.BaseResponseCode

enum class FaqType(val value: String) {
ACCOUNT_MANAGEMENT("계정관리"),
CONSULTING("컨설팅"),
PRODUCT("상품")
PRODUCT("상품");

companion object {
fun getTypeByName(name: String): FaqType {
return enumValues<FaqType>().find { it.value == name }
?: throw BaseException(BaseResponseCode.INVALID_FAQ_TYPE_NAME)
}
}
}
4 changes: 4 additions & 0 deletions src/main/kotlin/com/psr/psr/cs/repository/FaqRepository.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package com.psr.psr.cs.repository

import com.psr.psr.cs.entity.Faq
import com.psr.psr.cs.entity.FaqType
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.stereotype.Repository

@Repository
interface FaqRepository: JpaRepository<Faq, Long> {
fun findByOrderByCreatedAtDesc(): List<Faq>
fun findByTypeOrderByCreatedAtDesc(type: FaqType): List<Faq>
fun findByIdAndStatus(id: Long, status: String) : Faq?
}
17 changes: 17 additions & 0 deletions src/main/kotlin/com/psr/psr/cs/service/CsService.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package com.psr.psr.cs.service

import com.psr.psr.cs.dto.FaqListRes
import com.psr.psr.cs.dto.FaqRes
import com.psr.psr.cs.dto.NoticeListRes
import com.psr.psr.cs.dto.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
import com.psr.psr.global.Constant.USER_STATUS.USER_STATUS.ACTIVE_STATUS
Expand All @@ -28,4 +31,18 @@ class CsService(
return csAssembler.toNoticeRes(notice)
}

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

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ enum class BaseResponseCode(status: HttpStatus, message: String) {
// CS - notices
NOT_FOUND_NOTICE(HttpStatus.NOT_FOUND, "해당 공지사항를 찾을 수 없습니다."),

// CS - faqs
INVALID_FAQ_TYPE_NAME(HttpStatus.NOT_FOUND, "올바르지 않은 FAQ 카테고리입니다. "),
NOT_FOUND_FAQ(HttpStatus.NOT_FOUND, "해당 FAQ를 찾을 수 없습니다."),


// inquiry
NOT_FOUND_INQUIRY(HttpStatus.NOT_FOUND, "해당 문의를 찾을 수 없습니다."),
INVALID_INQUIRY_STATUS(HttpStatus.BAD_REQUEST, "올바르지 않은 문의 상태입니다."),
Expand Down

0 comments on commit 3868a33

Please sign in to comment.