adding project page
This commit is contained in:
@@ -2,10 +2,12 @@ import { Module } from '@nestjs/common';
|
||||
import { GitHubApiModule } from './github/github.module';
|
||||
import { APP_FILTER } from '@nestjs/core';
|
||||
import { HttpExceptionFilter } from './filters/http-exception.filter';
|
||||
import { ProjectModule } from './project/project.module';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
GitHubApiModule
|
||||
GitHubApiModule,
|
||||
ProjectModule
|
||||
],
|
||||
providers: [
|
||||
{
|
||||
|
||||
@@ -1,20 +0,0 @@
|
||||
export interface Content {
|
||||
name: string
|
||||
path: string
|
||||
sha: string
|
||||
size: number
|
||||
url: string
|
||||
html_url: string
|
||||
git_url: string
|
||||
download_url: string
|
||||
type: string
|
||||
content: string
|
||||
encoding: string
|
||||
_links: Links
|
||||
}
|
||||
|
||||
interface Links {
|
||||
self: string
|
||||
git: string
|
||||
html: string
|
||||
}
|
||||
@@ -1,120 +0,0 @@
|
||||
export interface Repo {
|
||||
id: number
|
||||
node_id: string
|
||||
name: string
|
||||
full_name: string
|
||||
private: boolean
|
||||
owner: Owner
|
||||
html_url: string
|
||||
description?: string
|
||||
fork: boolean
|
||||
url: string
|
||||
forks_url: string
|
||||
keys_url: string
|
||||
collaborators_url: string
|
||||
teams_url: string
|
||||
hooks_url: string
|
||||
issue_events_url: string
|
||||
events_url: string
|
||||
assignees_url: string
|
||||
branches_url: string
|
||||
tags_url: string
|
||||
blobs_url: string
|
||||
git_tags_url: string
|
||||
git_refs_url: string
|
||||
trees_url: string
|
||||
statuses_url: string
|
||||
languages_url: string
|
||||
stargazers_url: string
|
||||
contributors_url: string
|
||||
subscribers_url: string
|
||||
subscription_url: string
|
||||
commits_url: string
|
||||
git_commits_url: string
|
||||
comments_url: string
|
||||
issue_comment_url: string
|
||||
contents_url: string
|
||||
compare_url: string
|
||||
merges_url: string
|
||||
archive_url: string
|
||||
downloads_url: string
|
||||
issues_url: string
|
||||
pulls_url: string
|
||||
milestones_url: string
|
||||
notifications_url: string
|
||||
labels_url: string
|
||||
releases_url: string
|
||||
deployments_url: string
|
||||
created_at: string
|
||||
updated_at: string
|
||||
pushed_at: string
|
||||
git_url: string
|
||||
ssh_url: string
|
||||
clone_url: string
|
||||
svn_url: string
|
||||
homepage?: string
|
||||
size: number
|
||||
stargazers_count: number
|
||||
watchers_count: number
|
||||
language?: string
|
||||
has_issues: boolean
|
||||
has_projects: boolean
|
||||
has_downloads: boolean
|
||||
has_wiki: boolean
|
||||
has_pages: boolean
|
||||
has_discussions: boolean
|
||||
forks_count: number
|
||||
mirror_url: any
|
||||
archived: boolean
|
||||
disabled: boolean
|
||||
open_issues_count: number
|
||||
license?: License
|
||||
allow_forking: boolean
|
||||
is_template: boolean
|
||||
web_commit_signoff_required: boolean
|
||||
topics: any[]
|
||||
visibility: string
|
||||
forks: number
|
||||
open_issues: number
|
||||
watchers: number
|
||||
default_branch: string
|
||||
permissions: Permissions
|
||||
}
|
||||
|
||||
interface Owner {
|
||||
login: string
|
||||
id: number
|
||||
node_id: string
|
||||
avatar_url: string
|
||||
gravatar_id: string
|
||||
url: string
|
||||
html_url: string
|
||||
followers_url: string
|
||||
following_url: string
|
||||
gists_url: string
|
||||
starred_url: string
|
||||
subscriptions_url: string
|
||||
organizations_url: string
|
||||
repos_url: string
|
||||
events_url: string
|
||||
received_events_url: string
|
||||
type: string
|
||||
user_view_type: string
|
||||
site_admin: boolean
|
||||
}
|
||||
|
||||
interface License {
|
||||
key: string
|
||||
name: string
|
||||
spdx_id: string
|
||||
url: string
|
||||
node_id: string
|
||||
}
|
||||
|
||||
interface Permissions {
|
||||
admin: boolean
|
||||
maintain: boolean
|
||||
push: boolean
|
||||
triage: boolean
|
||||
pull: boolean
|
||||
}
|
||||
@@ -1,35 +0,0 @@
|
||||
import { Controller, Get, HttpException, Query } from '@nestjs/common';
|
||||
import { GitHubApiService } from './github.service';
|
||||
import { AxiosError } from 'axios'
|
||||
import { Content } from './github-content.interface';
|
||||
import { Repo } from './github-repo.interface';
|
||||
|
||||
@Controller('github')
|
||||
export class GitHubApiController {
|
||||
constructor(private readonly githubApiService: GitHubApiService) {}
|
||||
|
||||
@Get('readme')
|
||||
async getReadMe(@Query() query: any): Promise<Content> {
|
||||
try {
|
||||
const repoName = query['repoName'];
|
||||
|
||||
return await this.githubApiService.getReadMeContent(repoName)
|
||||
} catch (error) {
|
||||
const axiosError = error as AxiosError;
|
||||
|
||||
throw new HttpException(axiosError.message, axiosError.status)
|
||||
}
|
||||
}
|
||||
|
||||
@Get('repos')
|
||||
async getRepos(): Promise<Repo[]> {
|
||||
try {
|
||||
return await this.githubApiService.getRepos();
|
||||
} catch (error) {
|
||||
const axiosError = error as AxiosError
|
||||
|
||||
throw new HttpException(axiosError.message, axiosError.status)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,18 +0,0 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { GitHubApiController } from './github.controller';
|
||||
import { GitHubApiService } from './github.service';
|
||||
import { ConfigModule } from '@nestjs/config';
|
||||
import configuration from '../config/configuration';
|
||||
import { HttpModule } from '@nestjs/axios';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
ConfigModule.forRoot({
|
||||
load: [configuration]
|
||||
}),
|
||||
HttpModule
|
||||
],
|
||||
controllers: [GitHubApiController],
|
||||
providers: [GitHubApiService],
|
||||
})
|
||||
export class GitHubApiModule {}
|
||||
@@ -1,39 +0,0 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
import { HttpService } from '@nestjs/axios';
|
||||
import { AxiosResponse } from 'axios';
|
||||
import { Repo } from './github-repo.interface';
|
||||
import { Content } from './github-content.interface';
|
||||
|
||||
@Injectable()
|
||||
export class GitHubApiService {
|
||||
constructor(
|
||||
private readonly configService: ConfigService,
|
||||
private readonly httpService: HttpService
|
||||
) {}
|
||||
|
||||
async getReadMeContent(repoName: string): Promise<Content> {
|
||||
const githubApiUsername = this.configService.get<string>('githubApiUsername')
|
||||
const response: AxiosResponse = await this.httpService.axiosRef.get(`https://api.github.com/repos/${githubApiUsername}/${repoName}/contents/README.md`, {
|
||||
auth: {
|
||||
username: this.configService.get<string>('githubApiUsername'),
|
||||
password: this.configService.get<string>('githubApiPassword')
|
||||
}
|
||||
})
|
||||
const content: Content = response.data
|
||||
|
||||
return content;
|
||||
}
|
||||
|
||||
async getRepos(): Promise<Repo[]> {
|
||||
const response: AxiosResponse = await this.httpService.axiosRef.get('https://api.github.com/user/repos', {
|
||||
auth: {
|
||||
username: this.configService.get<string>('githubApiUsername'),
|
||||
password: this.configService.get<string>('githubApiPassword')
|
||||
}
|
||||
})
|
||||
const repos: Repo[] = response.data
|
||||
|
||||
return repos;
|
||||
}
|
||||
}
|
||||
@@ -1,26 +0,0 @@
|
||||
import { INestApplication, InternalServerErrorException } from '@nestjs/common';
|
||||
import { NestFactory } from '@nestjs/core';
|
||||
import { AppModule } from './app.module';
|
||||
import { HttpService } from '@nestjs/axios'
|
||||
import { HttpExceptionFilter } from './filters/http-exception.filter';
|
||||
|
||||
export async function createApp(): Promise<INestApplication> {
|
||||
const httpService = new HttpService();
|
||||
const app = await NestFactory.create(AppModule);
|
||||
|
||||
app.setGlobalPrefix('api');
|
||||
app.useGlobalFilters(new HttpExceptionFilter());
|
||||
httpService.axiosRef.interceptors.response.use(
|
||||
(response) => {
|
||||
return response;
|
||||
},
|
||||
(error) => {
|
||||
console.error('Internal server error exception', error);
|
||||
|
||||
throw new InternalServerErrorException();
|
||||
}
|
||||
);
|
||||
|
||||
await app.init();
|
||||
return app;
|
||||
}
|
||||
@@ -1,8 +1,26 @@
|
||||
import { NestFactory } from '@nestjs/core';
|
||||
import { AppModule } from './app.module';
|
||||
import { HttpService } from '@nestjs/axios';
|
||||
import { HttpExceptionFilter } from './filters/http-exception.filter';
|
||||
import { InternalServerErrorException } from '@nestjs/common';
|
||||
|
||||
async function bootstrap() {
|
||||
const httpService = new HttpService();
|
||||
const app = await NestFactory.create(AppModule);
|
||||
|
||||
app.enableCors();
|
||||
app.setGlobalPrefix('api');
|
||||
app.useGlobalFilters(new HttpExceptionFilter());
|
||||
httpService.axiosRef.interceptors.response.use(
|
||||
(response) => {
|
||||
return response;
|
||||
},
|
||||
(error) => {
|
||||
console.error('Internal server error exception', error);
|
||||
|
||||
throw new InternalServerErrorException();
|
||||
}
|
||||
);
|
||||
|
||||
await app.listen(3000);
|
||||
}
|
||||
|
||||
92
api/src/project/project.controller.ts
Normal file
92
api/src/project/project.controller.ts
Normal file
@@ -0,0 +1,92 @@
|
||||
import {
|
||||
Body,
|
||||
Controller,
|
||||
Delete,
|
||||
Get,
|
||||
HttpException,
|
||||
Param,
|
||||
Post,
|
||||
Put
|
||||
} from '@nestjs/common';
|
||||
import { ProjectDto } from './project.dto';
|
||||
import { Project } from './project.entity';
|
||||
import { ProjectService } from './project.service';
|
||||
import { CustomError } from '@noahspan/noahspan-modules';
|
||||
|
||||
@Controller('projects')
|
||||
export class ProjectController {
|
||||
constructor(private readonly projectService: ProjectService) {}
|
||||
|
||||
@Get(':partitionKey/:rowKey')
|
||||
async find(
|
||||
@Param('partitionKey') partitionKey: string,
|
||||
@Param('rowKey') rowKey: string
|
||||
) {
|
||||
try {
|
||||
return await this.projectService.find(partitionKey, rowKey);
|
||||
} catch (error) {
|
||||
const customError = error as CustomError;
|
||||
|
||||
throw new HttpException(customError.message, customError.statusCode);
|
||||
}
|
||||
}
|
||||
|
||||
@Get()
|
||||
async findAll() {
|
||||
try {
|
||||
return await this.projectService.findAll();
|
||||
} catch (error) {
|
||||
const customError = error as CustomError;
|
||||
|
||||
throw new HttpException(customError.message, customError.statusCode);
|
||||
}
|
||||
}
|
||||
|
||||
@Post()
|
||||
async create(@Body() projectDto: ProjectDto) {
|
||||
try {
|
||||
const project = new Project();
|
||||
|
||||
Object.assign(project, projectDto);
|
||||
|
||||
return await this.projectService.create(project);
|
||||
} catch (error) {
|
||||
const customError = error as CustomError;
|
||||
|
||||
throw new HttpException(customError.message, customError.statusCode);
|
||||
}
|
||||
}
|
||||
|
||||
@Put(':partitionKey/:rowKey')
|
||||
async update(
|
||||
@Param('partitionKey') partitionKey: string,
|
||||
@Param('rowKey') rowKey: string,
|
||||
@Body() projectDto: ProjectDto
|
||||
) {
|
||||
try {
|
||||
const project = new Project();
|
||||
|
||||
Object.assign(project, projectDto);
|
||||
|
||||
return await this.projectService.update(partitionKey, rowKey, project);
|
||||
} catch (error) {
|
||||
const customError = error as CustomError;
|
||||
|
||||
throw new HttpException(customError.message, customError.statusCode);
|
||||
}
|
||||
}
|
||||
|
||||
@Delete(':partitionKey/:rowKey')
|
||||
async delete(
|
||||
@Param('partitionKey') partitionKey: string,
|
||||
@Param('rowKey') rowKey: string
|
||||
) {
|
||||
try {
|
||||
return await this.projectService.delete(partitionKey, rowKey);
|
||||
} catch (error) {
|
||||
const customError = error as CustomError;
|
||||
|
||||
throw new HttpException(customError.message, customError.statusCode);
|
||||
}
|
||||
}
|
||||
}
|
||||
6
api/src/project/project.dto.ts
Normal file
6
api/src/project/project.dto.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
export class ProjectDto {
|
||||
rowKey: string;
|
||||
displayName: string;
|
||||
icon: string;
|
||||
summary: string;
|
||||
}
|
||||
7
api/src/project/project.entity.ts
Normal file
7
api/src/project/project.entity.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
export class Project {
|
||||
partitionKey: string;
|
||||
rowKey: string;
|
||||
displayName: string;
|
||||
icon: string;
|
||||
summary: string;
|
||||
}
|
||||
27
api/src/project/project.module.ts
Normal file
27
api/src/project/project.module.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { ProjectController } from './project.controller';
|
||||
import { ProjectService } from './project.service';
|
||||
import { AzureTableStorageModule } from '@noahspan/azure-database';
|
||||
import { Project } from './project.entity';
|
||||
import { ConfigModule, ConfigService } from '@nestjs/config';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
AzureTableStorageModule.forRootAsync({
|
||||
imports: [ConfigModule],
|
||||
useFactory: async (configService: ConfigService) => {
|
||||
return {
|
||||
connectionString: configService.get<string>('azureStorageConnectionString')
|
||||
};
|
||||
},
|
||||
inject: [ConfigService]
|
||||
}),
|
||||
AzureTableStorageModule.forFeature(Project, {
|
||||
createTableIfNotExists: true,
|
||||
table: 'projects'
|
||||
}),
|
||||
],
|
||||
controllers: [ProjectController],
|
||||
providers: [ProjectService]
|
||||
})
|
||||
export class ProjectModule {}
|
||||
38
api/src/project/project.service.ts
Normal file
38
api/src/project/project.service.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
import { InjectRepository, Repository } from '@noahspan/azure-database';
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { Project } from './project.entity';
|
||||
import { v4 as uuidv4 } from 'uuid';
|
||||
|
||||
@Injectable()
|
||||
export class ProjectService {
|
||||
constructor(
|
||||
@InjectRepository(Project)
|
||||
private readonly projectRepository: Repository<Project>
|
||||
) {}
|
||||
|
||||
async find(partitionKey: string, rowKey: string): Promise<Project> {
|
||||
return await this.projectRepository.find(partitionKey, rowKey);
|
||||
}
|
||||
|
||||
async findAll(): Promise<Project[]> {
|
||||
return await this.projectRepository.findAll();
|
||||
}
|
||||
|
||||
async create(project: Project): Promise<Project> {
|
||||
project.partitionKey = 'project';
|
||||
|
||||
return await this.projectRepository.create(project);
|
||||
}
|
||||
|
||||
async update(
|
||||
partitionKey: string,
|
||||
rowKey: string,
|
||||
project: Project
|
||||
): Promise<Project> {
|
||||
return await this.projectRepository.update(partitionKey, rowKey, project);
|
||||
}
|
||||
|
||||
async delete(partitionKey: string, rowKey: string): Promise<void> {
|
||||
await this.projectRepository.delete(partitionKey, rowKey);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user