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 영역을 정의해 보았습니다. #13

Merged
merged 14 commits into from
Jul 11, 2024
Merged
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//
// RecipeDetailDTO.swift
// HomeCafeRecipes
//
// Created by 김건호 on 6/29/24.
//

import Foundation

struct RecipeDetailDTO: Decodable {

let id: Int
let type: String
let name: String
let description: String
let likesCount: Int
let createdAt: String
let writer: UserDTO
let imageUrls: [RecipeImageDTO]
let isLikedByCurrentUser: Bool

enum CodingKeys: String, CodingKey {
case id = "recipeId"
case type = "recipeType"
case name = "recipeName"
case description = "recipeDescription"
case likesCount = "recipeLikesCnt"
case createdAt = "createdAt"
case writer = "writer"
case imageUrls = "recipeImgUrls"
case isLikedByCurrentUser = "isLiked"
}
}

extension RecipeDetailDTO {
func toDomain() -> Recipe {
return Recipe(
id: id,
type: RecipeType(rawValue: type) ?? .coffee,
name: name,
description: description,
writer: writer.toDomain(),
imageUrls: imageUrls.map { $0.recipeImageUrl },
isLikedByCurrentUser: isLikedByCurrentUser,
likeCount: likesCount,
createdAt: DateFormatter.iso8601.date(from: createdAt) ?? Date()
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//
// RecipeDetailFetchService.swift
// HomeCafeRecipes
//
// Created by 김건호 on 6/26/24.
//

import Foundation
import RxSwift

protocol RecipeDetailFetchService {
func fetchRecipeDetail(recipeId: Int) -> Single<Recipe>

Choose a reason for hiding this comment

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

Suggested change
func fetchRecipeDetail(recipeId: Int) -> Single<Recipe>
func fetchRecipeDetail(recipeID: Int) -> Single<Recipe>

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.

[6178184] 수정했습니다

}

class RecipeDetailFetchServiceImpl: RecipeDetailFetchService {

Choose a reason for hiding this comment

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

여기엔 final 필요 없을까요?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

필요 한거 같아요 final 추가 하겠습니다

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

[e117629] 수정했습니다

private let networkService: NetworkService
private static let baseURL: URL = URL(string: "https://meog0.store/api")!

init(networkService: NetworkService) {
self.networkService = networkService
}

private func makeURL(recipeId: Int) -> URL? {
return RecipeDetailFetchServiceImpl.baseURL.appendingPathComponent("recipes/\(recipeId)")
}

func fetchRecipeDetail(recipeId: Int) -> Single<Recipe> {
guard let URL = makeURL(recipeId: recipeId) else {
return Single.error(NSError(domain: "URLComponentsError", code: -1, userInfo: [NSLocalizedDescriptionKey: "Invalid URL"]))

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.

[8f79c55],[c91393f] 정의후 적용시켰습니다

}
return networkService.getRequest(url: URL, responseType: NetworkResponseDTO<RecipeDetailDTO>.self)
.map { $0.data.toDomain() }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//
// RecipeDetailRepository.swift
// HomeCafeRecipes
//
// Created by 김건호 on 6/26/24.
//

import Foundation
import RxSwift

protocol RecipeDetailRepository {

Choose a reason for hiding this comment

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

RecipeDetailFetchService 랑 분리한 이유가 있나요..?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

RecipeDetailFetchService는 네트워크 통신만을 담당하고 RecipeDetailRepository는 데이터 접근 로직을 추상화 할수 있습니다.

Choose a reason for hiding this comment

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

사실상 서비스에서는 url을 만드는 것 외에 복잡한 통신로직은 없는 것 같은데 이에 비해 레이어가 많은 것 같아요.
어떻게 생각하시나요~?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

[cda676a] service를 삭제후 repository에 service가 하는역할을 추가했습니다

func fetchRecipeDetail(recipeID: Int) -> Single<Recipe>
}

class FeedListRepositoryImpl: RecipeDetailRepository {
private let networkService: RecipeDetailFetchService

init(networkService: RecipeDetailFetchService) {
self.networkService = networkService
}

func fetchRecipeDetail(recipeID: Int) -> Single<Recipe> {
return networkService.fetchRecipeDetail(recipeId: recipeID)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
//
// RecipeDetailInteractor.swift
// HomeCafeRecipes
//
// Created by 김건호 on 6/27/24.
//

import Foundation
import RxSwift

protocol RecipeDetailInteractorDelegate: AnyObject {
func fetchedRecipe(result: Result<Recipe, Error>)
}

protocol InputRecipeDetailInteractor {
func viewDidLoad()
}
Comment on lines +15 to +17

Choose a reason for hiding this comment

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

InputRecipeDetailInteractor -> RecipeDetailInteractor

RecipeDetailInteractorImpl: RecipeDetailInteractor
// vc에서 행동에 따른 비즈니스 로직 호출 ex) viewDidLoad
// 비즈니스 로직 -> return 도메인 모델 -> delegate로 vc에 전달 -> mapper -> return 뷰 모델 -> 뷰 업데이트

output 빼고


protocol OutputRecipeDetailInteractor {
var recipe: Observable<Result<Recipe, Error>> { get }
}
Comment on lines +19 to +21

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.

recipeDetailSubject를 Observable로 반환함으로써 외부에서 데이터를 읽기 전용으로 사용할 수 있습니다


class RecipeDetailInteractor: InputRecipeDetailInteractor, OutputRecipeDetailInteractor {

private let fetchRecipeDetailUseCase: FetchRecipeDetailUseCase
private let recipeID: Int
private let disposeBag = DisposeBag()
private let recipeDetailSubject = PublishSubject<Result<Recipe, Error>>()
weak var delegate: RecipeDetailInteractorDelegate?

var recipe: Observable<Result<Recipe, Error>> {
return recipeDetailSubject.asObservable()
}

init(fetchRecipeDetailUseCase: FetchRecipeDetailUseCase, recipeID: Int) {

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.

[9ba8620] 수정했습니다

self.fetchRecipeDetailUseCase = fetchRecipeDetailUseCase
self.recipeID = recipeID
}

func setDelegate(_ delegate: RecipeDetailInteractorDelegate) {
self.delegate = delegate
bindOutputs()
}

private func bindOutputs() {
recipe
.subscribe(onNext: { [weak self] result in
self?.delegate?.fetchedRecipe(result: result)
})
.disposed(by: disposeBag)
Comment on lines +49 to +53

Choose a reason for hiding this comment

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

바로 델리게이트를 호출해도 될 것 같은데 recipe를 통하는 이유가 궁금해요.
output과 delegate가 같은 역할 같아서요.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

recipe를 통해서 비동기 데이터를 전달하고, Delegate는 이벤트만을 처리하기 위해 만들어 둔것 입니다.

}

func viewDidLoad() {
fetchRecipeDetail()
}

private func fetchRecipeDetail() {
fetchRecipeDetailUseCase.execute(recipeID: recipeID)
.subscribe { [weak self] result in
self?.handleResult(result)
}
.disposed(by: disposeBag)
}

private func handleResult(_ result: Result<Recipe, Error>) {
switch result {
case .success(let recipe):
self.recipeDetailSubject.onNext(.success(recipe))
case .failure(let error):
self.recipeDetailSubject.onNext(.failure(error))
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//
// FetchRecipeDetailUseCase.swift
// HomeCafeRecipes
//
// Created by 김건호 on 6/26/24.
//

import Foundation
import RxSwift

protocol FetchRecipeDetailUseCase {
func execute(recipeID: Int) -> Single<Result<Recipe, Error>>
}

class FetchRecipeDetailUseCaseImpl: FetchRecipeDetailUseCase {
private let repository: RecipeDetailRepository

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

func execute(recipeID: Int) -> Single<Result<Recipe, Error>> {
return repository.fetchRecipeDetail(recipeID: recipeID)
.map { recipe in
return .success(recipe)
}
.catch { error in
return .just(.failure(error))
}
}
}
Loading