diff --git a/HomeCafeRecipes/HomeCafeRecipes/Data/Network/CheckNicknameService.swift b/HomeCafeRecipes/HomeCafeRecipes/Data/Network/CheckNicknameService.swift new file mode 100644 index 0000000..de468e9 --- /dev/null +++ b/HomeCafeRecipes/HomeCafeRecipes/Data/Network/CheckNicknameService.swift @@ -0,0 +1,49 @@ +// +// CheckNicknameService.swift +// HomeCafeRecipes +// +// Created by 김건호 on 1/6/25. +// + +import Foundation + +import RxSwift + +protocol CheckNicknameService { + func checkNickname(nickname: String) -> Single +} + +final class CheckNicknameServiceImpl: CheckNicknameService { + private let network : BaseNetworkService + + init(network: BaseNetworkService) { + self.network = network + } + + private func makeURL(ednpoint: String) -> URL { + return APIConfig().baseURL.appendingPathComponent(ednpoint) + } + + func checkNickname(nickname: String) -> Single { + var components = URLComponents(url: makeURL(ednpoint: "verification/checkNickname"), resolvingAgainstBaseURL: false) + components?.queryItems = [ + URLQueryItem(name: "nickname", value: nickname), + ] + + guard let urlWithQuery = components?.url else { + return Single.error(NSError(domain: "Invalid URL", code: -1, userInfo: nil)) + } + + return network.getRequest( + url: urlWithQuery, + responseType: NetworkResponseDTO.self + ) + .map { $0.data.isDuplicate } + .do(onSuccess: { isDuplicate in + print("Is Duplicate: \(isDuplicate)") + print(nickname) + }, onError: { error in + print("Error occurred: \(error.localizedDescription)") + }) + } +} diff --git a/HomeCafeRecipes/HomeCafeRecipes/Data/Network/DTO/CheckNicknameDTO.swift b/HomeCafeRecipes/HomeCafeRecipes/Data/Network/DTO/CheckNicknameDTO.swift new file mode 100644 index 0000000..d76ff31 --- /dev/null +++ b/HomeCafeRecipes/HomeCafeRecipes/Data/Network/DTO/CheckNicknameDTO.swift @@ -0,0 +1,12 @@ +// +// CheckNicknameDTO.swift +// HomeCafeRecipes +// +// Created by 김건호 on 1/6/25. +// + +import Foundation + +struct CheckNicknameDTO: Decodable { + let isDuplicate: Bool +} diff --git a/HomeCafeRecipes/HomeCafeRecipes/Data/Repositories/CheckNicknameRepository.swift b/HomeCafeRecipes/HomeCafeRecipes/Data/Repositories/CheckNicknameRepository.swift new file mode 100644 index 0000000..4173689 --- /dev/null +++ b/HomeCafeRecipes/HomeCafeRecipes/Data/Repositories/CheckNicknameRepository.swift @@ -0,0 +1,25 @@ +// +// CheckNicknameRepository.swift +// HomeCafeRecipes +// +// Created by 김건호 on 1/6/25. +// + +import RxSwift + +protocol CheckNicknameRepository { + func checkNicknameAvailability(nickname: String) -> Single +} + +// Repository Implementation +final class CheckNicknameRepositoryImpl: CheckNicknameRepository { + private let service: CheckNicknameService + + init(service: CheckNicknameService) { + self.service = service + } + + func checkNicknameAvailability(nickname: String) -> Single { + return service.checkNickname(nickname: nickname) + } +}