From b19afd18c577a71d26f487bdfb5cb3265fc37a68 Mon Sep 17 00:00:00 2001 From: neetidesai <97416423+neetidesai@users.noreply.github.com> Date: Mon, 30 Sep 2024 19:58:30 -0400 Subject: [PATCH 1/2] Add getApplicationsEndpoint --- .../applications/applications.controller.ts | 16 ++++++++++++++ .../src/applications/applications.model.ts | 19 +++++++++++++++++ .../src/applications/applications.module.ts | 12 +++++++++++ .../src/applications/applications.service.ts | 21 +++++++++++++++++++ 4 files changed, 68 insertions(+) create mode 100644 apps/backend/src/applications/applications.controller.ts create mode 100644 apps/backend/src/applications/applications.model.ts create mode 100644 apps/backend/src/applications/applications.module.ts create mode 100644 apps/backend/src/applications/applications.service.ts diff --git a/apps/backend/src/applications/applications.controller.ts b/apps/backend/src/applications/applications.controller.ts new file mode 100644 index 00000000..3c758b59 --- /dev/null +++ b/apps/backend/src/applications/applications.controller.ts @@ -0,0 +1,16 @@ +import { + Controller, + Get, +} from "@nestjs/common"; +import { ApplicationsService } from "./applications.service"; +import { ApplicationsModel } from "./applications.model"; + +@Controller("applications") +export class ApplicationsController { + constructor(private applicationsService: ApplicationsService) {} + + @Get("applicationsInfo") + public async getApplications(): Promise<[ApplicationsModel]> { + return this.applicationsService.getApplications(); + } +} \ No newline at end of file diff --git a/apps/backend/src/applications/applications.model.ts b/apps/backend/src/applications/applications.model.ts new file mode 100644 index 00000000..175a63bc --- /dev/null +++ b/apps/backend/src/applications/applications.model.ts @@ -0,0 +1,19 @@ +/** + * Represents the model schema of an application + */ + +export type ApplicationsModel = { + appID: number, + userID: number, + siteID: number, + names: Array, + status: ApplicationStatus, + dateApplied: Date, + isFirstApplication: boolean +} + +export enum ApplicationStatus{ + APPROVED = "Approved", + PENDING = "Pending", + REJECTED = "Rejected" +} \ No newline at end of file diff --git a/apps/backend/src/applications/applications.module.ts b/apps/backend/src/applications/applications.module.ts new file mode 100644 index 00000000..b1cca530 --- /dev/null +++ b/apps/backend/src/applications/applications.module.ts @@ -0,0 +1,12 @@ +import { Module } from "@nestjs/common"; +import { ApplicationsService } from "./applications.service"; +import { ApplicationsController } from "./applications.controller"; + +@Module({ + imports: [], + providers: [ApplicationsService], + controllers: [ApplicationsController], + exports: [ApplicationsService], +}) + +export class ApplicationsModule{} \ No newline at end of file diff --git a/apps/backend/src/applications/applications.service.ts b/apps/backend/src/applications/applications.service.ts new file mode 100644 index 00000000..5546d735 --- /dev/null +++ b/apps/backend/src/applications/applications.service.ts @@ -0,0 +1,21 @@ +import { Injectable } from "@nestjs/common"; +import { ApplicationsModel } from "./applications.model"; + +@Injectable() +export class ApplicationsService { + + /** + * Gets all applications. + * + * @returns + */ + + public async getApplications(): Promise<[ApplicationsModel]> { + try{ + return await getApplicationsData() as Promise<[ApplicationsModel]> + } + catch(e) { + throw new Error("Unable to retrieve applications.") + } + } +} \ No newline at end of file From c51632c8fdc47d7ba1d8a42817c45edb1e647f20 Mon Sep 17 00:00:00 2001 From: neetidesai <97416423+neetidesai@users.noreply.github.com> Date: Sun, 20 Oct 2024 19:17:58 -0400 Subject: [PATCH 2/2] fixups --- apps/backend/src/app/app.module.ts | 3 +- .../applications/applications.controller.ts | 23 ++++----- .../src/applications/applications.model.ts | 26 +++++----- .../src/applications/applications.module.ts | 18 +++---- .../src/applications/applications.service.ts | 49 +++++++++++++------ apps/backend/src/dynamodb.ts | 21 ++++++-- 6 files changed, 85 insertions(+), 55 deletions(-) diff --git a/apps/backend/src/app/app.module.ts b/apps/backend/src/app/app.module.ts index cef0092e..725eda88 100644 --- a/apps/backend/src/app/app.module.ts +++ b/apps/backend/src/app/app.module.ts @@ -4,10 +4,11 @@ import { TypeOrmModule } from '@nestjs/typeorm'; import { AppController } from './app.controller'; import { AppService } from './app.service'; import { SiteModule } from '../site/site.module'; +import { ApplicationsModule } from '../applications/applications.module'; import { DynamoDbService } from '../dynamodb'; @Module({ - imports: [SiteModule], + imports: [SiteModule, ApplicationsModule], controllers: [AppController], providers: [AppService, DynamoDbService], }) diff --git a/apps/backend/src/applications/applications.controller.ts b/apps/backend/src/applications/applications.controller.ts index 3c758b59..6f129b24 100644 --- a/apps/backend/src/applications/applications.controller.ts +++ b/apps/backend/src/applications/applications.controller.ts @@ -1,16 +1,13 @@ -import { - Controller, - Get, -} from "@nestjs/common"; -import { ApplicationsService } from "./applications.service"; -import { ApplicationsModel } from "./applications.model"; +import { Controller, Get } from '@nestjs/common'; +import { ApplicationsService } from './applications.service'; +import { ApplicationsModel } from './applications.model'; -@Controller("applications") +@Controller('applications') export class ApplicationsController { - constructor(private applicationsService: ApplicationsService) {} + constructor(private applicationsService: ApplicationsService) {} - @Get("applicationsInfo") - public async getApplications(): Promise<[ApplicationsModel]> { - return this.applicationsService.getApplications(); - } -} \ No newline at end of file + @Get('applicationsInfo') + public async getApplications(): Promise { + return this.applicationsService.getApplications(); + } +} diff --git a/apps/backend/src/applications/applications.model.ts b/apps/backend/src/applications/applications.model.ts index 175a63bc..2ce7b043 100644 --- a/apps/backend/src/applications/applications.model.ts +++ b/apps/backend/src/applications/applications.model.ts @@ -3,17 +3,17 @@ */ export type ApplicationsModel = { - appID: number, - userID: number, - siteID: number, - names: Array, - status: ApplicationStatus, - dateApplied: Date, - isFirstApplication: boolean -} + appId: number; + userId: number; + siteId: number; + names: Array; + status: ApplicationStatus; + dateApplied: Date; + isFirstApplication: boolean; +}; -export enum ApplicationStatus{ - APPROVED = "Approved", - PENDING = "Pending", - REJECTED = "Rejected" -} \ No newline at end of file +export enum ApplicationStatus { + APPROVED = 'Approved', + PENDING = 'Pending', + REJECTED = 'Rejected', +} diff --git a/apps/backend/src/applications/applications.module.ts b/apps/backend/src/applications/applications.module.ts index b1cca530..2e32c1bc 100644 --- a/apps/backend/src/applications/applications.module.ts +++ b/apps/backend/src/applications/applications.module.ts @@ -1,12 +1,12 @@ -import { Module } from "@nestjs/common"; -import { ApplicationsService } from "./applications.service"; -import { ApplicationsController } from "./applications.controller"; +import { Module } from '@nestjs/common'; +import { ApplicationsService } from './applications.service'; +import { ApplicationsController } from './applications.controller'; +import { DynamoDbService } from '../dynamodb'; @Module({ - imports: [], - providers: [ApplicationsService], - controllers: [ApplicationsController], - exports: [ApplicationsService], + imports: [], + providers: [ApplicationsService, DynamoDbService], + controllers: [ApplicationsController], + exports: [ApplicationsService], }) - -export class ApplicationsModule{} \ No newline at end of file +export class ApplicationsModule {} diff --git a/apps/backend/src/applications/applications.service.ts b/apps/backend/src/applications/applications.service.ts index 5546d735..54f90e72 100644 --- a/apps/backend/src/applications/applications.service.ts +++ b/apps/backend/src/applications/applications.service.ts @@ -1,21 +1,38 @@ -import { Injectable } from "@nestjs/common"; -import { ApplicationsModel } from "./applications.model"; +import { Injectable } from '@nestjs/common'; +import { ApplicationsModel } from './applications.model'; +import { DynamoDbService } from '../dynamodb'; +import { ApplicationStatus } from './applications.model'; @Injectable() export class ApplicationsService { + private readonly tableName = 'gibostonApplications'; + constructor(private readonly dynamoDbService: DynamoDbService) {} - /** - * Gets all applications. - * - * @returns - */ - - public async getApplications(): Promise<[ApplicationsModel]> { - try{ - return await getApplicationsData() as Promise<[ApplicationsModel]> - } - catch(e) { - throw new Error("Unable to retrieve applications.") - } + /** + * Gets all applications. + * + * @returns a list of all applications as ApplicationsModel objects. + */ + public async getApplications(): Promise { + try { + const data = await this.dynamoDbService.scanTable(this.tableName); + return data.map(this.mapDynamoDBItemToApplication); + } catch (e) { + throw new Error('Unable to retrieve applications: ' + e); } -} \ No newline at end of file + } + + private mapDynamoDBItemToApplication = (item: { + [key: string]: any; + }): ApplicationsModel => { + return { + appId: item['appId'].N, + dateApplied: new Date(item['dateApplied'].S), + isFirstApplication: item['isFirstApplication'].BOOL, + names: item['names'].SS, + siteId: item['siteId'].N, + status: item['status'].S as ApplicationStatus, + userId: item['userId'].N, + }; + }; +} diff --git a/apps/backend/src/dynamodb.ts b/apps/backend/src/dynamodb.ts index 99fdbcb7..ac9c732f 100644 --- a/apps/backend/src/dynamodb.ts +++ b/apps/backend/src/dynamodb.ts @@ -1,5 +1,9 @@ -import { DynamoDBClient, GetItemCommand } from "@aws-sdk/client-dynamodb"; -import { Injectable } from "@nestjs/common"; +import { + DynamoDBClient, + GetItemCommand, + ScanCommand, +} from '@aws-sdk/client-dynamodb'; +import { Injectable } from '@nestjs/common'; @Injectable() export class DynamoDbService { @@ -15,7 +19,10 @@ export class DynamoDbService { }); } - public async getItem(tableName: string, key: { [key: string]: any }): Promise { + public async getItem( + tableName: string, + key: { [key: string]: any }, + ): Promise { const params = { TableName: tableName, Key: key, @@ -30,4 +37,12 @@ export class DynamoDbService { } } + public async scanTable(tableName: string): Promise { + const params = { + TableName: tableName, + }; + const command = new ScanCommand(params); + const result = await this.dynamoDbClient.send(command); + return result.Items; + } }