Skip to content

Commit

Permalink
service refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
przemyslawss committed Apr 3, 2024
1 parent 54f10e4 commit 002e67d
Show file tree
Hide file tree
Showing 22 changed files with 246 additions and 779 deletions.
53 changes: 18 additions & 35 deletions Credentials/controllers/fetching/Create.ts
Original file line number Diff line number Diff line change
@@ -1,52 +1,35 @@
import { checkIfTypeIsString, checkRequestBodyParamsForCreateOrUpdate } from "../../../_helpers/RequestParamsHelper";
import { checkIfRequestBodyExists, checkIfTypeIsString } from "../../../_common/utils/Request.utils";
import { checkFetchingRequestBodyParamsForCreateOrUpdate } from "../../../_common/utils/FetchingRequest.utils";

import Credential from '../../../_common/models/Credential.model';
import { HttpRequest } from "@azure/functions";
import { Password } from '../../models/Password';
import { throwIfDatabaseResourceExists } from "../../../_common/utils/DatabaseResponse.utils";

export const create = async (req: HttpRequest) => {
const { id_connection, password } = req.body;
// Check if request body exists
checkIfRequestBodyExists(req.body);

try {
// Chack body params
checkRequestBodyParamsForCreateOrUpdate(id_connection, password);
const { id_connection, password } = req.body;

// Check connection
checkIfTypeIsString(id_connection, 'id_connection');
// Chack body params
checkFetchingRequestBodyParamsForCreateOrUpdate(id_connection, password);

// Check password
checkIfTypeIsString(password, 'password');
// Check connection
checkIfTypeIsString(id_connection, 'id_connection');

// Check if row with id_connection already exists
const response_from_db = await Credential.get(id_connection);
// Check password
checkIfTypeIsString(password, 'password');

if (response_from_db) {
return {
status: 409,
body: {
status: 'Conflict',
description: 'Resource with the provided id_connection already exists.'
}
};
}
// Check if row with id_connection already exists
const response_from_db = await Credential.get(id_connection);

const encrypt_password = Password.encryptPassword(password);
// If exists throw error 409 - Conflict
throwIfDatabaseResourceExists(response_from_db, 'id_connection');

await Credential.create(encrypt_password, id_connection);
}
catch (error) {
if (error.status) {
return error;
}
const encrypt_password = Password.encryptPassword(password);

return {
status: 500,
body: {
status: 'Internal error',
description: 'An unexpected error occurred. Please try again later.'
}
};
}
await Credential.create(encrypt_password, id_connection);

return {
status: 201,
Expand Down
56 changes: 18 additions & 38 deletions Credentials/controllers/fetching/Get.ts
Original file line number Diff line number Diff line change
@@ -1,50 +1,30 @@
import { checkIfTypeIsString, checkRequestQueryParamsForGetOrRemove } from "../../../_helpers/RequestParamsHelper";
import { checkIfTypeIsString } from "../../../_common/utils/Request.utils";
import { checkFetchingRequestQueryParamsForGetOrDelete } from "../../../_common/utils/FetchingRequest.utils";

import Credential from '../../../_common/models/Credential.model';
import { HttpRequest } from "@azure/functions";
import { throwIfDatabaseResourceNotExists } from "../../../_common/utils/DatabaseResponse.utils";

export const get = async (req: HttpRequest) => {
const { id_connection } = req.query;

try {
// Chack body params
checkRequestQueryParamsForGetOrRemove(id_connection);
// Chack body params
checkFetchingRequestQueryParamsForGetOrDelete(id_connection);

// Check connection
checkIfTypeIsString(id_connection, 'id_connection');
// Check connection
checkIfTypeIsString(id_connection, 'id_connection');

// Check if row with id_connection already exists
const response_from_db = await Credential.get(id_connection);
// Check if row with id_connection already exists
const response_from_db = await Credential.get(id_connection);

if (!response_from_db) {
return {
status: 404,
body: {
status: 'Not found',
description: 'Resource with the provided id_connection not exists.'
}
};
}
// If not exists throw error 404 - Not found
throwIfDatabaseResourceNotExists(response_from_db, 'id_connection');

return {
status: 200,
body: {
status: 'OK',
payload: response_from_db
}
};
}
catch (error) {
if (error.status) {
return error;
return {
status: 200,
body: {
status: 'OK',
payload: response_from_db
}

return {
status: 500,
body: {
status: 'Internal error',
description: 'An unexpected error occurred. Please try again later.'
}
};
}
}
};
}
47 changes: 15 additions & 32 deletions Credentials/controllers/fetching/Remove.ts
Original file line number Diff line number Diff line change
@@ -1,46 +1,29 @@
import { checkIfTypeIsString, checkRequestQueryParamsForGetOrRemove } from "../../../_helpers/RequestParamsHelper";
import { checkIfRequestBodyExists, checkIfTypeIsString } from "../../../_common/utils/Request.utils";
import { checkFetchingRequestBodyParamsForDelete } from "../../../_common/utils/FetchingRequest.utils";

import Credential from '../../../_common/models/Credential.model';
import { HttpRequest } from "@azure/functions";
import { throwIfDatabaseResourceNotExists } from "../../../_common/utils/DatabaseResponse.utils";

export const remove = async (req: HttpRequest) => {
const { id_connection } = req.body;
// Check if request body exists
checkIfRequestBodyExists(req.body);

try {
// Chack body params
checkRequestQueryParamsForGetOrRemove(id_connection);
const { id_connection } = req.body;

// Check connection
checkIfTypeIsString(id_connection, 'id_connection');
// Chack body params
checkFetchingRequestBodyParamsForDelete(id_connection);

// Check if row with id_connection already exists
let response_from_db = await Credential.get(id_connection.toString());
// Check connection
checkIfTypeIsString(id_connection, 'id_connection');

if (!response_from_db) {
return {
status: 404,
body: {
status: 'Not found',
description: 'Resource with the provided id_connection does not exist.'
}
};
}
// Check if row with id_connection already exists
let response_from_db = await Credential.get(id_connection.toString());

await Credential.delete(response_from_db);
}
catch (error) {
if (error.status) {
return error;
}
// If not exists throw error 404 - Not found
throwIfDatabaseResourceNotExists(response_from_db, 'id_connection');

return {
status: 500,
body: {
status: 'Internal error',
description: 'An unexpected error occurred. Please try again later.'
}
};
}
await Credential.delete(response_from_db);

return {
status: 200,
Expand Down
61 changes: 21 additions & 40 deletions Credentials/controllers/fetching/Update.ts
Original file line number Diff line number Diff line change
@@ -1,58 +1,39 @@
import { checkIfTypeIsString, checkRequestBodyParamsForCreateOrUpdate } from "../../../_helpers/RequestParamsHelper";
import { checkIfRequestBodyExists, checkIfTypeIsString } from "../../../_common/utils/Request.utils";
import { checkFetchingRequestBodyParamsForCreateOrUpdate } from "../../../_common/utils/FetchingRequest.utils";

import Credential from '../../../_common/models/Credential.model';
import { HttpRequest } from "@azure/functions";
import { Password } from "../../models/Password";
import { throwIfDatabaseResourceNotExists } from "../../../_common/utils/DatabaseResponse.utils";

export const update = async (req: HttpRequest) => {
// Check if request body exists
checkIfRequestBodyExists(req.body);

const { id_connection, password } = req.body;

try {
// Chack body params
checkRequestBodyParamsForCreateOrUpdate(id_connection, password);
// Chack body params
checkFetchingRequestBodyParamsForCreateOrUpdate(id_connection, password);

// Check connection
checkIfTypeIsString(id_connection, 'id_connection');
// Check connection
checkIfTypeIsString(id_connection, 'id_connection');

// Check password
checkIfTypeIsString(password, 'password');
// Check password
checkIfTypeIsString(password, 'password');

const encrypt_password = Password.encryptPassword(password);
const encrypt_password = Password.encryptPassword(password);

// Check if row with id_connection already exists
let response_from_db = await Credential.get(id_connection.toString());
// Check if row with id_connection already exists
let response_from_db = await Credential.get(id_connection.toString());

// If not exist create
if (!response_from_db) {
return {
status: 404,
body: {
status: 'Not found',
description: 'Resource with the provided ID Credential does not exist.'
}
};
}
else {
// Update object properties
response_from_db.password = encrypt_password;
response_from_db.updated_at = new Date().toISOString();
// If not exists throw error 404 - Not found
throwIfDatabaseResourceNotExists(response_from_db, 'id_connection');

await Credential.update(response_from_db);
}
}
catch (error) {
if (error.status) {
return error;
}
// Update object properties
response_from_db.password = encrypt_password;
response_from_db.updated_at = new Date().toISOString();

return {
status: 500,
body: {
status: 'Internal error',
description: 'An unexpected error occurred. Please try again later.'
}
};
}
await Credential.update(response_from_db);

return {
status: 200,
Expand Down
5 changes: 3 additions & 2 deletions Credentials/controllers/receiving/Get.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { checkIfTypeIsString, checkRequestBodyParamsForGet, checkRequestQueryParamsForGetOrRemove } from "../../../_helpers/RequestParamsHelper";
import { checkIfTypeIsString } from "../../../_common/utils/Request.utils";
import { checkReceivingRequestBodyParamsForGet } from '../../../_common/utils/ReceivingRequest.utils';

import { HttpRequest } from "@azure/functions";
import ReceivingCredential from '../../../_common/models/ReceivingCredential.model';
Expand All @@ -8,7 +9,7 @@ export const getReceive = async (req: HttpRequest) => {

try {
// Chack body params
checkRequestBodyParamsForGet(uuid);
checkReceivingRequestBodyParamsForGet(uuid);

// Check connection
checkIfTypeIsString(uuid, 'uuid');
Expand Down
75 changes: 42 additions & 33 deletions Credentials/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,40 +7,49 @@ import { remove } from "./controllers/fetching/Remove";
import { update } from "./controllers/fetching/Update";

const httpTrigger: AzureFunction = async function (context: Context, req: HttpRequest): Promise<void> {
const { receive } = req.query;

if (receive) {
context.res = await getReceive(req);

return;
}

switch (req.method) {
case 'POST':
context.res = await create(req);

break;
case 'GET':
context.res = await get(req);

break;
case 'PUT':
context.res = await update(req);

break;

case 'DELETE':
context.res = await remove(req);
break;
default:
context.res = {
status: 405,
body: {
status: 'Method not allowed.'
}
};

break;
try {
const { receive } = req.query;

if (receive) {
context.res = await getReceive(req);
return;
}

switch (req.method) {
case 'POST':
context.res = await create(req);
break;

case 'GET':
context.res = await get(req);
break;

case 'PUT':
context.res = await update(req);
break;

case 'DELETE':
context.res = await remove(req);
break;

default:
context.res = {
status: 405,
body: {
status: 'Method not allowed.'
}
};
break;
}
} catch (error) {
context.res = {
status: error.status ?? 500,
body: error.body ?? {
status: 'Internal error',
description: 'An unexpected error occurred. Please try again later.'
}
};
}
};

Expand Down
Loading

0 comments on commit 002e67d

Please sign in to comment.