Created
December 29, 2017 20:39
-
-
Save azamsharp/7e9bcc4a392f00ececeb728ba1724873 to your computer and use it in GitHub Desktop.
BlockchainController
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class BlockchainController { | |
private (set) var drop :Droplet | |
private (set) var blockchainService :BlockchainService! | |
init(drop :Droplet) { | |
self.drop = drop | |
self.blockchainService = BlockchainService() | |
// setup the routes for the controller | |
setupRoutes() | |
} | |
private func setupRoutes() { | |
self.drop.get("mine") { request in | |
let block = Block() | |
self.blockchainService.addBlock(block) | |
return try JSONEncoder().encode(block) | |
} | |
// adding a new transaction | |
self.drop.post("transaction") { request in | |
if let transaction = Transaction(request: request) { | |
// add the transaction to the block | |
// get the last mined block | |
let block = self.blockchainService.getLastBlock() | |
block.addTransaction(transaction: transaction) | |
//let block = Block(transaction: transaction) | |
//self.blockchainService.addBlock(block) | |
return try JSONEncoder().encode(block) | |
} | |
return try JSONEncoder().encode(["message":"Something bad happend!"]) | |
} | |
// get the chain | |
self.drop.get("blockchain") { request in | |
if let blockchain = self.blockchainService.getBlockchain() { | |
return try JSONEncoder().encode(blockchain) | |
} | |
return try! JSONEncoder().encode(["message":"Blockchain is not initialized. Please mine a block"]) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment