-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(api): implement blockchain controller (#263)
* implement votes controller * style: resolve style guide violations * implement blockchain controller * style: resolve style guide violations --------- Co-authored-by: oXtxNt9U <[email protected]>
- Loading branch information
Showing
4 changed files
with
86 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { describe, Sandbox } from "../../../test-framework"; | ||
import { prepareSandbox, ApiContext } from "../../test/helpers/prepare-sandbox"; | ||
import { request } from "../../test/helpers/request"; | ||
|
||
import blocks from "../../test/fixtures/blocks.json"; | ||
|
||
describe<{ | ||
sandbox: Sandbox; | ||
}>("Blockchain", ({ it, afterAll, assert, afterEach, beforeAll, beforeEach, nock }) => { | ||
let apiContext: ApiContext; | ||
|
||
// TODO: | ||
let options = { transform: false }; | ||
|
||
beforeAll(async (context) => { | ||
nock.enableNetConnect(); | ||
apiContext = await prepareSandbox(context); | ||
}); | ||
|
||
afterAll((context) => { | ||
nock.disableNetConnect(); | ||
apiContext.dispose(); | ||
}); | ||
|
||
beforeEach(async (context) => { | ||
await apiContext.reset(); | ||
}); | ||
|
||
afterEach(async (context) => { | ||
await apiContext.reset(); | ||
}); | ||
|
||
it("/blockchain", async () => { | ||
await apiContext.blockRepository.save(blocks); | ||
|
||
const block = blocks[0]; | ||
|
||
const { statusCode, data } = await request("/blockchain", options); | ||
assert.equal(statusCode, 200); | ||
assert.equal(data.data, { block: { id: block.id, height: block.height }, supply: "0" }); | ||
}); | ||
}); |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { Contracts as ApiDatabaseContracts, Identifiers as ApiDatabaseIdentifiers } from "@mainsail/api-database"; | ||
import { inject, injectable } from "@mainsail/container"; | ||
|
||
import { Controller } from "./controller"; | ||
|
||
@injectable() | ||
export class BlockchainController extends Controller { | ||
@inject(ApiDatabaseIdentifiers.BlockRepositoryFactory) | ||
private readonly blockRepositoryFactory!: ApiDatabaseContracts.IBlockRepositoryFactory; | ||
|
||
public async index() { | ||
const block = await this.blockRepositoryFactory().getLatest(); | ||
|
||
return { | ||
data: { | ||
block: block | ||
? { | ||
height: block.height, | ||
id: block.id, | ||
} | ||
: null, | ||
|
||
// TODO: calculate supply | ||
supply: "0", | ||
}, | ||
}; | ||
} | ||
} |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import Hapi from "@hapi/hapi"; | ||
|
||
import { BlockchainController } from "../controllers/blockchain"; | ||
|
||
export const register = (server: Hapi.Server): void => { | ||
const controller = server.app.app.resolve(BlockchainController); | ||
server.bind(controller); | ||
|
||
server.route({ | ||
handler: (request: Hapi.Request) => controller.index(request), | ||
method: "GET", | ||
path: "/blockchain", | ||
}); | ||
}; |