From 74f65df5c437207594c3d806456cec11b189b369 Mon Sep 17 00:00:00 2001 From: David Li Date: Mon, 18 Nov 2024 18:21:35 -0500 Subject: [PATCH 1/2] all --- apps/backend/src/dynamodb.ts | 32 ++++++++++++++++++++++++ apps/backend/src/site/site.controller.ts | 5 ++++ apps/backend/src/site/site.service.ts | 10 +++++++- 3 files changed, 46 insertions(+), 1 deletion(-) diff --git a/apps/backend/src/dynamodb.ts b/apps/backend/src/dynamodb.ts index 60cb62f..1d71abf 100644 --- a/apps/backend/src/dynamodb.ts +++ b/apps/backend/src/dynamodb.ts @@ -143,4 +143,36 @@ export class DynamoDbService { const result = await this.dynamoDbClient.send(command); return result.Attributes; } + + public async updateField( + tableName: string, + key: { [key: string]: any }, + field: string, + value: string, + ): Promise { + const params = { + TableName: tableName, + Key: key, + UpdateExpression: `SET #field = :value`, + ExpressionAttributeNames: { + '#field': field, + }, + ExpressionAttributeValues: { + ':value': { S: value }, + }, + ReturnValue: 'ALL_NEW', + }; + console.log(key); + console.log(params) + try { + const command = new UpdateItemCommand(params); + await this.dynamoDbClient.send(command); + const result = await this.getItem(tableName, key); + console.log(result); + return result; + } catch (error) { + console.error('DynamoDB UpdateItem Error:', error); + throw new Error(`Unable to update item in ${tableName}`); + } + } } diff --git a/apps/backend/src/site/site.controller.ts b/apps/backend/src/site/site.controller.ts index 4848ea0..ec01b3c 100644 --- a/apps/backend/src/site/site.controller.ts +++ b/apps/backend/src/site/site.controller.ts @@ -3,6 +3,7 @@ import { Delete, Get, Post, + Put, Body, Param, Query @@ -57,6 +58,10 @@ export class SiteController { return this.siteService.deleteSite(siteId); } + @Put("/adopt/:id") + public async setSiteStatusAdopt(@Param("id") siteId: number): Promise { + return this.siteService.adoptSite(siteId); + } diff --git a/apps/backend/src/site/site.service.ts b/apps/backend/src/site/site.service.ts index e82fe56..37021f6 100644 --- a/apps/backend/src/site/site.service.ts +++ b/apps/backend/src/site/site.service.ts @@ -93,12 +93,20 @@ export class SiteService { } } + public async adoptSite(siteId: number): Promise { + try { + const key = { 'siteId': { S: siteId.toString() } }; + const result = await this.dynamoDbService.updateField(this.tableName, key, "siteStatus", "Adopted") + } catch (e) { + throw new Error("Unable to set site status to Adopted:" + e); + } + } private mapDynamoDBItemToSite = (objectId: number, item: { [key: string]: any }): SiteModel => { return { siteID: objectId, siteName: item["siteName"].S, - siteStatus: SiteStatus.AVAILABLE, //placeholder until table is updated + siteStatus: item["siteStatus"].S, assetType: item["assetType"].S, symbolType: item["symbolType"].S, siteLatitude: item["siteLatitude"].S, From 6194edeb14cdf6a2acd92fd0fdbcd04fb2a4e5e3 Mon Sep 17 00:00:00 2001 From: David Li Date: Mon, 2 Dec 2024 18:34:50 -0500 Subject: [PATCH 2/2] resolve merge --- apps/backend/src/dynamodb.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/apps/backend/src/dynamodb.ts b/apps/backend/src/dynamodb.ts index 1e04e7d..ad1bf9f 100644 --- a/apps/backend/src/dynamodb.ts +++ b/apps/backend/src/dynamodb.ts @@ -175,7 +175,6 @@ export class DynamoDbService { return result.Attributes; } -<<<<<<< HEAD public async updateField( tableName: string, key: { [key: string]: any }, @@ -207,7 +206,7 @@ export class DynamoDbService { throw new Error(`Unable to update item in ${tableName}`); } } -======= + public async updateItemWithExpression( tableName: string, key: { [key: string]: any }, @@ -243,7 +242,6 @@ export class DynamoDbService { } ->>>>>>> main }