diff --git a/petJournal/app/src/main/java/com/soujunior/petjournal/di/mainModule.kt b/petJournal/app/src/main/java/com/soujunior/petjournal/di/mainModule.kt index 7d71152a..ed057b28 100644 --- a/petJournal/app/src/main/java/com/soujunior/petjournal/di/mainModule.kt +++ b/petJournal/app/src/main/java/com/soujunior/petjournal/di/mainModule.kt @@ -29,6 +29,7 @@ import com.soujunior.domain.use_case.auth.SignUpUseCase import com.soujunior.domain.use_case.guardian.GetGuardianNameUseCase import com.soujunior.domain.use_case.guardian.GetPetRegistrationWentLive import com.soujunior.domain.use_case.guardian.SetPetRegistrationWentLive +import com.soujunior.domain.use_case.pet.CreatePetInformationApiUseCase import com.soujunior.domain.use_case.pet.GetListPetRacesUseCase import com.soujunior.domain.use_case.pet.GetListPetSizesUseCase import com.soujunior.domain.use_case.pet.GetPetInformationUseCase @@ -105,6 +106,7 @@ val mainModule = module { factory { SavedStateHandle() } factory { GetListPetSizesUseCase(get()) } factory { GetListPetRacesUseCase(get()) } + factory { CreatePetInformationApiUseCase(get()) } single { get().create(AuthService::class.java) } single { get().create(GuardianService::class.java) } @@ -154,6 +156,6 @@ val mainModule = module { ) } - viewModel { BirthDateViewModelImpl(get(), get(), get()) } + viewModel { BirthDateViewModelImpl(get(), get(), get(), get()) } viewModel { ViewModelRaceSizeImpl(get(), get(), get(), get(), get()) } } diff --git a/petJournal/app/src/main/java/com/soujunior/petjournal/ui/screens_app/screens_pets/petBirthDateScreen/BirthDateViewModel.kt b/petJournal/app/src/main/java/com/soujunior/petjournal/ui/screens_app/screens_pets/petBirthDateScreen/BirthDateViewModel.kt index a85a923a..d9761b57 100644 --- a/petJournal/app/src/main/java/com/soujunior/petjournal/ui/screens_app/screens_pets/petBirthDateScreen/BirthDateViewModel.kt +++ b/petJournal/app/src/main/java/com/soujunior/petjournal/ui/screens_app/screens_pets/petBirthDateScreen/BirthDateViewModel.kt @@ -28,6 +28,7 @@ abstract class BirthDateViewModel : ViewModel(){ ) abstract fun getPetInformation(id: Long) abstract fun updatePetInformation() + abstract fun createPetInformation() abstract fun successPetUpdate(unit: Unit) } \ No newline at end of file diff --git a/petJournal/app/src/main/java/com/soujunior/petjournal/ui/screens_app/screens_pets/petBirthDateScreen/BirthDateViewModelImpl.kt b/petJournal/app/src/main/java/com/soujunior/petjournal/ui/screens_app/screens_pets/petBirthDateScreen/BirthDateViewModelImpl.kt index 84aefb96..5db8b2ed 100644 --- a/petJournal/app/src/main/java/com/soujunior/petjournal/ui/screens_app/screens_pets/petBirthDateScreen/BirthDateViewModelImpl.kt +++ b/petJournal/app/src/main/java/com/soujunior/petjournal/ui/screens_app/screens_pets/petBirthDateScreen/BirthDateViewModelImpl.kt @@ -6,19 +6,26 @@ import androidx.compose.runtime.setValue import androidx.lifecycle.viewModelScope import com.soujunior.domain.model.PetInformationModel import com.soujunior.domain.repository.ValidationRepository +import com.soujunior.domain.use_case.pet.CreatePetInformationApiUseCase import com.soujunior.domain.use_case.pet.GetPetInformationUseCase import com.soujunior.domain.use_case.pet.UpdatePetInformationUseCase import com.soujunior.petjournal.ui.states.TaskState +import com.soujunior.petjournal.ui.util.Constants.ERROR_MESSAGE +import com.soujunior.petjournal.ui.util.Constants.SUCCESS_MESSAGE import com.soujunior.petjournal.ui.util.ValidationEvent import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.launch +import java.time.LocalDate +import java.time.ZoneOffset +import java.time.format.DateTimeFormatter class BirthDateViewModelImpl( val validation: ValidationRepository, private val getPetInformationUseCase: GetPetInformationUseCase, - private val updatePetInformationUseCase: UpdatePetInformationUseCase + private val updatePetInformationUseCase: UpdatePetInformationUseCase, + private val createPetInformationApiUseCase: CreatePetInformationApiUseCase ) : BirthDateViewModel() { override var state by mutableStateOf(BirthDateFormState()) @@ -42,14 +49,14 @@ class BirthDateViewModelImpl( viewModelScope.launch { validationEventChannel.send(ValidationEvent.Success) } - _message.value = "Sucesso" + _message.value = SUCCESS_MESSAGE } override fun failed(exception: Throwable?) { viewModelScope.launch { validationEventChannel.send(ValidationEvent.Failed) } - _message.value = "Error" + _message.value = ERROR_MESSAGE } override fun onEvent(event: BirthDateFormEvent) { @@ -60,6 +67,7 @@ class BirthDateViewModelImpl( change(petCastration = state.castration) change(petBirth = state.birth) } + is BirthDateFormEvent.PetCastration -> change(petCastration = event.petCastration) } } @@ -76,12 +84,14 @@ class BirthDateViewModelImpl( state = if (result.success) state.copy(birthError = null) else state.copy(birthError = result.errorMessage) } - petCastration == null || petCastration == true || petCastration == false-> { + + petCastration == null || petCastration == true || petCastration == false -> { state = state.copy(castration = petCastration) val result = validation.validatePetCastration(state.castration) state = if (result.success) state.copy(castrationError = null) else state.copy(castrationError = result.errorMessage) } + idPetInformation != null -> { state = state.copy(idPetInformation = idPetInformation) } @@ -99,7 +109,6 @@ class BirthDateViewModelImpl( } override fun updatePetInformation() { - _taskState.value = TaskState.Loading viewModelScope.launch { val petInformation = PetInformationModel( id = state.idPetInformation ?: 0L, @@ -108,13 +117,31 @@ class BirthDateViewModelImpl( gender = state.gender, size = state.size, petRace = state.race, - petAge = state.birth, - guardianId = 1, - castration = state.castration + petAge = formatToIso8601(state.birth), + guardianId = 1 ) val result = updatePetInformationUseCase.execute(petInformation) result.handleResult(::successPetUpdate, ::failed) + } + } + + override fun createPetInformation() { + _taskState.value = TaskState.Loading + viewModelScope.launch { + val petInformation = PetInformationModel( + id = state.idPetInformation ?: 0L, + species = state.specie, + name = state.name, + gender = state.gender, + size = state.size, + petRace = state.race, + petAge = formatToIso8601(state.birth), + guardianId = 1, + castrated = state.castration + ) + val result = createPetInformationApiUseCase.execute(petInformation) + result.handleResult(::successPetUpdate, ::failed) _taskState.value = TaskState.Idle } } @@ -123,7 +150,18 @@ class BirthDateViewModelImpl( viewModelScope.launch { validationEventChannel.send(ValidationEvent.Success) } - _message.value = "Sucesso" + _message.value = SUCCESS_MESSAGE + } + + + private fun formatToIso8601(date: String): String { + if (date.isNotEmpty()){ + val dateFormatter = DateTimeFormatter.ofPattern("ddMMyyyy") + val dateF = LocalDate.parse(date, dateFormatter) + val localDateTime = dateF.atStartOfDay() + return localDateTime.atOffset(ZoneOffset.UTC).format(DateTimeFormatter.ISO_DATE_TIME) + } + return "" } } \ No newline at end of file diff --git a/petJournal/app/src/main/java/com/soujunior/petjournal/ui/screens_app/screens_pets/petBirthDateScreen/components/Screen.kt b/petJournal/app/src/main/java/com/soujunior/petjournal/ui/screens_app/screens_pets/petBirthDateScreen/components/Screen.kt index 8b32c9fc..47491a53 100644 --- a/petJournal/app/src/main/java/com/soujunior/petjournal/ui/screens_app/screens_pets/petBirthDateScreen/components/Screen.kt +++ b/petJournal/app/src/main/java/com/soujunior/petjournal/ui/screens_app/screens_pets/petBirthDateScreen/components/Screen.kt @@ -1,8 +1,6 @@ package com.soujunior.petjournal.ui.screens_app.screens_pets.petBirthDateScreen.components import android.annotation.SuppressLint -import android.content.ContentValues.TAG -import android.util.Log import androidx.compose.foundation.BorderStroke import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Box @@ -19,6 +17,7 @@ import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.material3.ButtonDefaults import androidx.compose.material3.MaterialTheme import androidx.compose.runtime.Composable +import androidx.compose.runtime.collectAsState import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember @@ -32,18 +31,21 @@ import com.soujunior.petjournal.R import com.soujunior.petjournal.ui.components.Breadcrumb import com.soujunior.petjournal.ui.components.Button3 import com.soujunior.petjournal.ui.components.DateInputText +import com.soujunior.petjournal.ui.components.IndeterminateCircularIndicator import com.soujunior.petjournal.ui.components.NavigationBar import com.soujunior.petjournal.ui.components.ScaffoldCustom import com.soujunior.petjournal.ui.components.mask.formatDate import com.soujunior.petjournal.ui.screens_app.screens_pets.petBirthDateScreen.BirthDateFormEvent import com.soujunior.petjournal.ui.screens_app.screens_pets.petBirthDateScreen.BirthDateViewModel import com.soujunior.petjournal.ui.screens_app.screens_pets.petRaceAndSizeScreen.RaceSizeFormEvent +import com.soujunior.petjournal.ui.states.TaskState import org.koin.androidx.compose.getViewModel @SuppressLint("StateFlowValueCalledInComposition") @Composable fun Screen(idPetInformation: String?, navController: NavController) { val viewModel: BirthDateViewModel = getViewModel() + val taskState by viewModel.taskState.collectAsState() var isClearCastration by remember { mutableStateOf(false) } if (idPetInformation != null) { viewModel.getPetInformation(idPetInformation.toLong()) @@ -57,7 +59,9 @@ fun Screen(idPetInformation: String?, navController: NavController) { showBottomBarNavigation = true, bottomNavigationBar = { NavigationBar(navController) }, contentToUse = { it -> - Box(modifier = Modifier.padding(it)) { + if (taskState is TaskState.Loading) + IndeterminateCircularIndicator(modifier = Modifier.align(Alignment.CenterHorizontally)) + else Box(modifier = Modifier.padding(it)) { LazyColumn( horizontalAlignment = Alignment.CenterHorizontally, verticalArrangement = Arrangement.Top, @@ -153,8 +157,8 @@ fun Screen(idPetInformation: String?, navController: NavController) { viewModel.state.birth.isNotEmpty() && viewModel.state.castration != null ) { - Log.i(TAG, viewModel.state.birth + viewModel.state.castration.toString()) viewModel.updatePetInformation() + viewModel.createPetInformation() //navController.navigate("pets/birth/$it") } }, diff --git a/petJournal/app/src/main/java/com/soujunior/petjournal/ui/screens_app/screens_pets/petRaceAndSizeScreen/ViewModelRaceSizeImpl.kt b/petJournal/app/src/main/java/com/soujunior/petjournal/ui/screens_app/screens_pets/petRaceAndSizeScreen/ViewModelRaceSizeImpl.kt index 12900ff2..f1fcd186 100644 --- a/petJournal/app/src/main/java/com/soujunior/petjournal/ui/screens_app/screens_pets/petRaceAndSizeScreen/ViewModelRaceSizeImpl.kt +++ b/petJournal/app/src/main/java/com/soujunior/petjournal/ui/screens_app/screens_pets/petRaceAndSizeScreen/ViewModelRaceSizeImpl.kt @@ -44,7 +44,9 @@ class ViewModelRaceSizeImpl( private val _taskState: MutableStateFlow = MutableStateFlow(TaskState.Idle) override val taskState: StateFlow get() = _taskState override val shouldScrollToTop = MutableStateFlow(false) - + init { + _taskState.value = TaskState.Loading + } override fun success(petInformationModel: PetInformationModel) { state = state.copy( specie = petInformationModel.species ?: "", idPetInformation = petInformationModel.id, @@ -168,11 +170,9 @@ class ViewModelRaceSizeImpl( } override fun getPetInformation(id: Long) { - _taskState.value = TaskState.Loading viewModelScope.launch { val result = getPetInformationUseCase.execute(id) result.handleResult(::success, ::failed) - _taskState.value = TaskState.Idle } } @@ -207,7 +207,6 @@ class ViewModelRaceSizeImpl( } override suspend fun requestGetListSizes() { - _taskState.value = TaskState.Loading if (state.specie == CAT || state.specie == DOG) { val result = getListPetSizesUseCase.execute(state.specie) result.handleResult(::successGetPetSizes, ::failed) @@ -218,7 +217,7 @@ class ViewModelRaceSizeImpl( } override suspend fun requestGetListRaces() { - _taskState.value = TaskState.Loading + if (state.specie == CAT || state.specie == DOG) { val result = getListPetRacesUseCase.execute(state.specie) result.handleResult(::successGetPetRaces, ::failed) diff --git a/petJournal/app/src/main/java/com/soujunior/petjournal/ui/screens_app/screens_pets/petRaceAndSizeScreen/components/Screen.kt b/petJournal/app/src/main/java/com/soujunior/petjournal/ui/screens_app/screens_pets/petRaceAndSizeScreen/components/Screen.kt index 24f0cf67..37b7d960 100644 --- a/petJournal/app/src/main/java/com/soujunior/petjournal/ui/screens_app/screens_pets/petRaceAndSizeScreen/components/Screen.kt +++ b/petJournal/app/src/main/java/com/soujunior/petjournal/ui/screens_app/screens_pets/petRaceAndSizeScreen/components/Screen.kt @@ -38,10 +38,12 @@ import com.soujunior.petjournal.ui.components.Breadcrumb import com.soujunior.petjournal.ui.components.Button3 import com.soujunior.petjournal.ui.components.DashedInputText import com.soujunior.petjournal.ui.components.DropDown +import com.soujunior.petjournal.ui.components.IndeterminateCircularIndicator import com.soujunior.petjournal.ui.components.NavigationBar import com.soujunior.petjournal.ui.components.ScaffoldCustom import com.soujunior.petjournal.ui.screens_app.screens_pets.petRaceAndSizeScreen.RaceSizeFormEvent import com.soujunior.petjournal.ui.screens_app.screens_pets.petRaceAndSizeScreen.ViewModelRaceSize +import com.soujunior.petjournal.ui.states.TaskState import com.soujunior.petjournal.ui.util.getScreenHeightInch import org.koin.androidx.compose.getViewModel @@ -49,6 +51,7 @@ import org.koin.androidx.compose.getViewModel @Composable fun Screen(idPetInformation: String?, navController: NavController) { val viewModel: ViewModelRaceSize = getViewModel() + val taskState by viewModel.taskState.collectAsState() val isTextFiledOthersVisible by viewModel.isTextFiledOthersVisible.collectAsState() if (idPetInformation != null) { viewModel.getPetInformation(idPetInformation.toLong()) @@ -71,7 +74,9 @@ fun Screen(idPetInformation: String?, navController: NavController) { showBottomBarNavigation = true, bottomNavigationBar = { NavigationBar(navController) }, contentToUse = { - Box(modifier = Modifier.padding(it)) { + if (taskState is TaskState.Loading) + IndeterminateCircularIndicator(modifier = Modifier.align(Alignment.CenterHorizontally)) + else Box(modifier = Modifier.padding(it)) { Image( painter = painterResource(R.drawable.rastro), contentDescription = null, diff --git a/petJournal/app/src/test/java/com/soujunior/petjournal/setup/FormMock.kt b/petJournal/app/src/test/java/com/soujunior/petjournal/setup/FormMock.kt index 12eeb42d..9253d362 100644 --- a/petJournal/app/src/test/java/com/soujunior/petjournal/setup/FormMock.kt +++ b/petJournal/app/src/test/java/com/soujunior/petjournal/setup/FormMock.kt @@ -41,7 +41,7 @@ val perInformation = PetInformationModel( petRace = "Akita", petAge = "12122012", guardianId = 1, - castration = true + castrated = true ) val listDogRaces = listOf( diff --git a/petJournal/app/src/test/java/com/soujunior/petjournal/ui/petBirthDate/PetBirthDateViewModelTest.kt b/petJournal/app/src/test/java/com/soujunior/petjournal/ui/petBirthDate/PetBirthDateViewModelTest.kt index 2bef1ee0..a9d1e0ba 100644 --- a/petJournal/app/src/test/java/com/soujunior/petjournal/ui/petBirthDate/PetBirthDateViewModelTest.kt +++ b/petJournal/app/src/test/java/com/soujunior/petjournal/ui/petBirthDate/PetBirthDateViewModelTest.kt @@ -5,6 +5,7 @@ import assertk.assertThat import assertk.assertions.isFalse import assertk.assertions.isTrue import com.soujunior.domain.use_case.base.DataResult +import com.soujunior.domain.use_case.pet.CreatePetInformationApiUseCase import com.soujunior.domain.use_case.pet.GetPetInformationUseCase import com.soujunior.domain.use_case.pet.UpdatePetInformationUseCase import com.soujunior.domain.use_case.util.ValidationRepositoryImpl @@ -32,11 +33,12 @@ class PetBirthDateViewModelTest { private val validation = mockk(relaxed = true) private val getPetInformationUseCase = mockk(relaxed = true) private val updatePetInformationUseCase = mockk(relaxed = true) + private val createPetInformationApiUseCase = mockk(relaxed = true) @OptIn(ExperimentalCoroutinesApi::class) @Before fun setup() { Dispatchers.setMain(Dispatchers.Unconfined) - viewModelTest = BirthDateViewModelImpl(validation, getPetInformationUseCase, updatePetInformationUseCase) + viewModelTest = BirthDateViewModelImpl(validation, getPetInformationUseCase, updatePetInformationUseCase, createPetInformationApiUseCase) } @After @@ -296,5 +298,13 @@ class PetBirthDateViewModelTest { viewModelTest.updatePetInformation() assertEquals("Error", viewModelTest.message.value) } + @Test + fun `createPetInformation should call the use case to create pet information in the api`() { -} + coEvery { createPetInformationApiUseCase.execute(any()) } returns DataResult.Success( + Unit + ) + viewModelTest.createPetInformation() + assertEquals("Sucesso", viewModelTest.message.value) + } +} \ No newline at end of file diff --git a/petJournal/buildSrc/build/libs/buildSrc.jar b/petJournal/buildSrc/build/libs/buildSrc.jar index 623f3429..13d6adc1 100644 Binary files a/petJournal/buildSrc/build/libs/buildSrc.jar and b/petJournal/buildSrc/build/libs/buildSrc.jar differ diff --git a/petJournal/data/src/main/java/com/soujunior/data/remote/GuardianService.kt b/petJournal/data/src/main/java/com/soujunior/data/remote/GuardianService.kt index 7cca4431..c3f52282 100644 --- a/petJournal/data/src/main/java/com/soujunior/data/remote/GuardianService.kt +++ b/petJournal/data/src/main/java/com/soujunior/data/remote/GuardianService.kt @@ -3,9 +3,12 @@ package com.soujunior.data.remote import com.soujunior.domain.model.request.PetRaceItemModel import com.soujunior.domain.model.request.PetSizeItemModel import com.soujunior.domain.model.response.GuardianNameResponse +import com.soujunior.domain.model.response.PetInformationResponse import com.soujunior.domain.network.NetworkResult +import retrofit2.http.Body import retrofit2.http.GET import retrofit2.http.Header +import retrofit2.http.POST import retrofit2.http.Path interface GuardianService { @@ -23,4 +26,10 @@ interface GuardianService { @Header("Authorization") token: String, @Path("petSpecie") petSpecie: String ): NetworkResult> + + @POST("api/pet") + suspend fun savePetInformation( + @Header("Authorization") token: String, + @Body petInformationResponse: PetInformationResponse + ): NetworkResult } \ No newline at end of file diff --git a/petJournal/data/src/main/java/com/soujunior/data/repository/GuardianRepositoryImpl.kt b/petJournal/data/src/main/java/com/soujunior/data/repository/GuardianRepositoryImpl.kt index 6d452b2b..33f84ef0 100644 --- a/petJournal/data/src/main/java/com/soujunior/data/repository/GuardianRepositoryImpl.kt +++ b/petJournal/data/src/main/java/com/soujunior/data/repository/GuardianRepositoryImpl.kt @@ -3,6 +3,7 @@ package com.soujunior.data.repository import android.content.ContentValues.TAG import android.content.Context import android.util.Log +import com.petjournal.database.converter.Converter.toResponse import com.soujunior.data.remote.GuardianService import com.soujunior.data.util.manager.JwtManager import com.soujunior.domain.model.PetInformationModel @@ -75,7 +76,8 @@ class GuardianRepositoryImpl( } override suspend fun getListPetSizes(petSpecie: String): NetworkResult> { - val localListPetSizes = guardianLocalDataSourceImpl.getListPetSizes(petSpecie)?.success?.data + val localListPetSizes = + guardianLocalDataSourceImpl.getListPetSizes(petSpecie)?.success?.data return if (!localListPetSizes.isNullOrEmpty()) { NetworkResult.Success(localListPetSizes) } else { @@ -99,7 +101,8 @@ class GuardianRepositoryImpl( } override suspend fun getListPetRaces(petSpecie: String): NetworkResult> { - val localListPetRaces = guardianLocalDataSourceImpl.getListPetRaces(petSpecie)?.success?.data + val localListPetRaces = + guardianLocalDataSourceImpl.getListPetRaces(petSpecie)?.success?.data return if (!localListPetRaces.isNullOrEmpty()) { NetworkResult.Success(localListPetRaces) } else { @@ -122,4 +125,10 @@ class GuardianRepositoryImpl( } } + override suspend fun createPetInformationApi(petInformationModel: PetInformationModel): NetworkResult { + val token = "Bearer " + jwtManager.getToken() + val pet = petInformationModel.toResponse() + return guardianApi.savePetInformation(token, pet) + } + } diff --git a/petJournal/database/src/main/java/com/petjournal/database/converter/Converter.kt b/petJournal/database/src/main/java/com/petjournal/database/converter/Converter.kt index 7a4e4bb0..27f75142 100644 --- a/petJournal/database/src/main/java/com/petjournal/database/converter/Converter.kt +++ b/petJournal/database/src/main/java/com/petjournal/database/converter/Converter.kt @@ -6,6 +6,7 @@ import com.petjournal.database.database.entity.PetSize import com.soujunior.domain.model.PetInformationModel import com.soujunior.domain.model.request.PetRaceItemModel import com.soujunior.domain.model.request.PetSizeItemModel +import com.soujunior.domain.model.response.PetInformationResponse object Converter { fun PetInformation.toModel(): PetInformationModel { @@ -18,7 +19,7 @@ object Converter { petRace = this.petRace, petAge = this.petAge, guardianId = this.guardianId, - castration = this.castration + castrated = this.castration ) } @@ -32,9 +33,21 @@ object Converter { size = this.size, petRace = this.petRace, petAge = this.petAge, - castration = this.castration + castration = this.castrated ) } + fun PetInformationModel.toResponse(): PetInformationResponse { + return PetInformationResponse( + specieName = if (this.species == "Cat" ) "Gato" else if ( this.species == "Dog") "Cachorro" else this.species, + petName = this.name, + gender = this.gender, + breedName = this.petRace, + size = this.size, + castrated = this.castrated, + dateOfBirth = this.petAge + ) + } + fun List.toListPetSizeItemModel(): List { val listPetSizesItemModel: MutableList = mutableListOf() diff --git a/petJournal/domain/src/main/java/com/soujunior/domain/model/PetInformationModel.kt b/petJournal/domain/src/main/java/com/soujunior/domain/model/PetInformationModel.kt index a85dcd83..d17fc91f 100644 --- a/petJournal/domain/src/main/java/com/soujunior/domain/model/PetInformationModel.kt +++ b/petJournal/domain/src/main/java/com/soujunior/domain/model/PetInformationModel.kt @@ -1,5 +1,7 @@ package com.soujunior.domain.model +import java.time.LocalDate + data class PetInformationModel( val id: Long, val species: String? = null, @@ -9,5 +11,5 @@ data class PetInformationModel( val petRace: String? = null, val petAge: String? = null, val guardianId: Int? = null, - val castration: Boolean? = null + val castrated: Boolean? = null ) \ No newline at end of file diff --git a/petJournal/domain/src/main/java/com/soujunior/domain/model/response/PetInformationResponse.kt b/petJournal/domain/src/main/java/com/soujunior/domain/model/response/PetInformationResponse.kt new file mode 100644 index 00000000..821eb831 --- /dev/null +++ b/petJournal/domain/src/main/java/com/soujunior/domain/model/response/PetInformationResponse.kt @@ -0,0 +1,11 @@ +package com.soujunior.domain.model.response + +data class PetInformationResponse( + val specieName: String? = null, + val petName: String? = null, + val gender: String? = null, + val breedName: String? = null, + val size: String? = null, + val castrated: Boolean? = null, + val dateOfBirth: String? = null, +) diff --git a/petJournal/domain/src/main/java/com/soujunior/domain/repository/GuardianRepository.kt b/petJournal/domain/src/main/java/com/soujunior/domain/repository/GuardianRepository.kt index 6f934b1a..6fc9bf08 100644 --- a/petJournal/domain/src/main/java/com/soujunior/domain/repository/GuardianRepository.kt +++ b/petJournal/domain/src/main/java/com/soujunior/domain/repository/GuardianRepository.kt @@ -14,4 +14,5 @@ interface GuardianRepository { suspend fun updatePetInformation(petInformationModel: PetInformationModel) : DataResult suspend fun getListPetSizes(petSpecie: String): NetworkResult> suspend fun getListPetRaces(petSpecie: String): NetworkResult> + suspend fun createPetInformationApi(petInformationModel: PetInformationModel): NetworkResult } \ No newline at end of file diff --git a/petJournal/domain/src/main/java/com/soujunior/domain/use_case/pet/CreatePetInformationApiUseCase.kt b/petJournal/domain/src/main/java/com/soujunior/domain/use_case/pet/CreatePetInformationApiUseCase.kt new file mode 100644 index 00000000..4f648679 --- /dev/null +++ b/petJournal/domain/src/main/java/com/soujunior/domain/use_case/pet/CreatePetInformationApiUseCase.kt @@ -0,0 +1,24 @@ +package com.soujunior.domain.use_case.pet + +import com.soujunior.domain.model.PetInformationModel +import com.soujunior.domain.network.NetworkResult +import com.soujunior.domain.repository.GuardianRepository +import com.soujunior.domain.use_case.base.BaseUseCase +import com.soujunior.domain.use_case.base.DataResult + +class CreatePetInformationApiUseCase(private val repository: GuardianRepository) : + BaseUseCase() { + override suspend fun doWork(value: PetInformationModel): DataResult { + return try { + return when (val response = repository.createPetInformationApi(value)) { + is NetworkResult.Success -> { DataResult.Success(response.data) } + is NetworkResult.Error -> DataResult.Failure(Throwable(message = "${response.code} -> ${response.body?.error}")) + is NetworkResult.Exception -> DataResult.Failure(response.e) + } + + + } catch (e: Throwable) { + DataResult.Failure(e) + } + } +} diff --git a/petJournal/domain/src/test/java/com/soujunior/domain/use_case/pet/CreatePetInformationApiUseCaseTest.kt b/petJournal/domain/src/test/java/com/soujunior/domain/use_case/pet/CreatePetInformationApiUseCaseTest.kt new file mode 100644 index 00000000..0dd1e6cd --- /dev/null +++ b/petJournal/domain/src/test/java/com/soujunior/domain/use_case/pet/CreatePetInformationApiUseCaseTest.kt @@ -0,0 +1,44 @@ +package com.soujunior.domain.use_case.pet + +import assertk.assertions.isEqualTo +import com.soujunior.domain.network.NetworkResult +import com.soujunior.domain.repository.GuardianRepository +import com.soujunior.domain.setup.MainCoroutineRule +import com.soujunior.domain.setup.petInformation +import io.mockk.coEvery +import io.mockk.mockk +import kotlinx.coroutines.ExperimentalCoroutinesApi +import kotlinx.coroutines.runBlocking +import org.junit.Rule +import org.junit.Test + +class CreatePetInformationApiUseCaseTest { + @ExperimentalCoroutinesApi + @get:Rule + var coroutineTesteRule = MainCoroutineRule() + + private val repository = mockk(relaxed = true) + + @Test + fun `failure create pet information`() = runBlocking { + coEvery { repository.createPetInformationApi(petInformation) } returns NetworkResult.Exception( + Throwable() + ) + val createPetInformation = CreatePetInformationApiUseCase(repository = repository) + + val result = createPetInformation.execute(petInformation) + + assertk.assertThat(result.isFailure).isEqualTo(true) + } + + @Test + fun `successful in create pet Information`() = runBlocking { + coEvery { repository.createPetInformationApi(petInformation) } returns NetworkResult.Success(data = Unit) + val createPetInformationUseCase = CreatePetInformationApiUseCase(repository = repository) + + val result = createPetInformationUseCase.execute(petInformation) + + assertk.assertThat(result.success.data).isEqualTo(Unit) + } + +} \ No newline at end of file