-
Notifications
You must be signed in to change notification settings - Fork 1
[ALFMOB-81] Add to Bag #5
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
Merged
Merged
Changes from 15 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
823fc96
ALFMOB-81 Add product to bag and bag list
mrfreitas 4edd25c
Merge branch 'dev/Fixes_on_mapping' into dev/ALFMOB-81_add_to_bag
mrfreitas c60bfa3
Merge branch 'refs/heads/main' into dev/ALFMOB-81_add_to_bag
mrfreitas b070c89
Added unit test for use cases
mrfreitas 8e8afad
Added unit test for ViewModel
mrfreitas b910e64
Merge branch 'main' into dev/ALFMOB-81_add_to_bag
mrfreitas 9ff9a6b
Multiple improvements
mrfreitas 92a989f
Merge branch 'main' into dev/ALFMOB-81_add_to_bag
mrfreitas 30691b6
Multiple improvements
mrfreitas ada3510
chore: Add to bag fixed, unit tests fixed and static code analysis fixed
QaziMashhood 10fb65d
chore: passing of state removed from AddtoBag event
QaziMashhood ea5e467
chore: passing of state removed from AddtoBag event
QaziMashhood 151572a
feature: remove from bag functionality added
QaziMashhood 35fe894
chore: fixed tests
QaziMashhood 28a2b5d
chore: fixed tests
QaziMashhood bff7390
chore: Refactoring an code improvements
QaziMashhood f9b44d8
feature: [ALFMOB-81] changes due to feedback
a122bc5
feature: [ALFMOB-81] cleanup and fix unit tests
rsousamindera 681e038
feature: [ALFMOB-81] cleanup
rsousamindera e240a46
Merge branch 'main' into dev/ALFMOB-81_add_to_bag
rsousamindera aec4884
Merge branch 'main' into dev/ALFMOB-81_add_to_bag
artyomromanov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
data/src/main/java/au/com/alfie/ecomm/data/bag/BagRepositoryImpl.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| package au.com.alfie.ecomm.data.bag | ||
|
|
||
| import au.com.alfie.ecomm.data.toRepositoryResult | ||
| import au.com.alfie.ecomm.repository.bag.BagProduct | ||
| import au.com.alfie.ecomm.repository.bag.BagRepository | ||
| import au.com.alfie.ecomm.repository.result.RepositoryResult | ||
| import kotlinx.coroutines.flow.Flow | ||
| import kotlinx.coroutines.flow.MutableStateFlow | ||
| import kotlinx.coroutines.flow.map | ||
| import javax.inject.Inject | ||
|
|
||
| class BagRepositoryImpl @Inject constructor() : BagRepository { | ||
|
|
||
| // TODO consider removing this property when the products in the bag are saved on database or api | ||
| private val _bag = MutableStateFlow<List<BagProduct>>(listOf()) | ||
|
|
||
| // TODO change this implementation to a proper implementation using data base or api to save the product | ||
| override fun addToBag(bagProduct: BagProduct): RepositoryResult<Boolean> { | ||
| _bag.value.firstOrNull { | ||
| bagProduct.productId == it.productId && bagProduct.variantSku == it.variantSku | ||
| } ?: run { | ||
| _bag.value = _bag.value.toMutableList().apply { add(bagProduct) } | ||
| } | ||
| return RepositoryResult.Success(true) | ||
| } | ||
|
|
||
| // TODO change this implementation to a proper implementation using data base or api to get the products in the bag | ||
| override fun getBag(): Flow<RepositoryResult<List<BagProduct>>> { | ||
| return _bag.map { bag -> | ||
| Result.success(bag).toRepositoryResult() | ||
| } | ||
| } | ||
|
|
||
| override fun removeFromBag(bagProduct: BagProduct): RepositoryResult<Boolean> { | ||
| _bag.value = _bag.value.toMutableList().apply { remove(bagProduct) } | ||
| return RepositoryResult.Success(true) | ||
| } | ||
| } | ||
18 changes: 18 additions & 0 deletions
18
data/src/main/java/au/com/alfie/ecomm/data/bag/di/BagModule.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| package au.com.alfie.ecomm.data.bag.di | ||
|
|
||
| import au.com.alfie.ecomm.data.bag.BagRepositoryImpl | ||
| import au.com.alfie.ecomm.repository.bag.BagRepository | ||
| import dagger.Binds | ||
| import dagger.Module | ||
| import dagger.hilt.InstallIn | ||
| import dagger.hilt.components.SingletonComponent | ||
| import javax.inject.Singleton | ||
|
|
||
| @Module | ||
| @InstallIn(SingletonComponent::class) | ||
| internal abstract class BagModule { | ||
pmac8585 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| @Binds | ||
| @Singleton | ||
| abstract fun bindBagRepository(bagRepositoryImpl: BagRepositoryImpl): BagRepository | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
domain/repository/src/main/java/au/com/alfie/ecomm/repository/bag/BagProduct.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| package au.com.alfie.ecomm.repository.bag | ||
|
|
||
| data class BagProduct( | ||
| val productId: String, | ||
| val variantSku: String | ||
| ) |
13 changes: 13 additions & 0 deletions
13
domain/repository/src/main/java/au/com/alfie/ecomm/repository/bag/BagRepository.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| package au.com.alfie.ecomm.repository.bag | ||
|
|
||
| import au.com.alfie.ecomm.repository.result.RepositoryResult | ||
| import kotlinx.coroutines.flow.Flow | ||
|
|
||
| interface BagRepository { | ||
|
|
||
| fun addToBag(bagProduct: BagProduct): RepositoryResult<Boolean> | ||
|
|
||
| fun getBag(): Flow<RepositoryResult<List<BagProduct>>> | ||
|
|
||
| fun removeFromBag(bagProduct: BagProduct): RepositoryResult<Boolean> | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
domain/src/main/java/au/com/alfie/ecomm/domain/usecase/bag/AddToBagUseCase.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| package au.com.alfie.ecomm.domain.usecase.bag | ||
|
|
||
| import au.com.alfie.ecomm.domain.UseCaseInteractor | ||
| import au.com.alfie.ecomm.domain.UseCaseResult | ||
| import au.com.alfie.ecomm.domain.doOnResult | ||
| import au.com.alfie.ecomm.repository.bag.BagProduct | ||
| import au.com.alfie.ecomm.repository.bag.BagRepository | ||
| import javax.inject.Inject | ||
|
|
||
| class AddToBagUseCase @Inject constructor( | ||
| private val bagRepository: BagRepository | ||
| ) : UseCaseInteractor { | ||
|
|
||
| suspend operator fun invoke(productId: String, variantSku: String) = | ||
pmac8585 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| run(bagRepository.addToBag(BagProduct(productId = productId, variantSku = variantSku))).doOnResult( | ||
| onSuccess = { UseCaseResult.Success(it) }, | ||
| onError = { UseCaseResult.Error(it) } | ||
| ) | ||
| } | ||
19 changes: 19 additions & 0 deletions
19
domain/src/main/java/au/com/alfie/ecomm/domain/usecase/bag/GetBagUseCase.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| package au.com.alfie.ecomm.domain.usecase.bag | ||
|
|
||
| import au.com.alfie.ecomm.domain.UseCaseInteractor | ||
| import au.com.alfie.ecomm.domain.UseCaseResult | ||
| import au.com.alfie.ecomm.repository.bag.BagProduct | ||
| import au.com.alfie.ecomm.repository.bag.BagRepository | ||
| import kotlinx.coroutines.flow.Flow | ||
| import kotlinx.coroutines.flow.map | ||
| import javax.inject.Inject | ||
|
|
||
| class GetBagUseCase @Inject constructor( | ||
| private val bagRepository: BagRepository | ||
| ) : UseCaseInteractor { | ||
|
|
||
| suspend operator fun invoke(): Flow<UseCaseResult<List<BagProduct>>> = | ||
| bagRepository.getBag().map { repositoryResult -> | ||
| run(repositoryResult) | ||
| } | ||
| } |
19 changes: 19 additions & 0 deletions
19
domain/src/main/java/au/com/alfie/ecomm/domain/usecase/bag/RemoveFromBagUseCase.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| package au.com.alfie.ecomm.domain.usecase.bag | ||
|
|
||
| import au.com.alfie.ecomm.domain.UseCaseInteractor | ||
| import au.com.alfie.ecomm.domain.UseCaseResult | ||
| import au.com.alfie.ecomm.domain.doOnResult | ||
| import au.com.alfie.ecomm.repository.bag.BagProduct | ||
| import au.com.alfie.ecomm.repository.bag.BagRepository | ||
| import javax.inject.Inject | ||
|
|
||
| class RemoveFromBagUseCase @Inject constructor( | ||
| private val bagRepository: BagRepository | ||
| ) : UseCaseInteractor { | ||
|
|
||
| suspend operator fun invoke(bagProduct: BagProduct) = | ||
| run(bagRepository.removeFromBag(bagProduct)).doOnResult( | ||
| onSuccess = { UseCaseResult.Success(it) }, | ||
| onError = { UseCaseResult.Error(it) } | ||
| ) | ||
| } |
55 changes: 55 additions & 0 deletions
55
domain/src/test/java/au/com/alfie/ecomm/domain/bag/AddToBagUseCaseTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| package au.com.alfie.ecomm.domain.bag | ||
|
|
||
| import au.com.alfie.ecomm.domain.usecase.bag.AddToBagUseCase | ||
| import au.com.alfie.ecomm.repository.bag.BagProduct | ||
| import au.com.alfie.ecomm.repository.bag.BagRepository | ||
| import au.com.alfie.ecomm.repository.product.ProductRepository | ||
| import au.com.alfie.ecomm.repository.result.ErrorResult | ||
| import au.com.alfie.ecomm.repository.result.RepositoryResult | ||
| import io.mockk.coEvery | ||
| import io.mockk.coVerify | ||
| import io.mockk.every | ||
| import io.mockk.impl.annotations.InjectMockKs | ||
| import io.mockk.impl.annotations.RelaxedMockK | ||
| import io.mockk.junit5.MockKExtension | ||
| import io.mockk.mockk | ||
| import kotlinx.coroutines.test.runTest | ||
| import org.junit.jupiter.api.Test | ||
| import org.junit.jupiter.api.extension.ExtendWith | ||
|
|
||
| @ExtendWith(MockKExtension::class) | ||
| class AddToBagUseCaseTest { | ||
| @RelaxedMockK | ||
| private lateinit var bagRepository: BagRepository | ||
|
|
||
| @RelaxedMockK | ||
| private lateinit var productRepository: ProductRepository | ||
|
|
||
| @InjectMockKs | ||
| lateinit var subject: AddToBagUseCase | ||
|
|
||
| @Test | ||
| fun `add to bag, with success result`() = runTest { | ||
| val mockProduct = mockk<BagProduct> { | ||
| every { productId } returns "10" | ||
| every { variantSku } returns "34535" | ||
| } | ||
|
|
||
| coEvery { bagRepository.addToBag(mockProduct) } returns RepositoryResult.Success(true) | ||
| subject(productId = "10", variantSku = "34535") | ||
| coVerify { bagRepository.addToBag(mockProduct) } | ||
| } | ||
|
|
||
| @Test | ||
| fun `add to bag, with error result`() = runTest { | ||
| val mockProduct = mockk<BagProduct> { | ||
| every { productId } returns "10" | ||
| every { variantSku } returns "34535" | ||
| } | ||
| val errorResult = mockk<ErrorResult>() | ||
|
|
||
| coEvery { bagRepository.addToBag(mockProduct) } returns RepositoryResult.Error(errorResult) | ||
| subject("10", "34535") | ||
| coVerify { bagRepository.addToBag(mockProduct) } | ||
| } | ||
| } |
50 changes: 50 additions & 0 deletions
50
domain/src/test/java/au/com/alfie/ecomm/domain/bag/GetBagUseCaseTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| package au.com.alfie.ecomm.domain.bag | ||
|
|
||
| import au.com.alfie.ecomm.domain.UseCaseResult | ||
| import au.com.alfie.ecomm.domain.usecase.bag.GetBagUseCase | ||
| import au.com.alfie.ecomm.repository.bag.BagProduct | ||
| import au.com.alfie.ecomm.repository.bag.BagRepository | ||
| import au.com.alfie.ecomm.repository.result.ErrorResult | ||
| import au.com.alfie.ecomm.repository.result.RepositoryResult | ||
| import io.mockk.coEvery | ||
| import io.mockk.impl.annotations.InjectMockKs | ||
| import io.mockk.impl.annotations.RelaxedMockK | ||
| import io.mockk.junit5.MockKExtension | ||
| import io.mockk.mockk | ||
| import kotlinx.coroutines.flow.first | ||
| import kotlinx.coroutines.flow.flowOf | ||
| import kotlinx.coroutines.test.runTest | ||
| import org.junit.jupiter.api.Test | ||
| import org.junit.jupiter.api.extension.ExtendWith | ||
| import kotlin.test.assertEquals | ||
|
|
||
| @ExtendWith(MockKExtension::class) | ||
| class GetBagUseCaseTest { | ||
| @RelaxedMockK | ||
| private lateinit var bagRepository: BagRepository | ||
|
|
||
| @InjectMockKs | ||
| lateinit var subject: GetBagUseCase | ||
|
|
||
| @Test | ||
| fun `get list of products in the bag`() = runTest { | ||
mrfreitas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| val mockBag = mockk<List<BagProduct>>() | ||
| coEvery { bagRepository.getBag() } returns flowOf(RepositoryResult.Success(mockBag)) | ||
|
|
||
| val expected = UseCaseResult.Success(mockBag) | ||
| val result = subject().first() | ||
|
|
||
| assertEquals(expected, result) | ||
| } | ||
|
|
||
| @Test | ||
| fun `get list of products in the bag, and returns an error`() = runTest { | ||
| val errorResult = mockk<ErrorResult>() | ||
| coEvery { bagRepository.getBag() } returns flowOf(RepositoryResult.Error(errorResult)) | ||
|
|
||
| val expected = UseCaseResult.Error(errorResult) | ||
| val result = subject().first() | ||
|
|
||
| assertEquals(expected, result) | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.