From a01a1303a41c757f463f74401f048dde113bb61c Mon Sep 17 00:00:00 2001 From: GeonH0 Date: Thu, 12 Sep 2024 22:45:38 +0900 Subject: [PATCH] =?UTF-8?q?Feat:=20=ED=9A=8C=EC=9B=90=EA=B0=80=EC=9E=85=20?= =?UTF-8?q?=EB=84=A4=ED=8A=B8=EC=9B=8C=ED=81=AC=20=EC=9A=94=EC=B2=AD=20?= =?UTF-8?q?=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Data/Network/DTO/EmptyResponse.swift | 10 +++ .../Data/Network/NetworkService.swift | 49 +++++++++++++++ .../Data/Network/SignUpService.swift | 63 +++++++++++++++++++ .../Data/Repositories/SignUpRepository.swift | 31 +++++++++ 4 files changed, 153 insertions(+) create mode 100644 HomeCafeRecipes/HomeCafeRecipes/Data/Network/DTO/EmptyResponse.swift create mode 100644 HomeCafeRecipes/HomeCafeRecipes/Data/Network/SignUpService.swift create mode 100644 HomeCafeRecipes/HomeCafeRecipes/Data/Repositories/SignUpRepository.swift diff --git a/HomeCafeRecipes/HomeCafeRecipes/Data/Network/DTO/EmptyResponse.swift b/HomeCafeRecipes/HomeCafeRecipes/Data/Network/DTO/EmptyResponse.swift new file mode 100644 index 0000000..ed18218 --- /dev/null +++ b/HomeCafeRecipes/HomeCafeRecipes/Data/Network/DTO/EmptyResponse.swift @@ -0,0 +1,10 @@ +// +// EmptyResponse.swift +// HomeCafeRecipes +// +// Created by 김건호 on 9/11/24. +// + +import Foundation + +struct EmptyResponse: Decodable {} diff --git a/HomeCafeRecipes/HomeCafeRecipes/Data/Network/NetworkService.swift b/HomeCafeRecipes/HomeCafeRecipes/Data/Network/NetworkService.swift index 1baf3dc..47a0f82 100644 --- a/HomeCafeRecipes/HomeCafeRecipes/Data/Network/NetworkService.swift +++ b/HomeCafeRecipes/HomeCafeRecipes/Data/Network/NetworkService.swift @@ -13,6 +13,7 @@ import RxSwift protocol NetworkService { func getRequest(url: URL, responseType: T.Type) -> Single func postRequest(url: URL, parameters: [String: Any], imageDatas: [Data], responseType: T.Type) -> Single + func postJsonRequest(url: URL, parameters:[String: Any], responseType: T.Type) -> Single } class BaseNetworkService: NetworkService { @@ -98,4 +99,52 @@ class BaseNetworkService: NetworkService { } } } + + func postJsonRequest( + url: URL, + parameters: [String: Any], + responseType: T.Type + ) -> Single { + return Single.create { single in + // JSON으로 변환 + guard let httpBody = try? JSONSerialization.data(withJSONObject: parameters, options: []) else { + single(.failure(NSError(domain: "", code: -1, userInfo: [NSLocalizedDescriptionKey: "JSON 인코딩 실패"]))) + return Disposables.create() + } + + var request = URLRequest(url: url) + request.httpMethod = "POST" + request.setValue("application/json", forHTTPHeaderField: "Content-Type") + request.httpBody = httpBody + + let task = URLSession.shared.dataTask(with: request) { data, response, error in + if let error = error { + single(.failure(error)) + } else if let httpResponse = response as? HTTPURLResponse, httpResponse.statusCode != 200 { + let statusCode = httpResponse.statusCode + let responseString = data.flatMap { String(data: $0, encoding: .utf8) } ?? "No response data" + let error = NSError( + domain: "", + code: statusCode, + userInfo: [NSLocalizedDescriptionKey: "HTTP \(statusCode): \(responseString)"] + ) + single(.failure(error)) + } else if let data = data { + do { + let decoder = JSONDecoder() + decoder.dateDecodingStrategy = .iso8601 + let responseObject = try decoder.decode(T.self, from: data) + single(.success(responseObject)) + } catch let decodingError { + single(.failure(decodingError)) + } + } + } + task.resume() + + return Disposables.create { + task.cancel() + } + } + } } diff --git a/HomeCafeRecipes/HomeCafeRecipes/Data/Network/SignUpService.swift b/HomeCafeRecipes/HomeCafeRecipes/Data/Network/SignUpService.swift new file mode 100644 index 0000000..f8056b4 --- /dev/null +++ b/HomeCafeRecipes/HomeCafeRecipes/Data/Network/SignUpService.swift @@ -0,0 +1,63 @@ +// +// SignUpService.swift +// HomeCafeRecipes +// +// Created by 김건호 on 9/11/24. +// + +import Foundation + +import RxSwift + +protocol SignUpService { + func signUp( + userNickName: String, + userID: String, + password: String + ) -> Single +} + +final class SignUpServiceImpl: SignUpService { + private let networkService: NetworkService + + init(networkService: NetworkService) { + self.networkService = networkService + } + + private func makeURL(endpoint: String) -> URL { + return APIConfig().baseURL.appendingPathComponent(endpoint) + } + + func signUp( + userNickName: String, + userID: String, + password: String + ) -> Single { + let url = makeURL(endpoint: "auth/register") + + let parameters: [String: Any] = [ + "email": userID, + "password": password, + "nickname": userNickName + ] + + return networkService.postJsonRequest( + url: url, + parameters: parameters, + responseType: NetworkResponseDTO.self + ) + .flatMap { response in + if response.statusCode == 200 { + // 성공 시 data가 null이더라도 성공 처리 + return .just(()) + } else { + // statusCode가 200이 아니면 에러 처리 + return .error(NSError(domain: "SignUpError", code: response.statusCode, userInfo: [NSLocalizedDescriptionKey: response.message])) + } + } + .catch { error in + // 네트워크 또는 서버에서 에러가 발생했을 경우 처리 + return .error(error) + } + } +} diff --git a/HomeCafeRecipes/HomeCafeRecipes/Data/Repositories/SignUpRepository.swift b/HomeCafeRecipes/HomeCafeRecipes/Data/Repositories/SignUpRepository.swift new file mode 100644 index 0000000..a3c0925 --- /dev/null +++ b/HomeCafeRecipes/HomeCafeRecipes/Data/Repositories/SignUpRepository.swift @@ -0,0 +1,31 @@ +// +// SignUpRepository.swift +// HomeCafeRecipes +// +// Created by 김건호 on 9/10/24. +// + +import UIKit + +import RxSwift + +protocol SignUpRepository { + func signUp(userNickName: String, userID: String, password: String) -> Single +} + +final class SignUpRepositoryImpl: SignUpRepository { + + private let SignUpService: SignUpService + + init(SignUpService: SignUpService) { + self.SignUpService = SignUpService + } + + func signUp(userNickName: String, userID: String, password: String) -> Single { + return SignUpService.signUp( + userNickName: userNickName, + userID: userID, + password: password + ) + } +}