Skip to content

Commit

Permalink
Mongo datatabse connection
Browse files Browse the repository at this point in the history
  • Loading branch information
Vishwas1 committed Jan 16, 2023
1 parent d2456d8 commit 3574361
Show file tree
Hide file tree
Showing 8 changed files with 999 additions and 48 deletions.
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,16 @@
"dependencies": {
"@nestjs/common": "^9.0.0",
"@nestjs/core": "^9.0.0",
"@nestjs/mongoose": "^9.2.1",
"@nestjs/platform-express": "^9.0.0",
"@nestjs/swagger": "^6.1.4",
"class-transformer": "^0.5.1",
"class-validator": "^0.14.0",
"mongoose": "^6.8.3",
"reflect-metadata": "^0.1.13",
"rxjs": "^7.2.0",
"swagger-ui-express": "^4.6.0"
"swagger-ui-express": "^4.6.0",
"uuidv4": "^6.2.13"
},
"devDependencies": {
"@nestjs/cli": "^9.0.0",
Expand Down
6 changes: 5 additions & 1 deletion src/app-auth/app-auth.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@ import { MiddlewareConsumer, Module, NestModule, RequestMethod } from '@nestjs/c
import { AppAuthService } from './services/app-auth/app-auth.service';
import { AppAuthController } from './controllers/app-auth/app-auth.controller';
import { ValidateHeadersMiddleware } from './middlewares/validate-headers/validate-headers.middleware';
import { MongooseModule } from '@nestjs/mongoose';
import { App, AppSchema } from '../app-auth/schemas/App.schema';
import { AppRepository } from './repositories/App.repository';
@Module({
providers: [AppAuthService],
imports: [MongooseModule.forFeature([{ name: App.name, schema: AppSchema }])],
providers: [AppAuthService, AppRepository],
controllers: [AppAuthController]
})
export class AppAuthModule implements NestModule {
Expand Down
15 changes: 7 additions & 8 deletions src/app-auth/controllers/app-auth/app-auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import { Controller, Get, ValidationPipe, Post, UsePipes, Body, Req, Res, HttpSt
import { CreateAppDto } from 'src/app-auth/dtos/CreateApp.dto';
import { AppAuthService } from 'src/app-auth/services/app-auth/app-auth.service';
import { ApiBadRequestResponse, ApiCreatedResponse, ApiResponse, ApiTags } from '@nestjs/swagger';
import { IApp } from '../../types/App.types';
import { AppSchema } from '../../schemas/App.schema';
import { App } from '../../schemas/App.schema';
import { AppNotFoundException } from 'src/app-auth/exceptions/AppNotFound.exception';

@ApiTags('App')
Expand All @@ -14,21 +13,21 @@ export class AppAuthController {
@Get()
@ApiResponse({
description: 'List of apps',
type: [AppSchema]
type: [App]
})
getApps() {
getApps(): Promise<App[]>{
return this.appAuthService.getAllApps();
}

@Get(':appId')
@ApiResponse({
description: 'Fetch App by Id',
type: AppSchema
type: App
})
@ApiBadRequestResponse({
description: "Application not found"
})
getAppById(@Param('appId') appId: string): IApp {
getAppById(@Param('appId') appId: string): Promise<App> {
const app = this.appAuthService.getAppById(appId);
if(app) return app;
else throw new AppNotFoundException(); // Custom Exception handling
Expand All @@ -37,13 +36,13 @@ export class AppAuthController {
@Post('/register')
@ApiCreatedResponse({
description: 'Newly created app',
type: AppSchema
type: App
})
@ApiBadRequestResponse({
description: "Application could not be registered"
})
@UsePipes(ValidationPipe)
register(@Body() createAppDto: CreateAppDto): IApp{
register(@Body() createAppDto: CreateAppDto): Promise<App>{
return this.appAuthService.createAnApp(createAppDto)
}

Expand Down
29 changes: 29 additions & 0 deletions src/app-auth/repositories/App.repository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { App, AppDocument } from '../schemas/App.schema'
import { FilterQuery, Model } from 'mongoose';
import { InjectModel } from '@nestjs/mongoose'
import { Injectable } from '@nestjs/common';

@Injectable()
export class AppRepository {
constructor(@InjectModel(App.name) private readonly appModel: Model<AppDocument>) {}

async findOne(appFilterQuery: FilterQuery<App>): Promise<App>{
return this.appModel.findOne(appFilterQuery);
}

async find(appsFilterQuery: FilterQuery<App>): Promise<App[]>{
return this.appModel.find(appsFilterQuery);
}

async create(app: App): Promise<App>{
const newapp = new this.appModel(app)
return newapp.save();
}

async findOneAndUpdate(appFilterQuery: FilterQuery<App>, app: Partial<App>): Promise<App>{
return this.appModel.findOneAndUpdate(appFilterQuery, app);
}



}
35 changes: 17 additions & 18 deletions src/app-auth/schemas/App.schema.ts
Original file line number Diff line number Diff line change
@@ -1,56 +1,55 @@
import { ApiProperty } from '@nestjs/swagger';
import { IApp } from '../types/App.types';
import { Schema, Prop, SchemaFactory } from '@nestjs/mongoose';
import { Document } from 'mongoose';

export type AppDocument = App & Document;

@Schema()
export class App {

// TODO: this should be changed into mongo db schema
export class AppSchema implements IApp {
@ApiProperty({
description: "User DID",
example: "did:hid:testnet:123123"
})
@Prop()
userId: string;

@ApiProperty({
description: "Application name",
example: "demo app"
})
@Prop()
appName: string;

@ApiProperty({
description: "Application id",
example: "app-1"
})
@Prop()
appId: string;

@ApiProperty({
description: "Application Secret",
example: "app-secret-1"
})
@Prop()
appSecret: string;

@ApiProperty({
description: "Data Vault Id",
example: "hs-edv-id-1"
})
@Prop()
edvId: string;

@ApiProperty({
description: "Keymanagement Service Id",
example: "hs-kms-id-1"
})
@Prop()
kmsId: string;
constructor(params: {
userId,
appName,
appId,
appSecret,
edvId,
kmsId
}){
this.userId = params.userId
this.appName = params.appName;
this.appId = params.appId
this.appSecret = params.appSecret
this.edvId = params.edvId
this.kmsId = params.kmsId
}
}

}

export const AppSchema = SchemaFactory.createForClass(App)
25 changes: 12 additions & 13 deletions src/app-auth/services/app-auth/app-auth.service.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,28 @@
import { Injectable } from '@nestjs/common';
import { IApp } from '../../types/App.types';
import { CreateAppDto } from '../../dtos/CreateApp.dto';
import { AppSchema } from 'src/app-auth/schemas/App.schema';
import { App } from 'src/app-auth/schemas/App.schema';
import { AppRepository } from 'src/app-auth/repositories/App.repository';
import { uuid } from 'uuidv4';

@Injectable()
export class AppAuthService {
private apps: IApp[] = []
constructor(private readonly appRepository: AppRepository) {}

createAnApp(createAppDto: CreateAppDto): IApp{
const appSchema = new AppSchema({
createAnApp(createAppDto: CreateAppDto): Promise<App>{
return this.appRepository.create({
...createAppDto,
appId: 'demo-app1', // generate app id
appSecret: 'demo-secret-1', // generate app secret
appId: uuid(), // generate app id
appSecret: uuid(), // TODO: generate app secret and should be handled like password by hashing and all...
edvId: 'hs-edv-1', // generate edvId by called hypersign edv service
kmsId: 'demo-kms-1'
})
this.apps.push(appSchema);
return appSchema
}

getAllApps () {
return this.apps
getAllApps (): Promise<App[]>{
return this.appRepository.find({})
}

getAppById (appId: string) {
return this.apps.find(app => app.appId === appId)
getAppById (appId: string): Promise<App> {
return this.appRepository.findOne({ appId })
}
}
4 changes: 2 additions & 2 deletions src/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Module } from '@nestjs/common';
import { AppAuthModule } from './app-auth/app-auth.module';

import { MongooseModule } from '@nestjs/mongoose';
@Module({
imports: [AppAuthModule],
imports: [AppAuthModule, MongooseModule.forRoot('mongodb://127.0.0.1:27017/studio-api')],
controllers: [],
providers: [],
})
Expand Down
Loading

0 comments on commit 3574361

Please sign in to comment.