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

Usecase,Entities,FeedListRepository,FirebaseRemoteDataSource를 생성했습니다. #2

Closed
wants to merge 10 commits into from
Closed
162 changes: 148 additions & 14 deletions HomeCafeRecipes/HomeCafeRecipes.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

29 changes: 29 additions & 0 deletions HomeCafeRecipes/HomeCafeRecipes/Data/FeedListRepository.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//
// FeedListRepository.swift
// HomeCafeRecipes
//
// Created by 김건호 on 5/30/24.
//

import Foundation

protocol FeedListRepository {
func fetchFeedItems(completion: @escaping (Result<[FeedItem], Error>) -> Void)
func searchFeedItems(title: String, completion: @escaping (Result<[FeedItem], Error>) -> Void)
}

final class FeedListRepositoryImpl: FeedListRepository {
private let remoteDataSource: FirebaseRemoteDataSource

init(remoteDataSource: FirebaseRemoteDataSource) {
self.remoteDataSource = remoteDataSource
}

func fetchFeedItems(completion: @escaping (Result<[FeedItem], Error>) -> Void) {
remoteDataSource.fetchFeedItems(completion: completion)
}

func searchFeedItems(title : String, completion: @escaping (Result<[FeedItem], Error>) -> Void){
remoteDataSource.searchFeedItems(title: title, completion: completion)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
//
// FirebaseRemoteDataSource.swift
// HomeCafeRecipes
//
// Created by 김건호 on 5/30/24.
//

import FirebaseFirestore

class FirebaseRemoteDataSource {
private let firebaseDB = Firestore.firestore()

func fetchFeedItems(completion: @escaping (Result<[FeedItem], Error>) -> Void) {
firebaseDB.collection("feedItems").getDocuments { (querySnapshot, error) in
if let error = error {
completion(.failure(error))
return
}
guard let documents = querySnapshot?.documents else {
completion(.success([]))

Choose a reason for hiding this comment

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

documents가 없어도 성공인건가요?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

성공은 했지만 빈배열일 경우를 생각해서 넣었습니다!

return
}
let feedItems = self.convertToFeedItems(documents)
completion(.success(feedItems))
}
}

func searchFeedItems(title: String, completion: @escaping (Result<[FeedItem], Error>) -> Void) {
firebaseDB.collection("feedItems")
.whereField("title", isGreaterThanOrEqualTo: title)
.whereField("title", isLessThanOrEqualTo: "\(title)\u{f8ff}")
.getDocuments { (querySnapshot, error) in
if let error = error {
completion(.failure(error))
return
}
guard let documents = querySnapshot?.documents else {
completion(.success([]))
return
}

let feedItems = self.convertToFeedItems(documents)
completion(.success(feedItems))
}
}

private func convertToFeedItems(_ documents: [QueryDocumentSnapshot]) -> [FeedItem] {
return documents.compactMap { doc -> FeedItem? in
let data = doc.data()
guard
let id = data["id"] as? String,
let title = data["title"] as? String,
let imageURLs = data["imageURLs"] as? [String] else {
return nil
}
return FeedItem(id: id, title: title, imageURLs: imageURLs)
}
}

}


12 changes: 12 additions & 0 deletions HomeCafeRecipes/HomeCafeRecipes/Domain/Entities/FeedItem.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//
// FeedModel.swift
// HomeCafeRecipes
//
// Created by 김건호 on 5/16/24.
//

struct FeedItem {
let id: String
let title : String
let imageURLs : [String]
}

Choose a reason for hiding this comment

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

요거 새로 정의된 값 (Recipe)으로 업데이트만 부탁드릴게요.

Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//
// fetchFeedListUsecase.swift
// HomeCafeRecipes
//
// Created by 김건호 on 5/30/24.
//


protocol FetchFeedListUseCase {
func execute(completion: @escaping (Result<[FeedItem], Error>) -> Void)
}

class DefaultFetchFeedListUseCase: FetchFeedListUseCase {
private let repository: FeedListRepository

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

func execute(completion: @escaping (Result<[FeedItem], Error>) -> Void) {

Choose a reason for hiding this comment

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

FeedItem이 아닌 새로 정의한 도메인 Recipe로 받도록 변경해야할 것 같아요~

repository.fetchFeedItems { result in
switch result {
case .success(let feedItems):
completion(.success(feedItems))
case .failure(let error):
completion(.failure(error))
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//
// SearchFeedListusecase.swift
// HomeCafeRecipes
//
// Created by 김건호 on 5/30/24.
//


protocol SearchFeedListUseCase {
func execute(title: String, completion: @escaping (Result<[FeedItem], Error>) -> Void)

Choose a reason for hiding this comment

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

UseCase에서도 return 없이 Result 타입 completion 으로 처리하는 이유가 있을까요?
(비즈니스 로직 성공/실패는 어디서 핸들링하게 되는걸까요?)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Usecase에서도 FeedRlistepository에서 데이터를 가지고 받으면서 비동기 작업이 필요할거라 생각해서 completion으로 처리하였습니다!
비즈니스 로직 성공/실패는 viewmodel에서 핸들링 하려고 합니다!

Copy link

Choose a reason for hiding this comment

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

  • viewModel이 너무 많은 역할을 하게 되는 것, 뚱뚱해지는 것 방지
  • 비즈니스 로직 재사용성을 높임
  • SearchFeedListXXX에 대한 응집도 높임

}

class DefaultSearchFeedListUseCase: SearchFeedListUseCase {
private let repository: FeedListRepository

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

func execute(title: String, completion: @escaping (Result<[FeedItem], Error>) -> Void) {
repository.searchFeedItems(title: title) { result in
switch result {
case .success(let feedItems):
completion(.success(feedItems))
case .failure(let error):
completion(.failure(error))
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//
// FeedListViewcontroller.swift
// HomeCafeRecipes
//
// Created by 김건호 on 5/30/24.
//

import UIKit

class FeedListViewController : UIViewController {
Copy link

Choose a reason for hiding this comment

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

final이 붙어도 좋을까요? (: 옆에 띄어쓰기도 신경써주세요~)

Suggested change
class FeedListViewController : UIViewController {
final class FeedListViewController: UIViewController {


}
4 changes: 2 additions & 2 deletions HomeCafeRecipes/HomeCafeRecipes/SceneDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate {
// This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
guard let windowScene = (scene as? UIWindowScene) else { return }
window = UIWindow(windowScene: windowScene)
let mainViewController = ViewController()
let navigationController = UINavigationController(rootViewController : mainViewController)
let mainViewController = FeedListViewController()
let navigationController = UINavigationController(rootViewController : mainViewController)
window?.rootViewController = navigationController
window?.makeKeyAndVisible()
}
Expand Down
19 changes: 0 additions & 19 deletions HomeCafeRecipes/HomeCafeRecipes/ViewController.swift

This file was deleted.

Loading