-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #277 from cardanoapi/enhancement/dbsync-api
Enhancement/dbsync api
- Loading branch information
Showing
5 changed files
with
747 additions
and
395 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,68 @@ | ||
import { Request, Response, Router } from "express"; | ||
import { handlerWrapper } from "../errors/AppError"; | ||
import { convertToHexIfBech32, isHexValue } from "../helpers/validator"; | ||
import { fetchDrepDetails, fetchDrepList } from "../repository/drep"; | ||
import { | ||
fetchDrepDelegationDetails, | ||
fetchDrepDetails, | ||
fetchDrepList, | ||
fetchDrepRegistrationDetails, | ||
fetchDrepVoteDetails | ||
} from "../repository/drep"; | ||
import { DrepSortType, DrepStatusType } from "../types/drep"; | ||
|
||
const router = Router(); | ||
|
||
const getDrepDetails = async (req: Request, res: Response): Promise<any> => { | ||
const dRepId = convertToHexIfBech32(req.params.id as string) | ||
if (dRepId && !isHexValue(dRepId)){ | ||
return res.status(400).json({message:'Provide a valid Drep ID'}) | ||
} | ||
const result = await fetchDrepDetails(dRepId) | ||
return res.status(200).json(result); | ||
const dRepId = convertToHexIfBech32(req.params.id as string) | ||
if (dRepId && !isHexValue(dRepId)) { | ||
return res.status(400).json({message: 'Provide a valid Drep ID'}) | ||
} | ||
const result = await fetchDrepDetails(dRepId) | ||
return res.status(200).json(result); | ||
}; | ||
|
||
const getDrepList= async(req:Request,res:Response )=>{ | ||
const size = req.query.size ? +req.query.size : 10 | ||
const page = req.query.page ? +req.query.page : 1 | ||
const status = req.query.status ? req.query.status as DrepStatusType : undefined | ||
const sort = req.query.sort ? req.query.sort as DrepSortType : undefined | ||
const search = convertToHexIfBech32(req.query.search as string) | ||
const { items,totalCount } = await fetchDrepList(page,size,search,status,sort) | ||
return res.status(200).json({total:totalCount,page,size,items}) | ||
const getDrepList = async (req: Request, res: Response) => { | ||
const size = req.query.size ? +req.query.size : 10 | ||
const page = req.query.page ? +req.query.page : 1 | ||
const status = req.query.status ? req.query.status as DrepStatusType : undefined | ||
const sort = req.query.sort ? req.query.sort as DrepSortType : undefined | ||
const search = convertToHexIfBech32(req.query.search as string) | ||
const {items, totalCount} = await fetchDrepList(page, size, search, status, sort) | ||
return res.status(200).json({total: totalCount, page, size, items}) | ||
} | ||
|
||
const getDrepVoteDetails = async (req: Request, res: Response) => { | ||
const dRepId = convertToHexIfBech32(req.params.id as string) | ||
if (dRepId && !isHexValue(dRepId)) { | ||
return res.status(400).json({message: 'Provide a valid Drep ID'}) | ||
} | ||
const result = await fetchDrepVoteDetails(dRepId) | ||
return res.status(200).json(result); | ||
} | ||
|
||
const getDrepDelegationDetails = async (req: Request, res: Response) => { | ||
const dRepId = convertToHexIfBech32(req.params.id as string) | ||
if (dRepId && !isHexValue(dRepId)) { | ||
return res.status(400).json({message: 'Provide a valid Drep ID'}) | ||
} | ||
const result = await fetchDrepDelegationDetails(dRepId) | ||
return res.status(200).json(result); | ||
} | ||
|
||
const getDrepRegistrationDetails = async (req: Request, res: Response) => { | ||
const dRepId = convertToHexIfBech32(req.params.id as string) | ||
if (dRepId && !isHexValue(dRepId)) { | ||
return res.status(400).json({message: 'Provide a valid Drep ID'}) | ||
} | ||
const result = await fetchDrepRegistrationDetails(dRepId) | ||
return res.status(200).json(result); | ||
} | ||
|
||
router.get('/', handlerWrapper(getDrepList)); | ||
router.get('/:id', handlerWrapper(getDrepDetails)); | ||
router.get('/',handlerWrapper(getDrepList)); | ||
router.get('/:id/vote', handlerWrapper(getDrepVoteDetails)) | ||
router.get('/:id/delegation', handlerWrapper(getDrepDelegationDetails)) | ||
router.get('/:id/registration', handlerWrapper(getDrepRegistrationDetails)) | ||
|
||
|
||
export default router; |
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,3 +1,32 @@ | ||
export function formatResult(result:Array<Record<string,any>>){ | ||
return result.map((item)=>item.result) | ||
} | ||
} | ||
|
||
export function combineArraysWithSameObjectKey(array1: any, array2: any) { | ||
// Create a map for array2 based on the stake_address | ||
const map2 = new Map(); | ||
array2.forEach((item: any) => { | ||
const { stakeAddress, ...rest } = item.json_build_object; | ||
map2.set(stakeAddress, rest); // Use stake_address as the key | ||
}); | ||
|
||
// Combine the data from array1 and array2 based on stake_address | ||
return array1.map((item1: any) => { | ||
const { stakeAddress, ...rest1 } = item1.json_build_object; | ||
const matchedData = map2.get(stakeAddress); | ||
|
||
if (matchedData) { | ||
// Combine both objects if match is found | ||
return { | ||
json_build_object: { | ||
...rest1, | ||
...matchedData, | ||
stakeAddress | ||
} | ||
}; | ||
} else { | ||
// Return the original object if no match is found in array2 | ||
return item1; | ||
} | ||
}); | ||
} |
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
Oops, something went wrong.