-
Notifications
You must be signed in to change notification settings - Fork 0
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
Changes from 3 commits
dc51069
8895f47
188bf82
23aebe4
47f0309
9c2e460
de9d3e5
1b9b4af
fb64d7b
ab73c69
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 { | ||
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) | ||
} | ||
|
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 불필요한 개행인 것 같아요. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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,61 @@ | ||
// | ||
// FirebaseRemoteDataSource.swift | ||
// HomeCafeRecipes | ||
// | ||
// Created by 김건호 on 5/30/24. | ||
// | ||
|
||
import FirebaseFirestore | ||
|
||
class FirebaseRemoteDataSource { | ||
private let db = Firestore.firestore() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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([])) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. documents가 없어도 성공인건가요? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 배열이 반환되는 거면 imageURLs 와 같이 복수형으로 네이밍하는게 좋을 것 같아요 |
||
return nil | ||
} | ||
return FeedItem(id: doc.documentID, title: title, imageURL: imageURL) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 질문) document 모델은 어디서 정의하고 있나요? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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").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], | ||
title.contains(title) else { | ||
return nil | ||
} | ||
return FeedItem(id: doc.documentID, title: title, imageURL: imageURL) | ||
} | ||
completion(.success(feedItems)) | ||
} | ||
} | ||
} | ||
|
||
|
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] | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
} |
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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. UseCase에서도 return 없이 Result 타입 completion 으로 처리하는 이유가 있을까요? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Usecase에서도 FeedRlistepository에서 데이터를 가지고 받으면서 비동기 작업이 필요할거라 생각해서 completion으로 처리하였습니다! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} | ||
|
||
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,12 @@ | ||||||
// | ||||||
// FeedListViewcontroller.swift | ||||||
// HomeCafeRecipes | ||||||
// | ||||||
// Created by 김건호 on 5/30/24. | ||||||
// | ||||||
|
||||||
import UIKit | ||||||
|
||||||
class FeedListViewController : UIViewController { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. final이 붙어도 좋을까요? (: 옆에 띄어쓰기도 신경써주세요~)
Suggested change
|
||||||
|
||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
개행 부탁드리구요, implement 클래스는 final로 선언하는게 어떨까요?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
23aebe4 수정했습니다!