Added Metadata to Gist API routes

This commit is contained in:
Jyotirmoy Bandyopadhayaya 2023-06-24 13:09:28 +05:30
parent 3369a826c5
commit 42cbc2e6ec
Signed by: bravo68web
GPG Key ID: F5671FD7BCB9917A
3 changed files with 53 additions and 11 deletions

View File

@ -126,12 +126,12 @@ export default class GistController
limit: string;
page: string;
};
const gists = await this.listGistsS(
const { data, meta } = await this.listGistsS(
search,
parseInt(page),
parseInt(limit)
);
res.status(200).json(makeResponse(gists));
res.status(200).json(makeResponse(data, meta));
} catch (error) {
next(error);
}

View File

@ -96,13 +96,17 @@ export default class URLStoreController
next: NextFunction
): Promise<Response | void> => {
try {
const { query, limit, page } = req.query;
const urlstore = await this.getAllURLS(
query as string,
Number(limit),
Number(page)
const { query, limit, page } = req.query as {
query: string;
limit: string;
page: string;
};
const { data, meta } = await this.getAllURLS(
query,
parseInt(limit),
parseInt(page)
);
res.status(200).json(makeResponse(urlstore));
res.status(200).json(makeResponse(data, meta));
} catch (error) {
next(error);
}

View File

@ -1,6 +1,10 @@
import { gql } from 'graphql-request';
import { client } from '../helpers';
import { IGistService, IPrivate } from '../interfaces/gists.interface';
import {
IGistService,
IListGists,
IPrivate,
} from '../interfaces/gists.interface';
import { UserMeta } from '../types';
import { Gists, Gists_Mutation_Response } from '../graphql/types';
@ -127,7 +131,7 @@ export default class GistService implements IGistService {
searchString = '',
pageNo = 1,
pageSize = 10
): Promise<Gists[]> {
): Promise<IListGists> {
const query = gql`
query listGists($searchString: String!, $pageNo: Int!, $pageSize: Int!) {
gists(
@ -148,6 +152,19 @@ export default class GistService implements IGistService {
isOneTimeOnly
views
}
gists_aggregate(
where: {
_or: [
{ content: { _iregex: $searchString } }
{ gist_url_key: { _iregex: $searchString } }
]
}
) {
aggregate {
count
}
}
}
`;
const variables = {
@ -157,8 +174,29 @@ export default class GistService implements IGistService {
};
const result: {
gists: Gists[];
gists_aggregate: {
aggregate: {
count: number;
};
};
} = await client.request(query, variables);
return result.gists;
result.gists = result.gists.map(gist => {
if (gist.isPrivate) {
gist.content = 'Locked 🔒';
}
return gist;
});
return {
data: result.gists,
meta: {
pageNo,
pageSize,
total: result.gists_aggregate.aggregate.count,
totalPages: Math.ceil(
result.gists_aggregate.aggregate.count / pageSize
),
},
};
}
public async updateGistS(gistID: string, content: string): Promise<number> {