Skip to content

Commit 55d87e6

Browse files
committedFeb 23, 2023
[#496,#513] Return more specific errors on project deletion.
Since we had to rewrite the delete DAO operation, we did it in PDO terms, addressing #513 too.
1 parent 1b1aa84 commit 55d87e6

File tree

4 files changed

+58
-44
lines changed

4 files changed

+58
-44
lines changed
 

‎model/dao/ProjectDAO/PostgreSQLProjectDAO.php

+33-14
Original file line numberDiff line numberDiff line change
@@ -669,27 +669,46 @@ public function create(ProjectVO $projectVO) {
669669
* This function deletes the data of a Project by its {@link ProjectVO}.
670670
*
671671
* @param ProjectVO $projectVO the {@link ProjectVO} with the data we want to delete from database.
672-
* @return int the number of rows that have been affected (it should be 1).
673-
* @throws {@link SQLQueryErrorException}
672+
* @return OperationResult the result {@link OperationResult} with information about operation status
674673
*/
675674
public function delete(ProjectVO $projectVO) {
676-
$affectedRows = 0;
675+
$result = new OperationResult(false);
677676

678-
// Check for a user ID.
679-
if($projectVO->getId() >= 0) {
680-
$currProjectVO = $this->getById($projectVO->getId());
681-
}
677+
$sql = "DELETE FROM project WHERE id=:id";
678+
try {
679+
$statement = $this->pdo->prepare($sql);
680+
$statement->bindValue(":id", $projectVO->getId(), PDO::PARAM_INT);
681+
$statement->execute();
682682

683-
// If it exists, then delete.
684-
if(sizeof($currProjectVO) > 0) {
685-
$sql = "DELETE FROM project WHERE id=".$projectVO->getId();
683+
$result->setIsSuccessful(true);
684+
$result->setMessage('Project deleted successfully.');
685+
$result->setResponseCode(200);
686+
}
687+
catch (PDOException $ex) {
688+
$errorMessage = $ex->getMessage();
689+
error_log('Project deletion failed: ' . $errorMessage);
686690

687-
$res = pg_query($this->connect, $sql);
688-
if ($res == NULL) throw new SQLQueryErrorException(pg_last_error());
689-
$affectedRows = pg_affected_rows($res);
691+
$resultMessage = "Project deletion failed: \n";
692+
if(strpos($errorMessage, "Foreign key violation")) {
693+
if(strpos($errorMessage, "task")) {
694+
$resultMessage .= "The project has assigned tasks.\n";
695+
}
696+
else {
697+
// catch-all for any foreign key violations that we may add in the future
698+
$resultMessage .= "There is data associated to the project.\n";
699+
}
700+
}
701+
else {
702+
// unpredictable error, just return the native error code and message
703+
$resultMessage .= $errorMessage;
704+
}
705+
$result->setErrorNumber($ex->getCode());
706+
$result->setMessage($resultMessage);
707+
$result->setIsSuccessful(false);
708+
$result->setResponseCode(500);
690709
}
691710

692-
return $affectedRows;
711+
return $result;
693712
}
694713
}
695714

‎model/facade/ProjectsFacade.php

+8-16
Original file line numberDiff line numberDiff line change
@@ -162,15 +162,11 @@ static function CreateProjects($projects) {
162162
* This function is used for deleting a Project.
163163
*
164164
* @param ProjectVO $project the Project value object we want to delete.
165-
* @return int it just indicates if there was any error (<i>-1</i>) or not (<i>0</i>).
166-
* @throws {@link SQLQueryErrorException}
165+
* @return OperationResult the result {@link OperationResult} with information about operation status
167166
*/
168167
static function DeleteProject(ProjectVO $project) {
169-
170-
$action = new DeleteProjectAction($project);
171-
172-
return $action->execute();
173-
168+
$action = new DeleteProjectAction($project);
169+
return $action->execute();
174170
}
175171

176172
/** Delete Projects Function
@@ -179,17 +175,13 @@ static function DeleteProject(ProjectVO $project) {
179175
* If an error occurs, it stops deleting.
180176
*
181177
* @param array $projects the Project value objects we want to delete.
182-
* @return int it just indicates if there was any error (<i>-1</i>) or not (<i>0</i>).
183-
* @throws {@link SQLQueryErrorException}
178+
* @return array OperationResult the array of results {@link OperationResult} with information about operation status.
184179
*/
185180
static function DeleteProjects($projects) {
186-
187-
foreach((array)$projects as $project)
188-
if ((ProjectsFacade::DeleteProject($project)) == -1)
189-
return -1;
190-
191-
return 0;
192-
181+
$operationResults = [];
182+
foreach ((array) $projects as $project)
183+
$operationResults[] = ProjectsFacade::DeleteProject($project);
184+
return $operationResults;
193185
}
194186

195187
/** Project Users Assigning

‎model/facade/action/DeleteProjectAction.php

+3-9
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
include_once(PHPREPORT_ROOT . '/model/facade/action/Action.php');
3333
include_once(PHPREPORT_ROOT . '/model/dao/DAOFactory.php');
3434
include_once(PHPREPORT_ROOT . '/model/vo/ProjectVO.php');
35+
include_once(PHPREPORT_ROOT . '/model/OperationResult.php');
3536

3637
/** Delete Project Action
3738
*
@@ -68,19 +69,12 @@ public function __construct(ProjectVO $project) {
6869
*
6970
* This is the function that contains the code that deletes the Project from persistent storing.
7071
*
71-
* @return int it just indicates if there was any error (<i>-1</i>) or not (<i>0</i>).
72+
* @return OperationResult the result {@link OperationResult} with information about operation status
7273
*/
7374
protected function doExecute() {
74-
7575
$dao = DAOFactory::getProjectDAO();
76-
77-
if ($dao->delete($this->project)!=1) {
78-
return -1;
79-
}
80-
81-
return 0;
76+
return $dao->delete($this->project);
8277
}
83-
8478
}
8579

8680
/*

‎web/services/deleteProjectsService.php

+14-5
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,21 @@
9999

100100
}
101101

102+
$operationResults = ProjectsFacade::DeleteProjects($deleteProjects);
103+
$errors = array_filter($operationResults, function ($item) {
104+
return (!$item->getIsSuccessful());
105+
});
106+
if ($errors) {
107+
//if multiple failures, let's just return a 500
108+
http_response_code(500);
109+
$string = "<return service='deleteProjects'><errors>";
110+
foreach ($errors as $result) {
111+
$string .= "<error id='" . $result->getErrorNumber() . "'>" . $result->getMessage() . "</error>";
112+
}
113+
$string .= "</errors></return>";
114+
}
102115

103-
if (count($deleteProjects) >= 1)
104-
if (ProjectsFacade::DeleteProjects($deleteProjects) == -1)
105-
$string = "<return service='deleteProjects'><error id='1'>There was some error while deleting the projects</error></return>";
106-
107-
if (!$string)
116+
if (!isset($string))
108117
$string = "<return service='deleteProjects'><ok>Operation Success!</ok></return>";
109118

110119

0 commit comments

Comments
 (0)
Please sign in to comment.