Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import javax.inject.Inject
class LikeDataSource @Inject constructor(
private val likeService: LikeService
) {
suspend fun likeCourse(userId: Int, courseId: Int) =
likeService.likeCourse(userId, courseId)
suspend fun likeCourse(userId: Int, postId: Int) =
likeService.likeCourse(userId, postId)

suspend fun unlikeCourse(userId: Int, courseId: Int) =
likeService.unlikeCourse(userId, courseId)
suspend fun unlikeCourse(userId: Int, postId: Int) =
likeService.unlikeCourse(userId, postId)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,85 @@ package com.paw.key.data.repositoryimpl

import com.paw.key.data.remote.datasource.LikeDataSource
import com.paw.key.domain.repository.LikeRepository
import retrofit2.HttpException
import java.net.HttpURLConnection
import javax.inject.Inject

class LikeRepositoryImpl @Inject constructor(
private val dataSource: LikeDataSource
) : LikeRepository {

override suspend fun likeCourse(userId: Int, courseId: Int): Result<Unit> {
return try {
dataSource.likeCourse(userId, courseId)
Result.success(Unit)
} catch (e: Exception) {
Result.failure(e)
override suspend fun likeCourse(userId: Int, postId: Int): Result<Unit> {
return runCatching {
val response = dataSource.likeCourse(userId = userId, postId = postId)
if (response.code == "S000") {
Unit
} else {
throw Exception(response.message ?: "좋아요 처리에 실패했습니다")
}
}.recoverCatching { exception ->
when (exception) {
is HttpException -> {
when (exception.code()) {
HttpURLConnection.HTTP_BAD_REQUEST ->
throw Exception("본인이 작성한 게시글에는 좋아요를 할 수 없습니다")
HttpURLConnection.HTTP_CONFLICT ->
throw Exception("이미 좋아요를 누른 게시글입니다")
HttpURLConnection.HTTP_INTERNAL_ERROR ->
throw Exception("서버 오류가 발생했습니다. 잠시 후 다시 시도해주세요")
else ->
throw Exception("좋아요 처리에 실패했습니다 (${exception.code()})")
}
}
else -> {
if (isJsonParsingError(exception)) {
Unit
} else {
throw exception
}
}
}
}
}

override suspend fun unlikeCourse(userId: Int, courseId: Int): Result<Unit> {
return try {
dataSource.unlikeCourse(userId, courseId)
Result.success(Unit)
} catch (e: Exception) {
Result.failure(e)
override suspend fun unlikeCourse(userId: Int, postId: Int): Result<Unit> {
return runCatching {
val response = dataSource.unlikeCourse(userId = userId, postId = postId)
if (response.code == "S000") {
Unit
} else {
throw Exception(response.message ?: "좋아요 취소 처리에 실패했습니다")
}
}.recoverCatching { exception ->
when (exception) {
is HttpException -> {
when (exception.code()) {
HttpURLConnection.HTTP_BAD_REQUEST ->
throw Exception("잘못된 요청입니다")
HttpURLConnection.HTTP_CONFLICT ->
throw Exception("이미 처리된 요청입니다")
HttpURLConnection.HTTP_INTERNAL_ERROR ->
throw Exception("서버 오류가 발생했습니다. 잠시 후 다시 시도해주세요")
else ->
throw Exception("좋아요 취소 처리에 실패했습니다 (${exception.code()})")
}
}
else -> {
if (isJsonParsingError(exception)) {
Unit
} else {
throw exception
}
}
}
}
}

private fun isJsonParsingError(exception: Throwable): Boolean {
val message = exception.message ?: return false
return message.contains("Unexpected JSON token") ||
message.contains("Expected start of the object") ||
message.contains("JSON input") ||
message.contains("SerializationException")
}
}
4 changes: 2 additions & 2 deletions app/src/main/java/com/paw/key/data/service/LikeService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ interface LikeService {
suspend fun likeCourse(
@Header("X-USER-ID") userId: Int,
@Path("postId") postId: Int
): BaseResponse<Unit>
): BaseResponse<Unit?>

@DELETE("/api/v1/likes/{postId}")
suspend fun unlikeCourse(
@Header("X-USER-ID") userId: Int,
@Path("postId") postId: Int
): BaseResponse<Unit>
): BaseResponse<Unit?>
}
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,10 @@ fun TabListScreen(
listState.postsResult?.let { postsResult ->
val posts = postsResult.posts
if (posts.isNotEmpty()) {
items(posts) { post ->
items(
items = posts,
key = { post -> post.postId }
) { post ->
CourseCard(
title = post.title,
petName = post.writer.petName,
Expand All @@ -158,11 +161,10 @@ fun TabListScreen(
createdAt = post.createdAt,
isLiked = post.isLike,
onClickItem = {
//showBottomSheet = true
navigateToDetail(post.postId, post.routeId)
},
onClickLike = {
//viewModel.toggleLike(postId = post.postId)
onClickLike = { newLikeState ->
viewModel.toggleLike(post.postId, newLikeState)
}
)
}
Expand Down
Loading