Skip to content

Commit

Permalink
feat: EditReceivedEnvelopeUseCase
Browse files Browse the repository at this point in the history
  • Loading branch information
jinukeu committed Jan 31, 2024
1 parent 8fe39d4 commit 03db3b9
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import com.susu.data.remote.model.request.CategoryRequest
import com.susu.data.remote.model.request.EnvelopeRequest
import com.susu.data.remote.model.response.toModel
import com.susu.domain.repository.EnvelopesRepository
import kotlinx.datetime.LocalDateTime
import javax.inject.Inject

class EnvelopesRepositoryImpl @Inject constructor(
Expand Down Expand Up @@ -90,4 +91,38 @@ class EnvelopesRepositoryImpl @Inject constructor(
override suspend fun getEnvelope(id: Long): Envelope = envelopesService.getEnvelope(id).getOrThrow().toModel()

override suspend fun deleteEnvelope(id: Long) = envelopesService.deleteEnvelope(id).getOrThrow()

override suspend fun editEnvelope(
id: Long,
type: String,
friendId: Long,
ledgerId: Long?,
amount: Long,
gift: String?,
memo: String?,
hasVisited: Boolean?,
handedOverAt: LocalDateTime,
categoryId: Long?,
customCategory: String?,
): Envelope = envelopesService.editEnvelope(
id = id,
envelopeRequest = EnvelopeRequest(
type = type,
friendId = friendId,
ledgerId = ledgerId,
amount = amount,
gift = gift,
memo = memo,
hasVisited = hasVisited,
handedOverAt = handedOverAt,
category = if (categoryId != null) {
CategoryRequest(
id = categoryId,
customCategory = customCategory,
)
} else {
null
},
)
).getOrThrow().toModel()
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,20 @@ class FriendRepositoryImpl @Inject constructor(
override suspend fun searchFriend(name: String): List<FriendSearch> = friendService.searchFriend(
name = name,
).getOrThrow().data.map { it.toModel() }

override suspend fun editFriend(
id: Long,
name: String,
phoneNumber: String?,
relationshipId: Long,
customRelation: String?,
) = friendService.editFriend(
id = id,
friendRequest = FriendRequest(
name = name,
phoneNumber = phoneNumber,
relationshipId = relationshipId,
customRelation = customRelation,
)
).getOrThrow()
}
8 changes: 8 additions & 0 deletions data/src/main/java/com/susu/data/remote/api/FriendService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import com.susu.data.remote.retrofit.ApiResult
import retrofit2.http.Body
import retrofit2.http.GET
import retrofit2.http.POST
import retrofit2.http.PUT
import retrofit2.http.Path
import retrofit2.http.Query

interface FriendService {
Expand All @@ -20,4 +22,10 @@ interface FriendService {
suspend fun searchFriend(
@Query("name") name: String,
): ApiResult<FriendSearchListResponse>

@PUT("friends/{id}")
suspend fun editFriend(
@Path("id") id: Long,
@Body friendRequest: FriendRequest,
): ApiResult<Unit>
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,18 @@ interface EnvelopesRepository {
suspend fun deleteEnvelope(
id: Long,
)

suspend fun editEnvelope(
id: Long,
type: String,
friendId: Long,
ledgerId: Long? = null,
amount: Long,
gift: String? = null,
memo: String? = null,
hasVisited: Boolean? = null,
handedOverAt: LocalDateTime,
categoryId: Long? = null,
customCategory: String? = null,
): Envelope
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,12 @@ interface FriendRepository {
suspend fun searchFriend(
name: String,
): List<FriendSearch>

suspend fun editFriend(
id: Long,
name: String,
phoneNumber: String? = null,
relationshipId: Long,
customRelation: String? = null,
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.susu.domain.usecase.envelope

import com.susu.core.common.runCatchingIgnoreCancelled
import com.susu.domain.repository.EnvelopesRepository
import com.susu.domain.repository.FriendRepository
import kotlinx.datetime.LocalDateTime
import javax.inject.Inject

class EditReceivedEnvelopeUseCase @Inject constructor(
private val friendRepository: FriendRepository,
private val envelopesRepository: EnvelopesRepository,
) {
suspend operator fun invoke(param: Param) = runCatchingIgnoreCancelled {
with(param) {
friendRepository.editFriend(
id = friendId,
name = friendName,
phoneNumber = phoneNumber,
relationshipId = relationshipId,
customRelation = customRelation,
)

envelopesRepository.createEnvelope(
type = "RECEIVED",
friendId = friendId,
ledgerId = ledgerId,
amount = amount,
gift = gift,
memo = memo,
hasVisited = hasVisited,
handedOverAt = handedOverAt,
)
}
}

data class Param(
val friendId: Long,
val friendName: String,
val phoneNumber: String? = null,
val relationshipId: Long,
val customRelation: String? = null,
val ledgerId: Long,
val amount: Long,
val gift: String? = null,
val memo: String? = null,
val handedOverAt: LocalDateTime,
val hasVisited: Boolean? = null,
)
}

0 comments on commit 03db3b9

Please sign in to comment.