Improved the GetUserUseCase use case by moving some logic to this use case.
This commit is contained in:
parent
76814cfe8b
commit
c25ca4cf0f
@ -10,22 +10,76 @@ import APIService
|
||||
import DataModels
|
||||
import DependencyInjection
|
||||
import Dependencies
|
||||
import KeychainStorage
|
||||
|
||||
public struct GetUserUseCase {
|
||||
public actor GetUserUseCase {
|
||||
|
||||
// MARK: Dependencies
|
||||
// MARK: Properties
|
||||
|
||||
@Dependency(\.apiService) private var apiService
|
||||
private let apiService: APIService
|
||||
|
||||
private var account: Account?
|
||||
|
||||
// MARK: Initialisers
|
||||
|
||||
public init() {}
|
||||
public init(
|
||||
apiService: APIService,
|
||||
account: Account?
|
||||
) {
|
||||
self.apiService = apiService
|
||||
self.account = account
|
||||
}
|
||||
|
||||
// MARK: Functions
|
||||
|
||||
public func callAsFunction() async throws -> User {
|
||||
guard let account else { throw GetUserError .accountNotFound }
|
||||
|
||||
return try await getUser(
|
||||
username: account.username,
|
||||
password: account.password
|
||||
)
|
||||
}
|
||||
|
||||
public func callAsFunction(
|
||||
username: String,
|
||||
password: String
|
||||
) async throws -> User {
|
||||
let user = try await getUser(
|
||||
username: username,
|
||||
password: password
|
||||
)
|
||||
|
||||
account = .init(
|
||||
username: username,
|
||||
password: password
|
||||
)
|
||||
|
||||
return user
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// MARK: - Initialisers
|
||||
|
||||
public extension GetUserUseCase {
|
||||
init() {
|
||||
@Dependency(\.apiService) var apiService
|
||||
@KeychainStorage(key: .KeychainStorage.account) var account: Account?
|
||||
|
||||
self.init(
|
||||
apiService: apiService,
|
||||
account: account
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Helpers
|
||||
|
||||
private extension GetUserUseCase {
|
||||
func getUser(
|
||||
username: String,
|
||||
password: String
|
||||
) async throws -> User {
|
||||
let me = try await apiService.getUser(
|
||||
credentials: .init(
|
||||
@ -46,5 +100,10 @@ public struct GetUserUseCase {
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// MARK: - Errors
|
||||
|
||||
public enum GetUserError: Error {
|
||||
case accountNotFound
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user