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 ๊ตฌํ˜„ #95

Merged
merged 12 commits into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
12 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package andreas311.miso.domain.environment.adapter.input

import andreas311.miso.common.annotation.RequestController
import andreas311.miso.domain.environment.adapter.input.data.request.CreateEnvironmentRequest
import andreas311.miso.domain.environment.adapter.input.mapper.EnvironmentDataMapper
import andreas311.miso.domain.environment.application.port.input.CreateEnvironmentUseCase
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestPart
import org.springframework.web.multipart.MultipartFile
import javax.validation.Valid

@RequestController("/environment")
class EnvironmentAdapter(
private val environmentDataMapper: EnvironmentDataMapper,
private val createEnvironmentUseCase: CreateEnvironmentUseCase
) {
@PostMapping
fun create(
@RequestPart(value = "file") multipartFile: MultipartFile?,
@RequestPart(value = "environment") @Valid createEnvironmentRequest: CreateEnvironmentRequest
): ResponseEntity<Void> =
createEnvironmentUseCase.execute(environmentDataMapper toDto createEnvironmentRequest, multipartFile)
.let { ResponseEntity.status(HttpStatus.CREATED).build() }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package andreas311.miso.domain.environment.adapter.input.data.request

import javax.validation.constraints.NotNull

data class CreateEnvironmentRequest(
@field:NotNull
val title: String,
@field:NotNull
val content: String,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package andreas311.miso.domain.environment.adapter.input.mapper

import andreas311.miso.domain.environment.adapter.input.data.request.CreateEnvironmentRequest
import andreas311.miso.domain.environment.application.port.input.dto.CreateEnvironmentDto
import org.springframework.stereotype.Component

@Component
class EnvironmentDataMapper {
infix fun toDto(createEnvironmentRequest: CreateEnvironmentRequest): CreateEnvironmentDto =
CreateEnvironmentDto(
title = createEnvironmentRequest.title,
content = createEnvironmentRequest.content
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package andreas311.miso.domain.environment.adapter.output.persistence

import andreas311.miso.domain.environment.adapter.output.persistence.mapper.EnvironmentMapper
import andreas311.miso.domain.environment.adapter.output.persistence.repository.EnvironmentRepository
import andreas311.miso.domain.environment.application.port.output.CommandEnvironmentPort
import andreas311.miso.domain.environment.domain.Environment
import org.springframework.stereotype.Component

@Component
class CommandEnvironmentPersistenceAdapter(
private val environmentMapper: EnvironmentMapper,
private val environmentRepository: EnvironmentRepository
) : CommandEnvironmentPort {
override fun saveEnvironment(environment: Environment): Environment {
val environmentEntity = environmentRepository.save(environmentMapper toEntity environment)
return environmentMapper.toDomain(environmentEntity)!!
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package andreas311.miso.domain.environment.adapter.output.persistence.entity

import javax.persistence.*

@Entity
@Table(name = "environment")
data class EnvironmentEntity(
@Id
@Column(name = "environment_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
val id: Long,

@Column(name = "title", nullable = false)
val title: String,

@Column(name = "content", nullable = false, length = 10000)
val content: String,

@Column(name = "imageUrl", nullable = true)
val imageUrl: String?,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package andreas311.miso.domain.environment.adapter.output.persistence.mapper

import andreas311.miso.domain.environment.adapter.output.persistence.entity.EnvironmentEntity
import andreas311.miso.domain.environment.domain.Environment
import org.springframework.stereotype.Component

@Component
class EnvironmentMapper {
infix fun toEntity(domain: Environment): EnvironmentEntity =
EnvironmentEntity(
id = domain.id,
title = domain.title,
content = domain.content,
imageUrl = domain.imageUrl
)

infix fun toDomain(entity: EnvironmentEntity?): Environment? =
entity?.let {
Environment(
id = entity.id,
title = entity.title,
content = entity.content,
imageUrl = entity.imageUrl
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package andreas311.miso.domain.environment.adapter.output.persistence.repository

import andreas311.miso.domain.environment.adapter.output.persistence.entity.EnvironmentEntity
import org.springframework.data.repository.CrudRepository

interface EnvironmentRepository : CrudRepository<EnvironmentEntity, Long>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package andreas311.miso.domain.environment.application.port.input

import andreas311.miso.domain.environment.application.port.input.dto.CreateEnvironmentDto
import org.springframework.web.multipart.MultipartFile

interface CreateEnvironmentUseCase {
fun execute(createEnvironmentDto: CreateEnvironmentDto, multipartFile: MultipartFile?)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package andreas311.miso.domain.environment.application.port.input.dto

data class CreateEnvironmentDto(
val title: String,
val content: String,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package andreas311.miso.domain.environment.application.port.output

import andreas311.miso.domain.environment.domain.Environment

interface CommandEnvironmentPort {
fun saveEnvironment(environment: Environment): Environment
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package andreas311.miso.domain.environment.application.service

import andreas311.miso.common.annotation.RollbackService
import andreas311.miso.domain.environment.application.port.input.CreateEnvironmentUseCase
import andreas311.miso.domain.environment.application.port.input.dto.CreateEnvironmentDto
import andreas311.miso.domain.environment.application.port.output.CommandEnvironmentPort
import andreas311.miso.domain.environment.domain.Environment
import andreas311.miso.thirdparty.aws.s3.util.S3Util
import org.springframework.web.multipart.MultipartFile

@RollbackService
class CreateEnvironmentService(
private val s3Util: S3Util,
private val commandEnvironmentPort: CommandEnvironmentPort
) : CreateEnvironmentUseCase {
override fun execute(createEnvironmentDto: CreateEnvironmentDto, multipartFile: MultipartFile?) {
val imageUrl =
if (multipartFile != null) {
s3Util.execute(multipartFile)
} else null

commandEnvironmentPort.saveEnvironment(
Environment(
id = 0L,
title = createEnvironmentDto.title,
content = createEnvironmentDto.content,
imageUrl = imageUrl
)
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package andreas311.miso.domain.environment.domain

data class Environment(
val id: Long,
val title: String,
val content: String,
val imageUrl: String?,
)
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ class SecurityConfig(
.antMatchers(HttpMethod.GET, "/notification/{id}").authenticated()

.antMatchers(HttpMethod.GET, "/environment").authenticated()
.antMatchers(HttpMethod.POST, "/environment").hasAuthority("ROLE_ADMIN")

.anyRequest().denyAll()
.and()
Expand Down
Loading