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

내가 작성한 글 확인하기 기능 구현 #55

Merged
merged 3 commits into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions app/src/main/java/com/whyranoid/walkie/KoinModules.kt
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ val viewModelModule = module {
single { ChallengeMainViewModel(get(), get(), get()) }
single { ChallengeDetailViewModel(get()) }
single { ChallengeExitViewModel(get()) }
single { UserPageViewModel(get(), get(), get(), get(), get()) }
single { UserPageViewModel(get(), get(), get(), get(), get(), get()) }
factory { RunningViewModel(get(), get(), get(), get(), get(), get()) }
factory { RunningEditViewModel() }
factory { SplashViewModel(get()) }
Expand Down Expand Up @@ -131,7 +131,7 @@ val useCaseModule = module {
single { GetChallengeDetailUseCase(get()) }
single { GetChallengePreviewsByTypeUseCase(get()) }
single { GetPostUseCase(get()) }
single { GetUserPostPreviewsUseCase(get()) }
single { GetUserPostPreviewsUseCase(get(), get()) }
single { GetUserBadgesUseCase(get()) }
single { GetUserDetailUseCase(get()) }
single { GetRunningFollowerUseCase(get(), get()) }
Expand Down
4 changes: 4 additions & 0 deletions data/src/main/java/com/whyranoid/data/API.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ object API {

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

const val LIST_UP_MY_POST = "api/walkies/listup-my-post"

const val LIST_UP_POST = "api/community/listup-post"

object WalkingControl {
const val RUNNING_START = "api/walk/start"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,30 +24,24 @@ class FollowDataSourceImpl(private val followService: FollowService) : FollowDat
override suspend fun getWalkingFollowings(uid: Long): Result<List<User>> {
return kotlin.runCatching {
val response = followService.getWalkingFollowings(uid)
val followingList = requireNotNull(response.body()).list
followingList.map {
User(it.walkieId, "name", it.nickname, it.profileImg)
}
val followingList = requireNotNull(response.body())
followingList.map { it.toUser() }
}
}

override suspend fun getFollowings(uid: Long): Result<List<User>> {
return kotlin.runCatching {
val response = followService.getFollowings(uid)
val followingList = requireNotNull(response.body()).list
followingList.map {
User(it.walkieId, "name", it.nickname, it.profileImg)
}
val followingList = requireNotNull(response.body())
followingList.map { it.toUser() }
}
}

override suspend fun getFollowers(uid: Long): Result<List<User>> {
return kotlin.runCatching {
val response = followService.getFollowers(uid)
val followerList = requireNotNull(response.body()).list
followerList.map {
User(it.walkieId, "name", it.nickname, it.profileImg)
}
val followerList = requireNotNull(response.body())
followerList.map { it.toUser() }
}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.whyranoid.data.datasource.follow

import com.whyranoid.data.API
import com.whyranoid.data.model.account.UserResponse
import com.whyranoid.data.model.follow.FollowRequest
import com.whyranoid.data.model.follow.FollowResponse
import com.whyranoid.data.model.follow.FollowersResponse
import retrofit2.Response
import retrofit2.http.Body
import retrofit2.http.DELETE
Expand All @@ -21,17 +21,17 @@ interface FollowService {
@GET(API.WALKING_FOLLOWING)
suspend fun getWalkingFollowings(
@Path("uid") uid: Long,
): Response<FollowersResponse>
): Response<List<UserResponse>>

@GET(API.FOLLOWINGS)
suspend fun getFollowings(
@Path("uid") uid: Long,
): Response<FollowersResponse>
): Response<List<UserResponse>>

@GET(API.FOLLOWERS)
suspend fun getFollowers(
@Path("uid") uid: Long,
): Response<FollowersResponse>
): Response<List<UserResponse>>

@DELETE(API.UNFOLLOW)
suspend fun unfollow(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
package com.whyranoid.data.datasource.post

import android.util.Log
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
import java.util.Date

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)
return kotlin.runCatching {
val posts = requireNotNull(postService.getPosts(uid).body())
posts.map { it.toPostPreview() }
}
}

override suspend fun getPostPreviews(
Expand All @@ -20,7 +25,34 @@ class PostDataSourceImpl(private val postService: PostService) : PostDataSource
month: Int,
day: Int,
): Result<List<PostPreview>> {
return Result.success(PostPreview.DUMMY_LIST)
return getPostPreviews(uid).onSuccess { posts ->
posts.filter { postPreview ->
val date = Date(postPreview.date)
Log.d("TODOREMOVE", "${date.year}, ${date.month + 1}, ${date.date}")
date.month + 1 == month && year == date.year && day == date.date
Copy link
Member

Choose a reason for hiding this comment

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

이 조건을 만족하는 애들만 뽑히는거죠?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

넵 맞습니다

}
}
}

override suspend fun getMyPostPreviews(uid: Long): Result<List<PostPreview>> {
val response = postService.myPosts(uid)
response.body()?.map { it.toPostPreview() } ?: throw Exception(response.message())
Copy link
Member

Choose a reason for hiding this comment

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

추후에 Exception도 만들어야겠네요...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

넵 그리고 최근에 post 서버 내려갔었을 때 글 불러오니까 앱 터지더라구요 예외처리 안되어있는 부분 찾아서 해줘야 할 것 같습니다.

return Result.success(response.body()?.map { it.toPostPreview() }!!)
}

override suspend fun getMyPostPreviews(
uid: Long,
year: Int,
month: Int,
day: Int,
): Result<List<PostPreview>> {
return getMyPostPreviews(uid).onSuccess { posts ->
posts.filter { postPreview ->
val date = Date(postPreview.date)
Log.d("TODOREMOVE", "${date.year}, ${date.month + 1}, ${date.date}")
date.month + 1 == month && year == date.year && day == date.date
}
}
}

// TODO: change to api call
Expand All @@ -37,7 +69,8 @@ class PostDataSourceImpl(private val postService: PostService) : PostDataSource
): 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 colorModeBody =
RequestBody.create(MediaType.parse("text/plain"), colorMode.toString())
val historyBody = RequestBody.create(MediaType.parse("text/plain"), history)

val file = File(imagePath)
Expand All @@ -46,7 +79,13 @@ class PostDataSourceImpl(private val postService: PostService) : PostDataSource

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

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

interface PostService {
@Multipart
Expand All @@ -19,4 +22,10 @@ interface PostService {
@Part("historyContent") historyContent: RequestBody,
@Part image: MultipartBody.Part,
): Response<UploadPostResponse>

@GET(API.LIST_UP_MY_POST)
suspend fun myPosts(@Query("walkieId") uid: Long): Response<List<PostResponse>>

@GET(API.LIST_UP_POST)
suspend fun getPosts(@Query("walkieId") uid: Long): Response<List<PostResponse>>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.whyranoid.data.model.account

import com.google.gson.annotations.SerializedName
import com.whyranoid.domain.model.user.User

data class UserResponse(
@SerializedName("walkieId") val uid: Long,
@SerializedName("nickname") val nickname: String,
@SerializedName("profileImg") val profileImg: String,
@SerializedName("status") val status: String,
) {
fun toUser(): User {
return User(
uid,
status,
nickname,
profileImg,
)
}
}
38 changes: 38 additions & 0 deletions data/src/main/java/com/whyranoid/data/model/post/PostResponse.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.whyranoid.data.model.post

import com.google.gson.annotations.SerializedName
import com.whyranoid.data.model.account.UserResponse
import com.whyranoid.domain.model.post.PostPreview
import com.whyranoid.domain.model.post.TextVisibleState
import com.whyranoid.domain.util.dateFormatter

data class PostResponse(
@SerializedName("viewerId") val viewerId: Long,
@SerializedName("poster") val poster: UserResponse,
@SerializedName("postId") val postId: Long,
@SerializedName("liked") val liked: Boolean,
@SerializedName("likers") val likers: List<UserResponse>,
@SerializedName("photo") val photo: String,
@SerializedName("content") val content: String,
@SerializedName("date") val date: String,
@SerializedName("colorMode") val colorMode: Long,
@SerializedName("historyContent") val historyContent: String,
) {
fun toPostPreview(): PostPreview {
val destructedHistoryContent = historyContent.split('_')
println(destructedHistoryContent)
return PostPreview(
author = poster.toUser(),
id = this.poster.uid,
isLiked = this.liked,
likers = this.likers.map { it.toUser() },
imageUrl = this.photo,
date = dateFormatter.parse(this.date.replace("T", " ")).time,
textVisibleState = TextVisibleState.values()[this.colorMode.toInt()],
distanceText = destructedHistoryContent[2],
timeText = destructedHistoryContent[3],
paceText = destructedHistoryContent[4],
address = destructedHistoryContent[1],
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,19 @@ class PostRepositoryImpl(
return postDataSource.getPostPreviews(uid, year, month, day)
}

override suspend fun getMyPostPreviews(uid: Long): Result<List<PostPreview>> {
return postDataSource.getMyPostPreviews(uid)
}

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

override suspend fun getPost(postId: Long): Result<Post> {
return postDataSource.getPost(postId)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,22 @@ import com.whyranoid.domain.model.post.PostPreview
interface PostDataSource {
suspend fun getPostPreviews(uid: Long): Result<List<PostPreview>>

suspend fun getMyPostPreviews(uid: Long): Result<List<PostPreview>>

suspend fun getPostPreviews(
uid: Long,
year: Int,
month: Int,
day: Int,
): Result<List<PostPreview>>

suspend fun getMyPostPreviews(
uid: Long,
year: Int,
month: Int,
day: Int,
): Result<List<PostPreview>>

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

suspend fun uploadPost(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,33 +1,44 @@
package com.whyranoid.domain.model.post

import com.whyranoid.domain.model.user.User

data class PostPreview(
val author: User,
val id: Long,
val isLiked: Boolean = false,
val likers: List<User> = listOf(),
val imageUrl: String,
val date: Long = 0L,
val textVisibleState: TextVisibleState = TextVisibleState.HIDE,
val distanceText: String = "",
val timeText: String = "",
val paceText: String = "",
val address: String = "",
) {
companion object {
val DUMMY_LIST = listOf(
PostPreview(
author = User.DUMMY,
id = 0L,
imageUrl = "https://picsum.photos/250/250",
),
PostPreview(
author = User.DUMMY,
id = 1L,
imageUrl = "https://picsum.photos/250/250",
),
PostPreview(
author = User.DUMMY,
id = 2L,
imageUrl = "https://picsum.photos/250/250",
),
PostPreview(
author = User.DUMMY,
id = 3L,
imageUrl = "https://picsum.photos/250/250",
),
PostPreview(
author = User.DUMMY,
id = 4L,
imageUrl = "https://picsum.photos/250/250",
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,22 @@ import com.whyranoid.domain.model.post.PostPreview
interface PostRepository {
suspend fun getUserPostPreviews(uid: Long): Result<List<PostPreview>>

suspend fun getMyPostPreviews(uid: Long): Result<List<PostPreview>>

suspend fun getUserPostPreviews(
uid: Long,
year: Int,
month: Int,
day: Int,
): Result<List<PostPreview>>

suspend fun getMyPostPreviews(
uid: Long,
year: Int,
month: Int,
day: Int,
): Result<List<PostPreview>>

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

suspend fun uploadPost(
Expand Down
Loading
Loading