2021-12-23 13:10:33 +00:00
|
|
|
//
|
|
|
|
// File.swift
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// Created by William Brawner on 12/22/21.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
|
|
|
|
public struct User: Codable, Equatable, Hashable {
|
|
|
|
public let id: String
|
|
|
|
public let username: String
|
2022-06-26 16:00:42 +00:00
|
|
|
public let password: String?
|
2021-12-23 13:10:33 +00:00
|
|
|
public let email: String?
|
|
|
|
public let avatar: String?
|
2022-06-26 16:00:42 +00:00
|
|
|
|
|
|
|
public init(id: String, username: String, email: String?, password: String?, avatar: String?) {
|
|
|
|
self.id = id
|
|
|
|
self.username = username
|
|
|
|
self.email = email
|
|
|
|
self.password = password
|
|
|
|
self.avatar = avatar
|
|
|
|
}
|
|
|
|
|
|
|
|
public func copy(
|
|
|
|
username: String? = nil,
|
|
|
|
email: String? = nil,
|
|
|
|
password: String? = nil,
|
|
|
|
avatar: String? = nil
|
|
|
|
) -> User {
|
|
|
|
return User(
|
|
|
|
id: self.id,
|
|
|
|
username: username ?? self.username,
|
|
|
|
email: email ?? self.email,
|
|
|
|
password: password ?? self.password,
|
|
|
|
avatar: avatar ?? self.avatar
|
|
|
|
)
|
|
|
|
}
|
2021-12-23 13:10:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public struct LoginRequest: Codable {
|
|
|
|
public let username: String
|
|
|
|
public let password: String
|
2022-06-26 16:00:42 +00:00
|
|
|
|
|
|
|
public init(username: String, password: String) {
|
|
|
|
self.username = username
|
|
|
|
self.password = password
|
|
|
|
}
|
2021-12-23 13:10:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public struct LoginResponse: Codable {
|
|
|
|
public let token: String
|
|
|
|
public let expiration: String
|
|
|
|
public let userId: String
|
2022-06-26 16:00:42 +00:00
|
|
|
|
|
|
|
public init(token: String, expiration: String, userId: String) {
|
|
|
|
self.token = token
|
|
|
|
self.expiration = expiration
|
|
|
|
self.userId = userId
|
|
|
|
}
|
2021-12-23 13:10:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public struct RegistrationRequest: Codable {
|
|
|
|
public let username: String
|
|
|
|
public let email: String
|
|
|
|
public let password: String
|
2022-06-26 16:00:42 +00:00
|
|
|
|
|
|
|
public init(username: String, email: String, password: String) {
|
|
|
|
self.username = username
|
|
|
|
self.email = email
|
|
|
|
self.password = password
|
|
|
|
}
|
2021-12-23 13:10:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public protocol UserRepository {
|
|
|
|
func getUser(_ id: String) async throws -> User
|
|
|
|
func searchUsers(_ withUsername: String) async throws -> [User]
|
|
|
|
func login(username: String, password: String) async throws -> LoginResponse
|
|
|
|
func register(username: String, email: String, password: String) async throws -> User
|
|
|
|
}
|