Skip to content

Commit

Permalink
Merge pull request #43 from Code-4-Community/ns-refactor-sites-endpoints
Browse files Browse the repository at this point in the history
Refactor get sites by status and symbol into 2 separate endpoints
  • Loading branch information
nourshoreibah authored Oct 28, 2024
2 parents 24f1077 + 66a15ad commit 5310943
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 32 deletions.
34 changes: 24 additions & 10 deletions apps/backend/src/site/site.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import {
Post,
Body,
Param,
Query
Query,
Delete
} from "@nestjs/common";
import { SiteService } from "./site.service";
import { SiteModel } from "./site.model";
Expand All @@ -15,6 +16,26 @@ import { ApiQuery } from "@nestjs/swagger";
export class SiteController {
constructor(private siteService: SiteService) {}

@Get("/status/")
@ApiQuery({ name: "status", required: true })
public async getSitesByStatus(
@Query('status') status: string
): Promise<SiteModel[]> {
console.log("status: ", status);
return this.siteService.getSitesByStatus(status);
}



@Get("/symbolType/")
@ApiQuery({ name: "symbolType", required: true })
public async getSitesBySymbolType(
@Query('symbolType') symbolType: string
): Promise<SiteModel[]> {
return this.siteService.getSitesBySymbolType(symbolType);
}


@Get(":id")
public async getSite(
@Param("id") siteId: number
Expand All @@ -27,15 +48,8 @@ export class SiteController {
return this.siteService.postSite(siteData);
}

@Get()
@ApiQuery({ name: 'status', required: false }) // makes query parameter optional
@ApiQuery({ name: 'symbol-type', required: false })
public async getSites(
@Query("status") status?: string,
@Query("symbol-type") symbolType?: string
): Promise<SiteModel[]> {
return this.siteService.getFilteredSites({ status, symbolType });
}




}
54 changes: 32 additions & 22 deletions apps/backend/src/site/site.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export class SiteService {
throw new Error("Unable to get site data: "+ e)
}
}

public async postSite(siteData: NewSiteInput) {
const siteModel = this.PostInputToSiteModel(siteData);
const newId = await this.dynamoDbService.getHighestSiteId(this.tableName) + 1;
Expand All @@ -39,41 +39,51 @@ export class SiteService {
}
}

public async getFilteredSites(filters: { status?: string, symbolType?: string }): Promise<SiteModel[]> {


public async getSitesByStatus(status: string): Promise<SiteModel[]> {
try {
const filterExpressionParts = [];
const expressionAttributeValues: { [key: string]: any } = {};
// add filters based on provided values
if (filters.status) {
filterExpressionParts.push("siteStatus = :status");
expressionAttributeValues[":status"] = { S: filters.status };
}
if (filters.symbolType) {
filterExpressionParts.push("symbolType = :symbolType");
expressionAttributeValues[":symbolType"] = { S: filters.symbolType };
const data = await this.dynamoDbService.scanTable(this.tableName, "siteStatus = :status", { ":status": { S: status } } );
const sites: SiteModel[] = [];
for (let i = 0; i < data.length; i++) {
try {
sites.push(this.mapDynamoDBItemToSite(parseInt(data[i]["siteId"].S), data[i]));
} catch (error) {
console.error('Error mapping site:', error, data[i]);
}

}
const data = await this.dynamoDbService.scanTable(
this.tableName,
// if there are filter expression parts, join them with "AND", otherwise pass undefined
filterExpressionParts.length > 0 ? filterExpressionParts.join(" AND ") : undefined,
// if there are expression attribute values, pass them, otherwise pass undefined
Object.keys(expressionAttributeValues).length > 0 ? expressionAttributeValues : undefined
);
console.log("Found " + sites.length + " \"" + status + "' sites");
return sites;
}
catch(e) {
throw new Error("Unable to get site by status: "+ e)
}

}

public async getSitesBySymbolType(symbolType: string): Promise<SiteModel[]> {
try {
const data = await this.dynamoDbService.scanTable(this.tableName, "symbolType = :symbolType", { ":symbolType": { S: symbolType } } );
const sites: SiteModel[] = [];
for (let i = 0; i < data.length; i++) {
try {
sites.push(this.mapDynamoDBItemToSite(parseInt(data[i]["siteId"].S), data[i]));
} catch (error) {
console.error('Error mapping site:', error, data[i]);
}

}
console.log(`Found ${sites.length} sites matching the criteria.`);
console.log("Found " + sites.length + " \"" + symbolType + "' sites");
return sites;
} catch (e) {
throw new Error("Unable to get site data: " + e);
}
catch(e) {
throw new Error("Unable to get site by symbol: "+ e)
}

}


private mapDynamoDBItemToSite = (objectId: number, item: { [key: string]: any }): SiteModel => {
return {
siteID: objectId,
Expand Down

0 comments on commit 5310943

Please sign in to comment.