Skip to content

Commit

Permalink
rename transaction type column to transaction_type
Browse files Browse the repository at this point in the history
  • Loading branch information
hsharghi committed Jun 15, 2023
1 parent b369f76 commit a999bb0
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 39 deletions.
2 changes: 1 addition & 1 deletion Sources/VaporWallet/HasWallet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ extension Wallet {
.query(on: db)
.filter(\.$confirmed == true)
.sum(\.$amount)
.get()

self.balance = balance ?? 0
try await self.update(on: db)
return Double(self.balance)
Expand Down
13 changes: 9 additions & 4 deletions Sources/VaporWallet/Migrations/CreateWalletTransaction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,19 @@ public struct CreateWalletTransactionAsync: AsyncMigration {
public init() { }

public func prepare(on database: Database) async throws {
_ = try await database.enum("type")
do {
try await database.enum("transaction_type").delete()
} catch { }

let transactionType = try await database.enum("transaction_type")
.case("deposit")
.case("withdraw")
.create()

let transactionType = try await database.enum("type").read()
try await database.schema(WalletTransaction.schema)
.id()
.field("wallet_id", .uuid, .required, .references(Wallet.schema, "id", onDelete: .cascade))
.field("type", transactionType, .required)
.field("transaction_type", transactionType, .required)
.field("amount", .int, .required)
.field("confirmed", .bool, .required)
.field("meta", .json)
Expand All @@ -56,7 +59,9 @@ public struct CreateWalletTransactionAsync: AsyncMigration {


public func revert(on database: Database) async throws {
let _ = try await database.enum("type").delete()
do {
try await database.enum("transaction_type").delete()
} catch { }
try await database.schema(WalletTransaction.schema).delete()
}

Expand Down
17 changes: 9 additions & 8 deletions Sources/VaporWallet/Models/Entities/WalletTransaction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,23 @@ import Vapor
import Fluent


enum TransactionType: String, Content {
case deposit, withdraw
}

public final class WalletTransaction: Model {

public static let schema = "wallet_transactions"

enum TransactionType: String, Content {
case deposit, withdraw
}


@ID(key: .id)
public var id: UUID?

@Parent(key: "wallet_id")
var wallet: Wallet

@Enum(key: "type")
var type: TransactionType
@Enum(key: "transaction_type")
var transactionType: TransactionType

@Field(key: "amount")
var amount: Int
Expand All @@ -46,7 +47,7 @@ public final class WalletTransaction: Model {
init(
id: UUID? = nil,
walletID: UUID,
type: TransactionType,
transactionType: TransactionType,
amount: Int,
confirmed: Bool = true,
meta: [String: String]? = nil,
Expand All @@ -55,7 +56,7 @@ public final class WalletTransaction: Model {
) {
self.id = id
self.$wallet.id = walletID
self.type = type
self.transactionType = transactionType
self.amount = amount
self.meta = meta
self.confirmed = confirmed
Expand Down
84 changes: 58 additions & 26 deletions Tests/VaporWalletTests/VaporWalletTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ class VaporWalletTests: XCTestCase {
app = Application(.testing)
app.logger.logLevel = .debug

// app.databases.use(.postgres(
// hostname: "localhost",
// port: 5432,
// username: "catgpt",
// password: "catgpt",
// database: "catgpt"
// ), as: .psql)
// app.databases.use(.mysql(hostname: "127.0.0.1", port: 3306, username: "vapor", password: "vapor", database: "vp-test"), as: .mysql)
app.databases.use(.sqlite(.file("catgpt-sqlite-db.sqlite")), as: .sqlite)
app.databases.use(.postgres(
hostname: "localhost",
port: 5432,
username: "catgpt",
password: "catgpt",
database: "catgpt"
), as: .psql)
// app.databases.use(.mysql(hostname: "127.0.0.1", port: 3306, username: "vapor", password: "vapor", database: "vp-test"), as: .mysql)
// app.databases.use(.sqlite(.memory), as: .sqlite)

try! migrations(app)
try! app.autoRevert().wait()
Expand Down Expand Up @@ -368,25 +368,57 @@ class VaporWalletTests: XCTestCase {
app.databases.middleware.use(AsyncWalletMiddleware<Game>())
app.databases.middleware.use(AsyncWalletTransactionMiddleware())

let user = try await User.create(username: "user1", on: app.db)
let game = Game(id: user.id, name: "game1")
try await game.save(on: app.db)

let repo1 = user.walletsRepository(on: app.db)
let repo2 = game.walletsRepository(on: app.db)
do {
let user = try await User.create(username: "user1", on: app.db)
let game = Game(id: user.id, name: "game1")
try await game.save(on: app.db)

let repo1 = user.walletsRepository(on: app.db)
let repo2 = game.walletsRepository(on: app.db)

try await repo1.depositAsync(amount: 100)
try await repo2.depositAsync(amount: 500)

let balance1 = try await repo1.balanceAsync()
let balance2 = try await repo2.balanceAsync()

XCTAssertEqual(balance1, 100)
XCTAssertEqual(balance2, 500)
} catch {
print("error: \(String(reflecting: error))")
}

try await repo1.depositAsync(amount: 100)
// try await repo2.depositAsync(amount: 500)
}

// let userWallet = try await repo1.getAsync(type: .default)
// let gameWallet = try await repo2.getAsync(type: .default)

// let balance1 = try await repo1.balanceAsync()
// let balance2 = try await repo2.balanceAsync()
//
// XCTAssertEqual(balance1, 100)
// XCTAssertEqual(balance2, 500)
func testMultiModelWalletTransfer() async throws {
app.databases.middleware.use(AsyncWalletMiddleware<User>())
app.databases.middleware.use(AsyncWalletMiddleware<Game>())
app.databases.middleware.use(AsyncWalletTransactionMiddleware())

do {
let user = try await User.create(username: "user1", on: app.db)
let game = Game(id: user.id, name: "game1")
try await game.save(on: app.db)

let repo1 = user.walletsRepository(on: app.db)
let repo2 = game.walletsRepository(on: app.db)

try await repo1.depositAsync(amount: 100)
try await repo2.depositAsync(amount: 500)

let userWallet = try await repo1.defaultAsync()
let gameWallet = try await repo2.defaultAsync()

try await repo1.transferAsync(from: gameWallet, to: userWallet, amount: 100)

let balance1 = try await repo1.balanceAsync()
let balance2 = try await repo2.balanceAsync()

XCTAssertEqual(balance1, 200)
XCTAssertEqual(balance2, 400)
} catch {
print("###### error: #########\n\(String(reflecting: error))")
}

}

Expand All @@ -405,7 +437,7 @@ class VaporWalletTests: XCTestCase {
app.migrations.add(CreateUser())
app.migrations.add(CreateGame())
app.migrations.add(CreateWallet())
app.migrations.add(CreateWalletTransaction())
app.migrations.add(CreateWalletTransactionAsync())
}
}

Expand Down

0 comments on commit a999bb0

Please sign in to comment.