Skip to content

Commit

Permalink
[#496, #513] Replace project partial update operation.
Browse files Browse the repository at this point in the history
We had a specific operation in the backend to update only certain
fields of a project, but the UI was always sending the full project
data.

We think the ability of updating only some fields of the project is
not important, and we prefer to minimize the amount of code to
maintain. So we have:
* Switched the web service to the previously unused UpdateProjectAction
  and ProjectDAO::update.
* Modify ProjectDAO::update to detect specific errors and return an
  OperationResult, just like the partialUpdate operation did.
* Modify ProjectDAO::update to use PDO.
* Remove all code related to PartialUpdateProject.
  • Loading branch information
jaragunde committed Feb 28, 2023
1 parent 3fe9373 commit 0aeaf6e
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 339 deletions.
152 changes: 26 additions & 126 deletions model/dao/ProjectDAO/PostgreSQLProjectDAO.php
Original file line number Diff line number Diff line change
Expand Up @@ -475,106 +475,47 @@ public function getByDescription(string $desc): ?int
return $resultAux['id'] ?? NULL;
}

/** Project partial updater for PostgreSQL.
/** Project updater for PostgreSQL.
*
* This function updates only some fields of the data of a Project by its {@link ProjectVO}, reading
* the flags on the associative array <var>$update</var>.
* This function updates the data of a Project by its {@link ProjectVO}.
*
* @param ProjectVO $projectVO the {@link ProjectVO} with the data we want to update on database.
* @param array $update an array with flags for updating or not the different fields.
* @return OperationResult the result {@link OperationResult} with information about operation status
*/
public function partialUpdate(ProjectVO $projectVO, $update) {
public function update(ProjectVO $projectVO) {
$result = new OperationResult(false);

$sql = "UPDATE project SET ";

if ($update['activation'])
$sql .= "activation=:activation, ";

if ($update['init'])
$sql .= "init=:init, ";

if ($update['end'])
$sql .= "_end=:end, ";

if ($update['invoice'])
$sql .= "invoice=:invoice, ";

if ($update['estHours'])
$sql .= "est_hours=:est_hours, ";

if ($update['areaId'])
$sql .= "areaid=:areaid, ";

if ($update['customerId'])
$sql .= "customerid=:customerid, ";

if ($update['description'])
$sql .= "description=:description, ";

if ($update['type'])
$sql .= "type=:type, ";

if ($update['movHours'])
$sql .= "moved_hours=:moved_hours, ";

if ($update['schedType'])
$sql .= "sched_type=:sched_type, ";

if (strlen($sql) == strlen("UPDATE project SET ")) {
$result->setIsSuccessful(true);
$result->setMessage('No changes.');
$result->setResponseCode(200);

return $result;
}

// remove the last comma
$sql = substr($sql, 0, -2);

$sql .= " WHERE id=:id";
$sql = "UPDATE project SET activation=:activation" .
", init=:init" .
", _end=:end" .
", invoice=:invoice" .
", est_hours=:est_hours" .
", areaid=:areaid" .
", customerid=:customerid" .
", type=:type" .
", description=:description" .
", moved_hours=:moved_hours" .
", sched_type=:sched_type" .
" WHERE id=:id";

$initDateFormatted = (is_null($projectVO->getInit())) ? null : DBPostgres::formatDate($projectVO->getInit());
$endDateFormatted = (is_null($projectVO->getEnd())) ? null : DBPostgres::formatDate($projectVO->getEnd());

try {
$statement = $this->pdo->prepare($sql);
$statement->bindValue(":activation", $projectVO->getActivation(), PDO::PARAM_BOOL);
$statement->bindValue(":init", $initDateFormatted, PDO::PARAM_STR);
$statement->bindValue(":end", $endDateFormatted, PDO::PARAM_STR);
$statement->bindValue(":invoice", $projectVO->getInvoice(), PDO::PARAM_STR);
$statement->bindValue(":est_hours", $projectVO->getEstHours(), PDO::PARAM_STR);
$statement->bindValue(":areaid", $projectVO->getAreaId(), PDO::PARAM_INT);
$statement->bindValue(":customerid", $projectVO->getCustomerId(), PDO::PARAM_INT);
$statement->bindValue(":description", $projectVO->getDescription(), PDO::PARAM_STR);
$statement->bindValue(":type", $projectVO->getType(), PDO::PARAM_STR);
$statement->bindValue(":moved_hours", $projectVO->getMovedHours(), PDO::PARAM_STR);
$statement->bindValue(":sched_type", $projectVO->getSchedType(), PDO::PARAM_STR);
$statement->bindValue(":id", $projectVO->getId(), PDO::PARAM_INT);

if ($update['activation'])
$statement->bindValue(":activation", $projectVO->getActivation(), PDO::PARAM_BOOL);

if ($update['init'])
$statement->bindValue(":init", $initDateFormatted, PDO::PARAM_STR);

if ($update['end'])
$statement->bindValue(":end", $endDateFormatted, PDO::PARAM_STR);

if ($update['invoice'])
$statement->bindValue(":invoice", $projectVO->getInvoice(), PDO::PARAM_STR);

if ($update['estHours'])
$statement->bindValue(":est_hours", $projectVO->getEstHours(), PDO::PARAM_STR);

if ($update['areaId'])
$statement->bindValue(":areaid", $projectVO->getAreaId(), PDO::PARAM_INT);

if ($update['customerId'])
$statement->bindValue(":customerid", $projectVO->getCustomerId(), PDO::PARAM_INT);

if ($update['description'])
$statement->bindValue(":description", $projectVO->getDescription(), PDO::PARAM_STR);

if ($update['type'])
$statement->bindValue(":type", $projectVO->getType(), PDO::PARAM_STR);

if ($update['movHours'])
$statement->bindValue(":moved_hours", $projectVO->getMovedHours(), PDO::PARAM_STR);

if ($update['schedType'])
$statement->bindValue(":sched_type", $projectVO->getSchedType(), PDO::PARAM_STR);

$statement->execute();

$result->setIsSuccessful(true);
Expand Down Expand Up @@ -615,47 +556,6 @@ public function partialUpdate(ProjectVO $projectVO, $update) {
return $result;
}

/** Project updater for PostgreSQL.
*
* This function updates the data of a Project by its {@link ProjectVO}.
*
* @param ProjectVO $projectVO the {@link ProjectVO} with the data we want to update on database.
* @return int the number of rows that have been affected (it should be 1).
* @throws {@link SQLQueryErrorException}
*/
public function update(ProjectVO $projectVO) {
$affectedRows = 0;

if($projectVO->getId() != "") {
$currProjectVO = $this->getById($projectVO->getId());
}

// If the query returned a row then update
if(sizeof($currProjectVO) > 0) {

$sql = "UPDATE project SET activation=" . DBPostgres::boolToString($projectVO->getActivation()) .
", init=" . DBPostgres::formatDate($projectVO->getInit()) .
", _end=" . DBPostgres::formatDate($projectVO->getEnd()) .
", invoice=" . DBPostgres::checkNull($projectVO->getInvoice()) .
", est_hours=" . DBPostgres::checkNull($projectVO->getEstHours()) .
", areaid=" . DBPostgres::checkNull($projectVO->getAreaId()) .
", customerid=" . DBPostgres::checkNull($projectVO->getCustomerId()) .
", type=" . DBPostgres::checkStringNull($projectVO->getType()) .
", description=" . DBPostgres::checkStringNull($projectVO->getDescription()) .
", moved_hours=" . DBPostgres::checkNull($projectVO->getMovedHours()) .
", sched_type=" . DBPostgres::checkStringNull($projectVO->getSchedType()) .
" WHERE id=".$projectVO->getId();

$res = pg_query($this->connect, $sql);

if ($res == NULL) throw new SQLQueryErrorException(pg_last_error());

$affectedRows = pg_affected_rows($res);
}

return $affectedRows;
}

/** Project creator for PostgreSQL.
*
* This function creates a new row for a Project by its {@link ProjectVO}.
Expand Down
15 changes: 1 addition & 14 deletions model/dao/ProjectDAO/ProjectDAO.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,25 +171,12 @@ public abstract function getAllCustom($active = False, $orderField = 'id');

public abstract function getByDescription(string $description): ?int;

/** Project partial updater.
*
* This function updates only some fields of the data of a Project by its {@link ProjectVO}, reading
* the flags on the associative array <var>$update</var>.
*
* @param ProjectVO $projectVO the {@link ProjectVO} with the data we want to update on database.
* @param array $update an array with flags for updating or not the different fields.
* @return int the number of rows that have been affected (it should be 1).
* @throws {@link SQLQueryErrorException}
*/
public abstract function partialUpdate(ProjectVO $projectVO, $update);

/** Project updater.
*
* This function updates the data of a Project by its {@link ProjectVO}.
*
* @param ProjectVO $projectVO the {@link ProjectVO} with the data we want to update on database.
* @return int the number of rows that have been affected (it should be 1).
* @throws {@link OperationErrorException}
* @return OperationResult the result {@link OperationResult} with information about operation status
*/
public abstract function update(ProjectVO $projectVO);

Expand Down
36 changes: 8 additions & 28 deletions model/facade/ProjectsFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
include_once(PHPREPORT_ROOT . '/model/facade/action/DeassignUserFromProjectAction.php');
include_once(PHPREPORT_ROOT . '/model/facade/action/DeleteProjectAction.php');
include_once(PHPREPORT_ROOT . '/model/facade/action/UpdateProjectAction.php');
include_once(PHPREPORT_ROOT . '/model/facade/action/PartialUpdateProjectAction.php');
include_once(PHPREPORT_ROOT . '/model/facade/action/GetProjectsByCustomerUserLoginAction.php');
include_once(PHPREPORT_ROOT . '/model/dao/DAOFactory.php');
include_once(PHPREPORT_ROOT . '/model/vo/ProjectVO.php');
Expand Down Expand Up @@ -264,46 +263,27 @@ static function GetProjectByDescription(string $description) {

/** Update Project Function
*
* This function is used for updating a Project.
* This function is used for updating an entire Project object. It updates all the fields of the object.
*
* @param ProjectVO $project the Project value object we want to update.
* @return int it just indicates if there was any error (<i>-1</i>) or not (<i>0</i>).
* @throws {@link SQLQueryErrorException}
*/
static function UpdateProject(ProjectVO $project) {

$action = new UpdateProjectAction($project);

return $action->execute();

}

/** Partial Update Project Function
*
* This function is used for partially updating a Project.
*
* @param ProjectVO $task the Project value object we want to update.
* @param array $update the updating flags of the Project VO.
* @return OperationResult the result {@link OperationResult} with information about operation status
*/
static function PartialUpdateProject(ProjectVO $project, $update) {
$action = new PartialUpdateProjectAction($project, $update);
static function UpdateProject(ProjectVO $project) {
$action = new UpdateProjectAction($project);
return $action->execute();
}

/** Partial Update Projects Function
/** Update Projects Function
*
* This function is used for partially updating an array of Projects.
* If an error occurs, it stops updating.
* This function is used for updating an array of Project objects. It updates all the fields of the object.
*
* @param array $projects the Project value objects we want to update.
* @param array $updates the updating flag arrays of the Project VOs.
* @return array OperationResult the array of results {@link OperationResult} with information about operation status.
*/
static function PartialUpdateProjects($projects, $updates) {
static function UpdateProjects($projects) {
$operationResults = [];
foreach ((array) $projects as $i=>$project)
$operationResults[] = ProjectsFacade::PartialUpdateProject($project, $updates[$i]);
foreach ($projects as $project)
$operationResults[] = ProjectsFacade::UpdateProject($project);
return $operationResults;
}

Expand Down
112 changes: 0 additions & 112 deletions model/facade/action/PartialUpdateProjectAction.php

This file was deleted.

Loading

0 comments on commit 0aeaf6e

Please sign in to comment.