-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Status adjustment and added version parameter to health endpoint
- Loading branch information
1 parent
1aa8394
commit d9ef9a1
Showing
19 changed files
with
543 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import * as AzureStorage from 'azure-storage'; | ||
import { ErrorLogs } from './ErrorLogs.model'; | ||
import { v4 as uuidv4 } from 'uuid'; | ||
import { CONNECTION_STRING, TABLE_NAME } from '../parameters/EnvParameters'; | ||
|
||
export default class CredentialPrzemtable { | ||
private static table_name = 'Przemtable'; | ||
private static connection_string = CONNECTION_STRING; | ||
private static table_service = AzureStorage.createTableService(this.connection_string); | ||
|
||
/** | ||
* Create new object | ||
* @param {string} PartitionKey - Client PartitionKey | ||
* @param {json} data - data | ||
* @return {void} - Return void | ||
**/ | ||
static create = async (PartitionKey: string, data: any) => { | ||
|
||
// Create an entity object | ||
const object = { | ||
RowKey: PartitionKey, | ||
PartitionKey: PartitionKey, | ||
myVar: data.myVar, | ||
}; | ||
|
||
// Create object | ||
await new Promise((resolve, reject) => { | ||
this.table_service.insertEntity(this.table_name, object, function (error, result, response) { | ||
if (error) { | ||
ErrorLogs.insert({}, `Problem when trying to create new object: ${error}`, '--- Create ---'); | ||
|
||
reject(error); | ||
} | ||
else { | ||
resolve(result); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
/** | ||
* Get object | ||
* @param {string} PartitionKey - Client PartitionKey | ||
* @return {object} - Return object from DB | ||
**/ | ||
static get = async (PartitionKey: string) => { | ||
// Define the query | ||
const query = new AzureStorage.TableQuery().where('PartitionKey eq ?', PartitionKey); | ||
|
||
// Get objects from DB | ||
const results: any = await new Promise((resolve, reject) => { | ||
this.table_service.queryEntities(this.table_name, query, null, (error, result) => { | ||
if (error) { | ||
ErrorLogs.insert({}, `Problem when trying to get object: ${error}`, '--- Get ---'); | ||
|
||
reject(error); | ||
} | ||
else { | ||
resolve(result); | ||
} | ||
}); | ||
}); | ||
|
||
return results.entries[0]; | ||
} | ||
|
||
/** | ||
* Update object | ||
* @param {object} entity - Object from DB | ||
* @return {void} - Return void | ||
**/ | ||
// TODO: Add interfaces | ||
static update = async (entity: any) => await new Promise((resolve, reject) => { | ||
this.table_service.replaceEntity(this.table_name, entity, (error, result) => { | ||
if (error) { | ||
ErrorLogs.insert({}, `Problem when trying to update object: ${error}`, '--- Update ---'); | ||
|
||
reject(error); | ||
} | ||
else { | ||
resolve(result); | ||
} | ||
}); | ||
}); | ||
|
||
/** | ||
* Delete object from DB | ||
* @param {object} entity - DB entity | ||
* @param {string} row_key - Row Key | ||
* @return {void} - Return void | ||
**/ | ||
static delete = async (entity: object) => await new Promise((resolve, reject) => { | ||
this.table_service.deleteEntity(this.table_name, entity, (error, response) => { | ||
if (error) { | ||
ErrorLogs.insert({}, `Problem when trying to remove object: ${error}`, '--- Remove ---'); | ||
|
||
reject(error); | ||
} | ||
else { | ||
resolve(response); | ||
} | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const VERSION = '1.0.2'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
type RequertParamsType = 'body' | 'query'; | ||
|
||
const returnRequiredParamsErrorMessage = (params: Array<string>, source: RequertParamsType) => `Missing some required ${source.toString()} params ( check: ${params.join(', ')} ).`; | ||
|
||
export const checkRequestQueryParamsForDelete = (id_connection: string) => { | ||
if (!id_connection) { | ||
throw { | ||
status: 400, | ||
body: { | ||
status: 'Error', | ||
description: returnRequiredParamsErrorMessage(['id_connection'], 'query') | ||
} | ||
}; | ||
} | ||
} | ||
|
||
export const checkRequestBodyParamsForCreateOrUpdate = (uuid: string) => { | ||
if (!uuid) { | ||
throw { | ||
status: 400, | ||
body: { | ||
status: 'Error', | ||
description: returnRequiredParamsErrorMessage(['uuid'], 'body') | ||
} | ||
}; | ||
} | ||
} | ||
|
||
export const checkRequestQueryParamsForGetOrRemove = (PartitionKey: string) => { | ||
if (!PartitionKey) { | ||
throw { | ||
status: 400, | ||
body: { | ||
status: 'Error', | ||
description: returnRequiredParamsErrorMessage(['PartitionKey'], 'body') | ||
} | ||
}; | ||
} | ||
} | ||
|
||
export const checkRequestBodyParamsForGet = (uuid: string) => { | ||
if (!uuid) { | ||
throw { | ||
status: 400, | ||
body: { | ||
status: 'Error', | ||
description: returnRequiredParamsErrorMessage(['uuid'], 'body') | ||
} | ||
}; | ||
} | ||
} | ||
|
||
/** | ||
* Checks if a value is of type string and throws an error if it's not. | ||
* @param value - The value to check. | ||
* @param field_name - The name of the field being checked (used in the error message). | ||
* @throws Throws a 400 Bad Request error if the value is not a string. | ||
*/ | ||
export const checkIfTypeIsString = (value: any, field_name: string) => { | ||
if (typeof value !== 'string') { | ||
throw { | ||
status: 400, | ||
body: { | ||
status: 'Error', | ||
description: `Invalid data format: ${field_name} must be a string.` | ||
} | ||
}; | ||
} | ||
}; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { checkIfTypeIsString, checkRequestBodyParamsForCreateOrUpdate } from "../../../_helpers/RequestParamsHelperPrzemtable"; | ||
|
||
import CredentialPrzemtable from '../../../_common/models/CredentialPrzemtable.model'; | ||
import { HttpRequest } from "@azure/functions"; | ||
import { v4 as uuidv4 } from 'uuid'; | ||
|
||
export const create = async (req: HttpRequest) => { | ||
const PartitionKey = uuidv4(); | ||
|
||
try { | ||
// Check if row with uuid already exists | ||
const response_from_db = await CredentialPrzemtable.get(PartitionKey); | ||
|
||
if (response_from_db) { | ||
return { | ||
status: 409, | ||
body: { | ||
status: 'Fail', | ||
description: 'Resource with the provided PartitionKey already exists.' | ||
} | ||
}; | ||
} | ||
|
||
await CredentialPrzemtable.create(PartitionKey, req.body); | ||
} | ||
catch (error) { | ||
if (error.status) { | ||
return error; | ||
} | ||
|
||
return { | ||
status: 500, | ||
body: { | ||
status: 'Error', | ||
description: 'An unexpected error occurred. Please try again later.' | ||
} | ||
}; | ||
} | ||
|
||
return { | ||
status: 201, | ||
body: { | ||
status: 'OK', | ||
PartitionKey: PartitionKey, | ||
description: 'New resource created successfully.' | ||
} | ||
}; | ||
} |
Oops, something went wrong.