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

Class 변환 기능을 MapStruct를 사용하도록 리팩토링 #67

Merged
merged 2 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 6 additions & 0 deletions damaba/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ plugins {
kotlin("jvm") version "1.9.25"
kotlin("plugin.spring") version "1.9.25"
kotlin("plugin.jpa") version "1.9.25"
kotlin("kapt")
id("org.springframework.boot") version "3.3.4"
id("io.spring.dependency-management") version "1.1.6"
id("org.jlleitschuh.gradle.ktlint") version "12.1.1"
Expand Down Expand Up @@ -63,6 +64,11 @@ dependencies {
implementation(project(":common:common-exception"))
implementation(project(":common:common-file"))

// MapStruct
implementation("org.mapstruct:mapstruct:1.5.3.Final")
annotationProcessor("org.mapstruct:mapstruct-processor:1.5.3.Final")
kapt("org.mapstruct:mapstruct-processor:1.5.3.Final")

// Test container
testImplementation("org.testcontainers:testcontainers:1.20.2")
testImplementation("org.testcontainers:junit-jupiter:1.20.2")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.damaba.damaba.adapter.inbound.common.dto

import com.damaba.damaba.domain.common.Address
import io.swagger.v3.oas.annotations.media.Schema

data class AddressRequest(
Expand All @@ -15,6 +14,4 @@ data class AddressRequest(

@Schema(description = "지번 주소", example = "경기 성남시 분당구 백현동 532")
val jibunAddress: String,
) {
fun toDomain(): Address = Address(sido, sigungu, roadAddress, jibunAddress)
}
)
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.damaba.damaba.adapter.inbound.common.dto

import com.damaba.damaba.domain.common.Address
import io.swagger.v3.oas.annotations.media.Schema

data class AddressResponse(
Expand All @@ -15,13 +14,4 @@ data class AddressResponse(

@Schema(description = "지번 주소", example = "경기 성남시 분당구 백현동 532")
val jibunAddress: String,
) {
companion object {
fun from(address: Address) = AddressResponse(
sido = address.sido,
sigungu = address.sigungu,
roadAddress = address.roadAddress,
jibunAddress = address.jibunAddress,
)
}
}
)

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.damaba.damaba.adapter.inbound.common.dto

import io.swagger.v3.oas.annotations.media.Schema

data class ImageRequest(
@Schema(description = "Image name", example = "apple.jpg")
val name: String,

@Schema(description = "Image url", example = "https://damaba-file-server/apple.jpg")
val url: String,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.damaba.damaba.adapter.inbound.common.dto

import io.swagger.v3.oas.annotations.media.Schema

data class ImageResponse(
@Schema(description = "Image name", example = "apple.jpg")
val name: String,

@Schema(description = "Image url", example = "https://damaba-file-server/apple.jpg")
val url: String,
)
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.damaba.damaba.application.port.inbound.promotion.FindPromotionsUseCas
import com.damaba.damaba.application.port.inbound.promotion.GetPromotionDetailUseCase
import com.damaba.damaba.application.port.inbound.promotion.PostPromotionUseCase
import com.damaba.damaba.domain.common.Pagination
import com.damaba.damaba.mapper.PromotionMapper
import com.damaba.user.domain.user.User
import io.swagger.v3.oas.annotations.Operation
import io.swagger.v3.oas.annotations.Parameter
Expand Down Expand Up @@ -39,7 +40,7 @@ class PromotionController(
@GetMapping("/api/v1/promotions/{promotionId}")
fun getPromotionDetailV1(@PathVariable promotionId: Long): PromotionResponse {
val promotion = getPromotionDetailUseCase.getPromotionDetail(promotionId)
return PromotionResponse.from(promotion)
return PromotionMapper.INSTANCE.toPromotionResponse(promotion)
}

@Operation(summary = "프로모션 리스트 조회")
Expand All @@ -49,7 +50,7 @@ class PromotionController(
@RequestParam @Parameter(description = "페이지 크기") pageSize: Int,
): Pagination<PromotionResponse> {
val promotions = findPromotionsUseCase.findPromotions(FindPromotionsUseCase.Query(page, pageSize))
return promotions.map { PromotionResponse.from(it) }
return promotions.map { PromotionMapper.INSTANCE.toPromotionResponse(it) }
}

@Operation(
Expand All @@ -65,6 +66,6 @@ class PromotionController(
val promotion = postPromotionUseCase.postPromotion(request.toCommand(requestUser.id))
return ResponseEntity
.created(URI.create("/api/v*/promotions/${promotion.id}"))
.body(PromotionResponse.from(promotion))
.body(PromotionMapper.INSTANCE.toPromotionResponse(promotion))
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package com.damaba.damaba.adapter.inbound.promotion.dto

import com.damaba.damaba.adapter.inbound.common.dto.AddressRequest
import com.damaba.damaba.adapter.inbound.common.dto.FileRequest
import com.damaba.damaba.adapter.inbound.common.dto.ImageRequest
import com.damaba.damaba.adapter.inbound.region.dto.RegionRequest
import com.damaba.damaba.application.port.inbound.promotion.PostPromotionUseCase
import com.damaba.damaba.domain.promotion.constant.EventType
import com.damaba.damaba.domain.promotion.constant.PromotionType
import com.damaba.damaba.mapper.AddressMapper
import com.damaba.damaba.mapper.ImageMapper
import com.damaba.damaba.mapper.RegionMapper
import io.swagger.v3.oas.annotations.media.Schema
import java.time.LocalDate

Expand Down Expand Up @@ -41,7 +44,7 @@ data class PostPromotionRequest(
val photographerInstagramId: String?,

@Schema(description = "이미지 리스트. 이미지는 최소 1장부터 최대 10장까지 첨부할 수 있습니다.")
val images: List<FileRequest>,
val images: List<ImageRequest>,

@Schema(description = "활동 지역 리스트. 활동 지역은 최소 1개 이상 선택해야 합니다.")
val activeRegions: Set<RegionRequest>,
Expand All @@ -55,14 +58,14 @@ data class PostPromotionRequest(
eventType = eventType,
title = title,
content = content,
address = address.toDomain(),
address = AddressMapper.INSTANCE.toAddress(address),
externalLink = externalLink,
startedAt = startedAt,
endedAt = endedAt,
photographerName = photographerName,
photographerInstagramId = photographerInstagramId,
images = images.map { fileReq -> fileReq.toDomain() },
activeRegions = activeRegions.map { regionReq -> regionReq.toDomain() }.toSet(),
images = images.map { ImageMapper.INSTANCE.toImage(it) },
activeRegions = activeRegions.map { regionRequest -> RegionMapper.INSTANCE.toRegion(regionRequest) }.toSet(),
hashtags = hashtags,
)
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.damaba.damaba.adapter.inbound.promotion.dto

import com.damaba.damaba.adapter.inbound.common.dto.AddressResponse
import com.damaba.damaba.adapter.inbound.common.dto.FileResponse
import com.damaba.damaba.domain.promotion.Promotion
import com.damaba.damaba.adapter.inbound.common.dto.ImageResponse
import com.damaba.damaba.adapter.inbound.region.dto.RegionResponse
import com.damaba.damaba.domain.promotion.constant.EventType
import com.damaba.damaba.domain.promotion.constant.PromotionType
import io.swagger.v3.oas.annotations.media.Schema
Expand Down Expand Up @@ -46,31 +46,11 @@ data class PromotionResponse(
val photographerInstagramId: String?,

@Schema(description = "이미지 url 리스트", example = "[\"https://promotion-image\"]")
val images: List<FileResponse>,
val images: List<ImageResponse>,

@Schema(description = "활동 지역 리스트")
val activeRegions: Set<PromotionActiveRegionResponse>,
val activeRegions: Set<RegionResponse>,

@Schema(description = "해시태그 리스트", example = "[\"수원핫플\", \"스냅사진\"]")
val hashtags: Set<String>,
) {
companion object {
fun from(promotion: Promotion) = PromotionResponse(
id = promotion.id,
authorId = promotion.authorId,
type = promotion.type,
eventType = promotion.eventType,
title = promotion.title,
content = promotion.content,
address = AddressResponse.from(promotion.address),
externalLink = promotion.externalLink,
startedAt = promotion.startedAt,
endedAt = promotion.endedAt,
photographerName = promotion.photographerName,
photographerInstagramId = promotion.photographerInstagramId,
images = promotion.images.map { image -> FileResponse(image.name, image.url) },
activeRegions = promotion.activeRegions.map { PromotionActiveRegionResponse.from(it) }.toSet(),
hashtags = promotion.hashtags,
)
}
}
)
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.damaba.damaba.adapter.inbound.region.dto.RegionCategoriesResponse
import com.damaba.damaba.adapter.inbound.region.dto.RegionGroupsResponse
import com.damaba.damaba.application.port.inbound.region.FindRegionCategoriesUseCase
import com.damaba.damaba.application.port.inbound.region.FindRegionGroupsUseCase
import com.damaba.damaba.mapper.RegionMapper
import io.swagger.v3.oas.annotations.Operation
import io.swagger.v3.oas.annotations.tags.Tag
import org.springframework.web.bind.annotation.GetMapping
Expand All @@ -28,7 +29,8 @@ class RegionController(
)
@GetMapping("/api/v1/regions/groups")
fun findRegionGroupsV1(): RegionGroupsResponse {
val regionGroups = findRegionGroupsUseCase.findRegionGroups()
return RegionGroupsResponse.from(regionGroups)
val regionGroupResponses = findRegionGroupsUseCase.findRegionGroups()
.map { RegionMapper.INSTANCE.toRegionGroupResponse(it) }
return RegionGroupsResponse(regionGroupResponses)
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.damaba.damaba.adapter.inbound.region.dto

import com.damaba.damaba.domain.region.RegionGroup
import io.swagger.v3.oas.annotations.media.Schema

data class RegionGroupResponse(
Expand All @@ -9,11 +8,4 @@ data class RegionGroupResponse(

@Schema(description = "Region list", example = "[\"종로구\", \"용산구\"]")
val regions: List<String>,
) {
companion object {
fun from(regionGroup: RegionGroup): RegionGroupResponse = RegionGroupResponse(
category = regionGroup.category,
regions = regionGroup.regions,
)
}
}
)
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
package com.damaba.damaba.adapter.inbound.region.dto

import com.damaba.damaba.domain.region.RegionGroup
import io.swagger.v3.oas.annotations.media.Schema

data class RegionGroupsResponse(
@Schema(description = "Region group list")
val regionGroups: List<RegionGroupResponse>,
) {
companion object {
fun from(regionsGroups: List<RegionGroup>) =
RegionGroupsResponse(regionsGroups.map { regionGroup -> RegionGroupResponse.from(regionGroup) })
}
}
)
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.damaba.damaba.adapter.inbound.region.dto

import com.damaba.damaba.domain.region.Region
import io.swagger.v3.oas.annotations.media.Schema

data class RegionRequest(
Expand All @@ -9,6 +8,4 @@ data class RegionRequest(

@Schema(description = "지역 이름", example = "강남구")
val name: String,
) {
fun toDomain() = Region(category, name)
}
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.damaba.damaba.adapter.inbound.region.dto

import io.swagger.v3.oas.annotations.media.Schema

data class RegionResponse(
@Schema(description = "지역 카테고리", example = "서울")
val category: String,

@Schema(description = "지역 이름", example = "강남구")
val name: String,
)
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.damaba.damaba.adapter.outbound.common

import com.damaba.damaba.domain.common.Address
import jakarta.persistence.Column
import jakarta.persistence.Embeddable

Expand All @@ -17,20 +16,4 @@ data class AddressJpaEmbeddable(

@Column(name = "jibun_address")
val jibunAddress: String,
) {
fun toDomain() = Address(
sido = this.sido,
sigungu = this.sigungu,
roadAddress = this.roadAddress,
jibunAddress = this.jibunAddress,
)

companion object {
fun from(address: Address) = AddressJpaEmbeddable(
sido = address.sido,
sigungu = address.sigungu,
roadAddress = address.roadAddress,
jibunAddress = address.jibunAddress,
)
}
}
)
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.damaba.damaba.adapter.outbound.photographer

import com.damaba.damaba.domain.photographer.BusinessSchedule
import jakarta.persistence.Column
import jakarta.persistence.Convert
import jakarta.persistence.Embeddable
Expand All @@ -18,14 +17,4 @@ data class BusinessScheduleEmbeddable(

@Column(name = "business_end_time", nullable = true)
val endTime: LocalTime,
) {
fun toDomain() = BusinessSchedule(days, startTime, endTime)

companion object {
fun from(businessSchedule: BusinessSchedule) = BusinessScheduleEmbeddable(
days = businessSchedule.days,
startTime = businessSchedule.startTime,
endTime = businessSchedule.endTime,
)
}
}
)
Loading
Loading