diff --git a/app/src/main/java/org/sopt/and/data/mapper/todata/UserMapper.kt b/app/src/main/java/org/sopt/and/data/mapper/todata/UserMapper.kt new file mode 100644 index 0000000..2fa8df9 --- /dev/null +++ b/app/src/main/java/org/sopt/and/data/mapper/todata/UserMapper.kt @@ -0,0 +1,23 @@ +package org.sopt.and.data.mapper.todata + +import org.sopt.and.data.remote.model.request.LoginRequestDto +import org.sopt.and.data.remote.model.request.UserInfoUpdateRequestDto +import org.sopt.and.data.remote.model.request.UserRegistrationRequestDto +import org.sopt.and.domain.model.User + +fun User.toUserRegistrationRequestDto(): UserRegistrationRequestDto = UserRegistrationRequestDto( + username = this.username, + password = this.password, + hobby = this.hobby +) + +fun User.toLoginRequestDto(): LoginRequestDto = LoginRequestDto( + username = this.username, + password = this.password +) + + +fun User.toUserInfoUpdateRequestDto(): UserInfoUpdateRequestDto = UserInfoUpdateRequestDto( + hobby = this.hobby, + password = this.password +) \ No newline at end of file diff --git a/app/src/main/java/org/sopt/and/data/mapper/todomain/HobbyResponseDtoMapper.kt b/app/src/main/java/org/sopt/and/data/mapper/todomain/HobbyResponseDtoMapper.kt new file mode 100644 index 0000000..7614ae6 --- /dev/null +++ b/app/src/main/java/org/sopt/and/data/mapper/todomain/HobbyResponseDtoMapper.kt @@ -0,0 +1,8 @@ +package org.sopt.and.data.mapper.todomain + +import org.sopt.and.data.remote.model.response.HobbyResponseDto +import org.sopt.and.domain.model.Hobby + +fun HobbyResponseDto.toDomain(): Hobby = Hobby( + hobby = this.hobby +) \ No newline at end of file diff --git a/app/src/main/java/org/sopt/and/data/mapper/todomain/LoginResponseDtoMapper.kt b/app/src/main/java/org/sopt/and/data/mapper/todomain/LoginResponseDtoMapper.kt new file mode 100644 index 0000000..2179712 --- /dev/null +++ b/app/src/main/java/org/sopt/and/data/mapper/todomain/LoginResponseDtoMapper.kt @@ -0,0 +1,8 @@ +package org.sopt.and.data.mapper.todomain + +import org.sopt.and.data.remote.model.response.LoginResponseDto +import org.sopt.and.domain.model.Token + +fun LoginResponseDto.toDomain(): Token = Token( + token = this.token +) \ No newline at end of file diff --git a/app/src/main/java/org/sopt/and/data/mapper/todomain/UserRegistrationResponseDtoMapper.kt b/app/src/main/java/org/sopt/and/data/mapper/todomain/UserRegistrationResponseDtoMapper.kt new file mode 100644 index 0000000..3fb8702 --- /dev/null +++ b/app/src/main/java/org/sopt/and/data/mapper/todomain/UserRegistrationResponseDtoMapper.kt @@ -0,0 +1,8 @@ +package org.sopt.and.data.mapper.todomain + +import org.sopt.and.data.remote.model.response.UserRegistrationResponseDto +import org.sopt.and.domain.model.UserNo + +fun UserRegistrationResponseDto.toDomain(): UserNo = UserNo( + no = this.no +) \ No newline at end of file diff --git a/app/src/main/java/org/sopt/and/data/remote/util/handleApiResponse.kt b/app/src/main/java/org/sopt/and/data/remote/util/handleApiResponse.kt new file mode 100644 index 0000000..c9d5f2a --- /dev/null +++ b/app/src/main/java/org/sopt/and/data/remote/util/handleApiResponse.kt @@ -0,0 +1,8 @@ +package org.sopt.and.data.remote.util + +import org.sopt.and.data.remote.model.base.ApiResponse + +fun ApiResponse.handleApiResponse(): Result { + return if (this.result != null) Result.success(this.result) + else Result.failure(Exception("No data Available")) +} \ No newline at end of file diff --git a/app/src/main/java/org/sopt/and/data/repositoryimpl/AuthRepositoryImpl.kt b/app/src/main/java/org/sopt/and/data/repositoryimpl/AuthRepositoryImpl.kt new file mode 100644 index 0000000..a69a964 --- /dev/null +++ b/app/src/main/java/org/sopt/and/data/repositoryimpl/AuthRepositoryImpl.kt @@ -0,0 +1,32 @@ +package org.sopt.and.data.repositoryimpl + +import org.sopt.and.data.mapper.todata.toUserRegistrationRequestDto +import org.sopt.and.data.mapper.todomain.toDomain +import org.sopt.and.data.remote.datasource.AuthRemoteDataSource +import org.sopt.and.data.remote.model.request.LoginRequestDto +import org.sopt.and.data.remote.util.handleApiResponse +import org.sopt.and.domain.model.Token +import org.sopt.and.domain.model.User +import org.sopt.and.domain.model.UserNo +import org.sopt.and.domain.repository.AuthRepository +import javax.inject.Inject + +class AuthRepositoryImpl @Inject constructor( + private val authRemoteDataSource: AuthRemoteDataSource +): AuthRepository { + override suspend fun registerUser(user: User): Result { + return runCatching { + authRemoteDataSource.registerUser(userRegistrationRequestDto = user.toUserRegistrationRequestDto()).handleApiResponse().getOrThrow().toDomain() + } + } + + override suspend fun login(username: String, password: String): Result { + return runCatching { + authRemoteDataSource.login(loginRequestDto = LoginRequestDto( + username = username, + password = password + )).handleApiResponse().getOrThrow().toDomain() + } + } + +} \ No newline at end of file diff --git a/app/src/main/java/org/sopt/and/data/repositoryimpl/UserRepositoryImpl.kt b/app/src/main/java/org/sopt/and/data/repositoryimpl/UserRepositoryImpl.kt new file mode 100644 index 0000000..fb9001f --- /dev/null +++ b/app/src/main/java/org/sopt/and/data/repositoryimpl/UserRepositoryImpl.kt @@ -0,0 +1,38 @@ +package org.sopt.and.data.repositoryimpl + +import org.sopt.and.data.mapper.todomain.toDomain +import org.sopt.and.data.remote.datasource.UserRemoteDataSource +import org.sopt.and.data.remote.model.request.UserInfoUpdateRequestDto +import org.sopt.and.data.remote.util.handleApiResponse +import org.sopt.and.domain.model.Hobby +import org.sopt.and.domain.repository.UserRepository +import javax.inject.Inject + +class UserRepositoryImpl @Inject constructor( + private val userRemoteDataSource: UserRemoteDataSource +): UserRepository { + override suspend fun getMyHobby(token: String): Result { + return runCatching { + userRemoteDataSource.getMyHobby(token = token).handleApiResponse().getOrThrow().toDomain() + } + } + + override suspend fun getOthersHobby(token: String, userNo: Int): Result { + return runCatching { + userRemoteDataSource.getOthersHobby(token = token, userNo = userNo).handleApiResponse().getOrThrow().toDomain() + } + } + + override suspend fun updateUserInfo( + token: String, + password: String?, + hobby: String? + ): Result { + return runCatching { + userRemoteDataSource.updateUserInfo(token = token, userInfoUpdateRequestDto = UserInfoUpdateRequestDto( + password = password, + hobby = hobby + )).handleApiResponse().getOrThrow() + } + } +} \ No newline at end of file diff --git a/app/src/main/java/org/sopt/and/domain/User.kt b/app/src/main/java/org/sopt/and/domain/User.kt deleted file mode 100644 index 67756cb..0000000 --- a/app/src/main/java/org/sopt/and/domain/User.kt +++ /dev/null @@ -1,7 +0,0 @@ -package org.sopt.and.domain - -data class User( - val name: String, - val password: String, - val hobby: String -) \ No newline at end of file diff --git a/app/src/main/java/org/sopt/and/domain/model/Hobby.kt b/app/src/main/java/org/sopt/and/domain/model/Hobby.kt new file mode 100644 index 0000000..cff60c9 --- /dev/null +++ b/app/src/main/java/org/sopt/and/domain/model/Hobby.kt @@ -0,0 +1,5 @@ +package org.sopt.and.domain.model + +data class Hobby( + val hobby: String +) \ No newline at end of file diff --git a/app/src/main/java/org/sopt/and/domain/model/Token.kt b/app/src/main/java/org/sopt/and/domain/model/Token.kt new file mode 100644 index 0000000..947b584 --- /dev/null +++ b/app/src/main/java/org/sopt/and/domain/model/Token.kt @@ -0,0 +1,5 @@ +package org.sopt.and.domain.model + +data class Token( + val token: String +) \ No newline at end of file diff --git a/app/src/main/java/org/sopt/and/domain/model/User.kt b/app/src/main/java/org/sopt/and/domain/model/User.kt new file mode 100644 index 0000000..6d0efe2 --- /dev/null +++ b/app/src/main/java/org/sopt/and/domain/model/User.kt @@ -0,0 +1,7 @@ +package org.sopt.and.domain.model + +data class User( + val username: String, + val password: String, + val hobby: String, +) \ No newline at end of file diff --git a/app/src/main/java/org/sopt/and/domain/model/UserNo.kt b/app/src/main/java/org/sopt/and/domain/model/UserNo.kt new file mode 100644 index 0000000..8bad4cf --- /dev/null +++ b/app/src/main/java/org/sopt/and/domain/model/UserNo.kt @@ -0,0 +1,5 @@ +package org.sopt.and.domain.model + +data class UserNo( + val no: Int +) \ No newline at end of file diff --git a/app/src/main/java/org/sopt/and/domain/repository/AuthRepository.kt b/app/src/main/java/org/sopt/and/domain/repository/AuthRepository.kt new file mode 100644 index 0000000..d71c32b --- /dev/null +++ b/app/src/main/java/org/sopt/and/domain/repository/AuthRepository.kt @@ -0,0 +1,10 @@ +package org.sopt.and.domain.repository + +import org.sopt.and.domain.model.Token +import org.sopt.and.domain.model.User +import org.sopt.and.domain.model.UserNo + +interface AuthRepository { + suspend fun registerUser(user: User): Result + suspend fun login(username: String, password: String): Result +} \ No newline at end of file diff --git a/app/src/main/java/org/sopt/and/domain/repository/UserRepository.kt b/app/src/main/java/org/sopt/and/domain/repository/UserRepository.kt new file mode 100644 index 0000000..a69788d --- /dev/null +++ b/app/src/main/java/org/sopt/and/domain/repository/UserRepository.kt @@ -0,0 +1,9 @@ +package org.sopt.and.domain.repository + +import org.sopt.and.domain.model.Hobby + +interface UserRepository { + suspend fun getMyHobby(token: String): Result + suspend fun getOthersHobby(token: String, userNo: Int): Result + suspend fun updateUserInfo(token: String, password: String?, hobby: String?): Result +} \ No newline at end of file