Skip to content

Commit

Permalink
Merge pull request #33 from PawWithU/feature/network_home
Browse files Browse the repository at this point in the history
Feature/network_home : 홈 api 연동 완료
  • Loading branch information
mjkim1019 authored Nov 19, 2023
2 parents fb11919 + e7fcda4 commit bb3a220
Show file tree
Hide file tree
Showing 20 changed files with 298 additions and 137 deletions.
3 changes: 3 additions & 0 deletions core/data/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,15 @@ android {

dependencies {
implementation(project(":core:model"))
implementation(project(":core:util"))

kapt(libs.hilt.compiler)
implementation(libs.hilt.android)

implementation(libs.retrofit.core)
implementation(libs.retrofit.kotlin.serialization)
implementation(libs.retrofit.converter.moshi)
implementation(libs.moshi.kotlin)
implementation(libs.okhttp.logging)
implementation(libs.kotlinx.serialization.json)
implementation(libs.kotlinx.datetime)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,34 @@
package com.kusitms.connectdog.core.data.api

import com.kusitms.connectdog.core.data.api.model.ExampleResponse
import com.kusitms.connectdog.core.data.api.model.AnnouncementHomeResponseItem
import com.kusitms.connectdog.core.data.api.model.AnnouncementSearchResponseItem
import com.kusitms.connectdog.core.data.api.model.ReviewResponseItem
import retrofit2.http.GET
import retrofit2.http.Query

internal interface ApiService {
@GET("/droidknights/DroidKnights2023_App/main/core/data/src/main/assets/sponsors.json")
suspend fun getExample(): List<ExampleResponse>

@GET("/volunteers/posts/home")
suspend fun getAnnouncementPostsHome(): List<AnnouncementHomeResponseItem>

@GET("/volunteers/posts")
suspend fun getAnnouncementFilterPosts(
@Query("postStatus") postStatus: String?,
@Query("departureLoc") departureLoc: String?,
@Query("arrivalLoc") arrivalLoc: String?,
@Query("startDate") startDate: String?,
@Query("endDate") endDate: String?,
@Query("dogSize") dogSize: String?,
@Query("isKennel") isKennel: Boolean?,
@Query("intermediaryName") intermediaryName: String?,
@Query("orderCondition") orderCondition: String?,
@Query("page") page: Int,
@Query("size") size: Int
): List<AnnouncementSearchResponseItem>

@GET("/volunteers/reviews")
suspend fun getReviewsHome(
@Query("page") page: Int,
@Query("size") size: Int
): List<ReviewResponseItem>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.kusitms.connectdog.core.data.api.model

import com.squareup.moshi.JsonClass

@JsonClass(generateAdapter = true)
data class AnnouncementHomeResponseItem(
val arrivalLoc: String,
val departureLoc: String,
val endDate: String,
val intermediaryName: String,
val isKennel: Boolean,
val mainImage: String,
val postId: Int,
val startDate: String
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.kusitms.connectdog.core.data.api.model

import com.squareup.moshi.JsonClass

@JsonClass(generateAdapter = true)
data class AnnouncementSearchResponseItem(
val postId: Int,
val departureLoc: String,
val arrivalLoc: String,
val startDate: String,
val endDate: String,
val intermediaryName: String,
val isKennel: Boolean,
val mainImage: String
)

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.kusitms.connectdog.core.data.api.model

import com.squareup.moshi.JsonClass

@JsonClass(generateAdapter = true)
data class ReviewResponseItem(
val arrivalLoc: String,
val content: String,
val departureLoc: String,
val dogName: String,
val endDate: String,
val images: List<String>,
val intermediaryName: String,
val mainImage: String,
val startDate: String,
val volunteerNickname: String
)
Original file line number Diff line number Diff line change
@@ -1,24 +1,51 @@
package com.kusitms.connectdog.core.data.di

import com.jakewharton.retrofit2.converter.kotlinx.serialization.asConverterFactory
import com.kusitms.connectdog.core.data.api.ApiService
import com.squareup.moshi.Moshi
import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import kotlinx.serialization.json.Json
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.Interceptor
import okhttp3.OkHttpClient
import okhttp3.Protocol
import okhttp3.Response
import okhttp3.ResponseBody
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Converter
import retrofit2.Retrofit
import retrofit2.converter.moshi.MoshiConverterFactory
import java.util.concurrent.TimeUnit
import javax.inject.Singleton

@Module
@InstallIn(SingletonComponent::class)
internal object ApiModule {
private const val BASE_URL = "https://raw.githubusercontent.com"
private const val BASE_URL = "https://dev-api.connectdog.site"

private val networkInterceptor: Interceptor = Interceptor { chain ->
val request = chain.request()
val jwt = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJBY2Nlc3NUb2tlbiIsInJvbGVOYW1lIjoiVk9MVU5URUVSIiwiaWQiOjEsImV4cCI6MTcwMDQyMTc2NH0.sn-XWVtZEp4gI_yq4D9-UCXCJpxOcdqBdnlIW_n2PMcI_KSWWfYiLboZG3RE4uwyReo90_XjcLSUA4a2MPyHZA"
try {
chain.proceed(
request.newBuilder()
.addHeader(
"Authorization",
"Bearer $jwt"
) // todo
.build()
)
} catch (e: Exception) {
Response.Builder()
.request(request)
.protocol(Protocol.HTTP_2)
.code(1001)
.message(e.message ?: "")
.body(ResponseBody.create(null, e.message ?: ""))
.build()
}
}

@Provides
@Singleton
Expand All @@ -28,6 +55,7 @@ internal object ApiModule {
level = HttpLoggingInterceptor.Level.BODY
}
return OkHttpClient.Builder()
.addInterceptor(networkInterceptor)
.addNetworkInterceptor(httpLoggingInterceptor)
.connectTimeout(10, TimeUnit.SECONDS)
.readTimeout(10, TimeUnit.SECONDS)
Expand All @@ -37,19 +65,21 @@ internal object ApiModule {

@Provides
@Singleton
fun provideConverterFactory(json: Json): Converter.Factory {
return json.asConverterFactory("application/json".toMediaType())
fun provideMoshi(): Moshi {
return Moshi.Builder()
.addLast(KotlinJsonAdapterFactory())
.build()
}

@Provides
@Singleton
fun provideApiService(
okHttpClient: OkHttpClient,
converterFactory: Converter.Factory
moshi: Moshi
): ApiService {
return Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(converterFactory)
.addConverterFactory(MoshiConverterFactory.create(moshi))
.client(okHttpClient).build()
.create(ApiService::class.java)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
package com.kusitms.connectdog.core.data.di

import android.content.Context
import com.kusitms.connectdog.core.data.repository.DefaultExampleRepository
import com.kusitms.connectdog.core.data.repository.ExampleRepository
import com.kusitms.connectdog.core.data.api.ApiService
import com.kusitms.connectdog.core.data.repository.HomeRepository
import com.kusitms.connectdog.core.data.repository.HomeRepositoryImpl
import com.kusitms.connectdog.core.data.repository.LoginRepository
import dagger.Binds
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent
import javax.inject.Singleton

@InstallIn(SingletonComponent::class)
@Module
internal abstract class DataModule {
@Binds
abstract fun bindsExampleRepository(repository: DefaultExampleRepository): ExampleRepository
internal class DataModule {

@Binds
abstract fun bindsHomeRepository(repository: HomeRepositoryImpl): HomeRepository
@Provides
@Singleton
fun provideHomeRepository(apiService: ApiService): HomeRepository {
return HomeRepositoryImpl(apiService)
}

companion object {
@Provides
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.kusitms.connectdog.core.data.mapper

import com.kusitms.connectdog.core.data.api.model.AnnouncementHomeResponseItem
import com.kusitms.connectdog.core.data.api.model.AnnouncementSearchResponseItem
import com.kusitms.connectdog.core.model.Announcement
import com.kusitms.connectdog.core.util.dateFormat
import com.kusitms.connectdog.core.util.toLocalDate

internal fun AnnouncementHomeResponseItem.toData(): Announcement {
val datePattern = "yyyy-MM-dd"
val uiPattern = "YY.MM.dd(E)"
val start = this.startDate.toLocalDate(datePattern).dateFormat(uiPattern)
val end = this.endDate.toLocalDate(datePattern).dateFormat(uiPattern)
return Announcement(
imageUrl = this.mainImage,
location = "${this.departureLoc}${this.arrivalLoc}",
date = "$start-$end",
organization = this.intermediaryName,
hasKennel = this.isKennel
)
}

internal fun AnnouncementSearchResponseItem.toData(): Announcement {
val datePattern = "yyyy-MM-dd"
val uiPattern = "YY.MM.dd(E)"
val start = this.startDate.toLocalDate(datePattern).dateFormat(uiPattern)
val end = this.endDate.toLocalDate(datePattern).dateFormat(uiPattern)
return Announcement(
imageUrl = this.mainImage,
location = "${this.departureLoc}${this.arrivalLoc}",
date = "$start-$end",
organization = this.intermediaryName,
hasKennel = this.isKennel
)
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.kusitms.connectdog.core.data.mapper

import com.kusitms.connectdog.core.data.api.model.ReviewResponseItem
import com.kusitms.connectdog.core.model.Review
import com.kusitms.connectdog.core.util.toLocalDate

internal fun ReviewResponseItem.toData(): Review {
val datePattern = "yyyy-MM-dd"
val start = startDate.toLocalDate(datePattern)
val end = endDate.toLocalDate(datePattern)
return Review(
profileUrl = mainImage, // todo profileUrl로 바뀔 예정
dogName = dogName,
userName = volunteerNickname,
contentUrl = mainImage,
date = "$start-$end",
location = "$departureLoc->$arrivalLoc",
organization = intermediaryName,
content = content
)
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,19 @@ import com.kusitms.connectdog.core.model.Review
interface HomeRepository {
suspend fun getAnnouncementList(): List<Announcement>

suspend fun getAnnouncementListWithFilter(): List<Announcement>
suspend fun getAnnouncementListWithFilter(
postStatus: String? = null,
departureLoc: String? = null,
arrivalLoc: String? = null,
startDate: String? = "",
endDate: String? = "",
dogSize: String? = null,
isKennel: Boolean? = null,
intermediaryName: String? = null,
orderCondition: String? = null,
page: Int? = 0,
size: Int? = 50
): List<Announcement>

suspend fun getReviewList(): List<Review>
suspend fun getReviewList(page: Int? = 0, size: Int? = 5): List<Review>
}
Loading

0 comments on commit bb3a220

Please sign in to comment.