Skip to content

Commit

Permalink
step2 - 3차 개선사항
Browse files Browse the repository at this point in the history
 -  패키지명 변경
 - LocalDataSource, RemoteDataSource 인터페이스 수정
 - local Data 저장 로직 추가
 - GlobalScope 제거
 - 테스트 케이스 네이밍 수정
 - 오류 테스트 케이스 추가
  • Loading branch information
oyj7677 committed Sep 18, 2023
1 parent 759b5b6 commit d262f9b
Show file tree
Hide file tree
Showing 26 changed files with 612 additions and 230 deletions.
10 changes: 10 additions & 0 deletions github-data/src/main/java/com/example/github/GitHubApi.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.example.github

import com.example.github.data.repository.remote.data.RemoteGithubData
import retrofit2.Response
import retrofit2.http.GET

interface GitHubApi {
@GET("/repositories")
suspend fun getGitHubRepo(): Response<List<RemoteGithubData>>
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.example.github_data
package com.example.github

import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
Expand Down
35 changes: 35 additions & 0 deletions github-data/src/main/java/com/example/github/data/mapper/Mapper.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.example.github.data.mapper

import com.example.github.data.repository.remote.data.RemoteGithubData
import com.example.github.data.room.GithubRepoEntity
import com.example.githubdomain.GithubRepoData

fun List<GithubRepoEntity>.githubRepoEntityToGithubRepoData(): List<GithubRepoData> {
return this.toList().map {
GithubRepoData(
id = it.id,
fullName = it.fullName,
description = it.description ?: ""
)
}
}

fun List<RemoteGithubData>.remoteGithubDataToGithubRepoData(): List<GithubRepoData> {
return this.toList().map {
GithubRepoData(
id = it.id,
fullName = it.fullName,
description = it.description
)
}
}

fun List<GithubRepoData>.githubRepoDataToGithubRepoEntity(): List<GithubRepoEntity> {
return this.toList().map {
GithubRepoEntity(
id = it.id,
fullName = it.fullName,
description = it.description
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.example.github.data.repository
import com.example.github.data.mapper.githubRepoEntityToGithubRepoData
import com.example.github.data.mapper.remoteGithubDataToGithubRepoData
import com.example.github.data.repository.local.LocalGithubDataSource
import com.example.github.data.repository.remote.RemoteGithubDataSource
import com.example.githubdomain.GitHubRepository
import com.example.githubdomain.GithubRepoData
import java.net.SocketTimeoutException

internal class GithubRepositoryImpl(
private val localGithubDataSource: LocalGithubDataSource,
private val remoteGithubDataSource: RemoteGithubDataSource
): GitHubRepository {

override suspend fun getGitHubRepoData(): List<GithubRepoData> {
val localData = localGithubDataSource.getGitHubRepo()
return if(localData.isNullOrEmpty()) {
try {
remoteGithubDataSource.getGitHubRepo().remoteGithubDataToGithubRepoData().also {
localGithubDataSource.insertGitHubRepo(it)
}
} catch (e: Exception) {
e.printStackTrace()
emptyList()
}

} else {
localData.githubRepoEntityToGithubRepoData()
}
}

suspend fun isEmptyLocalData() = localGithubDataSource.getGitHubRepo().isNullOrEmpty()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.example.github.data.repository.local

import com.example.github.data.mapper.githubRepoDataToGithubRepoEntity
import com.example.github.data.room.GithubRepoEntity
import com.example.githubdomain.GithubRepoData

internal class FakeLocalGithubDataSourceImpl: LocalGithubDataSource {
private val gitHubRepoEntityList = mutableListOf<GithubRepoEntity>()
override suspend fun getGitHubRepo() = gitHubRepoEntityList

override fun insertGitHubRepo(gitHubRepoDataList: List<GithubRepoData>) {
gitHubRepoEntityList.addAll(gitHubRepoDataList.githubRepoDataToGithubRepoEntity())
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.example.github.data.repository.local

import com.example.github.data.room.GithubRepoEntity
import com.example.githubdomain.GithubRepoData

interface LocalGithubDataSource {
suspend fun getGitHubRepo(): List<GithubRepoEntity>

fun insertGitHubRepo(gitHubRepoDataList: List<GithubRepoData>)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.example.github.data.repository.local

import com.example.github.data.mapper.githubRepoDataToGithubRepoEntity
import com.example.github.data.room.GithubRepoDao
import com.example.github.data.room.GithubRepoEntity
import com.example.githubdomain.GithubRepoData
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async

internal class LocalGithubDataSourceImpl(private val githubDao: GithubRepoDao): LocalGithubDataSource {
override suspend fun getGitHubRepo(): List<GithubRepoEntity> {
return CoroutineScope(Dispatchers.IO).async {
githubDao.getGitHubRepo()
}.await()
}

override fun insertGitHubRepo(gitHubRepoDataList: List<GithubRepoData>) {
githubDao.insertGithubRepoEntityList(gitHubRepoDataList.githubRepoDataToGithubRepoEntity())
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.example.github.data.repository.remote

import com.example.github.data.repository.remote.data.RemoteGithubData

interface RemoteGithubDataSource {
suspend fun getGitHubRepo(): List<RemoteGithubData>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.example.github.data.repository.remote

import com.example.github.GitHubApi
import com.example.github.GitHubService
import com.example.github.data.repository.remote.data.RemoteGithubData
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async

internal class RemoteGithubDataSourceImpl(private val githubService: GitHubApi = GitHubService.getGitHubService()): RemoteGithubDataSource {

override suspend fun getGitHubRepo(): List<RemoteGithubData> {
return CoroutineScope(Dispatchers.IO).async {
val response = githubService.getGitHubRepo()
if(response.isSuccessful) {
response.body()?: emptyList()
} else {
emptyList()
}
}.await()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.example.github.data.repository.remote.data


import com.google.gson.annotations.SerializedName

data class Owner(
@SerializedName("avatar_url")
val avatarUrl: String,
@SerializedName("events_url")
val eventsUrl: String,
@SerializedName("followers_url")
val followersUrl: String,
@SerializedName("following_url")
val followingUrl: String,
@SerializedName("gists_url")
val gistsUrl: String,
@SerializedName("gravatar_id")
val gravatarId: String,
@SerializedName("html_url")
val htmlUrl: String,
@SerializedName("id")
val id: Int,
@SerializedName("login")
val login: String,
@SerializedName("node_id")
val nodeId: String,
@SerializedName("organizations_url")
val organizationsUrl: String,
@SerializedName("received_events_url")
val receivedEventsUrl: String,
@SerializedName("repos_url")
val reposUrl: String,
@SerializedName("site_admin")
val siteAdmin: Boolean,
@SerializedName("starred_url")
val starredUrl: String,
@SerializedName("subscriptions_url")
val subscriptionsUrl: String,
@SerializedName("type")
val type: String,
@SerializedName("url")
val url: String
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package com.example.github.data.repository.remote.data

import com.google.gson.annotations.SerializedName

data class RemoteGithubData(
@SerializedName("archive_url")
val archiveUrl: String,
@SerializedName("assignees_url")
val assigneesUrl: String,
@SerializedName("blobs_url")
val blobsUrl: String,
@SerializedName("branches_url")
val branchesUrl: String,
@SerializedName("collaborators_url")
val collaboratorsUrl: String,
@SerializedName("comments_url")
val commentsUrl: String,
@SerializedName("commits_url")
val commitsUrl: String,
@SerializedName("compare_url")
val compareUrl: String,
@SerializedName("contents_url")
val contentsUrl: String,
@SerializedName("contributors_url")
val contributorsUrl: String,
@SerializedName("deployments_url")
val deploymentsUrl: String,
@SerializedName("description")
val description: String,
@SerializedName("downloads_url")
val downloadsUrl: String,
@SerializedName("events_url")
val eventsUrl: String,
@SerializedName("fork")
val fork: Boolean,
@SerializedName("forks_url")
val forksUrl: String,
@SerializedName("full_name")
val fullName: String,
@SerializedName("git_commits_url")
val gitCommitsUrl: String,
@SerializedName("git_refs_url")
val gitRefsUrl: String,
@SerializedName("git_tags_url")
val gitTagsUrl: String,
@SerializedName("hooks_url")
val hooksUrl: String,
@SerializedName("html_url")
val htmlUrl: String,
@SerializedName("id")
val id: Int,
@SerializedName("issue_comment_url")
val issueCommentUrl: String,
@SerializedName("issue_events_url")
val issueEventsUrl: String,
@SerializedName("issues_url")
val issuesUrl: String,
@SerializedName("keys_url")
val keysUrl: String,
@SerializedName("labels_url")
val labelsUrl: String,
@SerializedName("languages_url")
val languagesUrl: String,
@SerializedName("merges_url")
val mergesUrl: String,
@SerializedName("milestones_url")
val milestonesUrl: String,
@SerializedName("name")
val name: String,
@SerializedName("node_id")
val nodeId: String,
@SerializedName("notifications_url")
val notificationsUrl: String,
@SerializedName("owner")
val owner: Owner,
@SerializedName("private")
val `private`: Boolean,
@SerializedName("pulls_url")
val pullsUrl: String,
@SerializedName("releases_url")
val releasesUrl: String,
@SerializedName("stargazers_url")
val stargazersUrl: String,
@SerializedName("statuses_url")
val statusesUrl: String,
@SerializedName("subscribers_url")
val subscribersUrl: String,
@SerializedName("subscription_url")
val subscriptionUrl: String,
@SerializedName("tags_url")
val tagsUrl: String,
@SerializedName("teams_url")
val teamsUrl: String,
@SerializedName("trees_url")
val treesUrl: String,
@SerializedName("url")
val url: String
)
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package com.example.github_data.room
package com.example.github.data.room

import androidx.room.Dao
import androidx.room.Insert
import androidx.room.Query
import androidx.room.Transaction
import com.example.githubdomain.GithubRepoData

@Dao
interface GithubRepoDao {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.example.github_data.room
package com.example.github.data.room

import android.content.Context
import androidx.room.Database
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.example.github_data.room
package com.example.github.data.room

import androidx.room.ColumnInfo
import androidx.room.Entity
Expand All @@ -9,14 +9,11 @@ import com.google.gson.annotations.SerializedName
data class GithubRepoEntity(
@PrimaryKey
@ColumnInfo(name = "id")
@SerializedName("id")
val id: String,
val id: Int,

@ColumnInfo(name = "fullName")
@SerializedName("full_name")
val fullName: String,

@ColumnInfo(name = "description")
@SerializedName("description")
val description: String?
)
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.example.github_data.room
package com.example.github.data.room

import com.example.github_data.repository.GithubRepositoryImpl
import com.example.github_data.repository.local.LocalGithubDataSource
import com.example.github_data.repository.remote.RemoteGithubDataSource
import com.example.github.data.repository.GithubRepositoryImpl
import com.example.github.data.repository.local.LocalGithubDataSource
import com.example.github.data.repository.remote.RemoteGithubDataSource
import com.example.githubdomain.GitHubRepository

object Injector {
Expand Down
10 changes: 0 additions & 10 deletions github-data/src/main/java/com/example/github_data/GitHubApi.kt

This file was deleted.

14 changes: 0 additions & 14 deletions github-data/src/main/java/com/example/github_data/mapper/Mapper.kt

This file was deleted.

Loading

0 comments on commit d262f9b

Please sign in to comment.