Skip to content

Commit c62340d

Browse files
committed
Merge upstream/develop into JeongIlhyuk/week6
2 parents a047a58 + 9ecbd44 commit c62340d

File tree

15 files changed

+340
-51
lines changed

15 files changed

+340
-51
lines changed

.idea/deploymentTargetSelector.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"java.configuration.updateBuildConfiguration": "automatic"
3+
}

app/build.gradle.kts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,12 @@ android {
2020

2121
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
2222

23-
// defaultConfig 블록 안에 추가
2423
val properties = Properties()
2524
val localPropertiesFile = rootProject.file("local.properties")
2625
if (localPropertiesFile.exists()) {
2726
properties.load(localPropertiesFile.inputStream())
2827
}
29-
val baseUrl = properties.getProperty("BASE_URL") ?: "http://10.0.2.2:8080/"
28+
val baseUrl = properties.getProperty("BASE_URL")
3029
buildConfigField("String", "BASE_URL", "\"$baseUrl\"")
3130
}
3231

@@ -86,8 +85,9 @@ dependencies {
8685
debugImplementation(libs.androidx.compose.ui.tooling)
8786
debugImplementation(libs.androidx.compose.ui.test.manifest)
8887

88+
// Retrofit & OkHttp
8989
implementation(libs.retrofit)
90-
implementation(libs.retrofit.converter.gson)
90+
implementation(libs.retrofit.converter.kotlinx.serialization)
9191
implementation(libs.okhttp)
9292
implementation(libs.okhttp.logging.interceptor)
9393
}

app/src/main/java/com/example/kuit6_android_api/data/api/ApiService.kt

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,52 @@ package com.example.kuit6_android_api.data.api
33
import com.example.kuit6_android_api.data.model.request.PostCreateRequest
44
import com.example.kuit6_android_api.data.model.response.BaseResponse
55
import com.example.kuit6_android_api.data.model.response.PostResponse
6+
import okhttp3.MultipartBody
67
import retrofit2.http.Body
8+
import retrofit2.http.DELETE
79
import retrofit2.http.GET
10+
import retrofit2.http.Multipart
811
import retrofit2.http.POST
12+
import retrofit2.http.PUT
13+
import retrofit2.http.Part
14+
import retrofit2.http.Path
915
import retrofit2.http.Query
1016

11-
1217
interface ApiService {
13-
@GET(value = "/api/posts")
18+
// 게시글 목록 조회
19+
@GET("/api/posts")
1420
suspend fun getPosts(): BaseResponse<List<PostResponse>>
1521

16-
@POST(value="/api/posts")
22+
// 게시글 생성
23+
@POST("/api/posts")
1724
suspend fun createPost(
18-
@Query(value = "author") author: String = "일혁",
25+
@Query("author") author: String = "규빈",
26+
@Body request: PostCreateRequest
27+
): BaseResponse<PostResponse>
28+
29+
// 게시글 상세 조회
30+
@GET("/api/posts/{id}")
31+
suspend fun getPostDetail(
32+
@Path("id") id: Long
33+
): BaseResponse<PostResponse>
34+
35+
// 게시글 수정
36+
@PUT("/api/posts/{id}")
37+
suspend fun updatePost(
38+
@Path("id") id: Long,
1939
@Body request: PostCreateRequest
2040
): BaseResponse<PostResponse>
41+
42+
// 게시글 삭제
43+
@DELETE("/api/posts/{id}")
44+
suspend fun deletePost(
45+
@Path("id") id: Long
46+
): BaseResponse<Unit>
47+
48+
// 이미지 업로드
49+
@Multipart
50+
@POST("/api/images/upload")
51+
suspend fun uploadImage(
52+
@Part file: MultipartBody.Part
53+
): BaseResponse<Map<String, String>>
2154
}

app/src/main/java/com/example/kuit6_android_api/data/api/RetrofitClient.kt

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package com.example.kuit6_android_api.data.api
22

3-
43
import com.example.kuit6_android_api.BuildConfig
4+
import kotlinx.serialization.json.Json
5+
import okhttp3.MediaType.Companion.toMediaType
56
import okhttp3.OkHttpClient
67
import okhttp3.logging.HttpLoggingInterceptor
78
import retrofit2.Retrofit
8-
import retrofit2.converter.gson.GsonConverterFactory
9+
import retrofit2.converter.kotlinx.serialization.asConverterFactory
910
import java.util.concurrent.TimeUnit
1011

1112
object RetrofitClient {
@@ -20,10 +21,15 @@ object RetrofitClient {
2021
.writeTimeout(30, TimeUnit.SECONDS)
2122
.build()
2223

24+
private val json = Json {
25+
ignoreUnknownKeys = true // 서버에서 추가 필드가 와도 무시
26+
coerceInputValues = true // null이 와야 할 곳에 다른 값이 와도 처리
27+
}
28+
2329
private val retrofit: Retrofit = Retrofit.Builder()
2430
.baseUrl(BuildConfig.BASE_URL)
2531
.client(okHttpClient)
26-
.addConverterFactory(GsonConverterFactory.create())
32+
.addConverterFactory(json.asConverterFactory("application/json".toMediaType()))
2733
.build()
2834

2935
val apiService: ApiService = retrofit.create(ApiService::class.java)
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package com.example.kuit6_android_api.data.model.request
22

33
import kotlinx.serialization.SerialName
4+
import kotlinx.serialization.Serializable
45

6+
@Serializable
57
data class PostCreateRequest(
6-
@SerialName(value = "title") val title: String,
7-
@SerialName(value = "content") val content: String,
8-
@SerialName(value = "imageUrl") val imageUrl: String?
8+
@SerialName("title") val title: String,
9+
@SerialName("content") val content: String,
10+
@SerialName("imageUrl") val imageUrl: String?
911
)
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package com.example.kuit6_android_api.data.model.response
22

33
import kotlinx.serialization.SerialName
4+
import kotlinx.serialization.Serializable
45

6+
@Serializable
57
data class AuthorResponse(
6-
@SerialName(value="id") val id: Long,
7-
@SerialName(value="username") val username: String,
8-
@SerialName(value="profileImageUrl") val profileImageUrl: String?
8+
@SerialName("id") val id: Long,
9+
@SerialName("username") val username: String,
10+
@SerialName("profileImageUrl") val profileImageUrl: String?
911
)
Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package com.example.kuit6_android_api.data.model.response
22

33
import kotlinx.serialization.SerialName
4+
import kotlinx.serialization.Serializable
45

6+
@Serializable
57
data class BaseResponse<T>(
6-
@SerialName(value = "success") val success: Boolean,
7-
@SerialName(value = "message") val message: String?,
8-
@SerialName(value = "data") val data: T?,
9-
@SerialName(value = "timestamp") val timestamp: String
8+
@SerialName("success") val success: Boolean,
9+
@SerialName("message") val message: String?,
10+
@SerialName("data") val data: T?,
11+
@SerialName("timestamp") val timestamp: String? = null
1012
)
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.response
2+
3+
import kotlinx.serialization.SerialName
4+
import kotlinx.serialization.Serializable
5+
6+
@Serializable
7+
data class ImageUploadResponse(
8+
@SerialName("imageUrl") val imageUrl: String
9+
)
Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package com.example.kuit6_android_api.data.model.response
22

3-
import com.google.gson.annotations.SerializedName
3+
import kotlinx.serialization.SerialName
4+
import kotlinx.serialization.Serializable
45

6+
@Serializable
57
data class PostResponse(
6-
@SerializedName(value="id") val id:Long,
7-
@SerializedName(value="title") val title:String,
8-
@SerializedName(value="content") val content:String,
9-
@SerializedName(value="imageUrl") val imageUrl:String?,
10-
@SerializedName(value="author") val author: AuthorResponse,
11-
@SerializedName(value="createdAt") val createdAt:String,
12-
@SerializedName(value="updatedAt") val updatedAt:String
13-
)
8+
@SerialName("id") val id: Long,
9+
@SerialName("title") val title: String,
10+
@SerialName("content") val content: String,
11+
@SerialName("imageUrl") val imageUrl: String?,
12+
@SerialName("author") val author: AuthorResponse,
13+
@SerialName("createdAt") val createdAt: String,
14+
@SerialName("updatedAt") val updatedAt: String
15+
)

0 commit comments

Comments
 (0)