Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Usecase 구조 #194

Merged
merged 4 commits into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ interface MusicApi {
@POST("musics")
suspend fun postMusic(
@Body music: MusicRequest
): Response<Unit>
)

@GET("musics/recent-uploads")
suspend fun getRecentUploads(): Response<List<MusicResponse>>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ interface UploadApi {

@GET("upload/uuid")
suspend fun getUuid(
): Response<UuidResponse>
): UuidResponse

@GET("upload")
suspend fun getPreSignedUrl(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import com.ohdodok.catchytape.core.data.repository.AuthRepositoryImpl
import com.ohdodok.catchytape.core.data.repository.MusicRepositoryImpl
import com.ohdodok.catchytape.core.data.repository.UrlRepositoryImpl
import com.ohdodok.catchytape.core.data.repository.UserTokenRepositoryImpl
import com.ohdodok.catchytape.core.data.repository.UuidRepositoryImpl
import com.ohdodok.catchytape.core.domain.repository.AuthRepository
import com.ohdodok.catchytape.core.domain.repository.MusicRepository
import com.ohdodok.catchytape.core.domain.repository.UrlRepository
import com.ohdodok.catchytape.core.domain.repository.UserTokenRepository
import com.ohdodok.catchytape.core.domain.repository.UuidRepository
import dagger.Binds
import dagger.Module
import dagger.hilt.InstallIn
Expand All @@ -34,4 +36,8 @@ interface RepositoryModule {
@Singleton
fun bindUrlRepository(urlRepositoryImpl: UrlRepositoryImpl): UrlRepository

@Binds
@Singleton
fun bindUuidRepository(uuidRepositoryImpl: UuidRepositoryImpl): UuidRepository

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package com.ohdodok.catchytape.core.data.model

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
data class MusicRequest (
@SerialName("music_id")
val musicId: String,
val title: String,
val cover: String,
val file: String,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package com.ohdodok.catchytape.core.data.model

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
data class MusicResponse (
val musicId: Int,
@SerialName("music_id")
val musicId: String,
val title: String,
val cover: String,
@SerialName("music_file")
val musicFile : String,
val genre: String,
val user: NicknameResponse
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,29 +30,27 @@ class MusicRepositoryImpl @Inject constructor(
else -> throw RuntimeException("네트워크 에러")
}
}

override fun postMusic(
musicId: String,
title: String,
imageUrl: String,
audioUrl: String,
genre: String
): Flow<Unit> = flow {
val response = musicApi.postMusic(
MusicRequest(
musicId = musicId,
title = title,
cover = imageUrl,
file = audioUrl,
genre = genre
)
)
when (response.code()) {
// TODO : 네트워크 에러 로직 처리
in 200..299 -> emit(response.body() ?: Unit)
else -> throw RuntimeException("네트워크 에러")
}
emit(response)
}
}

fun MusicResponse.toDomain(): Music {
return Music(
id = musicId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,6 @@ class UrlRepositoryImpl @Inject constructor(
private val uploadApi: UploadApi
) : UrlRepository {

override fun getUuid(): Flow<String> = flow {
val response = uploadApi.getUuid()
if (response.isSuccessful) {
response.body()?.let { uuidResponse -> emit(uuidResponse.uuid) }
} else {
// TODO : 네트워크 에러 로직
throw Exception("uuid 생성 실패")
}
}

override fun getImageUrl(file: File): Flow<String> = flow {
val response = uploadApi.postImage(file.toMultipart("image/png"))
if (response.isSuccessful) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.ohdodok.catchytape.core.data.repository

import com.ohdodok.catchytape.core.data.api.UploadApi
import com.ohdodok.catchytape.core.domain.repository.UuidRepository
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import javax.inject.Inject

class UuidRepositoryImpl @Inject constructor(
private val uploadApi: UploadApi
) : UuidRepository {

override fun getUuid(): Flow<String> = flow {
emit(uploadApi.getUuid().uuid)
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.ohdodok.catchytape.core.domain.model

data class Music(
val id: Int,
val id: String,
val title: String,
val artist: String,
val imageUrl: String
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ interface MusicRepository {

fun getRecentUploadedMusic(): Flow<List<Music>>

fun postMusic(title: String, imageUrl: String, audioUrl: String, genre: String): Flow<Unit>
fun postMusic(musicId: String, title: String, imageUrl: String, audioUrl: String, genre: String): Flow<Unit>

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import java.io.File

interface UrlRepository {

fun getUuid(): Flow<String>

fun getImageUrl(file: File): Flow<String>

fun getAudioUrl(file: File): Flow<String>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.ohdodok.catchytape.core.domain.repository

import kotlinx.coroutines.flow.Flow

interface UuidRepository {

fun getUuid(): Flow<String>
}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.ohdodok.catchytape.core.domain.usecase
package com.ohdodok.catchytape.core.domain.usecase.login

import com.ohdodok.catchytape.core.domain.repository.AuthRepository
import com.ohdodok.catchytape.core.domain.repository.UserTokenRepository
Expand All @@ -8,6 +8,7 @@ class AutomaticallyLoginUseCase @Inject constructor(
private val authRepository: AuthRepository,
private val userTokenRepository: UserTokenRepository
) {

suspend operator fun invoke(): Boolean {
val accessToken = userTokenRepository.getAccessToken()
if (accessToken.isBlank()) return false
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.ohdodok.catchytape.core.domain.usecase
package com.ohdodok.catchytape.core.domain.usecase.login

import com.ohdodok.catchytape.core.domain.repository.AuthRepository
import com.ohdodok.catchytape.core.domain.repository.UserTokenRepository
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.ohdodok.catchytape.core.domain.usecase
package com.ohdodok.catchytape.core.domain.usecase.signup

import com.ohdodok.catchytape.core.domain.repository.AuthRepository
import com.ohdodok.catchytape.core.domain.repository.UserTokenRepository
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.ohdodok.catchytape.core.domain.usecase.upload

import com.ohdodok.catchytape.core.domain.repository.MusicRepository
import com.ohdodok.catchytape.core.domain.repository.UuidRepository
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.map
import javax.inject.Inject

class UploadMusicUseCase @Inject constructor(
private val musicRepository: MusicRepository,
private val uuidRepository: UuidRepository
) {

operator fun invoke(
imageUrl: String, audioUrl: String, title: String, genre: String
Copy link
Member

@HamBP HamBP Nov 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

invoke(music: Music)
나라면 파라미터는 하나만 받을 것 같아!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이건 지금 존재하는 music model에 포함되지 않은 데이터 종류가 3개 있어서(id, audioUrl, genre)이렇게 받았어..!

): Flow<Unit> = uuidRepository.getUuid().map { uuid ->
musicRepository.postMusic(
musicId = uuid, title = title, genre = genre, imageUrl = imageUrl, audioUrl = audioUrl
).first()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.ohdodok.catchytape.feature.home
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.ohdodok.catchytape.core.domain.model.Music
import com.ohdodok.catchytape.core.domain.usecase.GetRecentUploadedMusic
import com.ohdodok.catchytape.core.domain.repository.MusicRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
Expand All @@ -19,14 +19,14 @@ data class HomeUiState(

@HiltViewModel
class HomeViewModel @Inject constructor(
private val getRecentUploadedMusicUseCase: GetRecentUploadedMusic
private val musicRepository: MusicRepository
) : ViewModel() {

private val _uiState = MutableStateFlow(HomeUiState())
val uiState: StateFlow<HomeUiState> = _uiState.asStateFlow()

fun fetchUploadedMusics() {
getRecentUploadedMusicUseCase()
musicRepository.getRecentUploadedMusic()
.onEach { musics ->
_uiState.update {
it.copy(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.ohdodok.catchytape.core.domain.model.CtErrorType
import com.ohdodok.catchytape.core.domain.model.CtException
import com.ohdodok.catchytape.core.domain.usecase.AutomaticallyLoginUseCase
import com.ohdodok.catchytape.core.domain.usecase.LoginUseCase
import com.ohdodok.catchytape.core.domain.usecase.login.AutomaticallyLoginUseCase
import com.ohdodok.catchytape.core.domain.usecase.login.LoginUseCase
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.CoroutineExceptionHandler
import kotlinx.coroutines.flow.MutableSharedFlow
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.ohdodok.catchytape.core.domain.usecase.signup.NicknameValidationResult
import com.ohdodok.catchytape.core.domain.usecase.signup.ValidateNicknameUseCase
import com.ohdodok.catchytape.core.domain.usecase.SignUpUseCase
import com.ohdodok.catchytape.core.domain.usecase.signup.SignUpUseCase
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import dagger.hilt.android.lifecycle.HiltViewModel
import java.io.File
import javax.inject.Inject
import androidx.lifecycle.viewModelScope
import com.ohdodok.catchytape.core.domain.usecase.UploadFileUseCase
import com.ohdodok.catchytape.core.domain.usecase.GetMusicGenresUseCase
import com.ohdodok.catchytape.core.domain.usecase.UploadMusicUseCase
import com.ohdodok.catchytape.core.domain.repository.MusicRepository
import com.ohdodok.catchytape.core.domain.repository.UrlRepository
import com.ohdodok.catchytape.core.domain.usecase.upload.UploadMusicUseCase
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
Expand All @@ -24,8 +24,8 @@ import kotlinx.coroutines.flow.stateIn

@HiltViewModel
class UploadViewModel @Inject constructor(
private val getMusicGenresUseCase: GetMusicGenresUseCase,
private val uploadFileUseCase: UploadFileUseCase,
private val musicRepository: MusicRepository,
private val urlRepository: UrlRepository,
private val uploadMusicUseCase: UploadMusicUseCase
) : ViewModel() {

Expand Down Expand Up @@ -69,13 +69,13 @@ class UploadViewModel @Inject constructor(
}

private fun fetchGenres() {
getMusicGenresUseCase().onEach {
musicRepository.getGenres().onEach {
_musicGenres.value = it
}.launchIn(viewModelScope)
}

fun uploadImage(imageFile: File) {
uploadFileUseCase.getImgUrl(imageFile).onStart {
urlRepository.getImageUrl(imageFile).onStart {
_imageState.value = imageState.value.copy(isLoading = true)
}.onEach { url ->
_imageState.value = imageState.value.copy(url = url)
Expand All @@ -85,7 +85,7 @@ class UploadViewModel @Inject constructor(
}

fun uploadAudio(audioFile: File) {
uploadFileUseCase.getAudioUrl(audioFile).onStart {
urlRepository.getAudioUrl(audioFile).onStart {
_audioState.value = audioState.value.copy(isLoading = true)
}.onEach { url ->
_audioState.value = audioState.value.copy(url = url)
Expand Down