-
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
Refactor/quests repository (1) #84
Changes from all commits
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,14 @@ | ||
// | ||
// DataConfigure.swift | ||
// DailyQuest | ||
// | ||
// Created by μ΄μ ν¬ on 2022/12/05. | ||
// | ||
|
||
import Foundation | ||
|
||
enum DateFilter { | ||
case today(_ date: Date) | ||
case month(_ date: Date) | ||
case year(_date: Date) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,80 +11,84 @@ import Foundation | |
final class DefaultQuestsRepository { | ||
|
||
private let persistentStorage: QuestsStorage | ||
private let networkService: NetworkService | ||
|
||
init(persistentStorage: QuestsStorage) { | ||
init(persistentStorage: QuestsStorage, networkService: NetworkService = FirebaseService.shared) { | ||
self.persistentStorage = persistentStorage | ||
self.networkService = networkService | ||
} | ||
} | ||
|
||
extension DefaultQuestsRepository: QuestsRepository { | ||
func save(with quest: [Quest]) -> Single<[Quest]> { | ||
return persistentStorage.saveQuests(with: quest) | ||
.flatMap { quests in | ||
return Observable.from(quests) | ||
.concatMap { quest in | ||
return self.networkService.create(userCase: .currentUser, | ||
access: .quests, | ||
dto: quest.toDTO()) | ||
} | ||
.map { $0.toDomain() } | ||
.toArray() | ||
.asObservable() | ||
.asSingle() | ||
Comment on lines
+32
to
+35
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. toArrayλ₯Ό νλ κ²½μ° μλ£νμ΄ λ°λμ΄λ²λ €μ asSingleμ λ°λ‘ μ€νμ΄ μ λμ΄ asObservableλ‘ obseravaleλ‘ λ°κΎΌλ€ singleλ‘ λ°κΎΈμμ΅λλ€ π 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. μ κ° μκΈ°λ‘ toArray()μ κ²°κ³Όλ singleμ΄ λλκ±Έλ‘ κΈ°μ΅ν΄μ. (μ ννμ§ μμ§λ§..!) 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. νμΈν΄λ³΄λ κ·Έλ λ€μ. λ°©κΈ κ³ μ³μ 컀λ°ν΄λ¨μ΅λλ€. toArrayμ λ°νκ°μ΄ PrimitiveSequence<SingleTrait, [Quest]>λΌμ κ³ μ³€λκ±΄λ° κ°λ₯νλ€μ κ°μ¬ν©λλ€ 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. μ¬μ€ Singleμ PrimitiveSequence<SingleTrait, Element>μ νμ
μ리μ΄μ€(typealias)λΌκ³ ν©λλ€! |
||
} | ||
} | ||
|
||
func fetch(by date: Date) -> Observable<[Quest]> { | ||
return persistentStorage.fetchQuests(by: date) | ||
.catch { _ in | ||
self.networkService.read(type: QuestDTO.self, userCase: .currentUser, access: .quests, filter: .month(Date())) | ||
.map { $0.toDomain() } | ||
.toArray() | ||
.asObservable() | ||
} | ||
} | ||
|
||
func update(with quest: Quest) -> Single<Quest> { | ||
return persistentStorage.updateQuest(with: quest) | ||
.flatMap { quest in | ||
self.networkService.update(userCase: .currentUser, access: .quests, dto: quest.toDTO()) | ||
} | ||
.map { $0.toDomain() } | ||
.asObservable() | ||
.asSingle() | ||
} | ||
|
||
func delete(with questId: UUID) -> Single<Quest> { | ||
return persistentStorage.deleteQuest(with: questId) | ||
.flatMap { quest in | ||
self.networkService.delete(userCase: .currentUser, access: .quests, dto: quest.toDTO()) | ||
} | ||
.map { $0.toDomain() } | ||
.asObservable() | ||
.asSingle() | ||
} | ||
|
||
func deleteAll(with groupId: UUID) -> Single<[Quest]> { | ||
return persistentStorage.deleteQuestGroup(with: groupId) | ||
} | ||
|
||
func fetch(by uuid: String) -> Observable<[Quest]> { | ||
return .just([]) | ||
} | ||
} | ||
|
||
extension DefaultQuestsRepository { | ||
static func test() { | ||
let persistentStorage = RealmQuestsStorage() | ||
let repository = DefaultQuestsRepository(persistentStorage: persistentStorage) | ||
let dummyDate = ["2022-11-17".toDate()!, | ||
"2022-11-18".toDate()!, | ||
"2022-11-19".toDate()!, | ||
"2022-11-20".toDate()!, | ||
"2022-11-21".toDate()!, | ||
"2022-11-22".toDate()!, | ||
"2022-11-23".toDate()!, | ||
"2022-11-24".toDate()!] | ||
let dummyData = (0...10).compactMap { i in | ||
Quest(groupId: UUID(), uuid: UUID(), date: dummyDate[i % dummyDate.count], title: "\(i) dummyData", currentCount: 0, totalCount: i % 5) | ||
} | ||
|
||
print("πsave") | ||
let _ = repository.save(with: dummyData) | ||
.subscribe { event in | ||
print(event) | ||
} | ||
|
||
print("πfetch \(Date())") | ||
|
||
let _ = repository.fetch(by: Date()) | ||
.subscribe { event in | ||
print(event) | ||
} | ||
|
||
let temp = dummyData[0] | ||
print("πupdate \(temp.uuid)") | ||
let quest = Quest(groupId: temp.groupId, uuid: temp.uuid, date: temp.date, title: "change", currentCount: temp.currentCount + 1, totalCount: temp.totalCount) | ||
let _ = repository.update(with: quest) | ||
.subscribe { event in | ||
print(event) | ||
} | ||
|
||
print("πdelete \(dummyData[1].uuid)") | ||
let _ = repository.delete(with: dummyData[1].uuid) | ||
.subscribe { event in | ||
print(event) | ||
.flatMap { quests in | ||
return Observable.from(quests) | ||
.concatMap { quest in | ||
self.networkService.delete(userCase: .currentUser, | ||
access: .quests, | ||
dto: quest.toDTO()) | ||
} | ||
.map { $0.toDomain() } | ||
.toArray() | ||
.asObservable() | ||
.asSingle() | ||
} | ||
} | ||
|
||
func fetch(by uuid: String) -> Observable<[Quest]> { // λ°μ λ μ§κΉμ§ λ°μμμΌν¨ | ||
return self.networkService.read(type: QuestDTO.self, | ||
userCase: .anotherUser(uuid), | ||
access: .quests, | ||
filter: .month(Date())) | ||
.map { $0.toDomain() } | ||
.toArray() | ||
.asObservable() | ||
} | ||
} |
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.
μ΄κ² μ°μ monthκ°μ λ°μ΄ν°λ₯Ό κ°μ Έμ¬λ νμνκ²μ΄μ§μ?
κ·Όλ° μμ§μ RealmServiceμλ§ μλ κ²μ΄μ£ ?