Skip to content
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

[Feat/#120] 게시물 떠먹기 API 연결 #123

Merged
merged 15 commits into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ import com.spoony.spoony.data.dto.base.BaseResponse
import com.spoony.spoony.data.dto.response.GetPostResponseDto

interface PostRemoteDataSource {
suspend fun postScoopPost(postId: Int, userId: Int): BaseResponse<Boolean>
suspend fun getPostData(postId: Int, userId: Int): BaseResponse<GetPostResponseDto>
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,19 @@ package com.spoony.spoony.data.datasourceimpl

import com.spoony.spoony.data.datasource.PostRemoteDataSource
import com.spoony.spoony.data.dto.base.BaseResponse
import com.spoony.spoony.data.dto.request.PostScoopRequestDto
import com.spoony.spoony.data.dto.response.GetPostResponseDto
import com.spoony.spoony.data.service.PostService
import javax.inject.Inject

class PostRemoteDataSourceImpl @Inject constructor(
private val postService: PostService
) : PostRemoteDataSource {
override suspend fun getPostData(postId: Int, userId: Int): BaseResponse<GetPostResponseDto> = postService.getPost(userId = userId, postId = postId)
override suspend fun getPostData(postId: Int, userId: Int): BaseResponse<GetPostResponseDto> =
postService.getPost(userId = userId, postId = postId)

override suspend fun postScoopPost(postId: Int, userId: Int): BaseResponse<Boolean> =
postService.postScoopPost(
PostScoopRequestDto(postId = postId, userId = userId)
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ data class BaseResponse<T>(
val error: ErrorResponse? = null,
@SerialName("data")
val data: T? = null
)

@Serializable
data class ErrorResponse(
@SerialName("message")
val message: String
)
) {
@Serializable
data class ErrorResponse(
@SerialName("message")
val message: String
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.spoony.spoony.data.dto.request

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
data class PostScoopRequestDto(
@SerialName("postId")
val postId: Int,
@SerialName("userId")
val userId: Int
)
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ class PostRepositoryImpl @Inject constructor(
}

override suspend fun postScoopPost(postId: Int, userId: Int): Result<Boolean> =
Result.success(
true
)
runCatching {
postRemoteDataSource.postScoopPost(postId = postId, userId = userId).success
}

override suspend fun postAddMap(postId: Int, userId: Int): Result<Boolean> =
Result.success(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package com.spoony.spoony.data.service

import com.spoony.spoony.data.dto.base.BaseResponse
import com.spoony.spoony.data.dto.request.PostScoopRequestDto
import com.spoony.spoony.data.dto.response.GetPostResponseDto
import retrofit2.http.Body
import retrofit2.http.GET
import retrofit2.http.POST
import retrofit2.http.Path

interface PostService {
Expand All @@ -11,4 +14,9 @@ interface PostService {
@Path("userId") userId: Int,
@Path("postId") postId: Int
): BaseResponse<GetPostResponseDto>

@POST("/api/v1/post/scoop")
suspend fun postScoopPost(
@Body postScoopRequestDto: PostScoopRequestDto
): BaseResponse<Boolean>
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ class PlaceDetailViewModel @Inject constructor(
}
}

fun useSpoon(userId: Int, postId: Int) {
fun useSpoon(postId: Int, userId: Int) {
viewModelScope.launch {
postRepository.postScoopPost(userId = userId, postId = postId)
.onSuccess { response ->
postRepository.postScoopPost(postId = postId, userId = userId)
.onSuccess {
(_state.value.postEntity as? UiState.Success)?.data?.let { currentPostEntity ->
with(currentPostEntity) {
_state.update {
Expand All @@ -75,7 +75,17 @@ class PlaceDetailViewModel @Inject constructor(
}
}
.onFailure {
// 실패 했을 경우
(_state.value.postEntity as? UiState.Success)?.data?.let { currentPostEntity ->
with(currentPostEntity) {
_state.update {
it.copy(
postEntity = UiState.Success(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: onFailure일 때도 UiState.Success로 넣어주고 있네요!! 제 생각에는 Failure로 해줘야 더 명확하지 않을까 싶습니다 세홍님 생각은 어떠세요?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

수정했습니다!

copy(isScooped = false)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: 여기에 copy가 왜 또 들어가나요..???? 위에서 copy 해주고 있어서 안쪽에서는 안해도 될 것 같습니다!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

수정했습니다!

)
)
}
}
}
}
}
}
Expand Down
Loading