Skip to content

Commit

Permalink
Merge pull request #43 from Team-Walkie/compose/add_post
Browse files Browse the repository at this point in the history
게시글 업로드 구현
  • Loading branch information
yonghanJu authored Sep 21, 2023
2 parents 2a1fae2 + d796054 commit cd7e19a
Show file tree
Hide file tree
Showing 13 changed files with 300 additions and 65 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ android {
minSdk = 26
targetSdk = 33
versionCode = 1
versionName = "1.0"
versionName = "1.0.3"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}
Expand Down
21 changes: 13 additions & 8 deletions app/src/main/java/com/whyranoid/walkie/KoinModules.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ import com.whyranoid.data.API
import com.whyranoid.data.AccountDataStore
import com.whyranoid.data.AppDatabase
import com.whyranoid.data.datasource.ChallengeDataSourceImpl
import com.whyranoid.data.datasource.PostDataSourceImpl
import com.whyranoid.data.datasource.UserDataSourceImpl
import com.whyranoid.data.datasource.account.AccountDataSourceImpl
import com.whyranoid.data.datasource.account.AccountService
import com.whyranoid.data.datasource.follow.FollowDataSourceImpl
import com.whyranoid.data.datasource.follow.FollowService
import com.whyranoid.data.datasource.post.PostDataSourceImpl
import com.whyranoid.data.datasource.post.PostService
import com.whyranoid.data.repository.AccountRepositoryImpl
import com.whyranoid.data.repository.ChallengeRepositoryImpl
import com.whyranoid.data.repository.FollowRepositoryImpl
Expand Down Expand Up @@ -41,19 +42,21 @@ import com.whyranoid.domain.usecase.GetUserBadgesUseCase
import com.whyranoid.domain.usecase.GetUserDetailUseCase
import com.whyranoid.domain.usecase.GetUserPostPreviewsUseCase
import com.whyranoid.domain.usecase.SignOutUseCase
import com.whyranoid.domain.usecase.UploadPostUseCase
import com.whyranoid.domain.usecase.running.GetRunningFollowerUseCase
import com.whyranoid.domain.usecase.running.RunningFinishUseCase
import com.whyranoid.domain.usecase.running.RunningStartUseCase
import com.whyranoid.presentation.viewmodel.challenge.ChallengeDetailViewModel
import com.whyranoid.presentation.viewmodel.challenge.ChallengeExitViewModel
import com.whyranoid.presentation.viewmodel.challenge.ChallengeMainViewModel
import com.whyranoid.presentation.screens.mypage.editprofile.EditProfileViewModel
import com.whyranoid.presentation.viewmodel.AddPostViewModel
import com.whyranoid.presentation.viewmodel.RunningEditViewModel
import com.whyranoid.presentation.viewmodel.RunningViewModel
import com.whyranoid.presentation.viewmodel.SelectHistoryViewModel
import com.whyranoid.presentation.viewmodel.SignInViewModel
import com.whyranoid.presentation.viewmodel.SplashViewModel
import com.whyranoid.presentation.viewmodel.UserPageViewModel
import com.whyranoid.presentation.viewmodel.challenge.ChallengeDetailViewModel
import com.whyranoid.presentation.viewmodel.challenge.ChallengeExitViewModel
import com.whyranoid.presentation.viewmodel.challenge.ChallengeMainViewModel
import okhttp3.Interceptor
import okhttp3.OkHttpClient
import okhttp3.Response
Expand All @@ -75,6 +78,7 @@ val viewModelModule = module {
factory { SignInViewModel(get()) }
factory { SelectHistoryViewModel(get()) }
factory { EditProfileViewModel(get()) }
factory { AddPostViewModel(get()) }
}

val repositoryModule = module {
Expand All @@ -89,7 +93,7 @@ val repositoryModule = module {

val dataSourceModule = module {
single<ChallengeDataSource> { ChallengeDataSourceImpl() }
single<PostDataSource> { PostDataSourceImpl() }
single<PostDataSource> { PostDataSourceImpl(get()) }
single<UserDataSource> { UserDataSourceImpl(get()) }
single<AccountDataSource> { AccountDataSourceImpl(get()) }
single<FollowDataSource> { FollowDataSourceImpl(get()) }
Expand All @@ -108,6 +112,7 @@ val useCaseModule = module {
single { RunningFinishUseCase() }
single { RunningStartUseCase() }
single { SignOutUseCase(get()) }
single { UploadPostUseCase(get(), get()) }
}

val databaseModule = module {
Expand All @@ -131,9 +136,7 @@ val databaseModule = module {
val networkModule = module {
class WalkieInterceptor : Interceptor {
override fun intercept(chain: Interceptor.Chain): Response {
val newRequest =
chain.request().newBuilder().header("Content-Type", "application/json")
.build()
val newRequest = chain.request()
return chain.proceed(newRequest)
}
}
Expand Down Expand Up @@ -171,4 +174,6 @@ val networkModule = module {
}

single { get<Retrofit>().create(FollowService::class.java) }

single { get<Retrofit>().create(PostService::class.java) }
}
2 changes: 2 additions & 0 deletions data/src/main/java/com/whyranoid/data/API.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ object API {
const val FOLLOWERS = "api/follow/{uid}/follower"

const val UNFOLLOW = "api/follow/unfollow"

const val UPLOAD_POST = "api/community/upload-post"
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.whyranoid.data.datasource.post

import com.whyranoid.domain.datasource.PostDataSource
import com.whyranoid.domain.model.post.Post
import com.whyranoid.domain.model.post.PostPreview
import okhttp3.MediaType
import okhttp3.MultipartBody
import okhttp3.RequestBody
import java.io.File

class PostDataSourceImpl(private val postService: PostService) : PostDataSource {
// TODO: change to api call
override suspend fun getPostPreviews(uid: Long): Result<List<PostPreview>> {
return Result.success(PostPreview.DUMMY_LIST)
}

override suspend fun getPostPreviews(
uid: Long,
year: Int,
month: Int,
day: Int,
): Result<List<PostPreview>> {
return Result.success(PostPreview.DUMMY_LIST)
}

// TODO: change to api call
override suspend fun getPost(postId: Long): Result<Post> {
return Result.success(Post.DUMMY)
}

override suspend fun uploadPost(
uid: Long,
content: String,
colorMode: Int,
history: String,
imagePath: String,
): Result<String> {
val uidBody = RequestBody.create(MediaType.parse("text/plain"), uid.toString())
val contentBody = RequestBody.create(MediaType.parse("text/plain"), content)
val colorModeBody = RequestBody.create(MediaType.parse("text/plain"), colorMode.toString())
val historyBody = RequestBody.create(MediaType.parse("text/plain"), history)

val file = File(imagePath)
val fileBody = RequestBody.create(MediaType.parse("image/*"), file)
val imagePart = MultipartBody.Part.createFormData("image", file.name, fileBody)

return kotlin.runCatching {
val uploadPostResponse =
postService.uploadPost(uidBody, contentBody, colorModeBody, historyBody, imagePart)
.body()
uploadPostResponse?.message.toString()
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.whyranoid.data.datasource.post

import com.whyranoid.data.API
import com.whyranoid.data.model.post.UploadPostResponse
import okhttp3.MultipartBody
import okhttp3.RequestBody
import retrofit2.Response
import retrofit2.http.Multipart
import retrofit2.http.POST
import retrofit2.http.Part

interface PostService {
@Multipart
@POST(API.UPLOAD_POST)
suspend fun uploadPost(
@Part("walkieId") walkieId: RequestBody,
@Part("content") content: RequestBody,
@Part("colorMode") colorMode: RequestBody,
@Part("historyContent") historyContent: RequestBody,
@Part image: MultipartBody.Part,
): Response<UploadPostResponse>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.whyranoid.data.model.post

data class UploadPostResponse(
val status: Long,
val message: String,
)
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,14 @@ class PostRepositoryImpl(
override suspend fun getPost(postId: Long): Result<Post> {
return postDataSource.getPost(postId)
}

override suspend fun uploadPost(
uid: Long,
content: String,
colorMode: Int,
history: String,
imagePath: String,
): Result<String> {
return postDataSource.uploadPost(uid, content, colorMode, history, imagePath)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,12 @@ interface PostDataSource {
): Result<List<PostPreview>>

suspend fun getPost(postId: Long): Result<Post>

suspend fun uploadPost(
uid: Long,
content: String,
colorMode: Int,
history: String,
imagePath: String,
): Result<String>
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,12 @@ interface PostRepository {
): Result<List<PostPreview>>

suspend fun getPost(postId: Long): Result<Post>

suspend fun uploadPost(
uid: Long,
content: String,
colorMode: Int,
history: String,
imagePath: String,
): Result<String>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.whyranoid.domain.usecase

import com.whyranoid.domain.repository.AccountRepository
import com.whyranoid.domain.repository.PostRepository
import kotlinx.coroutines.flow.first
import javax.inject.Inject

class UploadPostUseCase @Inject constructor(
private val postRepository: PostRepository,
private val accountRepository: AccountRepository,
) {
suspend operator fun invoke(
content: String,
colorMode: Int,
history: String,
imagePath: String,
): Result<String> {
return accountRepository.uId.first()?.let { uid ->
postRepository.uploadPost(uid, content, colorMode, history, imagePath)
} ?: kotlin.run {
Result.failure(Exception("Account Error"))
}
}
}
Loading

0 comments on commit cd7e19a

Please sign in to comment.