Skip to content

Commit

Permalink
Merge pull request #15 from joselinoneto/develop
Browse files Browse the repository at this point in the history
async insert
  • Loading branch information
joselinoneto authored Mar 3, 2023
2 parents 5ca2f90 + f0b9bb0 commit a833a88
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 67 deletions.
16 changes: 8 additions & 8 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/groue/GRDB.swift.git",
"state" : {
"revision" : "e76cda27dce9595a833a23cfd26bbc27553cd305",
"version" : "6.7.0"
"revision" : "d3862c042446d3025a8a4123b013e1b6d58fc315",
"version" : "6.8.0"
}
},
{
Expand All @@ -32,8 +32,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/joselinoneto/storageclient",
"state" : {
"revision" : "df4c3a893022b27694408c5ca828afe104ce95cd",
"version" : "1.0.11"
"revision" : "560fdbaf87766a80a0cb0dd13e7a8f98811f3e08",
"version" : "1.0.12"
}
},
{
Expand All @@ -59,17 +59,17 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/joselinoneto/ToolboxStorageClient",
"state" : {
"revision" : "62a2e86e0e022bd821cefc36787ccc2611fabda3",
"version" : "1.0.3"
"revision" : "8d6248ff790caeae43a0344f65ff2a9aa7418a86",
"version" : "1.0.5"
}
},
{
"identity" : "tools",
"kind" : "remoteSourceControl",
"location" : "https://github.com/joselinoneto/tools",
"state" : {
"revision" : "23bf34a9012f515e78abaf84d42136ebaa74f3cf",
"version" : "1.0.4"
"revision" : "d1b6ccc2948f8d474cd6ccd476380114021dc776",
"version" : "1.0.8"
}
}
],
Expand Down
38 changes: 18 additions & 20 deletions Sources/manager/Controllers/ApodManagerController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,32 +26,14 @@ public class ApodManagerController {
getLocalData()
}

private func getLocalData() {
self.storageController
.$items
.map{$0?.mapToEntity()}
.assign(to: \.items, on: self)
.store(in: &self.cancellables)
}

public func getRemoteData(per: Int, page: Int) async throws {
let response = try? await apiController.getApods(per: per, page: page)
try saveItems(response?.items)
try await saveItems(response?.items)
}

public func getMonthData(currentMonth: TimelineMonth) async throws {
let response = try? await apiController.getMonthsApods(startDate: currentMonth.startMonth, endDate: currentMonth.endMonth)
try saveItems(response?.items)
}

public func saveItems(_ items: [NasaApodDto]?) throws {
let itemsAdd: [ApodStorage] = items?.map { ApodStorage($0) } ?? []
try storageController.saveItems(itemsAdd)
}

public func saveItems(_ items: [ApodStorage]?) throws {
guard let items = items else { return }
try storageController.saveItems(items)
try await saveItems(response?.items)
}

public func getAll() throws -> [ApodStorage]? {
Expand All @@ -63,4 +45,20 @@ public class ApodManagerController {
try await FileStorage.shared.saveRemoteFile(imageUrl: item.imageUrl, fileName: item.id?.uuidString)
}
}

// MARK: Private methdos
private func getLocalData() {
self.storageController
.$items
.map{$0?.mapToEntity()}
.assign(to: \.items, on: self)
.store(in: &self.cancellables)
}

private func saveItems(_ items: [NasaApodDto]?) async throws {
let itemsAdd: [ApodStorage] = items?.map { ApodStorage($0) } ?? []
for item in itemsAdd {
try await storageController.asyncSaveItem(item)
}
}
}
43 changes: 4 additions & 39 deletions Tests/managerTests/managerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,53 +13,18 @@ final class managerTests: XCTestCase {
cancellables = []
}

func testApodController() throws {
let controller = ApodManagerController(currentMonth: TimelineMonth.currentMonth, pathToSqlite: nil)
Task(priority: .high) {
try await controller.getMonthData(currentMonth: TimelineMonth.currentMonth)
}

let mock = ApodStorage()
mock.id = UUID()
mock.title = "MockTitle"
mock.postedDate = Date()
try controller.saveItems([mock])

let countEmittedExpected: Int = 3
let apodPublisher = controller.$items.collect(countEmittedExpected).first()
let counterArray = try awaitPublisher(apodPublisher)
XCTAssertEqual(countEmittedExpected, counterArray.count)

let array: [Apod]? = counterArray.last ?? []
XCTAssertEqual(array?.first?.id, mock.id)
}

func testCrud() throws {
let controller = ApodManagerController(currentMonth: TimelineMonth.currentMonth, pathToSqlite: nil)
let countItems: Int = 10

for _ in 1...countItems {
let mock = ApodStorage()
mock.id = UUID()
mock.title = "MockTitle"
mock.postedDate = Date()
try controller.saveItems([mock])
}

let items: [ApodStorage]? = try controller.getAll()

XCTAssertNotNil(items)
XCTAssertEqual(countItems, items?.count)
}

func testRemoteData() async throws {
let controller = ApodManagerController(currentMonth: TimelineMonth.currentMonth, pathToSqlite: nil)
try await controller.getMonthData(currentMonth: TimelineMonth.currentMonth)
let items = try controller.getAll()
XCTAssertNotNil(items)
}

func testRemotePageData() async throws {
let controller = ApodManagerController(currentMonth: TimelineMonth.currentMonth, pathToSqlite: nil)
try await controller.getRemoteData(per: 100, page: 1)
let items = try controller.getAll()
XCTAssertNotNil(items)
}

func testDownloadContent() async throws {
Expand Down

0 comments on commit a833a88

Please sign in to comment.