Skip to content

Commit

Permalink
[FEAT/#120] 게시물 떠먹기 API 연결
Browse files Browse the repository at this point in the history
Roel4990 committed Jan 22, 2025
1 parent abb7a4a commit 638a4be
Showing 10 changed files with 111 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.spoony.spoony.data.datasource

import com.spoony.spoony.data.dto.base.BaseResponseDTO

interface PostRemoteDataSource {
suspend fun postScoopPost(postId: Int, userId: Int): BaseResponseDTO<Boolean>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.spoony.spoony.data.datasourceimpl

import com.spoony.spoony.data.datasource.PostRemoteDataSource
import com.spoony.spoony.data.dto.base.BaseResponseDTO
import com.spoony.spoony.data.dto.request.PostScoopRequestDTO
import com.spoony.spoony.data.service.PostService
import javax.inject.Inject

class PostRemoteDataSourceImpl @Inject constructor(
private val postService: PostService
) : PostRemoteDataSource {
override suspend fun postScoopPost(postId: Int, userId: Int): BaseResponseDTO<Boolean> = postService.postScoopPost(
PostScoopRequestDTO(postId = postId, userId = userId)
)
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.spoony.spoony.data.di

import com.spoony.spoony.data.datasource.DummyRemoteDataSource
import com.spoony.spoony.data.datasource.PostRemoteDataSource
import com.spoony.spoony.data.datasourceimpl.DummyRemoteDataSourceImpl
import com.spoony.spoony.data.datasourceimpl.PostRemoteDataSourceImpl
import dagger.Binds
import dagger.Module
import dagger.hilt.InstallIn
@@ -14,4 +16,8 @@ abstract class DataSourceModule {
@Binds
@Singleton
abstract fun bindDummyDataSource(dummyRemoteDataSourceImpl: DummyRemoteDataSourceImpl): DummyRemoteDataSource

@Binds
@Singleton
abstract fun bindPostDataSource(postRemoteDataSourceImpl: PostRemoteDataSourceImpl): PostRemoteDataSource
}
6 changes: 6 additions & 0 deletions app/src/main/java/com/spoony/spoony/data/di/ServiceModule.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.spoony.spoony.data.di

import com.spoony.spoony.data.service.DummyService
import com.spoony.spoony.data.service.PostService
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
@@ -15,4 +16,9 @@ object ServiceModule {
@Singleton
fun provideDummyService(retrofit: Retrofit): DummyService =
retrofit.create(DummyService::class.java)

@Provides
@Singleton
fun providePostService(retrofit: Retrofit): PostService =
retrofit.create(PostService::class.java)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.spoony.spoony.data.dto.base

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

@Serializable
data class BaseResponseDTO<T>(
@SerialName("success")
val success: Boolean,
@SerialName("error")
val error: ErrorResponse? = null,
@SerialName("data")
val data: T? = null
)

@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
@@ -1,12 +1,16 @@
package com.spoony.spoony.data.repositoryimpl

import com.spoony.spoony.data.datasource.PostRemoteDataSource
import com.spoony.spoony.data.dto.base.BaseResponseDTO
import com.spoony.spoony.domain.entity.CategoryEntity
import com.spoony.spoony.domain.entity.PostEntity
import com.spoony.spoony.domain.repository.PostRepository
import javax.inject.Inject
import kotlinx.collections.immutable.persistentListOf

class PostRepositoryImpl @Inject constructor() : PostRepository {
class PostRepositoryImpl @Inject constructor(
val postRemoteDataSource: PostRemoteDataSource
) : PostRepository {
override suspend fun getPost(postId: Int): Result<PostEntity> =
Result.success(
PostEntity(
@@ -42,10 +46,10 @@ class PostRepositoryImpl @Inject constructor() : PostRepository {
)
)

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

override suspend fun postAddMap(postId: Int, userId: Int): Result<Boolean> =
Result.success(
19 changes: 19 additions & 0 deletions app/src/main/java/com/spoony/spoony/data/service/PostService.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.spoony.spoony.data.service

import com.spoony.spoony.data.dto.base.BaseResponseDTO
import com.spoony.spoony.data.dto.request.PostScoopRequestDTO
import retrofit2.http.Body
import retrofit2.http.POST

interface PostService {
companion object {
const val API = "api"
const val V1 = "v1"
const val POST = "post"
}

@POST("/$API/$V1/$POST/scoop")
suspend fun postScoopPost(
@Body postScoopRequestDTO: PostScoopRequestDTO
): BaseResponseDTO<Boolean>
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package com.spoony.spoony.domain.repository

import com.spoony.spoony.data.dto.base.BaseResponseDTO
import com.spoony.spoony.domain.entity.PostEntity

interface PostRepository {
suspend fun getPost(postId: Int): Result<PostEntity>

suspend fun postScoopPost(postId: Int, userId: Int): Result<Boolean>
suspend fun postScoopPost(postId: Int, userId: Int): Result<BaseResponseDTO<Boolean>>

suspend fun postAddMap(postId: Int, userId: Int): Result<Boolean>

Original file line number Diff line number Diff line change
@@ -49,18 +49,25 @@ class PlaceDetailViewModel @Inject constructor(
viewModelScope.launch {
postRepository.postScoopPost(userId = userId, postId = postId)
.onSuccess { response ->
(_state.value.postEntity as? UiState.Success)?.data?.let { currentPostEntity ->
with(currentPostEntity) {
_state.value = _state.value.copy(
postEntity = UiState.Success(
copy(isScooped = true)
)
)
when (response.success) {
true -> {
(_state.value.postEntity as? UiState.Success)?.data?.let { currentPostEntity ->
with(currentPostEntity) {
_state.value = _state.value.copy(
postEntity = UiState.Success(
copy(isScooped = true)
)
)
}
}
}
false -> {
// 통신에 성공했지만 떠먹기에 실패했을 경우
}
}
}
.onFailure {
// 실패 했을 경우
// 통신에 실패 했을 경우
}
}
}

0 comments on commit 638a4be

Please sign in to comment.