Skip to content

Commit

Permalink
merge: 날짜를 커스텀 Serializer를 이용해 직렬화 및 역직렬화 하도록 변경 (#881)
Browse files Browse the repository at this point in the history
Related to: 880
  • Loading branch information
ki960213 authored Dec 29, 2023
1 parent 582e7c4 commit d93eeb8
Show file tree
Hide file tree
Showing 16 changed files with 104 additions and 101 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.emmsale.data.apiModel.response

import com.emmsale.data.apiModel.serializer.DateTimeSerializer
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import java.time.LocalDateTime

@Serializable
data class CommentResponse(
Expand All @@ -16,9 +18,11 @@ data class CommentResponse(
@SerialName("feedTitle")
val feedTitle: String,
@SerialName("createdAt")
val createdAt: String,
@Serializable(with = DateTimeSerializer::class)
val createdAt: LocalDateTime,
@SerialName("updatedAt")
val updatedAt: String,
@Serializable(with = DateTimeSerializer::class)
val updatedAt: LocalDateTime,
@SerialName("memberId")
val memberId: Long,
@SerialName("memberImageUrl")
Expand Down Expand Up @@ -50,5 +54,6 @@ data class CommentReportResponse(
@SerialName("contentId")
val contentId: Long,
@SerialName("createdAt")
val createdAt: String,
@Serializable(with = DateTimeSerializer::class)
val createdAt: LocalDateTime,
)
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.emmsale.data.apiModel.response

import com.emmsale.data.apiModel.serializer.DateTimeSerializer
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import java.time.LocalDateTime

@Serializable
data class EventResponse(
Expand All @@ -12,13 +14,17 @@ data class EventResponse(
@SerialName("informationUrl")
val informationUrl: String,
@SerialName("startDate")
val startDate: String,
@Serializable(with = DateTimeSerializer::class)
val startDate: LocalDateTime,
@SerialName("endDate")
val endDate: String,
@Serializable(with = DateTimeSerializer::class)
val endDate: LocalDateTime,
@SerialName("applyStartDate")
val applyStartDate: String,
@Serializable(with = DateTimeSerializer::class)
val applyStartDate: LocalDateTime,
@SerialName("applyEndDate")
val applyEndDate: String,
@Serializable(with = DateTimeSerializer::class)
val applyEndDate: LocalDateTime,
@SerialName("location")
val location: String,
@SerialName("tags")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.emmsale.data.apiModel.response

import com.emmsale.data.apiModel.serializer.DateTimeSerializer
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import java.time.LocalDateTime

@Serializable
data class FeedResponse(
Expand All @@ -20,7 +22,9 @@ data class FeedResponse(
@SerialName("commentCount")
val commentCount: Int,
@SerialName("createdAt")
val createdAt: String,
@Serializable(with = DateTimeSerializer::class)
val createdAt: LocalDateTime,
@SerialName("updatedAt")
val updatedAt: String,
@Serializable(with = DateTimeSerializer::class)
val updatedAt: LocalDateTime,
)
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.emmsale.data.apiModel.response

import com.emmsale.data.apiModel.serializer.DateTimeSerializer
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import java.time.LocalDateTime

@Serializable
data class MessageRoomResponse(
Expand All @@ -28,5 +30,6 @@ data class MessageResponse(
@SerialName("content")
val content: String,
@SerialName("createdAt")
val createdAt: String,
@Serializable(with = DateTimeSerializer::class)
val createdAt: LocalDateTime,
)
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.emmsale.data.apiModel.response

import com.emmsale.data.apiModel.serializer.DateSerializer
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import java.time.LocalDate

@Serializable
data class MyPostResponse(
Expand All @@ -16,5 +18,6 @@ data class MyPostResponse(
@SerialName("content")
val content: String,
@SerialName("updatedAt")
val updatedAt: String,
@Serializable(with = DateSerializer::class)
val updatedAt: LocalDate,
)
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.emmsale.data.apiModel.response

import com.emmsale.data.apiModel.serializer.DateTimeSerializer
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import java.time.LocalDateTime

@Serializable
data class NotificationResponse(
Expand All @@ -12,7 +14,8 @@ data class NotificationResponse(
@SerialName("redirectId")
val redirectId: Long,
@SerialName("createdAt")
val createdAt: String, // 2023:08:23:12:00:00
@Serializable(with = DateTimeSerializer::class)
val createdAt: LocalDateTime,
@SerialName("type")
val notificationType: NotificationType,
@SerialName("isRead")
Expand Down Expand Up @@ -48,39 +51,3 @@ data class EventTypeNotificationResponse(
@SerialName("title")
val eventTitle: String,
)

@Serializable
data class RecruitmentNotificationResponse(
@SerialName("notificationId")
val id: Long,
@SerialName("senderId")
val senderUid: Long,
@SerialName("receiverId")
val receiverUid: Long,
@SerialName("message")
val message: String,
@SerialName("eventId")
val eventId: Long,
@SerialName("status")
val status: String,
@SerialName("isRead")
val isRead: Boolean,
@SerialName("createdAt")
val createdAt: String,
)

@Serializable
data class NotificationReportResponse(
@SerialName("id")
val id: Long,
@SerialName("reporterId")
val reporterId: Long,
@SerialName("reportedId")
val reportedId: Long,
@SerialName("type")
val type: String,
@SerialName("contentId")
val contentId: Long,
@SerialName("createdAt")
val createdAt: String,
)
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package com.emmsale.data.apiModel.response

import com.emmsale.data.apiModel.serializer.DateSerializer
import com.emmsale.data.apiModel.serializer.DateTimeSerializer
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import java.time.LocalDate
import java.time.LocalDateTime

@Serializable
data class RecruitmentResponse(
Expand All @@ -10,7 +14,8 @@ data class RecruitmentResponse(
@SerialName("content")
val content: String,
@SerialName("updatedAt")
val updatedAt: String,
@Serializable(with = DateSerializer::class)
val updatedAt: LocalDate,
@SerialName("member")
val member: MemberResponse,
@SerialName("eventId")
Expand All @@ -30,5 +35,6 @@ data class RecruitmentReportResponse(
@SerialName("contentId")
val contentId: Long,
@SerialName("createdAt")
val createdAt: String,
@Serializable(with = DateTimeSerializer::class)
val createdAt: LocalDateTime,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.emmsale.data.apiModel.serializer

import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.KSerializer
import kotlinx.serialization.Serializer
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
import java.time.LocalDate
import java.time.format.DateTimeFormatter

@OptIn(ExperimentalSerializationApi::class)
@Serializer(forClass = LocalDate::class)
class DateSerializer : KSerializer<LocalDate> {

private val dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd")

override fun serialize(encoder: Encoder, value: LocalDate) {
encoder.encodeString(value.format(dateFormatter))
}

override fun deserialize(decoder: Decoder): LocalDate =
LocalDate.parse(decoder.decodeString(), dateFormatter)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.emmsale.data.apiModel.serializer

import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.KSerializer
import kotlinx.serialization.Serializer
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter

@OptIn(ExperimentalSerializationApi::class)
@Serializer(forClass = LocalDateTime::class)
object DateTimeSerializer : KSerializer<LocalDateTime> {
private val formatter = DateTimeFormatter.ofPattern("yyyy:MM:dd:HH:mm:ss")

override fun serialize(encoder: Encoder, value: LocalDateTime) {
encoder.encodeString(value.format(formatter))
}

override fun deserialize(decoder: Decoder): LocalDateTime =
LocalDateTime.parse(decoder.decodeString(), formatter)
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import com.emmsale.data.apiModel.response.CommentResponse
import com.emmsale.data.model.Comment
import com.emmsale.data.model.Feed
import com.emmsale.data.model.Member
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter

@JvmName("mapCommentFamilyApiModelsToData")
fun List<CommentFamilyApiModel>.toData(): List<Comment> = map(CommentFamilyApiModel::toData)
Expand All @@ -24,8 +22,8 @@ fun CommentFamilyApiModel.toData() = Comment(
),
parentCommentId = parentComment.parentId,
content = parentComment.content,
createdAt = LocalDateTime.parse(parentComment.createdAt, dateTimeFormatter),
updatedAt = LocalDateTime.parse(parentComment.updatedAt, dateTimeFormatter),
createdAt = parentComment.createdAt,
updatedAt = parentComment.updatedAt,
isDeleted = parentComment.deleted,
childComments = childComments.toData(),
)
Expand All @@ -46,10 +44,8 @@ fun CommentResponse.toData() = Comment(
),
parentCommentId = parentId,
content = content,
createdAt = LocalDateTime.parse(createdAt, dateTimeFormatter),
updatedAt = LocalDateTime.parse(updatedAt, dateTimeFormatter),
createdAt = createdAt,
updatedAt = updatedAt,
isDeleted = deleted,
childComments = listOf(),
)

private val dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy:MM:dd:HH:mm:ss")
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import com.emmsale.data.apiModel.response.EventResponse
import com.emmsale.data.model.Event
import com.emmsale.data.model.OnOfflineMode
import com.emmsale.data.model.PaymentType
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter

fun List<EventResponse>.toData(): List<Event> = map { it.toData() }

Expand All @@ -15,10 +13,10 @@ fun EventResponse.toData(): Event = Event(
name = name,
organization = organization,
informationUrl = informationUrl,
startDate = startDate.toLocalDateTime(),
endDate = endDate.toLocalDateTime(),
applyingStartDate = applyStartDate.toLocalDateTime(),
applyingEndDate = applyEndDate.toLocalDateTime(),
startDate = startDate,
endDate = endDate,
applyingStartDate = applyStartDate,
applyingEndDate = applyEndDate,
location = location,
tags = tags,
posterImageUrl = thumbnailUrl?.let { BuildConfig.IMAGE_URL_PREFIX + it } ?: "",
Expand All @@ -28,11 +26,6 @@ fun EventResponse.toData(): Event = Event(
detailImageUrls = imageUrls.map { BuildConfig.IMAGE_URL_PREFIX + it },
)

private fun String.toLocalDateTime(): LocalDateTime {
val format = DateTimeFormatter.ofPattern("yyyy:MM:dd:HH:mm:ss")
return LocalDateTime.parse(this, format)
}

private fun EventResponse.PaymentType.toPaymentType(): PaymentType = when (this) {
EventResponse.PaymentType.PAID -> PaymentType.PAID
EventResponse.PaymentType.FREE -> PaymentType.FREE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package com.emmsale.data.mapper
import com.emmsale.BuildConfig
import com.emmsale.data.apiModel.response.FeedResponse
import com.emmsale.data.model.Feed
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter

fun List<FeedResponse>.toData(): List<Feed> = map { it.toData() }

Expand All @@ -16,8 +14,6 @@ fun FeedResponse.toData(): Feed = Feed(
writer = writer.toData(),
imageUrls = imageUrls.map { BuildConfig.IMAGE_URL_PREFIX + it },
commentCount = commentCount,
createdAt = LocalDateTime.parse(createdAt, dateTimeFormatter),
updatedAt = LocalDateTime.parse(updatedAt, dateTimeFormatter),
createdAt = createdAt,
updatedAt = updatedAt,
)

private val dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy:MM:dd:HH:mm:ss")
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import com.emmsale.data.model.Message

fun List<MessageResponse>.toData(): List<Message> = map { it.toData() }

fun MessageResponse.toData(): Message = Message.create(
fun MessageResponse.toData(): Message = Message(
id = id,
sender = sender.toData(),
content = content,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,12 @@ import com.emmsale.data.apiModel.response.MyPostResponse
import com.emmsale.data.model.Event
import com.emmsale.data.model.Member
import com.emmsale.data.model.Recruitment
import java.time.LocalDate
import java.time.format.DateTimeFormatter

fun List<MyPostResponse>.toData(): List<Recruitment> = map { it.toData() }
fun MyPostResponse.toData() = Recruitment(
id = postId,
writer = Member(),
event = Event(id = eventId, name = eventName),
content = content,
updatedDate = updatedAt.toLocalDate(),
updatedDate = updatedAt,
)

private fun String.toLocalDate(): LocalDate {
val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd")
return LocalDate.parse(this, formatter)
}
Loading

0 comments on commit d93eeb8

Please sign in to comment.