Skip to content

Commit ca3c255

Browse files
committed
5주차 미션/ 안드로이드 1조 박경민
1 parent bd77f9d commit ca3c255

File tree

14 files changed

+193
-105
lines changed

14 files changed

+193
-105
lines changed

.idea/gradle.xml

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/build.gradle.kts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import java.util.Properties
2+
13
plugins {
24
alias(libs.plugins.android.application)
35
alias(libs.plugins.kotlin.android)
@@ -18,6 +20,13 @@ android {
1820

1921
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
2022

23+
val properties = Properties()
24+
val localPropertiesFile = rootProject.file("local.properties")
25+
if (localPropertiesFile.exists()) {
26+
properties.load(localPropertiesFile.inputStream())
27+
}
28+
val baseUrl = properties.getProperty("BASE_URL")
29+
buildConfigField("String", "BASE_URL", "\"$baseUrl\"")
2130
}
2231

2332
buildTypes {
@@ -38,6 +47,7 @@ android {
3847
}
3948
buildFeatures {
4049
compose = true
50+
buildConfig = true
4151
}
4252
}
4353

@@ -68,10 +78,16 @@ dependencies {
6878

6979
// Coroutines
7080
implementation(libs.kotlinx.coroutines.android)
81+
implementation(libs.adapter.guava)
7182

7283
testImplementation(libs.junit)
7384
androidTestImplementation(libs.androidx.junit)
7485
androidTestImplementation(libs.androidx.espresso.core)
7586
debugImplementation(libs.androidx.compose.ui.tooling)
7687
debugImplementation(libs.androidx.compose.ui.test.manifest)
88+
89+
implementation(libs.retrofit)
90+
implementation(libs.retrofit.converter.gson)
91+
implementation(libs.okhttp)
92+
implementation(libs.okhttp.logging.interceptor)
7793
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.example.kuit6_android_api.data.api
2+
3+
import android.R.attr.value
4+
import com.example.kuit6_android_api.data.model.request.PostCreateRequest
5+
import com.example.kuit6_android_api.data.model.response.BaseResponse
6+
import com.example.kuit6_android_api.data.model.response.PostResponse
7+
import retrofit2.http.Body
8+
import retrofit2.http.DELETE
9+
import retrofit2.http.GET
10+
import retrofit2.http.POST
11+
import retrofit2.http.PUT
12+
import retrofit2.http.Path
13+
import retrofit2.http.Query
14+
15+
interface ApiService {
16+
@GET(value = "/api/posts")
17+
suspend fun getPosts(): BaseResponse<List<PostResponse>>
18+
19+
@POST(value= "/api/posts")
20+
suspend fun createPost(
21+
@Query(value = "author") author: String = "경민",
22+
@Body request: PostCreateRequest
23+
): BaseResponse<PostResponse>
24+
25+
@GET(value= "/api/posts/{id}")
26+
suspend fun getPostDetail(
27+
@Path("id") id: Long
28+
): BaseResponse<PostResponse>
29+
30+
@PUT(value = "/api/posts/{id}")
31+
suspend fun editPost(
32+
@Path(value= "id") id: Long,
33+
@Body request: PostCreateRequest
34+
): BaseResponse<PostResponse>
35+
36+
@DELETE(value = "/api/posts/{id}")
37+
suspend fun deletePost(
38+
@Path(value = "id") id: Long,
39+
) : BaseResponse<Unit>
40+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.example.kuit6_android_api.data.api
2+
3+
import com.example.kuit6_android_api.BuildConfig
4+
import okhttp3.OkHttpClient
5+
import okhttp3.logging.HttpLoggingInterceptor
6+
import retrofit2.Retrofit
7+
import retrofit2.converter.gson.GsonConverterFactory
8+
import java.util.concurrent.TimeUnit
9+
10+
object RetrofitClient {
11+
12+
private val loggingInterceptor = HttpLoggingInterceptor().apply {
13+
level = HttpLoggingInterceptor.Level.BODY
14+
}
15+
16+
private val okHttpClient = OkHttpClient.Builder()
17+
.addInterceptor(loggingInterceptor)
18+
.connectTimeout(30, TimeUnit.SECONDS)
19+
.readTimeout(30, TimeUnit.SECONDS)
20+
.writeTimeout(30, TimeUnit.SECONDS)
21+
.build()
22+
23+
private val retrofit: Retrofit = Retrofit.Builder()
24+
.baseUrl(BuildConfig.BASE_URL)
25+
.client(okHttpClient)
26+
.addConverterFactory(GsonConverterFactory.create())
27+
.build()
28+
29+
val apiService: ApiService = retrofit.create(ApiService::class.java)
30+
31+
}

app/src/main/java/com/example/kuit6_android_api/data/model/Post.kt

Lines changed: 0 additions & 17 deletions
This file was deleted.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.example.kuit6_android_api.data.model.request
2+
3+
import kotlinx.serialization.SerialName
4+
5+
data class PostCreateRequest (
6+
@SerialName(value="title") val title: String,
7+
@SerialName(value="content") val content: String,
8+
@SerialName(value="imageUrl") val imageUrl: String?
9+
)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.example.kuit6_android_api.data.model.response
2+
3+
import android.R.attr.value
4+
import kotlinx.serialization.SerialName
5+
6+
data class AuthorResponse(
7+
@SerialName( value = "id") val id: Long,
8+
@SerialName( value = "username") val username: String,
9+
@SerialName( value = "profileImageUrl") val profileImageUrl: String?
10+
)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.example.kuit6_android_api.data.model.response
2+
3+
import android.R.attr.value
4+
import kotlinx.serialization.SerialName
5+
6+
data class BaseResponse<T>(
7+
@SerialName (value= "success") val success: Boolean,
8+
@SerialName (value= "message") val message: String?,
9+
@SerialName (value= "data") val data: T?,
10+
@SerialName (value= "timestamp") val timestamp: String
11+
)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.example.kuit6_android_api.data.model.response
2+
3+
import kotlinx.serialization.SerialName
4+
5+
data class PostResponse(
6+
@SerialName(value = "id") val id: Long,
7+
@SerialName(value = "title") val title: String,
8+
@SerialName(value = "content") val content: String,
9+
@SerialName(value = "imageUrl") val imageUrl: String?,
10+
@SerialName(value = "author") val author : AuthorResponse,
11+
@SerialName(value = "createdAt") val createdAt : String,
12+
@SerialName(value = "updatedAt") val updatedAt : String
13+
)

app/src/main/java/com/example/kuit6_android_api/ui/post/component/PostItem.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@ import androidx.compose.ui.text.style.TextOverflow
2929
import androidx.compose.ui.tooling.preview.Preview
3030
import androidx.compose.ui.unit.dp
3131
import coil.compose.AsyncImage
32-
import com.example.kuit6_android_api.data.model.Author
33-
import com.example.kuit6_android_api.data.model.Post
32+
import com.example.kuit6_android_api.data.model.response.AuthorResponse
33+
import com.example.kuit6_android_api.data.model.response.PostResponse
3434
import com.example.kuit6_android_api.util.formatDateTime
3535

3636
@Composable
3737
fun PostItem(
38-
post: Post,
38+
post: PostResponse,
3939
onClick: () -> Unit
4040
) {
4141
Card(
@@ -153,12 +153,12 @@ fun PostItem(
153153
fun PostItemPreview() {
154154
MaterialTheme {
155155
PostItem(
156-
post = Post(
156+
post = PostResponse(
157157
id = 1,
158158
title = "샘플 게시글 제목",
159159
content = "이것은 샘플 게시글 내용입니다. 미리보기에서는 두 줄까지만 표시됩니다.",
160160
imageUrl = null,
161-
author = Author(1, "testuser", null),
161+
author = AuthorResponse(1, "testuser", null),
162162
createdAt = "2025-10-03T12:00:00",
163163
updatedAt = "2025-10-03T12:00:00"
164164
),

0 commit comments

Comments
 (0)