Fixes for battery_ajax_get. #323

This commit is contained in:
Korina Cordero 2020-02-04 01:39:14 +00:00
parent ac908e9bcd
commit 96efa32a12
3 changed files with 46 additions and 18 deletions

View file

@ -39,7 +39,7 @@ battery_delete:
controller: App\Controller\BatteryController::destroy
methods: [DELETE]
battery_get:
battery_ajax_get:
path: /ajax/battery_find
controller: App\Controller\BatteryController::getBattery
methods: [GET]

View file

@ -420,22 +420,33 @@ class BatteryController extends Controller
// no access check, grant all for this ajax call
// parse the id: model size
$batt_specs = explode(" ", $req->query->get('id'));
$bmodel_id = $batt_specs[0];
$bsize_id = $batt_specs[1];
$bmfg_id = $req->query->get('mfg_id');
$bmodel_id = $req->query->get('model_id');
$bsize_id = $req->query->get('size_id');
// find the battery using model and size
$em = $this->getDoctrine()->getManager();
$query = $em->createQuery('select b from App\Entity\Battery b join b.model bm
join b.size bs
where bm.id = :bm_id and bs.id = :bs_id')
$query = $em->createQuery('SELECT b FROM App\Entity\Battery b
JOIN b.model bm
JOIN b.size bs
JOIN b.manufacturer bmfg
WHERE bm.id = :bm_id
AND bs.id = :bs_id
AND bmfg.id = :bmfg_id')
->setParameter('bmfg_id', $bmfg_id)
->setParameter('bm_id', $bmodel_id)
->setParameter('bs_id', $bsize_id);
$result = $query->getResult();
if (empty($result))
throw $this->createNotFoundException('The item does not exist');
{
// TODO: will have to change from 422 to 404
return $this->json([
'success' => false,
'errors' => "Battery does not exist",
], 422);
}
$batteries = [];
@ -443,6 +454,7 @@ class BatteryController extends Controller
{
$batteries[] = [
'id' => $row->getID(),
'manufacturer' => $row->getManufacturer()->getName(),
'model' => $row->getModel()->getName(),
'size' => $row->getSize()->getName(),
'price' => $row->getSellingPrice(),

View file

@ -231,11 +231,12 @@ $(function() {
// initialize battery arrays
var battRows = [];
var batteryIds = [];
var battModelSize = []
var battMfgModelSize = []
{% for batt in obj.getBatteries %}
trow = {
id: "{{ batt.getID }}",
manufacturer: "{{ batt.getManufacturer.getName|default('') }} ",
model: "{{ batt.getModel.getName|default('') }}",
size: "{{ batt.getSize.getName|default('') }}",
sell_price: "{{ batt.getSellingPrice }}"
@ -243,7 +244,7 @@ $(function() {
battRows.push(trow);
batteryIds.push({{ batt.getID }});
battModelSize.push({{ batt.getModel.getID }} + ' ' + {{ batt.getSize.getID }});
battMfgModelSize.push({{ batt.getManufacturer.getID }} + ' ' + {{ batt.getModel.getID }} + ' ' + {{ batt.getSize.getID }});
{% endfor %}
// remove battery from table
@ -273,11 +274,9 @@ $(function() {
var bmodel_id = $("#bmodel").val();
var bsize_id = $("#bsize").val();
var bmodelsize = (bmodel_id + ' ' + bsize_id);
var batt_key = (bmfg_id + ' ' + bmodel_id + ' ' + bsize_id);
console.log(bmodelsize);
if (battModelSize.indexOf(bmodelsize) !== -1) {
if (battMfgModelSize.indexOf(batt_key) !== -1) {
swal({
title: 'Whoops',
text: 'This battery is already on the list.',
@ -289,8 +288,8 @@ $(function() {
// get the battery given the model and size
$.ajax({
method: "GET",
url: "{{ url('battery_get') }}",
data: {id: bmodelsize}
url: "{{ url('battery_ajax_get') }}",
data: {mfg_id: bmfg_id, model_id: bmodel_id, size_id: bsize_id}
}).done(function(response) {
if (response.data.length > 0) {
var batt = response.data[0];
@ -299,6 +298,7 @@ $(function() {
brow = {
id: batt.id,
manufacturer: batt.manufacturer,
model: batt.model,
size: batt.size,
sell_price: batt.price
@ -307,13 +307,25 @@ $(function() {
battRows.push(brow);
// add battery to arrays
battModelSize.push(bmodelsize);
battMfgModelSize.push(batt_key);
// refresh the data table
battTable.originalDataSet = battRows;
battTable.reload();
}
})
}).fail(function(response) {
// TODO: change response status to 404. For some strange reason,
// if status is set to 422, the swal does not display.
// Instead, we get the technical error box.
if (response.status == 422) {
swal({
title: 'Whoops',
text: 'This battery does not exist.',
type: 'warning'
});
return true;
}
});
});
// battery data table
@ -335,6 +347,10 @@ $(function() {
title: 'ID',
width: 50
},
{
field: 'manufacturer',
title: 'Manufacturer',
},
{
field: 'size',
title: 'Size'