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
30 changes: 30 additions & 0 deletions HomeCafeRecipes/HomeCafeRecipes/Data/FeedListRepository.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//
// 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)
}
class FeedListRepositoryImpl: FeedListRepository {

Choose a reason for hiding this comment

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

개행 부탁드리구요, implement 클래스는 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.

23aebe4 수정했습니다!

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)
}


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.

23aebe4 수정했습니다!

}
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 db = Firestore.firestore()

Choose a reason for hiding this comment

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

더 명확한 변수 네이밍이면 좋을 것 같아요.


func fetchFeedItems(completion: @escaping (Result<[FeedItem], Error>) -> Void) {
db.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 = documents.compactMap { doc -> FeedItem? in
let data = doc.data()
guard
let title = data["title"] as? String,
let imageURL = data["imageURL"] as? [String] else {

Choose a reason for hiding this comment

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

배열이 반환되는 거면 imageURLs 와 같이 복수형으로 네이밍하는게 좋을 것 같아요

return nil
}
return FeedItem(id: doc.documentID, title: title, imageURL: imageURL)
Copy link

@f-lab-barry f-lab-barry Jun 4, 2024

Choose a reason for hiding this comment

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

질문) document 모델은 어디서 정의하고 있나요?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

위 document는 파이어 베이스에서 제공하는 문서 자체의 ID를 선언했습니다. 하지만 해당 데이터의 id를 넣어야 할거 같아 수정하겠습니다!

}
completion(.success(feedItems))
}
}

func searchFeedItems(title: String, completion: @escaping (Result<[FeedItem], Error>) -> Void) {
db.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 = documents.compactMap { doc -> FeedItem? in
let data = doc.data()
guard
let title = data["title"] as? String,
let imageURL = data["imageURL"] as? [String] else {
return nil
}
return FeedItem(id: doc.documentID, title: title, imageURL: imageURL)
}
completion(.success(feedItems))

Choose a reason for hiding this comment

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

repository에서 받은 data를 entity로 변환하는 과정(도메인으로 변환하는 과정)을 거치는데,
따로 메서드로 분리하거나 entity 내에서 메서드를 별도로 갖는 것에 대해 어떻게 생각하세요?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

적용시켜보겠습니다!

Copy link

Choose a reason for hiding this comment

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

response 모델이 정의된 곳에 toDomain()
struct xxxResponse {
let title: String
let imageURLs: [String]
}

extension xxxResponse {
func toDomain() -> FeedItem {
return FeedItem(
title: title,
imageURLs: imageURLs.map { URL(string: $0) }
)
}
}


func toDomain() -> [FeedItem] { ... } // mapping data to domain

}
}

}
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 imageURL : [String]

Choose a reason for hiding this comment

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

Suggested change
let imageURL : [String]
let imageURLs : [String]

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//
// 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
completion(result)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//
// 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
completion(result)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//
// FeedListViewcontroller.swift
// HomeCafeRecipes
//
// Created by 김건호 on 5/30/24.
//

import Foundation
19 changes: 0 additions & 19 deletions HomeCafeRecipes/HomeCafeRecipes/ViewController.swift

This file was deleted.