From 8e1649bce8600b6c0f572ad9c11e5351af20c98f Mon Sep 17 00:00:00 2001 From: baegteun Date: Thu, 27 Oct 2022 13:22:12 +0900 Subject: [PATCH 1/5] files API --- Projects/Services/APIKit/Sources/DmsAPI.swift | 2 +- .../Services/APIKit/Sources/FilesAPI.swift | 44 +++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 Projects/Services/APIKit/Sources/FilesAPI.swift diff --git a/Projects/Services/APIKit/Sources/DmsAPI.swift b/Projects/Services/APIKit/Sources/DmsAPI.swift index 1fc9be4b..09f14ddc 100644 --- a/Projects/Services/APIKit/Sources/DmsAPI.swift +++ b/Projects/Services/APIKit/Sources/DmsAPI.swift @@ -28,7 +28,7 @@ public enum DmsDomain: String { case losts case notices case meals - case images + case files case schools } diff --git a/Projects/Services/APIKit/Sources/FilesAPI.swift b/Projects/Services/APIKit/Sources/FilesAPI.swift new file mode 100644 index 00000000..eb2a6786 --- /dev/null +++ b/Projects/Services/APIKit/Sources/FilesAPI.swift @@ -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 + ] + } +} From a52bc58546bccdb975c87f95f329d6e122a90b00 Mon Sep 17 00:00:00 2001 From: baegteun Date: Thu, 27 Oct 2022 13:27:33 +0900 Subject: [PATCH 2/5] feat :: RemoteFilesDataSourceImpl --- .../Files/Response/UploadFileResponseDTO.swift | 9 +++++++++ .../Remote/Impl/RemoteFilesDataSourceImpl.swift | 13 +++++++++++++ .../Files/Remote/RemoteFilesDataSource.swift | 8 ++++++++ 3 files changed, 30 insertions(+) create mode 100644 Projects/Services/DataMappingModule/Sources/Files/Response/UploadFileResponseDTO.swift create mode 100644 Projects/Services/NetworkModule/Sources/Files/Remote/Impl/RemoteFilesDataSourceImpl.swift create mode 100644 Projects/Services/NetworkModule/Sources/Files/Remote/RemoteFilesDataSource.swift diff --git a/Projects/Services/DataMappingModule/Sources/Files/Response/UploadFileResponseDTO.swift b/Projects/Services/DataMappingModule/Sources/Files/Response/UploadFileResponseDTO.swift new file mode 100644 index 00000000..c0dc08e8 --- /dev/null +++ b/Projects/Services/DataMappingModule/Sources/Files/Response/UploadFileResponseDTO.swift @@ -0,0 +1,9 @@ +import Foundation + +public struct UploadFileResponseDTO: Decodable { + public let fileURL: String + + enum CodingKeys: String, CodingKey { + case fileURL = "file_url" + } +} diff --git a/Projects/Services/NetworkModule/Sources/Files/Remote/Impl/RemoteFilesDataSourceImpl.swift b/Projects/Services/NetworkModule/Sources/Files/Remote/Impl/RemoteFilesDataSourceImpl.swift new file mode 100644 index 00000000..fe1b4409 --- /dev/null +++ b/Projects/Services/NetworkModule/Sources/Files/Remote/Impl/RemoteFilesDataSourceImpl.swift @@ -0,0 +1,13 @@ +import APIKit +import Combine +import DataMappingModule +import ErrorModule +import Foundation + +public final class RemoteFilesDataSourceImpl: BaseRemoteDataSource, RemoteFilesDataSource { + public func uploadFile(data: Data) -> AnyPublisher { + request(.uploadFile(data: data), dto: UploadFileResponseDTO.self) + .map(\.fileURL) + .eraseToAnyPublisher() + } +} diff --git a/Projects/Services/NetworkModule/Sources/Files/Remote/RemoteFilesDataSource.swift b/Projects/Services/NetworkModule/Sources/Files/Remote/RemoteFilesDataSource.swift new file mode 100644 index 00000000..34d7a401 --- /dev/null +++ b/Projects/Services/NetworkModule/Sources/Files/Remote/RemoteFilesDataSource.swift @@ -0,0 +1,8 @@ +import Combine +import DataMappingModule +import ErrorModule +import Foundation + +public protocol RemoteFilesDataSource { + func uploadFile(data: Data) -> AnyPublisher +} From bc4d279e264d51e12da4eea619eed51f18bece48 Mon Sep 17 00:00:00 2001 From: baegteun Date: Thu, 27 Oct 2022 13:31:34 +0900 Subject: [PATCH 3/5] feat :: UploadFileUseCaseImpl --- .../Repositories/FilesRepositoryImpl.swift | 17 +++++++++++++++++ .../Files/UseCases/UploadFileUseCaseImpl.swift | 16 ++++++++++++++++ .../Files/Repository/FilesRepository.swift | 7 +++++++ .../Files/UseCases/UploadFileUseCase.swift | 7 +++++++ 4 files changed, 47 insertions(+) create mode 100644 Projects/Services/DataModule/Sources/Files/Repositories/FilesRepositoryImpl.swift create mode 100644 Projects/Services/DataModule/Sources/Files/UseCases/UploadFileUseCaseImpl.swift create mode 100644 Projects/Services/DomainModule/Sources/Files/Repository/FilesRepository.swift create mode 100644 Projects/Services/DomainModule/Sources/Files/UseCases/UploadFileUseCase.swift diff --git a/Projects/Services/DataModule/Sources/Files/Repositories/FilesRepositoryImpl.swift b/Projects/Services/DataModule/Sources/Files/Repositories/FilesRepositoryImpl.swift new file mode 100644 index 00000000..a5aa851b --- /dev/null +++ b/Projects/Services/DataModule/Sources/Files/Repositories/FilesRepositoryImpl.swift @@ -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 { + remoteFilesDataSource.uploadFile(data: data) + } +} diff --git a/Projects/Services/DataModule/Sources/Files/UseCases/UploadFileUseCaseImpl.swift b/Projects/Services/DataModule/Sources/Files/UseCases/UploadFileUseCaseImpl.swift new file mode 100644 index 00000000..838c0ea3 --- /dev/null +++ b/Projects/Services/DataModule/Sources/Files/UseCases/UploadFileUseCaseImpl.swift @@ -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 { + filesRepository.uploadFile(data: data) + } +} diff --git a/Projects/Services/DomainModule/Sources/Files/Repository/FilesRepository.swift b/Projects/Services/DomainModule/Sources/Files/Repository/FilesRepository.swift new file mode 100644 index 00000000..b6080fc6 --- /dev/null +++ b/Projects/Services/DomainModule/Sources/Files/Repository/FilesRepository.swift @@ -0,0 +1,7 @@ +import Combine +import ErrorModule +import Foundation + +public protocol FilesRepository { + func uploadFile(data: Data) -> AnyPublisher +} diff --git a/Projects/Services/DomainModule/Sources/Files/UseCases/UploadFileUseCase.swift b/Projects/Services/DomainModule/Sources/Files/UseCases/UploadFileUseCase.swift new file mode 100644 index 00000000..cad32304 --- /dev/null +++ b/Projects/Services/DomainModule/Sources/Files/UseCases/UploadFileUseCase.swift @@ -0,0 +1,7 @@ +import Combine +import ErrorModule +import Foundation + +public protocol UploadFileUseCase { + func execute(data: Data) -> AnyPublisher +} From 6b81a513c3b5aa770d8e6e96c4b124ac253a9448 Mon Sep 17 00:00:00 2001 From: baegteun Date: Thu, 27 Oct 2022 13:34:27 +0900 Subject: [PATCH 4/5] files DI --- .../Application/DI/Files/AppComponent+Files.swift | 15 +++++++++++++++ .../App/Sources/Application/NeedleGenerated.swift | 3 +++ 2 files changed, 18 insertions(+) create mode 100644 Projects/App/Sources/Application/DI/Files/AppComponent+Files.swift diff --git a/Projects/App/Sources/Application/DI/Files/AppComponent+Files.swift b/Projects/App/Sources/Application/DI/Files/AppComponent+Files.swift new file mode 100644 index 00000000..3a371e04 --- /dev/null +++ b/Projects/App/Sources/Application/DI/Files/AppComponent+Files.swift @@ -0,0 +1,15 @@ +import DomainModule +import DataModule +import NetworkModule + +public extension AppComponent { + var remoteFilesDataSource: any RemoteFilesDataSource { + RemoteFilesDataSourceImpl(keychain: keychain) + } + var filesRepository: any FilesRepository { + FilesRepositoryImpl(remoteFilesDataSource: remoteFilesDataSource) + } + var uploadFileUseCase: any UploadFileUseCase { + UploadFileUseCaseImpl(filesRepository: filesRepository) + } +} diff --git a/Projects/App/Sources/Application/NeedleGenerated.swift b/Projects/App/Sources/Application/NeedleGenerated.swift index 9db8dc3f..1944ce73 100644 --- a/Projects/App/Sources/Application/NeedleGenerated.swift +++ b/Projects/App/Sources/Application/NeedleGenerated.swift @@ -123,6 +123,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 } From 00ec0e51eb960143668ad459162891c129f73cae Mon Sep 17 00:00:00 2001 From: baegteun Date: Thu, 27 Oct 2022 13:37:12 +0900 Subject: [PATCH 5/5] =?UTF-8?q?feat=20::=20=EC=A7=80=EA=B8=88=EC=9D=80=20I?= =?UTF-8?q?mpl=EB=8C=80=EC=8B=A0=20Stub?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Application/DI/Files/AppComponent+Files.swift | 3 ++- .../Remote/Stub/RemoteFilesDataSourceStub.swift | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 Projects/Services/NetworkModule/Sources/Files/Remote/Stub/RemoteFilesDataSourceStub.swift diff --git a/Projects/App/Sources/Application/DI/Files/AppComponent+Files.swift b/Projects/App/Sources/Application/DI/Files/AppComponent+Files.swift index 3a371e04..61f36644 100644 --- a/Projects/App/Sources/Application/DI/Files/AppComponent+Files.swift +++ b/Projects/App/Sources/Application/DI/Files/AppComponent+Files.swift @@ -4,7 +4,8 @@ import NetworkModule public extension AppComponent { var remoteFilesDataSource: any RemoteFilesDataSource { - RemoteFilesDataSourceImpl(keychain: keychain) + RemoteFilesDataSourceStub() +// RemoteFilesDataSourceImpl(keychain: keychain) } var filesRepository: any FilesRepository { FilesRepositoryImpl(remoteFilesDataSource: remoteFilesDataSource) diff --git a/Projects/Services/NetworkModule/Sources/Files/Remote/Stub/RemoteFilesDataSourceStub.swift b/Projects/Services/NetworkModule/Sources/Files/Remote/Stub/RemoteFilesDataSourceStub.swift new file mode 100644 index 00000000..0f8d033d --- /dev/null +++ b/Projects/Services/NetworkModule/Sources/Files/Remote/Stub/RemoteFilesDataSourceStub.swift @@ -0,0 +1,14 @@ +import Combine +import DataMappingModule +import ErrorModule +import Foundation + +public struct RemoteFilesDataSourceStub: RemoteFilesDataSource { + public init() {} + + public func uploadFile(data: Data) -> AnyPublisher { + Just("https://avatars.githubusercontent.com/u/74440939?v=4") + .setFailureType(to: DmsError.self) + .eraseToAnyPublisher() + } +}