Skip to content

Commit

Permalink
uncomment all legacy methods
Browse files Browse the repository at this point in the history
  • Loading branch information
hsharghi committed Jun 17, 2023
1 parent 713b577 commit 7b6756e
Show file tree
Hide file tree
Showing 7 changed files with 312 additions and 310 deletions.
37 changes: 19 additions & 18 deletions Sources/VaporWallet/HasWallet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import FluentPostgresDriver
public protocol HasWallet: FluentKit.Model {
static var idKey: KeyPath<Self, Self.ID<UUID>> { get }
func walletsRepository(on db: Database) -> WalletsRepository<Self>

}

extension HasWallet {
Expand All @@ -28,15 +28,15 @@ extension HasWallet {

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

var balance: Int
// Temporary workaround for sum and average aggregates on Postgres DB
if let _ = db as? PostgresDatabase {
let balanceOptional = try? await self.$transactions
.query(on: db)
.filter(\.$confirmed == true)
.aggregate(.sum, \.$amount, as: Double.self)

balance = balanceOptional == nil ? 0 : Int(balanceOptional!)
} else {
let intBalance = try await self.$transactions
Expand All @@ -48,23 +48,24 @@ extension Wallet {
}

self.balance = balance

try await self.update(on: db)
return Double(self.balance)
}
//
// public func refreshBalance(on db: Database) -> EventLoopFuture<Double> {
// 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)
// }
// }
// }

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)
}
}
}
}

26 changes: 13 additions & 13 deletions Sources/VaporWallet/Middlewares/WalletModelMiddleware.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@
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 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 {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,19 @@ 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 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 {

Expand Down
64 changes: 32 additions & 32 deletions Sources/VaporWallet/Migrations/CreateWallet.swift
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
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 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 {
private var idKey: String
Expand Down
63 changes: 32 additions & 31 deletions Sources/VaporWallet/Migrations/CreateWalletTransaction.swift
Original file line number Diff line number Diff line change
@@ -1,36 +1,37 @@
import Fluent

//public struct CreateWalletTransaction: Migration {
// public init() { }
//
// public func prepare(on database: Database) -> EventLoopFuture<Void> {
// return database.enum("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("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.enum("type")
// .deleteCase("deposit")
// .deleteCase("withdraw")
// .update().flatMap { _ in
// return database.schema(WalletTransaction.schema).delete()
// }
//
// }
//}
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 init() { }
Expand Down
10 changes: 5 additions & 5 deletions Sources/VaporWallet/Models/Entities/WalletTransaction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ extension WalletTransaction {
public var isConfirmed: Bool {
return self.confirmed
}
//
// public func confirm(on db: Database) -> EventLoopFuture<Void> {
// self.confirmed = true
// return self.update(on: db)
// }

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

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

0 comments on commit 7b6756e

Please sign in to comment.