Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🔀 :: 재활용품 방법 글 상세보기 API 구현 #81

Merged
merged 9 commits into from
Apr 1, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@ package andreas311.miso.domain.recyclables.adapter.input
import andreas311.miso.common.annotation.RequestController
import andreas311.miso.domain.recyclables.adapter.input.data.request.CreateRecyclablesRequest
import andreas311.miso.domain.recyclables.adapter.input.data.request.EditRecyclablesRequest
import andreas311.miso.domain.recyclables.adapter.input.data.response.DetailRecyclablesResponse
import andreas311.miso.domain.recyclables.adapter.input.mapper.RecyclablesDataMapper
import andreas311.miso.domain.recyclables.application.port.input.CreateRecyclablesUseCase
import andreas311.miso.domain.recyclables.application.port.input.DeleteRecyclablesUseCase
import andreas311.miso.domain.recyclables.application.port.input.DetailRecyclablesUseCase
import andreas311.miso.domain.recyclables.application.port.input.EditRecyclablesUseCase
import andreas311.miso.domain.recyclables.domain.RecyclablesType
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.DeleteMapping
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PatchMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.PostMapping
Expand All @@ -22,7 +26,8 @@ class RecyclablesAdapter(
private val recyclablesDataMapper: RecyclablesDataMapper,
private val editRecyclablesUseCase: EditRecyclablesUseCase,
private val createRecyclablesUseCase: CreateRecyclablesUseCase,
private val deleteRecyclablesUseCase: DeleteRecyclablesUseCase
private val detailRecyclablesUseCase: DetailRecyclablesUseCase,
private val deleteRecyclablesUseCase: DeleteRecyclablesUseCase,
) {
@PostMapping
fun create(
Expand All @@ -32,10 +37,11 @@ class RecyclablesAdapter(
createRecyclablesUseCase.execute(recyclablesDataMapper toDto createRecyclablesRequest, multipartFile)
.let { ResponseEntity.status(HttpStatus.CREATED).build() }

@DeleteMapping("/{id}")
fun delete(@PathVariable id: Long): ResponseEntity<Void> =
deleteRecyclablesUseCase.execute(id)
.let { ResponseEntity.status(HttpStatus.OK).build() }
@GetMapping("/{type}")
fun detail(@PathVariable(name = "type") recyclablesType: RecyclablesType): ResponseEntity<DetailRecyclablesResponse> =
detailRecyclablesUseCase.execute(recyclablesType)
.let { recyclablesDataMapper.toResponse(it) }
.let { ResponseEntity.status(HttpStatus.OK).body(it) }

@PatchMapping("/{id}")
fun edit(
Expand All @@ -45,4 +51,10 @@ class RecyclablesAdapter(
): ResponseEntity<Void> =
editRecyclablesUseCase.execute(id, recyclablesDataMapper toDto editRecyclablesRequest, multipartFile)
.let { ResponseEntity.status(HttpStatus.OK).build() }

@DeleteMapping("/{id}")
fun delete(@PathVariable id: Long): ResponseEntity<Void> =
deleteRecyclablesUseCase.execute(id)
.let { ResponseEntity.status(HttpStatus.OK).build() }

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package andreas311.miso.domain.recyclables.adapter.input.data.response

import andreas311.miso.domain.recyclables.domain.RecyclablesType

data class DetailRecyclablesResponse(
val id: Long,
val title: String,
val subTitle: String,
val recycleMethod: String,
val recycleTip: String,
val recycleCaution: String,
val imageUrl: String?,
val recyclablesType: RecyclablesType,
val recycleMark: String
)
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package andreas311.miso.domain.recyclables.adapter.input.mapper

import andreas311.miso.domain.recyclables.adapter.input.data.request.CreateRecyclablesRequest
import andreas311.miso.domain.recyclables.adapter.input.data.request.EditRecyclablesRequest
import andreas311.miso.domain.recyclables.adapter.input.data.response.DetailRecyclablesResponse
import andreas311.miso.domain.recyclables.application.port.input.dto.CreateRecyclablesDto
import andreas311.miso.domain.recyclables.application.port.input.dto.DetailRecyclablesDto
import andreas311.miso.domain.recyclables.application.port.input.dto.EditRecyclablesDto
import org.springframework.stereotype.Component

Expand All @@ -29,4 +31,17 @@ class RecyclablesDataMapper {
recyclablesType = editRecyclablesRequest.recyclablesType,
recycleMark = editRecyclablesRequest.recycleMark
)

fun toResponse(detailRecyclablesDto: DetailRecyclablesDto): DetailRecyclablesResponse =
DetailRecyclablesResponse(
id = detailRecyclablesDto.id,
title = detailRecyclablesDto.title,
subTitle = detailRecyclablesDto.subTitle,
recycleMethod = detailRecyclablesDto.recycleMethod,
recycleTip = detailRecyclablesDto.recycleTip,
recycleCaution = detailRecyclablesDto.recycleCaution,
imageUrl = detailRecyclablesDto.imageUrl,
recyclablesType = detailRecyclablesDto.recyclablesType,
recycleMark = detailRecyclablesDto.recycleMark
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import andreas311.miso.domain.recyclables.adapter.output.persistence.mapper.Recy
import andreas311.miso.domain.recyclables.adapter.output.persistence.repository.RecyclablesRepository
import andreas311.miso.domain.recyclables.application.port.output.QueryRecyclablesPort
import andreas311.miso.domain.recyclables.domain.Recyclables
import andreas311.miso.domain.recyclables.domain.RecyclablesType
import org.springframework.data.repository.findByIdOrNull
import org.springframework.stereotype.Component

Expand All @@ -16,4 +17,9 @@ class QueryRecyclablesPersistenceAdapter(
val recyclablesEntity = recyclablesRepository.findByIdOrNull(id)
return recyclablesMapper toDomain recyclablesEntity
}

override fun findByRecyclablesTypeOrNull(recyclablesType: RecyclablesType): Recyclables? {
val recyclablesEntity = recyclablesRepository.findByRecyclablesType(recyclablesType)
return recyclablesMapper toDomain recyclablesEntity
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package andreas311.miso.domain.recyclables.adapter.output.persistence.repository

import andreas311.miso.domain.recyclables.adapter.output.persistence.entity.RecyclablesEntity
import andreas311.miso.domain.recyclables.domain.RecyclablesType
import org.springframework.data.repository.CrudRepository

interface RecyclablesRepository : CrudRepository<RecyclablesEntity, Long> {
fun findByRecyclablesType(recyclablesType: RecyclablesType): RecyclablesEntity?
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package andreas311.miso.domain.recyclables.application.port.input

import andreas311.miso.domain.recyclables.application.port.input.dto.DetailRecyclablesDto
import andreas311.miso.domain.recyclables.domain.RecyclablesType

interface DetailRecyclablesUseCase {
fun execute(recyclablesType: RecyclablesType): DetailRecyclablesDto
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package andreas311.miso.domain.recyclables.application.port.input.dto

import andreas311.miso.domain.recyclables.domain.Recyclables
import andreas311.miso.domain.recyclables.domain.RecyclablesType

data class DetailRecyclablesDto(
val id: Long,
val title: String,
val subTitle: String,
val recycleMethod: String,
val recycleTip: String,
val recycleCaution: String,
val imageUrl: String?,
val recyclablesType: RecyclablesType,
val recycleMark: String
) {
constructor(recyclables: Recyclables) : this(
id = recyclables.id,
title = recyclables.title,
subTitle = recyclables.subTitle,
recycleMethod = recyclables.recycleMethod,
recycleTip = recyclables.recycleTip,
recycleCaution = recyclables.recycleCaution,
imageUrl = recyclables.imageUrl,
recyclablesType = recyclables.recyclablesType,
recycleMark = recyclables.recycleMark
)
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package andreas311.miso.domain.recyclables.application.port.output

import andreas311.miso.domain.recyclables.domain.Recyclables
import andreas311.miso.domain.recyclables.domain.RecyclablesType

interface QueryRecyclablesPort {
fun findByIdOrNull(id: Long): Recyclables?
fun findByRecyclablesTypeOrNull(recyclablesType: RecyclablesType): Recyclables?
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package andreas311.miso.domain.recyclables.application.service

import andreas311.miso.common.annotation.ReadOnlyRollbackService
import andreas311.miso.domain.recyclables.application.exception.RecyclablesNotFoundException
import andreas311.miso.domain.recyclables.application.port.input.DetailRecyclablesUseCase
import andreas311.miso.domain.recyclables.application.port.input.dto.DetailRecyclablesDto
import andreas311.miso.domain.recyclables.application.port.output.QueryRecyclablesPort
import andreas311.miso.domain.recyclables.domain.RecyclablesType

@ReadOnlyRollbackService
class DetailRecyclablesService(
private val queryRecyclablesPort: QueryRecyclablesPort
) : DetailRecyclablesUseCase {
override fun execute(recyclablesType: RecyclablesType): DetailRecyclablesDto {
val recyclables = queryRecyclablesPort.findByRecyclablesTypeOrNull(recyclablesType)
?: throw RecyclablesNotFoundException()

return DetailRecyclablesDto(recyclables)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class SecurityConfig(

.antMatchers(HttpMethod.POST, "/recyclables").hasAuthority("ROLE_ADMIN")
.antMatchers(HttpMethod.POST, "/recyclables/process").authenticated()
.antMatchers(HttpMethod.GET, "/recyclables").authenticated()
.antMatchers(HttpMethod.GET, "/recyclables/{type}").authenticated()
.antMatchers(HttpMethod.GET, "/recyclables/search").authenticated()
.antMatchers(HttpMethod.GET, "/recyclables/all").authenticated()
.antMatchers(HttpMethod.PATCH, "/recyclables/{id}").hasAuthority("ROLE_ADMIN")
Expand Down
Loading