-
Notifications
You must be signed in to change notification settings - Fork 1
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
좋아요 기능 구현, Reponse(retrofit)에서 결과를 가져오는 확장함수 구현 #65
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.whyranoid.data | ||
|
||
import retrofit2.Response | ||
|
||
fun <T, R> Response<T>.getResult(transform: (T) -> R): R { | ||
if (this.isSuccessful.not()) | ||
throw Exception(this.errorBody().toString()) | ||
else if (this.body() == null) | ||
throw Exception(this.message()) | ||
|
||
return requireNotNull(this.body()?.let(transform) ?: throw Exception("empty response")) | ||
} |
6 changes: 6 additions & 0 deletions
6
data/src/main/java/com/whyranoid/data/datasource/community/CommunityDataSource.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package com.whyranoid.data.datasource.community | ||
|
||
interface CommunityDataSource { | ||
|
||
suspend fun likePost(postId: Long, likerId: Long): Result<Long> | ||
} |
20 changes: 20 additions & 0 deletions
20
data/src/main/java/com/whyranoid/data/datasource/community/CommunityDataSourceImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.whyranoid.data.datasource.community | ||
|
||
import com.whyranoid.data.getResult | ||
import com.whyranoid.data.model.community.request.PostLikeRequest | ||
|
||
class CommunityDataSourceImpl( | ||
private val service: CommunityService | ||
) : CommunityDataSource { | ||
override suspend fun likePost(postId: Long, likerId: Long): Result<Long> { | ||
|
||
return kotlin.runCatching { | ||
val request = PostLikeRequest(likerId, postId) | ||
val response = service.likePost(request) | ||
|
||
response.getResult { | ||
it.likerCount | ||
} | ||
} | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
data/src/main/java/com/whyranoid/data/datasource/community/CommunityService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.whyranoid.data.datasource.community | ||
|
||
import com.whyranoid.data.API | ||
import com.whyranoid.data.model.community.request.PostLikeRequest | ||
import com.whyranoid.data.model.community.response.PostLikeResponse | ||
import retrofit2.Response | ||
import retrofit2.http.Body | ||
import retrofit2.http.POST | ||
|
||
interface CommunityService { | ||
|
||
@POST(API.SEND_LIKE) | ||
suspend fun likePost(@Body postLikeRequest: PostLikeRequest): Response<PostLikeResponse> | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
data/src/main/java/com/whyranoid/data/model/community/request/PostLikeRequest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.whyranoid.data.model.community.request | ||
|
||
import com.google.gson.annotations.SerializedName | ||
|
||
data class PostLikeRequest( | ||
@SerializedName("likerId") val likerId: Long, | ||
@SerializedName("postId") val postId: Long, | ||
) |
10 changes: 10 additions & 0 deletions
10
data/src/main/java/com/whyranoid/data/model/community/response/LikerProfile.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.whyranoid.data.model.community.response | ||
|
||
import com.google.gson.annotations.SerializedName | ||
|
||
data class LikerProfile( | ||
@SerializedName("nickname") val nickname: String, | ||
@SerializedName("profileImg") val profileImg: String, | ||
@SerializedName("status") val status: String, | ||
@SerializedName("walkieId") val walkieId: Long | ||
) |
10 changes: 10 additions & 0 deletions
10
data/src/main/java/com/whyranoid/data/model/community/response/PostLikeResponse.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.whyranoid.data.model.community.response | ||
|
||
import com.google.gson.annotations.SerializedName | ||
|
||
data class PostLikeResponse( | ||
@SerializedName("likerCount") val likerCount: Long, | ||
@SerializedName("likerId") val likerId: Long, | ||
@SerializedName("likerProfiles") val likerProfiles: List<LikerProfile>, | ||
@SerializedName("postId") val postId: Long | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
data/src/main/java/com/whyranoid/data/repository/CommunityRepositoryImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.whyranoid.data.repository | ||
|
||
import com.whyranoid.data.datasource.community.CommunityDataSource | ||
import com.whyranoid.domain.repository.CommunityRepository | ||
|
||
class CommunityRepositoryImpl( | ||
private val communityDataSource: CommunityDataSource | ||
): CommunityRepository { | ||
override suspend fun likePost(postId: Long, likerId: Long): Result<Long> { | ||
return communityDataSource.likePost(postId, likerId) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
domain/src/main/java/com/whyranoid/domain/repository/CommunityRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package com.whyranoid.domain.repository | ||
|
||
interface CommunityRepository { | ||
|
||
suspend fun likePost(postId: Long, likerId: Long): Result<Long> | ||
} |
16 changes: 16 additions & 0 deletions
16
domain/src/main/java/com/whyranoid/domain/usecase/LikePostUseCase.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.whyranoid.domain.usecase | ||
|
||
import com.whyranoid.domain.repository.AccountRepository | ||
import com.whyranoid.domain.repository.CommunityRepository | ||
import kotlinx.coroutines.flow.first | ||
|
||
class LikePostUseCase( | ||
private val communityRepository: CommunityRepository, | ||
private val accountRepository: AccountRepository | ||
) { | ||
|
||
suspend operator fun invoke(postId: Long): Result<Long> { | ||
val uid = requireNotNull(accountRepository.uId.first()) | ||
return communityRepository.likePost(postId, uid) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
제가 잘 몰라서 질문 드립니다!
레트로핏 객체를 응집도에 따라서 여러개 생성하나요??
baseUrl이 다른것 이외에 차이점이 없더라고 여러개 생성하나요?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@yonghanJu
어... 이러면 안되지만 그렇게 되어있길래요...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
근데 생각해보니까 서비스 객체들은 원래 여러 개 생성할 수 밖에 없지않나요