-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
[Home] 후원사 목록 data layer 구현
- Loading branch information
Showing
17 changed files
with
223 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
[ | ||
{ | ||
"name": "헤이딜러", | ||
"imageUrl": "https://raw.githubusercontent.com/droidknights/DroidKnights2020_App/master/androidapp/app/src/main/res/drawable-xxxhdpi/ic_sponsor_heydealer.png", | ||
"homepage": "https://heydealer.co.kr/", | ||
"grade": "gold" | ||
} | ||
] |
10 changes: 10 additions & 0 deletions
10
core/data/src/main/java/com/droidknights/app2023/core/data/api/GithubRawApi.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.droidknights.app2023.core.data.api | ||
|
||
import com.droidknights.app2023.core.data.api.model.SponsorResponse | ||
import retrofit2.http.GET | ||
|
||
internal interface GithubRawApi { | ||
|
||
@GET("/droidknights/DroidKnights2023_App/main/data/src/main/assets/sponsors.json") | ||
suspend fun getSponsors(): List<SponsorResponse> | ||
} |
18 changes: 18 additions & 0 deletions
18
core/data/src/main/java/com/droidknights/app2023/core/data/api/fake/AssetsGithubRawApi.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.droidknights.app2023.core.data.api.fake | ||
|
||
import android.content.Context | ||
import com.droidknights.app2023.core.data.api.GithubRawApi | ||
import com.droidknights.app2023.core.data.api.model.SponsorResponse | ||
import kotlinx.serialization.json.Json | ||
import kotlinx.serialization.json.decodeFromStream | ||
|
||
internal class AssetsGithubRawApi( | ||
context: Context, | ||
private val json: Json = Json { ignoreUnknownKeys = true }, | ||
) : GithubRawApi { | ||
private val assets = context.assets.open("sponsors.json") | ||
|
||
override suspend fun getSponsors(): List<SponsorResponse> { | ||
return json.decodeFromStream(assets) | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
core/data/src/main/java/com/droidknights/app2023/core/data/api/model/SponsorResponse.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.droidknights.app2023.core.data.api.model | ||
|
||
import kotlinx.serialization.SerialName | ||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
internal data class SponsorResponse( | ||
@SerialName("name") val name: String, | ||
@SerialName("imageUrl") val imageUrl: String, | ||
@SerialName("homepage") val homepage: String, | ||
@SerialName("grade") val grade: Grade, | ||
) { | ||
|
||
enum class Grade { | ||
@SerialName("platinum") | ||
PLATINUM, | ||
|
||
@SerialName("gold") | ||
GOLD, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
core/data/src/main/java/com/droidknights/app2023/core/data/mapper/SponsorMapper.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.droidknights.app2023.core.data.mapper | ||
|
||
import com.droidknights.app2023.core.data.api.model.SponsorResponse | ||
import com.droidknights.app2023.core.data.model.SponsorEntity | ||
|
||
internal fun SponsorResponse.toData(): SponsorEntity = SponsorEntity( | ||
name = name, | ||
imageUrl = imageUrl, | ||
homepage = homepage, | ||
grade = when (grade) { | ||
SponsorResponse.Grade.PLATINUM -> SponsorEntity.Grade.PLATINUM | ||
SponsorResponse.Grade.GOLD -> SponsorEntity.Grade.GOLD | ||
} | ||
) |
10 changes: 10 additions & 0 deletions
10
core/data/src/main/java/com/droidknights/app2023/core/data/model/SponsorEntity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.droidknights.app2023.core.data.model | ||
|
||
data class SponsorEntity( | ||
val name: String, | ||
val imageUrl: String, | ||
val homepage: String, | ||
val grade: Grade, | ||
) { | ||
enum class Grade { PLATINUM, GOLD, } | ||
} |
15 changes: 15 additions & 0 deletions
15
...a/src/main/java/com/droidknights/app2023/core/data/repository/DefaultSponsorRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.droidknights.app2023.core.data.repository | ||
|
||
import com.droidknights.app2023.core.data.api.GithubRawApi | ||
import com.droidknights.app2023.core.data.mapper.toData | ||
import com.droidknights.app2023.core.data.model.SponsorEntity | ||
import javax.inject.Inject | ||
|
||
internal class DefaultSponsorRepository @Inject constructor( | ||
private val githubRawApi: GithubRawApi, | ||
) : SponsorRepository { | ||
|
||
override suspend fun getSponsors(): List<SponsorEntity> { | ||
return githubRawApi.getSponsors().map { it.toData() } | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
core/data/src/main/java/com/droidknights/app2023/core/data/repository/SponsorRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.droidknights.app2023.core.data.repository | ||
|
||
import com.droidknights.app2023.core.data.model.SponsorEntity | ||
|
||
interface SponsorRepository { | ||
|
||
suspend fun getSponsors(): List<SponsorEntity> | ||
} |
2 changes: 1 addition & 1 deletion
2
...023/core/data/repository/FakeGithubApi.kt → ...p2023/core/data/api/fake/FakeGithubApi.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
core/data/src/test/java/com/droidknights/app2023/core/data/api/fake/FakeGithubRawApi.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.droidknights.app2023.core.data.api.fake | ||
|
||
import com.droidknights.app2023.core.data.api.GithubRawApi | ||
import com.droidknights.app2023.core.data.api.model.SponsorResponse | ||
import kotlinx.serialization.json.Json | ||
import kotlinx.serialization.json.decodeFromStream | ||
import java.io.File | ||
|
||
internal class FakeGithubRawApi( | ||
private val json: Json = Json { ignoreUnknownKeys = true }, | ||
) : GithubRawApi { | ||
private val assets = File("src/main/assets/sponsors.json") | ||
|
||
override suspend fun getSponsors(): List<SponsorResponse> { | ||
return json.decodeFromStream(assets.inputStream()) | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...st/java/com/droidknights/app2023/core/data/repository/DefaultContributorRepositoryTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
...c/test/java/com/droidknights/app2023/core/data/repository/DefaultSponsorRepositoryTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.droidknights.app2023.core.data.repository | ||
|
||
import com.droidknights.app2023.core.data.api.fake.FakeGithubRawApi | ||
import com.droidknights.app2023.core.data.model.SponsorEntity | ||
import io.kotest.core.spec.style.StringSpec | ||
import io.kotest.matchers.shouldBe | ||
import kotlinx.serialization.json.Json | ||
|
||
internal class DefaultSponsorRepositoryTest : StringSpec() { | ||
|
||
init { | ||
val repository: SponsorRepository = DefaultSponsorRepository( | ||
githubRawApi = FakeGithubRawApi( | ||
json = Json { ignoreUnknownKeys = true }, | ||
) | ||
) | ||
"역직렬화 테스트" { | ||
val expected = SponsorEntity( | ||
name = "헤이딜러", | ||
imageUrl = "https://raw.githubusercontent.com/droidknights/DroidKnights2020_App/master/androidapp/app/src/main/res/drawable-xxxhdpi/ic_sponsor_heydealer.png", | ||
homepage = "https://heydealer.co.kr/", | ||
grade = SponsorEntity.Grade.GOLD, | ||
) | ||
val actual = repository.getSponsors().first() | ||
actual shouldBe expected | ||
} | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
core/domain/src/main/java/com/droidknights/app2023/core/domain/mapper/SponsorMapper.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.droidknights.app2023.core.domain.mapper | ||
|
||
import com.droidknights.app2023.core.data.model.SponsorEntity | ||
import com.droidknights.app2023.core.domain.model.Sponsor | ||
|
||
internal fun SponsorEntity.toDomain(): Sponsor = Sponsor( | ||
name = name, | ||
imageUrl = imageUrl, | ||
homepage = homepage, | ||
grade = when (grade) { | ||
SponsorEntity.Grade.PLATINUM -> Sponsor.Grade.PLATINUM | ||
SponsorEntity.Grade.GOLD -> Sponsor.Grade.GOLD | ||
} | ||
) |
10 changes: 10 additions & 0 deletions
10
core/domain/src/main/java/com/droidknights/app2023/core/domain/model/Sponsor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.droidknights.app2023.core.domain.model | ||
|
||
data class Sponsor( | ||
val name: String, | ||
val imageUrl: String, | ||
val homepage: String, | ||
val grade: Grade, | ||
) { | ||
enum class Grade { PLATINUM, GOLD, } | ||
} |
15 changes: 15 additions & 0 deletions
15
core/domain/src/main/java/com/droidknights/app2023/core/domain/usecase/GetSponsorsUseCase.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.droidknights.app2023.core.domain.usecase | ||
|
||
import com.droidknights.app2023.core.data.repository.SponsorRepository | ||
import com.droidknights.app2023.core.domain.mapper.toDomain | ||
import com.droidknights.app2023.core.domain.model.Sponsor | ||
import javax.inject.Inject | ||
|
||
class GetSponsorsUseCase @Inject constructor( | ||
private val sponsorRepository: SponsorRepository, | ||
) { | ||
|
||
suspend operator fun invoke(): List<Sponsor> { | ||
return sponsorRepository.getSponsors().map { it.toDomain() } | ||
} | ||
} |