Skip to content

Commit

Permalink
[AN] feat: 내 강아지 조회 및 추가하기 API 연결 (#183)
Browse files Browse the repository at this point in the history
  • Loading branch information
junjange authored and takoyakimchi committed Oct 23, 2024
1 parent c1e441c commit f3b7300
Show file tree
Hide file tree
Showing 48 changed files with 719 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,27 @@ import com.woowacourse.friendogly.data.repository.ClubRepositoryImpl
import com.woowacourse.friendogly.data.repository.KakaoLoginRepositoryImpl
import com.woowacourse.friendogly.data.repository.LocalRepositoryImpl
import com.woowacourse.friendogly.data.repository.MemberRepositoryImpl
import com.woowacourse.friendogly.data.repository.PetRepositoryImpl
import com.woowacourse.friendogly.data.source.ClubDataSource
import com.woowacourse.friendogly.data.source.KakaoLoginDataSource
import com.woowacourse.friendogly.data.source.LocalDataSource
import com.woowacourse.friendogly.data.source.MemberDataSource
import com.woowacourse.friendogly.data.source.PetDataSource
import com.woowacourse.friendogly.domain.repository.ClubRepository
import com.woowacourse.friendogly.domain.repository.KakaoLoginRepository
import com.woowacourse.friendogly.domain.repository.LocalRepository
import com.woowacourse.friendogly.domain.repository.MemberRepository
import com.woowacourse.friendogly.domain.repository.PetRepository
import com.woowacourse.friendogly.domain.usecase.DeleteClubUseCase
import com.woowacourse.friendogly.domain.usecase.DeleteLocalDataUseCase
import com.woowacourse.friendogly.domain.usecase.GetClubMineUseCase
import com.woowacourse.friendogly.domain.usecase.GetJwtTokenUseCase
import com.woowacourse.friendogly.domain.usecase.GetPetsMineUseCase
import com.woowacourse.friendogly.domain.usecase.KakaoLoginUseCase
import com.woowacourse.friendogly.domain.usecase.PostClubParticipationUseCase
import com.woowacourse.friendogly.domain.usecase.PostClubUseCase
import com.woowacourse.friendogly.domain.usecase.PostMemberUseCase
import com.woowacourse.friendogly.domain.usecase.PostPetUseCase
import com.woowacourse.friendogly.domain.usecase.SaveJwtTokenUseCase
import com.woowacourse.friendogly.kakao.source.KakaoLoginDataSourceImpl
import com.woowacourse.friendogly.local.di.LocalModule
Expand All @@ -30,6 +35,7 @@ import com.woowacourse.friendogly.remote.api.BaseUrl
import com.woowacourse.friendogly.remote.di.RemoteModule
import com.woowacourse.friendogly.remote.source.ClubDataSourceImpl
import com.woowacourse.friendogly.remote.source.MemberDataSourceImpl
import com.woowacourse.friendogly.remote.source.PetDataSourceImpl

class AppModule(context: Context) {
private val baseUrl = BaseUrl(BuildConfig.base_url)
Expand All @@ -48,18 +54,26 @@ class AppModule(context: Context) {
localModule = localModule,
)

private val petService =
RemoteModule.createPetService(
baseUrl = baseUrl,
localModule = localModule,
)

// data source
private val clubDataSource: ClubDataSource = ClubDataSourceImpl(service = clubService)
private val localDataSource: LocalDataSource = LocalDataSourceImpl(localModule = localModule)
private val kakaoLoginDataSource: KakaoLoginDataSource = KakaoLoginDataSourceImpl()
private val memberDataSource: MemberDataSource = MemberDataSourceImpl(service = memberService)
private val petDataSource: PetDataSource = PetDataSourceImpl(service = petService)

// repository
private val clubRepository: ClubRepository = ClubRepositoryImpl(source = clubDataSource)
private val localRepository: LocalRepository = LocalRepositoryImpl(source = localDataSource)
private val kakaoLoginRepository: KakaoLoginRepository =
KakaoLoginRepositoryImpl(dataSource = kakaoLoginDataSource)
private val memberRepository: MemberRepository = MemberRepositoryImpl(source = memberDataSource)
private val petRepository: PetRepository = PetRepositoryImpl(source = petDataSource)

// use case
val kakaoLoginUseCase: KakaoLoginUseCase = KakaoLoginUseCase(repository = kakaoLoginRepository)
Expand All @@ -73,6 +87,8 @@ class AppModule(context: Context) {
val deleteLocalDataUseCase: DeleteLocalDataUseCase =
DeleteLocalDataUseCase(repository = localRepository)
val postMemberUseCase: PostMemberUseCase = PostMemberUseCase(repository = memberRepository)
val getPetsMineUseCase: GetPetsMineUseCase = GetPetsMineUseCase(repository = petRepository)
val postPetUseCase: PostPetUseCase = PostPetUseCase(repository = petRepository)

companion object {
private var instance: AppModule? = null
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.woowacourse.friendogly.data.mapper

import com.woowacourse.friendogly.data.model.GenderDto
import com.woowacourse.friendogly.domain.model.Gender

fun GenderDto.toDomain(): Gender {
return when (this) {
GenderDto.MALE -> Gender.MALE
GenderDto.FEMALE -> Gender.FEMALE
GenderDto.MALE_NEUTERED -> Gender.MALE_NEUTERED
GenderDto.FEMALE_NEUTERED -> Gender.FEMALE_NEUTERED
}
}

fun Gender.toData(): GenderDto {
return when (this) {
Gender.MALE -> GenderDto.MALE
Gender.FEMALE -> GenderDto.FEMALE
Gender.MALE_NEUTERED -> GenderDto.MALE_NEUTERED
Gender.FEMALE_NEUTERED -> GenderDto.FEMALE_NEUTERED
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.woowacourse.friendogly.data.mapper

import com.woowacourse.friendogly.data.model.PetDto
import com.woowacourse.friendogly.domain.model.Pet

fun PetDto.toDomain(): Pet {
return Pet(
id = this.id,
memberId = this.memberId,
name = this.name,
description = this.description,
birthDate = this.birthDate,
sizeType = this.sizeType.toDomain(),
gender = this.gender.toDomain(),
imageUrl = this.imageUrl,
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.woowacourse.friendogly.data.mapper

import com.woowacourse.friendogly.data.model.PetsDto
import com.woowacourse.friendogly.domain.model.Pets

fun PetsDto.toDomain(): Pets {
return Pets(
contents =
this.contents.map { petResponse ->
petResponse.toDomain()
},
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.woowacourse.friendogly.data.mapper

import com.woowacourse.friendogly.data.model.SizeTypeDto
import com.woowacourse.friendogly.domain.model.SizeType

fun SizeTypeDto.toDomain(): SizeType {
return when (this) {
SizeTypeDto.SMALL -> SizeType.SMALL
SizeTypeDto.MEDIUM -> SizeType.MEDIUM
SizeTypeDto.LARGE -> SizeType.LARGE
}
}

fun SizeType.toData(): SizeTypeDto {
return when (this) {
SizeType.SMALL -> SizeTypeDto.SMALL
SizeType.MEDIUM -> SizeTypeDto.MEDIUM
SizeType.LARGE -> SizeTypeDto.LARGE
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.woowacourse.friendogly.data.model

enum class GenderDto {
MALE,
FEMALE,
MALE_NEUTERED,
FEMALE_NEUTERED,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.woowacourse.friendogly.data.model

import com.woowacourse.friendogly.remote.util.LocalDateSerializer
import kotlinx.datetime.LocalDate
import kotlinx.serialization.Serializable

data class PetDto(
val id: Long,
val memberId: Long,
val name: String,
val description: String,
@Serializable(with = LocalDateSerializer::class)
val birthDate: LocalDate,
val sizeType: SizeTypeDto,
val gender: GenderDto,
val imageUrl: String,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.woowacourse.friendogly.data.model

data class PetsDto(
val contents: List<PetDto>,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.woowacourse.friendogly.data.model

enum class SizeTypeDto {
SMALL,
MEDIUM,
LARGE,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.woowacourse.friendogly.data.repository

import com.woowacourse.friendogly.data.mapper.toData
import com.woowacourse.friendogly.data.mapper.toDomain
import com.woowacourse.friendogly.data.source.PetDataSource
import com.woowacourse.friendogly.domain.model.Gender
import com.woowacourse.friendogly.domain.model.Pet
import com.woowacourse.friendogly.domain.model.SizeType
import com.woowacourse.friendogly.domain.repository.PetRepository
import kotlinx.datetime.LocalDate

class PetRepositoryImpl(private val source: PetDataSource) : PetRepository {
override suspend fun getPetsMine(): Result<List<Pet>> =
source.getPetsMine().mapCatching { result -> result.map { petDto -> petDto.toDomain() } }

override suspend fun postPet(
name: String,
description: String,
birthday: LocalDate,
sizeType: SizeType,
gender: Gender,
imageUrl: String,
): Result<Pet> =
source.postPet(
name = name,
description = description,
birthday = birthday,
sizeType = sizeType.toData(),
gender = gender.toData(),
imageUrl = imageUrl,
).mapCatching { result -> result.toDomain() }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.woowacourse.friendogly.data.source

import com.woowacourse.friendogly.data.model.GenderDto
import com.woowacourse.friendogly.data.model.PetDto
import com.woowacourse.friendogly.data.model.SizeTypeDto
import kotlinx.datetime.LocalDate

interface PetDataSource {
suspend fun getPetsMine(): Result<List<PetDto>>

suspend fun postPet(
name: String,
description: String,
birthday: LocalDate,
sizeType: SizeTypeDto,
gender: GenderDto,
imageUrl: String,
): Result<PetDto>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.woowacourse.friendogly.domain.model

enum class Gender {
MALE,
FEMALE,
MALE_NEUTERED,
FEMALE_NEUTERED,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.woowacourse.friendogly.domain.model

import kotlinx.datetime.LocalDate

data class Pet(
val id: Long,
val memberId: Long,
val name: String,
val description: String,
val birthDate: LocalDate,
val sizeType: SizeType,
val gender: Gender,
val imageUrl: String,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.woowacourse.friendogly.domain.model

data class Pets(
val contents: List<Pet>,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.woowacourse.friendogly.domain.model

enum class SizeType {
SMALL,
MEDIUM,
LARGE,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.woowacourse.friendogly.domain.repository

import com.woowacourse.friendogly.domain.model.Gender
import com.woowacourse.friendogly.domain.model.Pet
import com.woowacourse.friendogly.domain.model.SizeType
import kotlinx.datetime.LocalDate

interface PetRepository {
suspend fun getPetsMine(): Result<List<Pet>>

suspend fun postPet(
name: String,
description: String,
birthday: LocalDate,
sizeType: SizeType,
gender: Gender,
imageUrl: String,
): Result<Pet>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.woowacourse.friendogly.domain.usecase

import com.woowacourse.friendogly.domain.model.Pet
import com.woowacourse.friendogly.domain.repository.PetRepository

class GetPetsMineUseCase(
private val repository: PetRepository,
) {
suspend operator fun invoke(): Result<List<Pet>> = repository.getPetsMine()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.woowacourse.friendogly.domain.usecase

import com.woowacourse.friendogly.domain.model.Gender
import com.woowacourse.friendogly.domain.model.Pet
import com.woowacourse.friendogly.domain.model.SizeType
import com.woowacourse.friendogly.domain.repository.PetRepository
import kotlinx.datetime.LocalDate

class PostPetUseCase(
private val repository: PetRepository,
) {
suspend operator fun invoke(
name: String,
description: String,
birthday: LocalDate,
sizeType: SizeType,
gender: Gender,
imageUrl: String,
): Result<Pet> =
repository.postPet(
name = name,
description = description,
birthday = birthday,
sizeType = sizeType,
gender = gender,
imageUrl = imageUrl,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@ package com.woowacourse.friendogly.presentation.ui.mypage

import androidx.fragment.app.viewModels
import com.woowacourse.friendogly.R
import com.woowacourse.friendogly.application.di.AppModule
import com.woowacourse.friendogly.databinding.FragmentMyPageBinding
import com.woowacourse.friendogly.presentation.base.BaseFragment
import com.woowacourse.friendogly.presentation.base.observeEvent
import com.woowacourse.friendogly.presentation.ui.mypage.adapter.DogProfileAdapter
import com.woowacourse.friendogly.presentation.ui.mypage.adapter.PetProfileAdapter

class MyPageFragment : BaseFragment<FragmentMyPageBinding>(R.layout.fragment_my_page) {
private val viewModel: MyPageViewModel by viewModels()
private val viewModel: MyPageViewModel by viewModels {
MyPageViewModel.factory(getPetsMineUseCase = AppModule.getInstance().getPetsMineUseCase)
}

private val adapter: DogProfileAdapter by lazy { DogProfileAdapter(viewModel) }
private val adapter: PetProfileAdapter by lazy { PetProfileAdapter(viewModel) }

override fun initViewCreated() {
initDataBinding()
Expand All @@ -31,7 +34,11 @@ class MyPageFragment : BaseFragment<FragmentMyPageBinding>(R.layout.fragment_my_
when (action) {
is MyPageNavigationAction.NavigateToSetting -> TODO()
is MyPageNavigationAction.NavigateToDogDetail ->
navigate(MyPageFragmentDirections.actionMyPageFragmentToDogDetailFragment(action.id))
navigate(
MyPageFragmentDirections.actionMyPageFragmentToDogDetailFragment(
action.id,
),
)

is MyPageNavigationAction.NavigateToDogRegister ->
navigate(
Expand All @@ -43,7 +50,12 @@ class MyPageFragment : BaseFragment<FragmentMyPageBinding>(R.layout.fragment_my_
}

viewModel.uiState.observe(viewLifecycleOwner) { uiState ->
adapter.submitList(uiState.dogs)
adapter.submitList(uiState.pets)
}
}

override fun onResume() {
super.onResume()
viewModel.fetchPetMine()
}
}
Loading

0 comments on commit f3b7300

Please sign in to comment.