Skip to content

Commit

Permalink
feat(governance): add unpaged routes + try catch
Browse files Browse the repository at this point in the history
  • Loading branch information
1000101 committed Jul 21, 2024
1 parent 41a1c5c commit 6010cd2
Show file tree
Hide file tree
Showing 15 changed files with 248 additions and 127 deletions.
29 changes: 19 additions & 10 deletions src/routes/governance/dreps/drep-id/delegators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as ResponseTypes from '../../../../types/responses/governance.js';
import { getDbSync } from '../../../../utils/database.js';
import { SQLQuery } from '../../../../sql/index.js';
import { getSchemaForEndpoint } from '@blockfrost/openapi';
import { isUnpaged } from '../../../../utils/routes.js';

async function route(fastify: FastifyInstance) {
fastify.route({
Expand All @@ -14,19 +15,27 @@ async function route(fastify: FastifyInstance) {
handler: async (request: FastifyRequest<QueryTypes.RequestParametersDRepID>, reply) => {
const clientDbSync = await getDbSync(fastify);

const { rows }: { rows: ResponseTypes.DRepsDrepIDDelegators } =
await clientDbSync.query<QueryTypes.DRepsDrepIDDelegators>(
SQLQuery.get('governance_dreps_drep_id_delegators'),
[request.query.order, request.query.count, request.query.page, request.params.drep_id],
);
try {
const unpaged = isUnpaged(request);
const { rows }: { rows: ResponseTypes.DRepsDrepIDDelegators } = unpaged
? await clientDbSync.query<QueryTypes.DRepsDrepIDDelegators>(
SQLQuery.get('governance_dreps_drep_id_delegators_unpaged'),
[request.query.order],
)
: await clientDbSync.query<QueryTypes.DRepsDrepIDDelegators>(
SQLQuery.get('governance_dreps_drep_id_delegators'),
[request.query.order, request.query.count, request.query.page],
);

clientDbSync.release();
clientDbSync.release();

if (rows.length === 0) {
return reply.send([]);
return reply.send(rows);
} catch (error) {
if (clientDbSync) {
clientDbSync.release();
}
throw error;
}

return reply.send(rows);
},
});
}
Expand Down
26 changes: 17 additions & 9 deletions src/routes/governance/dreps/drep-id/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,26 @@ async function route(fastify: FastifyInstance) {
handler: async (request: FastifyRequest<QueryTypes.RequestDRepID>, reply) => {
const clientDbSync = await getDbSync(fastify);

const { rows }: { rows: ResponseTypes.DRepsDrepID[] } =
await clientDbSync.query<QueryTypes.DRepsDrepID>(SQLQuery.get('governance_dreps_drep_id'), [
request.params.drep_id,
]);
try {
const { rows }: { rows: ResponseTypes.DRepsDrepID[] } =
await clientDbSync.query<QueryTypes.DRepsDrepID>(
SQLQuery.get('governance_dreps_drep_id'),
[request.params.drep_id],
);

clientDbSync.release();
const row = rows[0];
clientDbSync.release();
const row = rows[0];

if (!row) {
return handle404(reply);
if (!row) {
return handle404(reply);
}
return reply.send(row);
} catch (error) {
if (clientDbSync) {
clientDbSync.release();
}
throw error;
}
return reply.send(row);
},
});
}
Expand Down
27 changes: 17 additions & 10 deletions src/routes/governance/dreps/drep-id/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,27 @@ async function route(fastify: FastifyInstance) {
handler: async (request: FastifyRequest<QueryTypes.RequestDRepID>, reply) => {
const clientDbSync = await getDbSync(fastify);

const { rows }: { rows: ResponseTypes.DRepsDrepIDMetadata[] } =
await clientDbSync.query<QueryTypes.DRepsDrepIDMetadata>(
SQLQuery.get('governance_dreps_drep_id_metadata'),
[request.params.drep_id],
);
try {
const { rows }: { rows: ResponseTypes.DRepsDrepIDMetadata[] } =
await clientDbSync.query<QueryTypes.DRepsDrepIDMetadata>(
SQLQuery.get('governance_dreps_drep_id_metadata'),
[request.params.drep_id],
);

clientDbSync.release();
clientDbSync.release();

const row = rows[0];
const row = rows[0];

if (!row) {
return handle404(reply);
if (!row) {
return handle404(reply);
}
return reply.send(row);
} catch (error) {
if (clientDbSync) {
clientDbSync.release();
}
throw error;
}
return reply.send(row);
},
});
}
Expand Down
34 changes: 24 additions & 10 deletions src/routes/governance/dreps/drep-id/updates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as ResponseTypes from '../../../../types/responses/governance.js';
import { getDbSync } from '../../../../utils/database.js';
import { SQLQuery } from '../../../../sql/index.js';
import { getSchemaForEndpoint } from '@blockfrost/openapi';
import { isUnpaged } from '../../../../utils/routes.js';

async function route(fastify: FastifyInstance) {
fastify.route({
Expand All @@ -14,19 +15,32 @@ async function route(fastify: FastifyInstance) {
handler: async (request: FastifyRequest<QueryTypes.RequestParametersDRepID>, reply) => {
const clientDbSync = await getDbSync(fastify);

const { rows }: { rows: ResponseTypes.DRepsDrepIDUpdates } =
await clientDbSync.query<QueryTypes.DRepsDrepIDUpdates>(
SQLQuery.get('governance_dreps_drep_id_updates'),
[request.query.order, request.query.count, request.query.page, request.params.drep_id],
);
try {
const unpaged = isUnpaged(request);
const { rows }: { rows: ResponseTypes.DRepsDrepIDUpdates } = unpaged
? await clientDbSync.query<QueryTypes.DRepsDrepIDUpdates>(
SQLQuery.get('governance_dreps_drep_id_updates_unpaged'),
[request.query.order, request.params.drep_id],
)
: await clientDbSync.query<QueryTypes.DRepsDrepIDUpdates>(
SQLQuery.get('governance_dreps_drep_id_updates'),
[
request.query.order,
request.query.count,
request.query.page,
request.params.drep_id,
],
);

clientDbSync.release();
clientDbSync.release();

if (rows.length === 0) {
return reply.send([]);
return reply.send(rows);
} catch (error) {
if (clientDbSync) {
clientDbSync.release();
}
throw error;
}

return reply.send(rows);
},
});
}
Expand Down
34 changes: 24 additions & 10 deletions src/routes/governance/dreps/drep-id/votes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as ResponseTypes from '../../../../types/responses/governance.js';
import { getDbSync } from '../../../../utils/database.js';
import { SQLQuery } from '../../../../sql/index.js';
import { getSchemaForEndpoint } from '@blockfrost/openapi';
import { isUnpaged } from '../../../../utils/routes.js';

async function route(fastify: FastifyInstance) {
fastify.route({
Expand All @@ -14,19 +15,32 @@ async function route(fastify: FastifyInstance) {
handler: async (request: FastifyRequest<QueryTypes.RequestParametersDRepID>, reply) => {
const clientDbSync = await getDbSync(fastify);

const { rows }: { rows: ResponseTypes.DRepsDrepIDVotes } =
await clientDbSync.query<QueryTypes.DRepsDrepIDVotes>(
SQLQuery.get('governance_dreps_drep_id_votes'),
[request.query.order, request.query.count, request.query.page, request.params.drep_id],
);
try {
const unpaged = isUnpaged(request);
const { rows }: { rows: ResponseTypes.DRepsDrepIDUpdates } = unpaged
? await clientDbSync.query<QueryTypes.DRepsDrepIDUpdates>(
SQLQuery.get('governance_dreps_drep_id_votes_unpaged'),
[request.query.order, request.params.drep_id],
)
: await clientDbSync.query<QueryTypes.DRepsDrepIDUpdates>(
SQLQuery.get('governance_dreps_drep_id_votes'),
[
request.query.order,
request.query.count,
request.query.page,
request.params.drep_id,
],
);

clientDbSync.release();
clientDbSync.release();

if (rows.length === 0) {
return reply.send([]);
return reply.send(rows);
} catch (error) {
if (clientDbSync) {
clientDbSync.release();
}
throw error;
}

return reply.send(rows);
},
});
}
Expand Down
15 changes: 11 additions & 4 deletions src/routes/governance/dreps/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as QueryTypes from '../../../types/queries/governance.js';
import * as ResponseTypes from '../../../types/responses/governance.js';
import { getDbSync } from '../../../utils/database.js';
import { getSchemaForEndpoint } from '@blockfrost/openapi';
import { isUnpaged } from '../../../utils/routes.js';

async function route(fastify: FastifyInstance) {
fastify.route({
Expand All @@ -14,10 +15,16 @@ async function route(fastify: FastifyInstance) {
const clientDbSync = await getDbSync(fastify);

try {
const { rows }: { rows: ResponseTypes.DReps } = await clientDbSync.query<QueryTypes.DReps>(
SQLQuery.get('governance_dreps'),
[request.query.order, request.query.count, request.query.page],
);
const unpaged = isUnpaged(request);
const { rows }: { rows: ResponseTypes.DReps } = unpaged
? await clientDbSync.query<QueryTypes.DReps>(SQLQuery.get('governance_dreps_unpaged'), [
request.query.order,
])
: await clientDbSync.query<QueryTypes.DReps>(SQLQuery.get('governance_dreps'), [
request.query.order,
request.query.count,
request.query.page,
]);

clientDbSync.release();

Expand Down
30 changes: 20 additions & 10 deletions src/routes/governance/proposals/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as ResponseTypes from '../../../types/responses/governance.js';
import { getDbSync } from '../../../utils/database.js';
import { SQLQuery } from '../../../sql/index.js';
import { getSchemaForEndpoint } from '@blockfrost/openapi';
import { isUnpaged } from '../../../utils/routes.js';

async function route(fastify: FastifyInstance) {
fastify.route({
Expand All @@ -14,19 +15,28 @@ async function route(fastify: FastifyInstance) {
handler: async (request: FastifyRequest<QueryTypes.RequestParametersDRepID>, reply) => {
const clientDbSync = await getDbSync(fastify);

const { rows }: { rows: ResponseTypes.DRepsDrepIDVotes } =
await clientDbSync.query<QueryTypes.DRepsDrepIDVotes>(
SQLQuery.get('governance_proposals'),
[request.query.order, request.query.count, request.query.page],
);
try {
const unpaged = isUnpaged(request);
const { rows }: { rows: ResponseTypes.Proposals } = unpaged
? await clientDbSync.query<QueryTypes.Proposals>(
SQLQuery.get('governance_proposals_unpaged'),
[request.query.order],
)
: await clientDbSync.query<QueryTypes.Proposals>(SQLQuery.get('governance_proposals'), [
request.query.order,
request.query.count,
request.query.page,
]);

clientDbSync.release();
clientDbSync.release();

if (rows.length === 0) {
return reply.send([]);
return reply.send(rows);
} catch (error) {
if (clientDbSync) {
clientDbSync.release();
}
throw error;
}

return reply.send(rows);
},
});
}
Expand Down
27 changes: 17 additions & 10 deletions src/routes/governance/proposals/tx-hash/cert-index/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,26 @@ async function route(fastify: FastifyInstance) {
handler: async (request: FastifyRequest<QueryTypes.RequestParametersProposal>, reply) => {
const clientDbSync = await getDbSync(fastify);

const { rows }: { rows: ResponseTypes.ProposalsProposal[] } =
await clientDbSync.query<QueryTypes.ProposalsProposal>(
SQLQuery.get('governance_proposals_proposal'),
[request.params.tx_hash, request.params.cert_index],
);
try {
const { rows }: { rows: ResponseTypes.ProposalsProposal[] } =
await clientDbSync.query<QueryTypes.ProposalsProposal>(
SQLQuery.get('governance_proposals_proposal'),
[request.params.tx_hash, request.params.cert_index],
);

clientDbSync.release();
const row = rows[0];
clientDbSync.release();
const row = rows[0];

if (!row) {
return handle404(reply);
if (!row) {
return handle404(reply);
}
return reply.send(row);
} catch (error) {
if (clientDbSync) {
clientDbSync.release();
}
throw error;
}
return reply.send(row);
},
});
}
Expand Down
27 changes: 17 additions & 10 deletions src/routes/governance/proposals/tx-hash/cert-index/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,27 @@ async function route(fastify: FastifyInstance) {
handler: async (request: FastifyRequest<QueryTypes.RequestParametersProposal>, reply) => {
const clientDbSync = await getDbSync(fastify);

const { rows }: { rows: ResponseTypes.ProposalsProposalMetadata[] } =
await clientDbSync.query<QueryTypes.ProposalsProposalMetadata>(
SQLQuery.get('governance_proposals_proposal_metadata'),
[request.params.tx_hash, request.params.cert_index],
);
try {
const { rows }: { rows: ResponseTypes.ProposalsProposalMetadata[] } =
await clientDbSync.query<QueryTypes.ProposalsProposalMetadata>(
SQLQuery.get('governance_proposals_proposal_metadata'),
[request.params.tx_hash, request.params.cert_index],
);

clientDbSync.release();
clientDbSync.release();

const row = rows[0];
const row = rows[0];

if (!row) {
return handle404(reply);
if (!row) {
return handle404(reply);
}
return reply.send(row);
} catch (error) {
if (clientDbSync) {
clientDbSync.release();
}
throw error;
}
return reply.send(row);
},
});
}
Expand Down
Loading

0 comments on commit 6010cd2

Please sign in to comment.