feat: improve Location API error handling
- Add Phone/Email fields to LocationAddressModel allowedFields - Fix saveLocation() to throw exceptions instead of returning error arrays - Update controller to properly handle model responses - Include actual database error message in transaction failures
This commit is contained in:
parent
011ea11cc9
commit
a47db49f81
@ -43,8 +43,8 @@ class LocationController extends BaseController {
|
||||
$input = $this->request->getJSON(true);
|
||||
if (!$this->validateData($input, $this->rules)) { return $this->failValidationErrors($this->validator->getErrors()); }
|
||||
try {
|
||||
$id = $this->model->saveLocation($input);
|
||||
return $this->respondCreated([ 'status' => 'success', 'message' => 'data created successfully', 'data' => $id ], 201);
|
||||
$result = $this->model->saveLocation($input);
|
||||
return $this->respondCreated([ 'status' => 'success', 'message' => 'data created successfully', 'data' => $result ], 201);
|
||||
} catch (\Throwable $e) {
|
||||
return $this->failServerError('Something went wrong: ' . $e->getMessage());
|
||||
}
|
||||
@ -54,8 +54,8 @@ class LocationController extends BaseController {
|
||||
$input = $this->request->getJSON(true);
|
||||
try {
|
||||
if (!$this->validateData($input, $this->rules)) { return $this->failValidationErrors( $this->validator->getErrors()); }
|
||||
$id = $this->model->saveLocation($input, true);
|
||||
return $this->respondCreated([ 'status' => 'success', 'message' => 'data updated successfully', 'data' => $id ], 201);
|
||||
$result = $this->model->saveLocation($input, true);
|
||||
return $this->respondCreated([ 'status' => 'success', 'message' => 'data updated successfully', 'data' => $result ], 201);
|
||||
} catch (\Throwable $e) {
|
||||
return $this->failServerError('Something went wrong: ' . $e->getMessage());
|
||||
}
|
||||
|
||||
@ -24,12 +24,14 @@ class CreateLocationTable extends Migration {
|
||||
$this->forge->addField([
|
||||
'LocationID' => ['type' => 'INT', 'unsigned' => true],
|
||||
'Street1' => ['type' => 'Varchar', 'constraint' => 255, 'null' => true],
|
||||
'Street2' => ['type' => 'VARCHAR', 'constraint' => 255, 'null' => false],
|
||||
'Street2' => ['type' => 'VARCHAR', 'constraint' => 255, 'null' => true],
|
||||
'City' => ['type' => 'int', 'null' => true],
|
||||
'Province' => ['type' => 'int', 'null' => true],
|
||||
'PostCode' => ['type' => 'varchar', 'constraint' => 255, 'null' => true],
|
||||
'GeoLocationSystem' => ['type' => 'varchar', 'constraint' => 255, 'null' => true],
|
||||
'GeoLocationData' => ['type' => 'varchar', 'constraint' => 255, 'null' => true],
|
||||
'Phone' => ['type' => 'varchar', 'constraint' => 100, 'null' => true],
|
||||
'Email' => ['type' => 'varchar', 'constraint' => 150, 'null' => true],
|
||||
'CreateDate' => ['type' => 'DATETIME', 'null' => true],
|
||||
'EndDate' => ['type' => 'DATETIME', 'null' => true]
|
||||
]);
|
||||
|
||||
@ -5,7 +5,8 @@ use App\Models\BaseModel;
|
||||
class LocationAddressModel extends BaseModel {
|
||||
protected $table = 'locationaddress';
|
||||
protected $primaryKey = 'LocationID';
|
||||
protected $allowedFields = ['LocationID', 'Street1', 'Street2', 'City', 'Province', 'PostCode', 'GeoLocationSystem', 'GeoLocationData', 'CreateDate', 'EndDate'];
|
||||
protected $allowedFields = ['LocationID', 'Street1', 'Street2', 'City', 'Province', 'PostCode',
|
||||
'GeoLocationSystem', 'GeoLocationData', 'Phone', 'Email', 'CreateDate', 'EndDate'];
|
||||
|
||||
protected $useTimestamps = true;
|
||||
protected $createdField = 'CreateDate';
|
||||
|
||||
@ -50,14 +50,15 @@ class LocationModel extends BaseModel {
|
||||
$modelAddress->insert($data);
|
||||
}
|
||||
if ($db->transStatus() === false) {
|
||||
$error = $db->error();
|
||||
$db->transRollback();
|
||||
throw new \Exception('Transaction failed');
|
||||
throw new \Exception($error['message'] ?? 'Transaction failed');
|
||||
}
|
||||
$db->transCommit();
|
||||
return [ 'status' => 'success', 'LocationID' => $LocationID ];
|
||||
} catch (\Throwable $e) {
|
||||
$db->transRollback();
|
||||
return [ 'status' => 'error', 'message' => $e->getMessage() ];
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
|
||||
@ -70,14 +71,15 @@ class LocationModel extends BaseModel {
|
||||
$this->delete($LocationID);
|
||||
$modelAddress->delete($LocationID);
|
||||
if ($db->transStatus() === false) {
|
||||
$error = $db->error();
|
||||
$db->transRollback();
|
||||
throw new \Exception('Transaction failed');
|
||||
throw new \Exception($error['message'] ?? 'Transaction failed');
|
||||
}
|
||||
$db->transCommit();
|
||||
return [ 'status' => 'success', 'LocationID' => $LocationID ];
|
||||
} catch (\Throwable $e) {
|
||||
$db->transRollback();
|
||||
return [ 'status' => 'error', 'message' => $e->getMessage() ];
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user