-
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 9 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
Large diffs are not rendered by default.
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([])) | ||
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) | ||
} | ||
} | ||
|
||
} | ||
|
||
|
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] | ||
} | ||
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. 요거 새로 정의된 값 (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) { | ||
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 | ||
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) | ||
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 | ||
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 { | ||||||
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
|
||||||
|
||||||
} |
This file was deleted.
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.
documents가 없어도 성공인건가요?
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.
성공은 했지만 빈배열일 경우를 생각해서 넣었습니다!