diff --git a/src/main/kotlin/andreas311/miso/domain/environment/adapter/input/EnvironmentAdapter.kt b/src/main/kotlin/andreas311/miso/domain/environment/adapter/input/EnvironmentAdapter.kt new file mode 100644 index 00000000..4f402d0b --- /dev/null +++ b/src/main/kotlin/andreas311/miso/domain/environment/adapter/input/EnvironmentAdapter.kt @@ -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 = + createEnvironmentUseCase.execute(environmentDataMapper toDto createEnvironmentRequest, multipartFile) + .let { ResponseEntity.status(HttpStatus.CREATED).build() } +} \ No newline at end of file diff --git a/src/main/kotlin/andreas311/miso/domain/environment/adapter/input/data/request/CreateEnvironmentRequest.kt b/src/main/kotlin/andreas311/miso/domain/environment/adapter/input/data/request/CreateEnvironmentRequest.kt new file mode 100644 index 00000000..6059c92a --- /dev/null +++ b/src/main/kotlin/andreas311/miso/domain/environment/adapter/input/data/request/CreateEnvironmentRequest.kt @@ -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, +) diff --git a/src/main/kotlin/andreas311/miso/domain/environment/adapter/input/mapper/EnvironmentDataMapper.kt b/src/main/kotlin/andreas311/miso/domain/environment/adapter/input/mapper/EnvironmentDataMapper.kt new file mode 100644 index 00000000..1a25e4ea --- /dev/null +++ b/src/main/kotlin/andreas311/miso/domain/environment/adapter/input/mapper/EnvironmentDataMapper.kt @@ -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 + ) +} \ No newline at end of file diff --git a/src/main/kotlin/andreas311/miso/domain/environment/adapter/output/persistence/CommandEnvironmentPersistenceAdapter.kt b/src/main/kotlin/andreas311/miso/domain/environment/adapter/output/persistence/CommandEnvironmentPersistenceAdapter.kt new file mode 100644 index 00000000..c882ae8a --- /dev/null +++ b/src/main/kotlin/andreas311/miso/domain/environment/adapter/output/persistence/CommandEnvironmentPersistenceAdapter.kt @@ -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)!! + } +} \ No newline at end of file diff --git a/src/main/kotlin/andreas311/miso/domain/environment/adapter/output/persistence/entity/EnvironmentEntity.kt b/src/main/kotlin/andreas311/miso/domain/environment/adapter/output/persistence/entity/EnvironmentEntity.kt new file mode 100644 index 00000000..33e0320e --- /dev/null +++ b/src/main/kotlin/andreas311/miso/domain/environment/adapter/output/persistence/entity/EnvironmentEntity.kt @@ -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?, +) \ No newline at end of file diff --git a/src/main/kotlin/andreas311/miso/domain/environment/adapter/output/persistence/mapper/EnvironmentMapper.kt b/src/main/kotlin/andreas311/miso/domain/environment/adapter/output/persistence/mapper/EnvironmentMapper.kt new file mode 100644 index 00000000..221c5139 --- /dev/null +++ b/src/main/kotlin/andreas311/miso/domain/environment/adapter/output/persistence/mapper/EnvironmentMapper.kt @@ -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 + ) + } +} \ No newline at end of file diff --git a/src/main/kotlin/andreas311/miso/domain/environment/adapter/output/persistence/repository/EnvironmentRepository.kt b/src/main/kotlin/andreas311/miso/domain/environment/adapter/output/persistence/repository/EnvironmentRepository.kt new file mode 100644 index 00000000..c0549224 --- /dev/null +++ b/src/main/kotlin/andreas311/miso/domain/environment/adapter/output/persistence/repository/EnvironmentRepository.kt @@ -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 \ No newline at end of file diff --git a/src/main/kotlin/andreas311/miso/domain/environment/application/port/input/CreateEnvironmentUseCase.kt b/src/main/kotlin/andreas311/miso/domain/environment/application/port/input/CreateEnvironmentUseCase.kt new file mode 100644 index 00000000..8b5232f8 --- /dev/null +++ b/src/main/kotlin/andreas311/miso/domain/environment/application/port/input/CreateEnvironmentUseCase.kt @@ -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?) +} \ No newline at end of file diff --git a/src/main/kotlin/andreas311/miso/domain/environment/application/port/input/dto/CreateEnvironmentDto.kt b/src/main/kotlin/andreas311/miso/domain/environment/application/port/input/dto/CreateEnvironmentDto.kt new file mode 100644 index 00000000..bba14481 --- /dev/null +++ b/src/main/kotlin/andreas311/miso/domain/environment/application/port/input/dto/CreateEnvironmentDto.kt @@ -0,0 +1,6 @@ +package andreas311.miso.domain.environment.application.port.input.dto + +data class CreateEnvironmentDto( + val title: String, + val content: String, +) diff --git a/src/main/kotlin/andreas311/miso/domain/environment/application/port/output/CommandEnvironmentPort.kt b/src/main/kotlin/andreas311/miso/domain/environment/application/port/output/CommandEnvironmentPort.kt new file mode 100644 index 00000000..3868699b --- /dev/null +++ b/src/main/kotlin/andreas311/miso/domain/environment/application/port/output/CommandEnvironmentPort.kt @@ -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 +} \ No newline at end of file diff --git a/src/main/kotlin/andreas311/miso/domain/environment/application/service/CreateEnvironmentService.kt b/src/main/kotlin/andreas311/miso/domain/environment/application/service/CreateEnvironmentService.kt new file mode 100644 index 00000000..7e077726 --- /dev/null +++ b/src/main/kotlin/andreas311/miso/domain/environment/application/service/CreateEnvironmentService.kt @@ -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 + ) + ) + } +} \ No newline at end of file diff --git a/src/main/kotlin/andreas311/miso/domain/environment/domain/Environment.kt b/src/main/kotlin/andreas311/miso/domain/environment/domain/Environment.kt new file mode 100644 index 00000000..0e1f552d --- /dev/null +++ b/src/main/kotlin/andreas311/miso/domain/environment/domain/Environment.kt @@ -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?, +) diff --git a/src/main/kotlin/andreas311/miso/global/security/config/SecurityConfig.kt b/src/main/kotlin/andreas311/miso/global/security/config/SecurityConfig.kt index 6657ee5b..02fadf43 100644 --- a/src/main/kotlin/andreas311/miso/global/security/config/SecurityConfig.kt +++ b/src/main/kotlin/andreas311/miso/global/security/config/SecurityConfig.kt @@ -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()