Added Metadata to URLStore API routes

This commit is contained in:
Jyotirmoy Bandyopadhayaya 2023-06-24 13:11:31 +05:30
parent 5e5bbc2bed
commit 658cbe5039
Signed by: bravo68web
GPG Key ID: F5671FD7BCB9917A
3 changed files with 46 additions and 8 deletions

View File

@ -43,11 +43,21 @@ export interface IURLStoreService {
searchQuery: string,
limit: number,
offset: number
): Promise<Shorturls[]>;
): Promise<IListURLS>;
getaURLS(urlID: string): Promise<Shorturls>;
updateURLS(urlKey: string, updateObject: any): Promise<number>;
}
export interface IListURLS {
data: Shorturls[];
meta: {
total: number;
pageNo: number;
pageSize: number;
totalPages: number;
};
}
export interface URLStoreRep extends Shorturls {
url?: string;
}

View File

@ -1,7 +1,7 @@
import { gql } from 'graphql-request';
import { client } from '../helpers';
import { UserMeta } from '../types';
import { IURLStoreService } from '../interfaces/urlstore.interface';
import { IListURLS, IURLStoreService } from '../interfaces/urlstore.interface';
import { Shorturls, Shorturls_Mutation_Response } from '../graphql/types';
export default class URLStore implements IURLStoreService {
@ -64,8 +64,8 @@ export default class URLStore implements IURLStoreService {
public async getAllURLS(
searchQuery = '',
limit = 10,
offset = 0
): Promise<Shorturls[]> {
offset = 1
): Promise<IListURLS> {
const query = gql`
query getAllURLS($searchQuery: String!, $limit: Int!, $offset: Int!) {
shorturls(
@ -83,17 +83,45 @@ export default class URLStore implements IURLStoreService {
short_key
urlID
}
shorturls_aggregate(
where: {
_or: [
{ short_key: { _iregex: $searchQuery } }
{ original_url: { _iregex: $searchQuery } }
]
}
) {
aggregate {
count
}
}
}
`;
const variables = {
searchQuery,
limit,
offset,
offset: (offset - 1) * limit,
};
const result: {
shorturls: Shorturls[];
shorturls_aggregate: {
aggregate: {
count: number;
};
};
} = await client.request(query, variables);
return result.shorturls;
return {
data: result.shorturls,
meta: {
pageNo: offset,
pageSize: limit,
total: result.shorturls_aggregate.aggregate.count,
totalPages: Math.ceil(
result.shorturls_aggregate.aggregate.count / limit
),
},
};
}
public async getaURLS(urlID: string): Promise<Shorturls> {

View File

@ -58,8 +58,8 @@ export const urlGelAllValidation = async (
try {
const schema = Joi.object().keys({
query: Joi.string().optional(),
page: Joi.number().optional(),
limit: Joi.number().optional(),
page: Joi.number().optional().default(1),
limit: Joi.number().optional().default(10),
});
req.query = await schema.validateAsync(req.query);
next();