Skip to content

Commit

Permalink
[feat/#10] repository를 구현합니다.
Browse files Browse the repository at this point in the history
  • Loading branch information
SYAAINN committed Dec 2, 2024
1 parent f48e44e commit daf06f5
Show file tree
Hide file tree
Showing 14 changed files with 166 additions and 7 deletions.
23 changes: 23 additions & 0 deletions app/src/main/java/org/sopt/and/data/mapper/todata/UserMapper.kt
Original file line number Diff line number Diff line change
@@ -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
)
Original file line number Diff line number Diff line change
@@ -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
)
Original file line number Diff line number Diff line change
@@ -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
)
Original file line number Diff line number Diff line change
@@ -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
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.sopt.and.data.remote.util

import org.sopt.and.data.remote.model.base.ApiResponse

fun <T> ApiResponse<T>.handleApiResponse(): Result<T> {
return if (this.result != null) Result.success(this.result)
else Result.failure(Exception("No data Available"))
}
Original file line number Diff line number Diff line change
@@ -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<UserNo> {
return runCatching {
authRemoteDataSource.registerUser(userRegistrationRequestDto = user.toUserRegistrationRequestDto()).handleApiResponse().getOrThrow().toDomain()
}
}

override suspend fun login(username: String, password: String): Result<Token> {
return runCatching {
authRemoteDataSource.login(loginRequestDto = LoginRequestDto(
username = username,
password = password
)).handleApiResponse().getOrThrow().toDomain()
}
}

}
Original file line number Diff line number Diff line change
@@ -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<Hobby> {
return runCatching {
userRemoteDataSource.getMyHobby(token = token).handleApiResponse().getOrThrow().toDomain()
}
}

override suspend fun getOthersHobby(token: String, userNo: Int): Result<Hobby> {
return runCatching {
userRemoteDataSource.getOthersHobby(token = token, userNo = userNo).handleApiResponse().getOrThrow().toDomain()
}
}

override suspend fun updateUserInfo(
token: String,
password: String?,
hobby: String?
): Result<Unit> {
return runCatching {
userRemoteDataSource.updateUserInfo(token = token, userInfoUpdateRequestDto = UserInfoUpdateRequestDto(
password = password,
hobby = hobby
)).handleApiResponse().getOrThrow()
}
}
}
7 changes: 0 additions & 7 deletions app/src/main/java/org/sopt/and/domain/User.kt

This file was deleted.

5 changes: 5 additions & 0 deletions app/src/main/java/org/sopt/and/domain/model/Hobby.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.sopt.and.domain.model

data class Hobby(
val hobby: String
)
5 changes: 5 additions & 0 deletions app/src/main/java/org/sopt/and/domain/model/Token.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.sopt.and.domain.model

data class Token(
val token: String
)
7 changes: 7 additions & 0 deletions app/src/main/java/org/sopt/and/domain/model/User.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.sopt.and.domain.model

data class User(
val username: String,
val password: String,
val hobby: String,
)
5 changes: 5 additions & 0 deletions app/src/main/java/org/sopt/and/domain/model/UserNo.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.sopt.and.domain.model

data class UserNo(
val no: Int
)
10 changes: 10 additions & 0 deletions app/src/main/java/org/sopt/and/domain/repository/AuthRepository.kt
Original file line number Diff line number Diff line change
@@ -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<UserNo>
suspend fun login(username: String, password: String): Result<Token>
}
Original file line number Diff line number Diff line change
@@ -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<Hobby>
suspend fun getOthersHobby(token: String, userNo: Int): Result<Hobby>
suspend fun updateUserInfo(token: String, password: String?, hobby: String?): Result<Unit>
}

0 comments on commit daf06f5

Please sign in to comment.