Skip to content

Commit

Permalink
Refactor: Remove Location Entity
Browse files Browse the repository at this point in the history
  • Loading branch information
huGgW committed Feb 15, 2024
1 parent 1870638 commit 31e19e9
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 63 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.wafflestudio.csereal.common.utils

import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue
import jakarta.persistence.AttributeConverter
import jakarta.persistence.Converter

@Converter
class StringListConverter : AttributeConverter<MutableList<String>, String> {
override fun convertToDatabaseColumn(p0: MutableList<String>?): String =
ObjectMapper().writeValueAsString(p0 ?: mutableListOf<String>())

override fun convertToEntityAttribute(p0: String?): MutableList<String> =
ObjectMapper().readValue(p0 ?: "[]")
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.wafflestudio.csereal.common.config.BaseTimeEntity
import com.wafflestudio.csereal.common.controller.AttachmentContentEntityType
import com.wafflestudio.csereal.common.controller.MainImageContentEntityType
import com.wafflestudio.csereal.common.properties.LanguageType
import com.wafflestudio.csereal.common.utils.StringListConverter
import com.wafflestudio.csereal.core.about.dto.AboutDto
import com.wafflestudio.csereal.core.resource.attachment.database.AttachmentEntity
import com.wafflestudio.csereal.core.resource.mainImage.database.MainImageEntity
Expand All @@ -22,8 +23,9 @@ class AboutEntity(

var year: Int?,

@OneToMany(mappedBy = "about", cascade = [CascadeType.ALL], orphanRemoval = true)
val locations: MutableList<LocationEntity> = mutableListOf(),
@Column(columnDefinition = "TEXT")
@Convert(converter = StringListConverter::class)
var locations: MutableList<String> = mutableListOf(),

@OneToMany(mappedBy = "")
var attachments: MutableList<AttachmentEntity> = mutableListOf(),
Expand All @@ -42,7 +44,8 @@ class AboutEntity(
language = languageType,
name = aboutDto.name,
description = aboutDto.description,
year = aboutDto.year
year = aboutDto.year,
locations = aboutDto.locations?.toMutableList() ?: mutableListOf()
)
}
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ data class AboutDto(
year = this.year,
createdAt = this.createdAt,
modifiedAt = this.modifiedAt,
locations = this.locations.map { it.name },
locations = this.locations,
imageURL = imageURL,
attachments = attachmentResponses
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ data class FacilityDto(
language = LanguageType.makeLowercase(this.language),
name = this.name!!,
description = this.description,
locations = this.locations.map { it.name }
locations = this.locations
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,6 @@ class AboutServiceImpl(
val enumLanguageType = LanguageType.makeStringToLanguageType(request.language)
val newAbout = AboutEntity.of(enumPostType, enumLanguageType, request)

if (request.locations != null) {
for (location in request.locations) {
LocationEntity.create(location, newAbout)
}
}

if (mainImage != null) {
mainImageService.uploadMainImage(newAbout, mainImage)
}
Expand Down Expand Up @@ -277,39 +271,31 @@ class AboutServiceImpl(
}

@Transactional
override fun migrateFacilities(requestList: List<FacilityDto>): List<FacilityDto> {
val list = mutableListOf<FacilityDto>()

for (request in requestList) {
val language = request.language
val name = request.name
val description = request.description
val aboutDto = AboutDto(
override fun migrateFacilities(requestList: List<FacilityDto>): List<FacilityDto> =
requestList.map {
AboutDto(
id = null,
language = language,
name = name,
description = description,
language = it.language,
name = it.name,
description = it.description,
year = null,
createdAt = null,
modifiedAt = null,
locations = null,
locations = it.locations,
imageURL = null,
attachments = listOf()
)

val languageType = LanguageType.makeStringToLanguageType(language)
val newAbout = AboutEntity.of(AboutPostType.FACILITIES, languageType, aboutDto)

for (location in request.locations) {
LocationEntity.create(location, newAbout)
).let { dto ->
AboutEntity.of(
AboutPostType.FACILITIES,
LanguageType.makeStringToLanguageType(it.language),
dto
)
}

aboutRepository.save(newAbout)

list.add(FacilityDto.of(newAbout))
}.let {
aboutRepository.saveAll(it)
}.map {
FacilityDto.of(it)
}
return list
}

@Transactional
override fun migrateDirections(requestList: List<DirectionDto>): List<DirectionDto> {
Expand Down

0 comments on commit 31e19e9

Please sign in to comment.