-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
129 additions
and
97 deletions.
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
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
24 changes: 8 additions & 16 deletions
24
src/api/endpoints/actions/domain/models/list-actions.output.ts
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 |
---|---|---|
@@ -1,29 +1,21 @@ | ||
import { ContractAction, Result } from '@alien-worlds/aw-core'; | ||
import { ContractAction, IO, Result, UnknownObject } from '@alien-worlds/aw-core'; | ||
|
||
export class ListActionsOutput { | ||
export class ListActionsOutput implements IO { | ||
public static create(result: Result<ContractAction[]>): ListActionsOutput { | ||
return new ListActionsOutput(result); | ||
} | ||
|
||
private constructor(public readonly result: Result<ContractAction[]>) {} | ||
constructor(public readonly result: Result<ContractAction[]>) {} | ||
|
||
public toResponse() { | ||
toJSON(): UnknownObject { | ||
const { result } = this; | ||
|
||
if (result.isFailure) { | ||
const { | ||
failure: { error }, | ||
} = result; | ||
if (error) { | ||
return { | ||
status: 500, | ||
body: [], | ||
}; | ||
} | ||
return {}; | ||
} | ||
|
||
return { | ||
status: 200, | ||
body: result.content | ||
result: result.content.map(r => r.toJSON()), | ||
}; | ||
} | ||
} |
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,81 @@ | ||
import { | ||
IO, | ||
Response, | ||
RouteIO, | ||
UnknownObject, | ||
Request, | ||
parseToBigInt, | ||
} from '@alien-worlds/aw-core'; | ||
import { ListActionsInput } from '../domain/models/list-actions.input'; | ||
import { ListActionsQueryParams } from '../domain/actions.types'; | ||
import { ListActionsOutput } from '../domain/models/list-actions.output'; | ||
|
||
export class ListActionsRouteIO implements RouteIO { | ||
public toResponse(output: ListActionsOutput): Response { | ||
const { result } = output; | ||
|
||
if (result.isFailure) { | ||
const { | ||
failure: { error }, | ||
} = result; | ||
if (error) { | ||
return { | ||
status: 500, | ||
body: [], | ||
}; | ||
} | ||
} | ||
|
||
return { | ||
status: 200, | ||
body: output.toJSON(), | ||
}; | ||
} | ||
|
||
public fromRequest( | ||
request: Request<unknown, unknown, ListActionsQueryParams> | ||
): ListActionsInput { | ||
const { | ||
query: { contracts, names, accounts, from, to, limit, offset, block_numbers }, | ||
} = request; | ||
|
||
let fromBlock: bigint; | ||
let toBlock: bigint; | ||
let fromDate: Date; | ||
let toDate: Date; | ||
let blockNumbers = []; | ||
|
||
if (from) { | ||
if (/^[0-9]+$/.test(from)) { | ||
fromBlock = parseToBigInt(from); | ||
} else { | ||
fromDate = new Date(from); | ||
} | ||
} | ||
|
||
if (to) { | ||
if (/^[0-9]+$/.test(to)) { | ||
toBlock = parseToBigInt(to); | ||
} else { | ||
toDate = new Date(to); | ||
} | ||
} | ||
|
||
if (block_numbers) { | ||
blockNumbers = block_numbers.split(',').map(parseToBigInt); | ||
} | ||
|
||
return new ListActionsInput( | ||
contracts ? contracts.split(',') : [], | ||
names ? names.split(',') : [], | ||
accounts ? accounts.split(',') : [], | ||
fromBlock, | ||
toBlock, | ||
fromDate, | ||
toDate, | ||
blockNumbers, | ||
offset || 0, | ||
limit || 10 | ||
); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,25 +1,12 @@ | ||
import { GetRoute, RouteHandler } from '@alien-worlds/aw-core'; | ||
import { ListActionsInput } from '../domain/models/list-actions.input'; | ||
import { ListActionsOutput } from '../domain/models/list-actions.output'; | ||
import { ListActionsRouteIO } from './list-actions.route-io'; | ||
|
||
/*imports*/ | ||
|
||
/** | ||
* @class | ||
* | ||
* | ||
*/ | ||
export class ListActionsRoute extends GetRoute { | ||
public static create(handler: RouteHandler) { | ||
return new ListActionsRoute(handler); | ||
} | ||
|
||
private constructor(handler: RouteHandler) { | ||
super('actions', handler, { | ||
hooks: { | ||
pre: ListActionsInput.fromRequest, | ||
post: (output: ListActionsOutput) => output.toResponse(), | ||
}, | ||
}); | ||
super('actions', handler, new ListActionsRouteIO()); | ||
} | ||
} |
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