Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
rkamysz committed Jul 28, 2023
1 parent 65abc54 commit 146cc96
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 97 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@
"typescript": "^4.8.2"
},
"dependencies": {
"@alien-worlds/aw-broadcast": "^0.0.2",
"@alien-worlds/aw-core": "^0.0.2",
"@alien-worlds/aw-broadcast": "^0.0.3",
"@alien-worlds/aw-core": "^0.0.3",
"@alien-worlds/aw-workers": "^0.0.2",
"async": "^3.2.4",
"commander": "^10.0.1",
Expand Down
82 changes: 27 additions & 55 deletions src/api/endpoints/actions/domain/models/list-actions.input.ts
Original file line number Diff line number Diff line change
@@ -1,66 +1,14 @@
import { ListActionsQueryParams } from '../actions.types';
import { Request, parseToBigInt } from '@alien-worlds/aw-core';
import { IO, UnknownObject } from '@alien-worlds/aw-core';
/**
* @class
*/
export class ListActionsInput {
/**
*
* @param {ListActionsRequestDto} dto
* @returns {ListActionsInput}
*/
public static 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
);
}
export class ListActionsInput implements IO {
/**
*
* @constructor
* @private
*/
private constructor(
constructor(
public readonly contracts: string[],
public readonly names: string[],
public readonly accounts: string[],
Expand All @@ -72,4 +20,28 @@ export class ListActionsInput {
public readonly offset: number,
public readonly limit: number
) {}

public toJSON(): UnknownObject {
const {
contracts,
names,
accounts,
blockNumbers,
startBlock,
endBlock,
offset,
limit,
} = this;

return {
contracts,
names,
accounts,
block_numbers: blockNumbers.map(blockNumber => blockNumber.toString()),
from: startBlock.toString(),
to: endBlock.toString(),
offset,
limit,
};
}
}
24 changes: 8 additions & 16 deletions src/api/endpoints/actions/domain/models/list-actions.output.ts
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()),
};
}
}
81 changes: 81 additions & 0 deletions src/api/endpoints/actions/routes/list-actions.route-io.ts
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
);
}
}
17 changes: 2 additions & 15 deletions src/api/endpoints/actions/routes/list-actions.route.ts
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());
}
}
18 changes: 9 additions & 9 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@
# yarn lockfile v1


"@alien-worlds/aw-broadcast@^0.0.2":
version "0.0.2"
resolved "https://npm.pkg.github.com/download/@alien-worlds/aw-broadcast/0.0.2/ae8f1ae3485c3c1d0581dc06999cbfcd342803de#ae8f1ae3485c3c1d0581dc06999cbfcd342803de"
integrity sha512-I2X5akaAfvARklvq1fCu6syzEUtBHgyc45wbZKQ3GL7LVP6UAbU765GGR1vFQ+dA3r3wMEe9CdXNKVnZMv7+pA==
"@alien-worlds/aw-broadcast@^0.0.3":
version "0.0.3"
resolved "https://npm.pkg.github.com/download/@alien-worlds/aw-broadcast/0.0.3/ee62d2dccf49ae3da426e8c92ddf01520a677616#ee62d2dccf49ae3da426e8c92ddf01520a677616"
integrity sha512-L254cFKKRey6ni68ftE1w0rRPtz5nedUG1lB4sMe+Q5e0wXDyMLGi09dlAUN6PBBQQDCW6BDiD5pyTGgk1I5SA==
dependencies:
"@alien-worlds/aw-core" "^0.0.2"
"@alien-worlds/aw-core" "^0.0.3"
nanoid "^3.0.0"

"@alien-worlds/aw-core@^0.0.2":
version "0.0.2"
resolved "https://npm.pkg.github.com/download/@alien-worlds/aw-core/0.0.2/a0ff00b1c50a06173e99a1185af0428a24b4a3ac#a0ff00b1c50a06173e99a1185af0428a24b4a3ac"
integrity sha512-Ld5QrJVhtFqvmgxFYNQUlojPBp4AEw+iqPJ8xX00pFJ2shUO9wJk0cVPMP1a7KADkCYirA9AxPn45C8CXcHKjA==
"@alien-worlds/aw-core@^0.0.3":
version "0.0.3"
resolved "https://npm.pkg.github.com/download/@alien-worlds/aw-core/0.0.3/1b5af094fced3d97824d909190d446c116aa6bec#1b5af094fced3d97824d909190d446c116aa6bec"
integrity sha512-5aWRA7svi9qWEZO0ROJNUNGnw/KftSPLkcekCjm5ecB0ufMo01eI68A+RlufuCbLaeTKH8K8pKWFgycSxGRBwQ==
dependencies:
inversify "^6.0.1"
node-fetch "2.6.6"
Expand Down

0 comments on commit 146cc96

Please sign in to comment.