Skip to content

Instantly share code, notes, and snippets.

@dhmlau
Last active June 12, 2021 00:20
Show Gist options
  • Save dhmlau/64a81f4d0a5dc9766abced29bdfca6e4 to your computer and use it in GitHub Desktop.
Save dhmlau/64a81f4d0a5dc9766abced29bdfca6e4 to your computer and use it in GitHub Desktop.
GitHubQueryController
// Uncomment these imports to begin using these cool features!
import {inject} from '@loopback/context';
import {get, param} from '@loopback/openapi-v3';
import {GhQueryService, QueryResponse} from '../services';
// import {inject} from '@loopback/core';
export class GhQueryController {
// inject the GhQueryService service proxy
constructor(@inject('services.GhQueryService') protected queryService:GhQueryService) {}
// create the API that get the issues by providing:
// repo: <GitHub org>/<GitHub repo>. For example, `strongloop/loopback-next`
// label: If it has special characters, you need to escape it.
// For example, if the label is "help wanted", it will be "help+wanted".
@get('/issues/repo/{repo}/label/{label}')
async getIssuesByLabel(
@param.path.string('repo') repo: string,
@param.path.string('label') label:string): Promise<QueryResult> {
let result:QueryResponse = await this.queryService.getIssuesByLabel(repo, label);
let queryResult = new QueryResult();
queryResult.items = [];
queryResult.total_count = result.body.total_count;
result.body.items.forEach(issue => {
let issueInfo:ResultIssueInfo = new ResultIssueInfo();
issueInfo.html_url = issue.html_url;
issueInfo.title = issue.title;
issueInfo.state = issue.state;
issueInfo.age = this.getIssueAge(issue.created_at);
queryResult.items.push(issueInfo);
});
return queryResult;
}
getIssueAge(created_at: string): number {
let todayDate: Date = new Date();
let createDate: Date = new Date(created_at);
let differenceInTime = todayDate.getTime() - createDate.getTime();
//get the difference in day
return Math.floor(differenceInTime / (1000 * 3600 * 24));
}
}
/**
* QueryResult
*/
class QueryResult {
total_count: number;
items: ResultIssueInfo[];
}
class ResultIssueInfo {
title: string;
html_url: string;
state: string;
age: number;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment