Skip to content

Commit

Permalink
CheoCharm#25 feat: 일기 작성 요청
Browse files Browse the repository at this point in the history
  • Loading branch information
Hyesung82 committed Nov 14, 2023
1 parent a251f31 commit 95b0f32
Show file tree
Hide file tree
Showing 13 changed files with 45 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.cheocharm.data.error.ErrorData
import com.cheocharm.data.error.toDomain
import com.cheocharm.data.source.WriteRemoteDataSource
import com.cheocharm.domain.model.WriteDiaryRequest
import com.cheocharm.domain.model.WriteDiaryResponse
import com.cheocharm.domain.model.WriteImageRequest
import com.cheocharm.domain.model.WriteImageResponse
import com.cheocharm.domain.repository.WriteRepository
Expand All @@ -22,7 +23,7 @@ class WriteRepositoryImpl @Inject constructor(
}
}

override suspend fun requestWriteDiary(request: WriteDiaryRequest): Result<Int> {
override suspend fun requestWriteDiary(request: WriteDiaryRequest): Result<WriteDiaryResponse> {
val result = writeRemoteDataSource.requestWriteDiary(request)

return when (val exception = result.exceptionOrNull()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package com.cheocharm.data.source

import com.cheocharm.domain.model.WriteDiaryRequest
import com.cheocharm.domain.model.WriteDiaryResponse
import com.cheocharm.domain.model.WriteImageRequest
import com.cheocharm.domain.model.WriteImageResponse

interface WriteRemoteDataSource {
suspend fun requestWriteImages(request: WriteImageRequest): Result<WriteImageResponse>

suspend fun requestWriteDiary(request: WriteDiaryRequest): Result<Int>
suspend fun requestWriteDiary(request: WriteDiaryRequest): Result<WriteDiaryResponse>
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.cheocharm.domain.model

data class WriteDiaryRequest(
val id: Int,
val id: Long,
val title: String,
val content: String
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.cheocharm.domain.model

data class WriteDiaryResponse(val diaryId: Long)
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
package com.cheocharm.domain.model

data class WriteImageResponse(val statusCode: Int, val customCode: String, val data: Data) {
inner class Data(val diary: Long, val imageURLs: List<String>, message: String)
}
data class WriteImageResponse(val diaryId: Long, val imageURLs: List<String>)
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package com.cheocharm.domain.repository

import com.cheocharm.domain.model.WriteDiaryRequest
import com.cheocharm.domain.model.WriteDiaryResponse
import com.cheocharm.domain.model.WriteImageRequest
import com.cheocharm.domain.model.WriteImageResponse

interface WriteRepository {
suspend fun requestWriteImages(request: WriteImageRequest): Result<WriteImageResponse>

suspend fun requestWriteDiary(request: WriteDiaryRequest): Result<Int>
suspend fun requestWriteDiary(request: WriteDiaryRequest): Result<WriteDiaryResponse>
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package com.cheocharm.domain.usecase.write

import com.cheocharm.domain.model.WriteDiaryRequest
import com.cheocharm.domain.model.WriteDiaryResponse
import com.cheocharm.domain.repository.WriteRepository
import javax.inject.Inject

class RequestWriteDiaryUseCase @Inject constructor(
private val repository: WriteRepository
) {
suspend operator fun invoke(request: WriteDiaryRequest): Result<Int> =
suspend operator fun invoke(request: WriteDiaryRequest): Result<WriteDiaryResponse> =
repository.requestWriteDiary(request)
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package com.cheocharm.presentation.model

data class Result(val isSuccessful: Boolean, val message: String? = null)
data class Result<T>(val isSuccessful: Boolean, val message: String? = null, val data: T? = null)
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import javax.inject.Inject
class LocationViewModel @Inject constructor(
private val requestWriteImagesUseCase: RequestWriteImagesUseCase
) : ViewModel() {
private val _result = MutableLiveData<Result>()
val result: LiveData<Result> = _result
private val _result = MutableLiveData<Result<Long>>()
val result: LiveData<Result<Long>> = _result

fun uploadImages(
groupId: Long,
Expand All @@ -36,7 +36,7 @@ class LocationViewModel @Inject constructor(
images
)
).onSuccess {
_result.value = Result(true)
_result.value = Result(true, data = it.diaryId)
}.onFailure {
_result.value = Result(false, it.message)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,16 @@ import com.cheocharm.presentation.R
import com.cheocharm.presentation.base.BaseFragment
import com.cheocharm.presentation.databinding.FragmentWriteBinding
import com.cheocharm.presentation.ui.MainActivity
import dagger.hilt.android.AndroidEntryPoint
import jp.wasabeef.richeditor.RichEditor

@AndroidEntryPoint
class WriteFragment : BaseFragment<FragmentWriteBinding>(R.layout.fragment_write), MenuProvider {
private val locationViewModel: LocationViewModel by navGraphViewModels(R.id.write)
private val writeViewModel: WriteViewModel by navGraphViewModels(R.id.write) { defaultViewModelProviderFactory }

private lateinit var editor: RichEditor
private var diaryId: Long? = null

override fun onCreateView(
inflater: LayoutInflater,
Expand All @@ -43,6 +47,7 @@ class WriteFragment : BaseFragment<FragmentWriteBinding>(R.layout.fragment_write
locationViewModel.result.observe(viewLifecycleOwner) {
if (it.isSuccessful) {
Log.d(javaClass.name, "이미지 업로드 성공")
diaryId = it.data
} else {
Log.e(javaClass.name, "이미지 업로드 실패: ${it.message}")
}
Expand Down Expand Up @@ -102,6 +107,14 @@ class WriteFragment : BaseFragment<FragmentWriteBinding>(R.layout.fragment_write
binding.btnWritePicture.setOnClickListener {
// TODO: 사진 목록
}

writeViewModel.result.observe(viewLifecycleOwner) {
if (it.isSuccessful) {
Toast.makeText(context, "일기 작성 성공", Toast.LENGTH_SHORT).show()
} else {
Toast.makeText(context, "일기 작성 실패", Toast.LENGTH_SHORT).show()
}
}
}

override fun onCreateMenu(menu: Menu, menuInflater: MenuInflater) {
Expand All @@ -111,8 +124,16 @@ class WriteFragment : BaseFragment<FragmentWriteBinding>(R.layout.fragment_write
override fun onMenuItemSelected(menuItem: MenuItem): Boolean {
return when (menuItem.itemId) {
R.id.menu_base_confirm -> {
// TODO: POST 요청 및 일기 상세 화면으로 이동
println(editor.html)
// TODO: 일기 작성 완료 화면으로 이동

diaryId?.let {
writeViewModel.writeDiary(
it,
binding.etWriteTitle.text.toString(),
editor.html ?: ""
)
}

true
}
else -> false
Expand Down
1 change: 1 addition & 0 deletions remote/src/main/java/com/cheocharm/remote/api/WriteApi.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.cheocharm.remote.api

import com.cheocharm.domain.model.WriteDiaryResponse
import com.cheocharm.remote.model.BaseResponse
import com.cheocharm.remote.model.response.write.MyGroup
import retrofit2.http.GET
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package com.cheocharm.remote.model.request

import com.google.gson.annotations.SerializedName

data class WriteDiaryDto(
@SerializedName("diary_id") val id: Int,
val diaryId: Long,
val title: String,
@SerializedName("html_content") val html: String
val content: String
)
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.cheocharm.remote.source
import com.cheocharm.data.error.ErrorData
import com.cheocharm.data.source.WriteRemoteDataSource
import com.cheocharm.domain.model.WriteDiaryRequest
import com.cheocharm.domain.model.WriteDiaryResponse
import com.cheocharm.domain.model.WriteImageRequest
import com.cheocharm.remote.api.WriteApi
import com.cheocharm.remote.mapper.toDto
Expand Down Expand Up @@ -40,7 +41,7 @@ class WriteRemoteDataSourceImpl @Inject constructor(
}
}

override suspend fun requestWriteDiary(request: WriteDiaryRequest): Result<Int> {
override suspend fun requestWriteDiary(request: WriteDiaryRequest): Result<WriteDiaryResponse> {
val result = runCatching { api.writeDiary(request.toDto()) }

return when (val exception = result.exceptionOrNull()) {
Expand Down

0 comments on commit 95b0f32

Please sign in to comment.