Skip to content

Commit

Permalink
Implement the foreign constraint action
Browse files Browse the repository at this point in the history
  • Loading branch information
vc-urvin committed Jun 28, 2023
1 parent f8d3249 commit 1f78b8b
Show file tree
Hide file tree
Showing 5 changed files with 200 additions and 49 deletions.
5 changes: 5 additions & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,10 @@


Route::post('change-constraint', [DisplayController::class, 'changeConstraint']);

Route::get('foreign-key-table', [DisplayController::class, 'getForeignKeyTableList']);
Route::get('foreign-key-field/{table}', [DisplayController::class, 'getForeignKeyFieldList']);
Route::post('add-foreign-constraint', [DisplayController::class, 'addForeignKeyConstraint']);

});

6 changes: 3 additions & 3 deletions src/Commands/DBConstraintCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,11 @@ public function foreignKeyConstraint(string $tableName, string $selectField): vo
$fields = Constant::ARRAY_DECLARATION;

do {
$referenceTable = $this->anticipate(__('Lang::messages.constraint.question.foreign_table'), $this->getTablesList());
$referenceTable = $this->anticipate(__('Lang::messages.constraint.question.foreign_table'), $this->getTableList());

if ($referenceTable && $this->checkTableExistOrNot($referenceTable)) {
if ($referenceTable && $this->checkTableExist($referenceTable)) {

foreach ($this->getTableFields($referenceTable) as $field) {
foreach ($this->getFieldsDetails($referenceTable) as $field) {
$fields[] = $field->COLUMN_NAME;
}
do {
Expand Down
60 changes: 57 additions & 3 deletions src/Controllers/DisplayController.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public function getTableConstraint(string $tableName): JsonResponse
}

foreach ($data['constrain']['foreign'] as $foreign) {
if ($table->COLUMN_NAME === $foreign['column_name']) {
if ($table->COLUMN_NAME === $foreign) {
$foreignKeyTooltip = '<div class="inline-flex">
<img src=' . asset("auditor/icon/gray-key.svg") . ' alt="key" class="mr-2">
<div class="relative flex flex-col items-center group">
Expand Down Expand Up @@ -139,6 +139,13 @@ public function getTableConstraint(string $tableName): JsonResponse
}
}
break;
case Constant::CONSTRAINT_FOREIGN_KEY:
if(in_array($table->COLUMN_NAME, $noConstraintFields['integer'])) {
if(!$this->tableHasValue($tableName)) {
$foreignKey = '<img src=' . asset("auditor/icon/add.svg") . ' alt="key" class="m-auto add-constraint-'.$table->COLUMN_NAME.'-'.Constant::CONSTRAINT_FOREIGN_KEY.'" style="height:30px;cursor: pointer;" onclick="add(`'.$table->COLUMN_NAME.'`, `'.Constant::CONSTRAINT_FOREIGN_KEY.'`,`'.$tableName.'`)"/>';
}
}
break;
default:
break;
}
Expand All @@ -152,10 +159,57 @@ public function getTableConstraint(string $tableName): JsonResponse
));
}

public function changeConstraint(Request $request)
/**
* Update the field Constraint
* @param Request
* @return
*/
public function changeConstraint(Request $request): bool
{
$data = $request->all();
$this->addConstraint($data['table_name'], $data['colum_name'], $data['constraint']);
return $data['colum_name'];
return Constant::STATUS_TRUE;
}

/**
* Get Foreign Key Details
* @return array
*/
public function getForeignKeyTableList(): array
{
return $this->getTableList();
}

/**
* Get Foreign Key Field List
* @param string
* @return array
*/
public function getForeignKeyFieldList(string $tableName): array
{
return $this->getFieldsDetails($tableName);
}

/**
* Add Foreign Key Constraint
* @param Request
* @return mixed
*/
public function addForeignKeyConstraint(Request $request): mixed
{
$data= $request->all();

if($data['reference_table'] === $data['table_name']) {
return __('Lang::messages.constraint.error_message.foreign_selected_table_match', ['foreign' => $data['reference_table'], 'selected' => $data['table_name']]);
}

$referenceFieldType = $this->getFieldDataType($data['reference_table'], $data['reference_field']);
$selectedFieldType = $this->getFieldDataType($data['table_name'], $data['select_field']);
if ($referenceFieldType['data_type'] !== $selectedFieldType['data_type']) {
return __('Lang::messages.constraint.error_message.foreign_not_apply');
}

$this->addConstraint($data['table_name'], $data['select_field'], Constant::CONSTRAINT_FOREIGN_KEY, $data['reference_table'], $data['reference_field']);
return Constant::STATUS_TRUE;
}
}
27 changes: 0 additions & 27 deletions src/Datatable/AuditDatatable.php

This file was deleted.

151 changes: 135 additions & 16 deletions src/views/auditor/pages/audit.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ class="btn dropdown-toggle bg-light-black border-0 rounded-2xl text-white w-64 t
</dialog>

<p class="colum-value"></p>
<p class="table-value"></p>
<p class="constraint-value"></p>
@endsection

Expand Down Expand Up @@ -201,6 +202,7 @@ class="btn dropdown-toggle bg-light-black border-0 rounded-2xl text-white w-64 t
});
$(document).ready(function() {
// Standards
var table = $('#standards').DataTable({
scrollX: true,
Expand Down Expand Up @@ -288,8 +290,6 @@ function format(d) {
tableComment += '</tbody>' + '</table>';
}
var table =
'<table class="table table-stripped table-bordered display nowrap w-100 border-gray" cellpadding="5" cellspacing="0" border="0">' +
'<thead class="bg-black">' +
Expand Down Expand Up @@ -411,13 +411,28 @@ function confirmDialog() {
addConstraint();
}
function add(columnName, constraint) {
$('#confDialog h2').replaceWith("<h2>ADD " + constraint.toUpperCase() + " KEY</h2>");
$('#confDialog p').replaceWith('<p>Do you want to add ' + constraint.toLowerCase() +
' in <span style="color:red;">' + columnName + '</span> field?</p>');
$('.colum-value').replaceWith("<p class='colum-value'>" + columnName + "</p>");
$('.constraint-value').replaceWith("<p class='constraint-value'>" + constraint + "</p>");
openDialog();
function add(columnName, constraint, tableName) {
if(constraint.toLowerCase() === "foreign") {
$('.colum-value').replaceWith("<p class='colum-value'>" + columnName + "</p>");
$('.table-value').replaceWith("<p class='table-value'>" + tableName + "</p>");
$('.constraint-value').replaceWith("<p class='constraint-value'>" + constraint + "</p>");
getForeignTables();
} else {
console.log($(".main-dialog")[0]);
if ( $(".main-dialog")[0] ) {
$('#confDialog h2').replaceWith("<h2>ADD " + constraint.toUpperCase() + " KEY</h2>");
$('.main-dialog').replaceWith('<p>Do you want to add ' + constraint.toLowerCase() +' in <span style="color:red;">' + columnName + '</span> field?</p>');
$('.confirm-dialog-btn').replaceWith('<button onclick="confirmDialog()" class="btn btn-success confirm-dialog-btn">Yes</button>');
} else {
$('#confDialog h2').replaceWith("<h2>ADD " + constraint.toUpperCase() + " KEY</h2>");
$('#confDialog p').replaceWith('<p>Do you want to add ' + constraint.toLowerCase() +' in <span style="color:red;">' + columnName + '</span> field?</p>');
}
$('.colum-value').replaceWith("<p class='colum-value'>" + columnName + "</p>");
$('.constraint-value').replaceWith("<p class='constraint-value'>" + constraint + "</p>");
openDialog();
}
}
function addConstraint() {
Expand All @@ -437,18 +452,17 @@ function addConstraint() {
}),
success: function(response) {
if (response) {
var key;
var key = "";
if(constraint.toLowerCase() === "primary") {
key = $('<img src="auditor/icon/green-key.svg" alt="key" class="m-auto" />');
key = '<img src="auditor/icon/green-key.svg" alt="key" class="m-auto" />';
} else {
key = $('<img src="auditor/icon/gray-key.svg" alt="key" class="m-auto" />');
key = '<img src="auditor/icon/gray-key.svg" alt="key" class="m-auto" />';
}
$(".add-constraint-" + response + '-' + constraint).replaceWith(key);
$(".add-constraint-" + columnName + "-" + constraint).replaceWith(key);
$(".toast-container").css("display", "block");
$(".toastCustom").replaceWith("<p class='toastCustom'>" + constraint.toLowerCase() +
" key added successfully</p>");
$(".toastCustom").replaceWith("<p class='toastCustom'>" + constraint.toLowerCase() + " key added successfully</p>");
setTimeout(function() {
$(".toast-container").css("display", "none");;
Expand All @@ -461,6 +475,111 @@ function addConstraint() {
});
}
function getForeignTables()
{
$.ajax({
url: 'api/foreign-key-table',
type: 'get',
headers: {
'Content-Type': 'application/json',
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
success: function(response) {
if (response) {
$('#confDialog h2').replaceWith("<h2>Add Foreign Key</h2>");
var html = "<div class='row main-dialog' style='margin-top: 30px;margin-bottom: 20px;'>";
// foreign table
html += "<div class='col-md-6 sub-dialog'><select class='form-control select-foreign-table' id='select-foreign-tbl'>";
html += "<option disabled selected>Select Foreign Table</option>";
$.each(response, function(key, value) {
html += "<option value="+value+">"+value+"</option>";
});
html += "</select></div>";
html += "<div class='field-list'></div>"
html += "</div>";
$('#confDialog p').replaceWith(html);
$('#confDialog .confirm-dialog-btn').replaceWith('<button onclick="addforeignKey()" class="btn btn-success confirm-dialog-btn">Add</button>');
openDialog();
}
},
error: function(xhr, status, error) {
console.error(error);
}
});
}
$(document).on('change','#select-foreign-tbl', function() {
$.ajax({
url: 'api/foreign-key-field/'+this.value,
type: 'get',
headers: {
'Content-Type': 'application/json',
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
success: function(response) {
if (response) {
// foreign column
var html = "<div class='col-md-6 field-list'><select class='form-control' id='select-foreign-field'>";
html += "<option disabled selected>Select Foreign Column</option>";
$.each(response, function(key, value) {
html += "<option value="+value.COLUMN_NAME+">"+value.COLUMN_NAME+"</option>";
});
html += "</select></div>";
$('.field-list').replaceWith(html);
}
},
error: function(xhr, status, error) {
console.error(error);
}
});
});
function addforeignKey()
{
var foreignTable = document.getElementById('select-foreign-tbl');
var foreignField = document.getElementById('select-foreign-field');
var columnName = $('.colum-value').text();
var tableName = $('.table-value').text();
$.ajax({
url: 'api/add-foreign-constraint',
type: 'POST',
headers: {
'Content-Type': 'application/json',
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
data: JSON.stringify({
"table_name": tableName,
"select_field": columnName,
"reference_table": foreignTable.value,
"reference_field": foreignField.value
}),
success: function(response) {
if (response) {
$(".add-constraint-" + columnName + '-FOREIGN').replaceWith('<img src="auditor/icon/gray-key.svg" alt="key" class="m-auto" />');
closeDialog();
$(".toast-container").css("display", "block");
$(".toastCustom").replaceWith("<p class='toastCustom'>foreign key added successfully</p>");
setTimeout(function() {
$(".toast-container").css("display", "none");;
}, 1000);
}
},
error: function(xhr, status, error) {
console.error(error);
}
});
}
$(document).on("click", ".custom-action", function() {
$("#constraints").DataTable().draw();
$("#standards").DataTable().draw();
Expand Down

0 comments on commit 1f78b8b

Please sign in to comment.