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

레시피 업로드 Domain, Data 영역을 정의해 보았습니다. #18

Merged
merged 13 commits into from
Jul 24, 2024
60 changes: 60 additions & 0 deletions HomeCafeRecipes/HomeCafeRecipes/Data/Network/NetworkService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@
//

import Foundation
import UIKit

import RxSwift

protocol NetworkService {
func getRequest<T: Decodable>(url: URL, responseType: T.Type) -> Single<T>
func postRequest<T: Decodable>(url: URL, parameters: [String: Any], imageDatas: [Data], responseType: T.Type) -> Single<T>
}

class BaseNetworkService: NetworkService {
Expand Down Expand Up @@ -39,4 +42,61 @@ class BaseNetworkService: NetworkService {
}
}
}

func postRequest<T: Decodable>(
url: URL, parameters: [String: Any],
imageDatas: [Data],
responseType: T.Type
)
-> Single<T> {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

여긴 왜 줄바꿈하셨나요?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

어떤식으로 줄바꿈 해야할지 몰라서 저런식으로 한건데 저 부분은 50번째 줄로 올리겠습니다!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[ed32a83] 수정했습니다

return Single.create { single in
var formDataRequest = MultipartFormDataRequest(url: url)

for (key, value) in parameters {
formDataRequest.addTextField(named: key, value: String(describing: value))
}

for (index, imageData) in imageDatas.enumerated() {
let filename = "image\(index).jpg"
formDataRequest.addDataField(
named: "recipeImgUrls",
data: imageData,
filename: filename,
mimeType: "image/jpeg"
)
}

formDataRequest.finalize()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for문 내에서 하지 않고 밖에서 하는 이유가 있을까요?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

finalize는 전체 요청이 완성된후 1번만 호출되어야 하기 떄문입니다

let request = formDataRequest.asURLRequest()

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()
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
//
// AddRecipeInteractor.swift
// HomeCafeRecipes
//
// Created by 김건호 on 7/1/24.
//

import Foundation
import UIKit

import RxSwift

protocol AddRecipeInteractorDelegate: AnyObject {
func didLoadRecipeData(viewModel: AddRecipeViewModel)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

didLoadRecipe 정도로 해도 괜찮을 것 같아요. viewModel을 넘겨서 구성하는데 너무 도메인적인 네이밍 같아요.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[58b4cc8] 수정했습니다

}

protocol AddRecipeInteractor {
func saveRecipe(userID: Int, recipeType: RecipeType) -> Single<Result<Recipe, AddRecipeError>>
func updateRecipeTitle(_ title: String)
func updateRecipeDescription(_ description: String)
func addRecipeImage(_ image: UIImage)
func removeRecipeImage(at index: Int)
func loadRecipeData()
}

class AddRecipeInteractorImpl: AddRecipeInteractor {
private var recipeImages: [UIImage] = []
private var recipeTitle: String = ""
private var recipeDescription: String = ""

weak var delegate: AddRecipeInteractorDelegate?

private let saveRecipeUseCase: AddRecipeUseCase

init(saveRecipeUseCase: AddRecipeUseCase) {
self.saveRecipeUseCase = saveRecipeUseCase
}

func saveRecipe(userID: Int, recipeType: RecipeType) -> Single<Result<Recipe, AddRecipeError>> {
return saveRecipeUseCase.execute(
userID: userID,
recipeType: recipeType.rawValue,
title: recipeTitle,
description: recipeDescription,
images: recipeImages
)
}

func updateRecipeTitle(_ title: String) {
self.recipeTitle = title
}

func updateRecipeDescription(_ description: String) {
self.recipeDescription = description
}

func addRecipeImage(_ image: UIImage) {
self.recipeImages.append(image)
}

func removeRecipeImage(at index: Int) {
self.recipeImages.remove(at: index)
}

func loadRecipeData() {
let viewModel = AddRecipeViewModel(images: recipeImages, title: recipeTitle, description: recipeDescription)
delegate?.didLoadRecipeData(viewModel: viewModel)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
//
// SaveRecipeUseCase.swift
// HomeCafeRecipes
//
// Created by 김건호 on 7/1/24.
//

import UIKit

import RxSwift

protocol AddRecipeUseCase {
func execute(
userID: Int,
recipeType: String,
title: String,
description: String,
images: [UIImage]
) -> Single<Result<Recipe, AddRecipeError>>
}

class AddRecipeUseCaseImpl: AddRecipeUseCase {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

final class 검토부탁해요

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[54cdfca] 수정했습니다

private let repository: AddRecipeRepository

init(repository: AddRecipeRepository) {
self.repository = repository
}

func execute(
userID: Int,
recipeType: String,
title: String,
description: String,
images: [UIImage]
) -> Single<Result<Recipe, AddRecipeError>> {

guard !images.isEmpty else {
return .just(.failure(.noImages))
}

guard !title.isBlank else {
return .just(.failure(.titleIsEmpty))
}

guard description.count > 10 else {
return .just(.failure(.descriptionTooShort))
}
Comment on lines +37 to +47

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

좋네요 👍


return repository.saveRecipe(
userID: userID,
recipeType: recipeType,
title: title,
description: description,
images: images
)
.map { .success($0) }
.catch { .just(.failure(.genericError($0))) }
}
}