Skip to content

Commit

Permalink
remove all legacy methods (EventLoopFuture), switch fully to async/aw…
Browse files Browse the repository at this point in the history
…ait.
  • Loading branch information
hsharghi committed Jun 17, 2023
1 parent 7b6756e commit 091b1cc
Show file tree
Hide file tree
Showing 10 changed files with 195 additions and 554 deletions.
20 changes: 0 additions & 20 deletions Sources/VaporWallet/Extensions/EventLoopFuture+extensions.swift

This file was deleted.

16 changes: 1 addition & 15 deletions Sources/VaporWallet/HasWallet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ extension HasWallet {
}

extension Wallet {
public func refreshBalanceAsync(on db: Database) async throws -> Double {
public func refreshBalance(on db: Database) async throws -> Double {

var balance: Int
// Temporary workaround for sum and average aggregates on Postgres DB
Expand All @@ -53,19 +53,5 @@ extension Wallet {
return Double(self.balance)
}

public func refreshBalance(on db: Database) -> EventLoopFuture<Double> {
// Temporary workaround for sum and average aggregates on Postgres DB
self.$transactions
.query(on: db)
.filter(\.$confirmed == true)
.sum(\.$amount)
.unwrap(orReplace: 0)
.flatMap { (balance) -> EventLoopFuture<Double> in
self.balance = balance
return self.update(on: db).map {
return Double(balance)
}
}
}
}

17 changes: 2 additions & 15 deletions Sources/VaporWallet/Middlewares/WalletModelMiddleware.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,16 @@

import Vapor
import Fluent
//
public struct WalletMiddleware<M:HasWallet>: ModelMiddleware {

public init() {}

public func create(model: M, on db: Database, next: AnyModelResponder) -> EventLoopFuture<Void> {

// Create `default` wallet when new model is created
return next.create(model, on: db).flatMap {
db.logger.log(level: .info, "default wallet for user \(model._$idKey) has been created")
return model.walletsRepository(on: db).create().transform(to: ())
}
}
}

public struct AsyncWalletMiddleware<M:HasWallet>: AsyncModelMiddleware {
public struct WalletMiddleware<M:HasWallet>: AsyncModelMiddleware {

public init() {}

public func create(model: M, on db: Database, next: AnyAsyncModelResponder) async throws {
try await next.create(model, on: db)
db.logger.log(level: .info, "default wallet for user \(model._$idKey) has been created")
let repo = model.walletsRepository(on: db)
try await repo.createAsync()
try await repo.create()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,14 @@
import Vapor
import Fluent


public struct WalletTransactionMiddleware: ModelMiddleware {

public init() {}

public func create(model: WalletTransaction, on db: Database, next: AnyModelResponder) -> EventLoopFuture<Void> {
return next.create(model, on: db).flatMap {
return model
.$wallet.get(on: db)
.map { $0.refreshBalance(on: db) }
.transform(to: ())
}
}
}

public struct AsyncWalletTransactionMiddleware: AsyncModelMiddleware {
public struct WalletTransactionMiddleware: AsyncModelMiddleware {

public init() {}

public func create(model: WalletTransaction, on db: Database, next: AnyAsyncModelResponder) async throws {
try await next.create(model, on: db)
let wallet = try await model.$wallet.get(on: db)
_ = try await wallet.refreshBalanceAsync(on: db)
_ = try await wallet.refreshBalance(on: db)
}
}

34 changes: 1 addition & 33 deletions Sources/VaporWallet/Migrations/CreateWallet.swift
Original file line number Diff line number Diff line change
@@ -1,40 +1,8 @@
import Fluent
import SQLKit

public struct CreateWallet: Migration {
private var idKey: String
public init(foreignKeyColumnName idKey: String = "id") {
self.idKey = idKey
}

public func prepare(on database: Database) -> EventLoopFuture<Void> {
return database.schema(Wallet.schema)
.id()
.field("name", .string, .required)
.field("owner_type", .string, .required)
.field("owner_id", .uuid, .required)
.field("min_allowed_balance", .int, .required)
.field("balance", .int, .required)
.field("decimal_places", .uint8, .required)
.field("created_at", .datetime, .required)
.field("updated_at", .datetime, .required)
.field("deleted_at", .datetime)
.create().flatMap { _ in
let sqlDB = (database as! SQLDatabase)
return sqlDB
.create(index: "type_idx")
.on(Wallet.schema)
.column("owner_type")
.run()
}
}

public func revert(on database: Database) -> EventLoopFuture<Void> {
return database.schema(Wallet.schema).delete()
}
}

public struct CreateWalletAsync: AsyncMigration {
public struct CreateWallet: AsyncMigration {
private var idKey: String
public init(foreignKeyColumnName idKey: String = "id") {
self.idKey = idKey
Expand Down
34 changes: 1 addition & 33 deletions Sources/VaporWallet/Migrations/CreateWalletTransaction.swift
Original file line number Diff line number Diff line change
@@ -1,39 +1,7 @@
import Fluent

public struct CreateWalletTransaction: Migration {
public init() { }

public func prepare(on database: Database) -> EventLoopFuture<Void> {
return database.enum("transaction_type")
.case("deposit")
.case("withdraw")
.create().flatMap { transactionType in
return database.schema(WalletTransaction.schema)
.id()
.field("wallet_id", .uuid, .required, .references(Wallet.schema, "id", onDelete: .cascade))
.field("transaction_type", transactionType, .required)
.field("amount", .int, .required)
.field("confirmed", .bool, .required)
.field("meta", .json)
.field("created_at", .datetime, .required)
.field("updated_at", .datetime, .required)
.create()
}
}

public func revert(on database: Database) -> EventLoopFuture<Void> {
return database.schema(WalletTransaction.schema).delete()
.flatMap { _ in
return database.enum("transaction_type")
.deleteCase("deposit")
.deleteCase("withdraw")
.update()
.transform(to: ())
}
}
}

public struct CreateWalletTransactionAsync: AsyncMigration {
public struct CreateWalletTransaction: AsyncMigration {
public init() { }

public func prepare(on database: Database) async throws {
Expand Down
7 changes: 1 addition & 6 deletions Sources/VaporWallet/Models/Entities/WalletTransaction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,7 @@ extension WalletTransaction {
return self.confirmed
}

public func confirm(on db: Database) -> EventLoopFuture<Void> {
self.confirmed = true
return self.update(on: db)
}

public func confirmAsync(on db: Database) async throws {
public func confirm(on db: Database) async throws {
self.confirmed = true
try await self.update(on: db)
}
Expand Down
Loading

0 comments on commit 091b1cc

Please sign in to comment.