2022-12-12 00:53:53 +01:00
|
|
|
//
|
|
|
|
// GetUserUseCase.swift
|
|
|
|
// Login
|
|
|
|
//
|
|
|
|
// Created by Javier Cicchelli on 12/12/2022.
|
|
|
|
// Copyright © 2022 Röck+Cöde. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import APIService
|
2022-12-12 23:06:56 +01:00
|
|
|
import DataModels
|
2022-12-12 00:53:53 +01:00
|
|
|
import DependencyInjection
|
|
|
|
import Dependencies
|
2022-12-18 16:12:09 +01:00
|
|
|
import KeychainStorage
|
2022-12-12 00:53:53 +01:00
|
|
|
|
2022-12-18 16:12:09 +01:00
|
|
|
public actor GetUserUseCase {
|
2022-12-12 00:53:53 +01:00
|
|
|
|
2022-12-18 16:12:09 +01:00
|
|
|
// MARK: Properties
|
2022-12-12 00:53:53 +01:00
|
|
|
|
2022-12-18 16:12:09 +01:00
|
|
|
private let apiService: APIService
|
|
|
|
|
|
|
|
private var account: Account?
|
2022-12-12 00:53:53 +01:00
|
|
|
|
2022-12-12 23:06:56 +01:00
|
|
|
// MARK: Initialisers
|
2022-12-12 00:53:53 +01:00
|
|
|
|
2022-12-18 16:12:09 +01:00
|
|
|
public init(
|
|
|
|
apiService: APIService,
|
|
|
|
account: Account?
|
|
|
|
) {
|
|
|
|
self.apiService = apiService
|
|
|
|
self.account = account
|
|
|
|
}
|
2022-12-12 00:53:53 +01:00
|
|
|
|
|
|
|
// MARK: Functions
|
|
|
|
|
2022-12-18 16:12:09 +01:00
|
|
|
public func callAsFunction() async throws -> User {
|
|
|
|
guard let account else { throw GetUserError .accountNotFound }
|
|
|
|
|
|
|
|
return try await getUser(
|
|
|
|
username: account.username,
|
|
|
|
password: account.password
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-12-12 23:06:56 +01:00
|
|
|
public func callAsFunction(
|
2022-12-12 00:53:53 +01:00
|
|
|
username: String,
|
|
|
|
password: String
|
2022-12-18 16:12:09 +01:00
|
|
|
) 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
|
2022-12-12 23:06:56 +01:00
|
|
|
) async throws -> User {
|
2022-12-12 00:53:53 +01:00
|
|
|
let me = try await apiService.getUser(
|
|
|
|
credentials: .init(
|
|
|
|
username: username,
|
|
|
|
password: password
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2022-12-12 23:06:56 +01:00
|
|
|
return .init(
|
|
|
|
profile: .init(
|
|
|
|
firstName: me.firstName,
|
|
|
|
lastName: me.lastName
|
2022-12-12 00:53:53 +01:00
|
|
|
),
|
2022-12-12 23:06:56 +01:00
|
|
|
rootFolder: .init(
|
|
|
|
id: me.rootItem.id,
|
|
|
|
name: me.rootItem.name,
|
|
|
|
lastModifiedAt: me.rootItem.lastModifiedAt
|
2022-12-12 00:53:53 +01:00
|
|
|
)
|
|
|
|
)
|
|
|
|
}
|
2022-12-18 16:12:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - Errors
|
2022-12-12 00:53:53 +01:00
|
|
|
|
2022-12-18 16:12:09 +01:00
|
|
|
public enum GetUserError: Error {
|
|
|
|
case accountNotFound
|
2022-12-12 00:53:53 +01:00
|
|
|
}
|