Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

merge :: Files API #100

Merged
merged 6 commits into from
Oct 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import DomainModule
import DataModule
import NetworkModule

public extension AppComponent {
var remoteFilesDataSource: any RemoteFilesDataSource {
RemoteFilesDataSourceStub()
// RemoteFilesDataSourceImpl(keychain: keychain)
}
var filesRepository: any FilesRepository {
FilesRepositoryImpl(remoteFilesDataSource: remoteFilesDataSource)
}
var uploadFileUseCase: any UploadFileUseCase {
UploadFileUseCaseImpl(filesRepository: filesRepository)
}
}
3 changes: 3 additions & 0 deletions Projects/App/Sources/Application/NeedleGenerated.swift
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,9 @@ extension AppComponent: Registration {
localTable["remoteMealDataSource-any RemoteMealDataSource"] = { self.remoteMealDataSource as Any }
localTable["mealRepository-any MealRepository"] = { self.mealRepository as Any }
localTable["fetchMealListUseCase-any FetchMealListUseCase"] = { self.fetchMealListUseCase as Any }
localTable["remoteFilesDataSource-any RemoteFilesDataSource"] = { self.remoteFilesDataSource as Any }
localTable["filesRepository-any FilesRepository"] = { self.filesRepository as Any }
localTable["uploadFileUseCase-any UploadFileUseCase"] = { self.uploadFileUseCase as Any }
localTable["remoteSchoolDataSource-any RemoteSchoolDataSource"] = { self.remoteSchoolDataSource as Any }
localTable["schoolRepository-any SchoolRepository"] = { self.schoolRepository as Any }
localTable["fetchSchoolListUseCase-any FetchSchoolListUseCase"] = { self.fetchSchoolListUseCase as Any }
Expand Down
2 changes: 1 addition & 1 deletion Projects/Services/APIKit/Sources/DmsAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public enum DmsDomain: String {
case losts
case notices
case meals
case images
case files
case schools
}

Expand Down
44 changes: 44 additions & 0 deletions Projects/Services/APIKit/Sources/FilesAPI.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import Foundation
import ErrorModule
import Moya

public enum FilesAPI {
case uploadFile(data: Data)
}

extension FilesAPI: DmsAPI {
public var domain: DmsDomain {
.files
}

public var urlPath: String {
return ""
}

public var method: Moya.Method {
return .post
}

public var task: Moya.Task {
switch self {
case let .uploadFile(data):
return .uploadMultipart([
.init(
provider: .data(data),
name: "file"
)
])
}
}

public var jwtTokenType: JwtTokenType {
.none
}

public var errorMap: [Int: DmsError] {
[
400: .badRequest,
500: .internalServerError
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import Foundation

public struct UploadFileResponseDTO: Decodable {
public let fileURL: String

enum CodingKeys: String, CodingKey {
case fileURL = "file_url"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import Combine
import DomainModule
import ErrorModule
import Foundation
import NetworkModule

public struct FilesRepositoryImpl: FilesRepository {
private let remoteFilesDataSource: any RemoteFilesDataSource

public init(remoteFilesDataSource: any RemoteFilesDataSource) {
self.remoteFilesDataSource = remoteFilesDataSource
}

public func uploadFile(data: Data) -> AnyPublisher<String, DmsError> {
remoteFilesDataSource.uploadFile(data: data)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import Combine
import DomainModule
import ErrorModule
import Foundation

public struct UploadFileUseCaseImpl: UploadFileUseCase {
private let filesRepository: any FilesRepository

public init(filesRepository: any FilesRepository) {
self.filesRepository = filesRepository
}

public func execute(data: Data) -> AnyPublisher<String, DmsError> {
filesRepository.uploadFile(data: data)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import Combine
import ErrorModule
import Foundation

public protocol FilesRepository {
func uploadFile(data: Data) -> AnyPublisher<String, DmsError>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import Combine
import ErrorModule
import Foundation

public protocol UploadFileUseCase {
func execute(data: Data) -> AnyPublisher<String, DmsError>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import APIKit
import Combine
import DataMappingModule
import ErrorModule
import Foundation

public final class RemoteFilesDataSourceImpl: BaseRemoteDataSource<FilesAPI>, RemoteFilesDataSource {
public func uploadFile(data: Data) -> AnyPublisher<String, DmsError> {
request(.uploadFile(data: data), dto: UploadFileResponseDTO.self)
.map(\.fileURL)
.eraseToAnyPublisher()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import Combine
import DataMappingModule
import ErrorModule
import Foundation

public protocol RemoteFilesDataSource {
func uploadFile(data: Data) -> AnyPublisher<String, DmsError>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import Combine
import DataMappingModule
import ErrorModule
import Foundation

public struct RemoteFilesDataSourceStub: RemoteFilesDataSource {
public init() {}

public func uploadFile(data: Data) -> AnyPublisher<String, DmsError> {
Just("https://avatars.githubusercontent.com/u/74440939?v=4")
.setFailureType(to: DmsError.self)
.eraseToAnyPublisher()
}
}