From 03db3b95589106625e1a40a15424cdfb3b90e54b Mon Sep 17 00:00:00 2001 From: jinukeu Date: Wed, 31 Jan 2024 14:58:58 +0900 Subject: [PATCH] feat: EditReceivedEnvelopeUseCase --- .../repository/EnvelopesRepositoryImpl.kt | 35 +++++++++++++ .../data/repository/FriendRepositoryImpl.kt | 16 ++++++ .../com/susu/data/remote/api/FriendService.kt | 8 +++ .../domain/repository/EnvelopesRepository.kt | 14 ++++++ .../domain/repository/FriendRepository.kt | 8 +++ .../envelope/EditReceivedEnvelopeUseCase.kt | 49 +++++++++++++++++++ 6 files changed, 130 insertions(+) create mode 100644 domain/src/main/java/com/susu/domain/usecase/envelope/EditReceivedEnvelopeUseCase.kt diff --git a/data/src/main/java/com/susu/data/data/repository/EnvelopesRepositoryImpl.kt b/data/src/main/java/com/susu/data/data/repository/EnvelopesRepositoryImpl.kt index 3c322612..55f80128 100644 --- a/data/src/main/java/com/susu/data/data/repository/EnvelopesRepositoryImpl.kt +++ b/data/src/main/java/com/susu/data/data/repository/EnvelopesRepositoryImpl.kt @@ -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( @@ -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() } diff --git a/data/src/main/java/com/susu/data/data/repository/FriendRepositoryImpl.kt b/data/src/main/java/com/susu/data/data/repository/FriendRepositoryImpl.kt index 4812ca9c..6126cba7 100644 --- a/data/src/main/java/com/susu/data/data/repository/FriendRepositoryImpl.kt +++ b/data/src/main/java/com/susu/data/data/repository/FriendRepositoryImpl.kt @@ -27,4 +27,20 @@ class FriendRepositoryImpl @Inject constructor( override suspend fun searchFriend(name: String): List = 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() } diff --git a/data/src/main/java/com/susu/data/remote/api/FriendService.kt b/data/src/main/java/com/susu/data/remote/api/FriendService.kt index 9274a7b4..b7fd40aa 100644 --- a/data/src/main/java/com/susu/data/remote/api/FriendService.kt +++ b/data/src/main/java/com/susu/data/remote/api/FriendService.kt @@ -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 { @@ -20,4 +22,10 @@ interface FriendService { suspend fun searchFriend( @Query("name") name: String, ): ApiResult + + @PUT("friends/{id}") + suspend fun editFriend( + @Path("id") id: Long, + @Body friendRequest: FriendRequest, + ): ApiResult } diff --git a/domain/src/main/java/com/susu/domain/repository/EnvelopesRepository.kt b/domain/src/main/java/com/susu/domain/repository/EnvelopesRepository.kt index 33fc7104..274b9149 100644 --- a/domain/src/main/java/com/susu/domain/repository/EnvelopesRepository.kt +++ b/domain/src/main/java/com/susu/domain/repository/EnvelopesRepository.kt @@ -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 } diff --git a/domain/src/main/java/com/susu/domain/repository/FriendRepository.kt b/domain/src/main/java/com/susu/domain/repository/FriendRepository.kt index 2a1fb65a..e7c7e31a 100644 --- a/domain/src/main/java/com/susu/domain/repository/FriendRepository.kt +++ b/domain/src/main/java/com/susu/domain/repository/FriendRepository.kt @@ -13,4 +13,12 @@ interface FriendRepository { suspend fun searchFriend( name: String, ): List + + suspend fun editFriend( + id: Long, + name: String, + phoneNumber: String? = null, + relationshipId: Long, + customRelation: String? = null, + ) } diff --git a/domain/src/main/java/com/susu/domain/usecase/envelope/EditReceivedEnvelopeUseCase.kt b/domain/src/main/java/com/susu/domain/usecase/envelope/EditReceivedEnvelopeUseCase.kt new file mode 100644 index 00000000..83b22a25 --- /dev/null +++ b/domain/src/main/java/com/susu/domain/usecase/envelope/EditReceivedEnvelopeUseCase.kt @@ -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, + ) +}