From 9be135d13844a9d1cd972c312c6a59e839737fda Mon Sep 17 00:00:00 2001 From: William Brawner Date: Fri, 13 Mar 2020 08:35:31 -0500 Subject: [PATCH] Add transaction sum endpoints Signed-off-by: William Brawner --- appinfo/routes.php | 18 ++++-- lib/Controller/TransactionController.php | 79 ++++++++++++++++++++++++ lib/Db/TransactionMapper.php | 61 ++++++++++++++++++ 3 files changed, 154 insertions(+), 4 deletions(-) diff --git a/appinfo/routes.php b/appinfo/routes.php index 33e28c3..f65ccad 100644 --- a/appinfo/routes.php +++ b/appinfo/routes.php @@ -1,4 +1,5 @@ [ - 'budget' => ['url' => '/budgets'], - 'category' => ['url' => '/categories'], - 'transaction' => ['url' => '/transactions'], + 'budget' => ['url' => "/api/v1.0/budgets"], + 'category' => ['url' => "/api/v1.0/categories"], + 'transaction' => ['url' => "/api/v1.0/transactions"], ], 'routes' => [ - ['name' => 'page#index', 'url' => '/', 'verb' => 'GET'], + [ + 'name' => 'page#index', + 'url' => '/', + 'verb' => 'GET', + ], + [ + 'name' => 'transaction#sum', + 'url' => '/api/v1.0/transactions/sum', + 'verb' => 'POST', + ] ] ]; diff --git a/lib/Controller/TransactionController.php b/lib/Controller/TransactionController.php index 235df24..a8fe07e 100644 --- a/lib/Controller/TransactionController.php +++ b/lib/Controller/TransactionController.php @@ -209,4 +209,83 @@ class TransactionController extends Controller } return new DataResponse($this->transactionMapper->delete($transaction)); } + + /** + * @NoAdminRequired + * @NoCSRFRequired + * + * @param int $budgetId + * @param int $categoryId + * @param string $startDate + * @param string $endDate + */ + public function sum( + ?int $budgetId, + ?int $categoryId, + ?string $startDate, + ?string $endDate + ) { + $startDateTime = null; + if ($startDate === null) { + $startDateTime = new DateTime(); + $startDateTime->setDate( + $startDateTime->format('Y'), + $startDateTime->format('m'), + 1 + ); + $startDateTime->setTime(0, 0, 0, 0); + } else { + $startDateTime = DateTime::createFromFormat(DateTime::ATOM, $startDate); + } + if (!$startDateTime) { + return new DataResponse([], Http::STATUS_BAD_REQUEST); + } + $endDateTime = null; + if ($endDate === null) { + $endDateTime = new DateTime(); + $endDateTime->setDate( + $endDateTime->format('Y'), + $endDateTime->format('m'), + $endDateTime->format('t'), + ); + $endDateTime->setTime(23, 59, 59, 999); + } else { + $endDateTime = DateTime::createFromFormat(DateTime::ATOM, $endDate); + } + if (!$endDateTime) { + return new DataResponse([], Http::STATUS_BAD_REQUEST); + } + if ($budgetId != null) { + try { + $this->userPermissionMapper->find($budgetId, $this->userId); + } catch (Exception $e) { + return new DataResponse([], Http::STATUS_NOT_FOUND); + } + return new DataResponse([ + 'budgetId' => $budgetId, + 'sum' => $this->transactionMapper->sumByBudgetId( + $budgetId, + $startDateTime->getTimestamp(), + $endDateTime->getTimestamp() + ) + ], Http::STATUS_OK); + } + if ($categoryId != null) { + try { + $category = $this->categoryMapper->find($categoryId); + $this->userPermissionMapper->find($category->getBudgetId(), $this->userId); + } catch (Exception $e) { + return new DataResponse([], Http::STATUS_NOT_FOUND); + } + return new DataResponse([ + 'categoryId' => $categoryId, + 'sum' => $this->transactionMapper->sumByCategoryId( + $categoryId, + $startDateTime->getTimestamp(), + $endDateTime->getTimestamp() + ) + ], Http::STATUS_OK); + } + return new DataResponse([], Http::STATUS_BAD_REQUEST); + } } diff --git a/lib/Db/TransactionMapper.php b/lib/Db/TransactionMapper.php index 862949e..56dbee8 100644 --- a/lib/Db/TransactionMapper.php +++ b/lib/Db/TransactionMapper.php @@ -2,6 +2,7 @@ namespace OCA\Twigs\Db; +use Doctrine\DBAL\FetchMode; use OCP\IDbConnection; use OCP\AppFramework\Db\QBMapper; @@ -54,4 +55,64 @@ class TransactionMapper extends QBMapper ->where('budget_id', $budgetId); return $qb->execute(); } + + public function sumByBudgetId(int $budgetId, int $startDate, int $endDate) + { + $sql = <<= ? + AND date <= ? + ), 0)) - (COALESCE(( + SELECT SUM(amount) + FROM `*PREFIX*twigs_transactions` + WHERE budget_id = ? + AND expense = 1 + AND date >= ? + AND date <= ? + ), 0)); + EOD; + $statement = $this->db->prepare($sql); + $statement->bindParam(1, $budgetId); + $statement->bindParam(2, $startDate); + $statement->bindParam(3, $endDate); + $statement->bindParam(4, $budgetId); + $statement->bindParam(5, $startDate); + $statement->bindParam(6, $endDate); + $statement->execute(); + return (int) $statement->fetch(FetchMode::COLUMN); + } + + public function sumByCategoryId(int $categoryId, int $startDate, int $endDate) + { + $sql = <<= ? + AND date <= ? + ), 0)) - (COALESCE(( + SELECT SUM(amount) + FROM `*PREFIX*twigs_transactions` + WHERE category_id = ? + AND expense = 1 + AND date >= ? + AND date <= ? + ), 0)); + EOD; + $statement = $this->db->prepare($sql); + $statement->bindParam(1, $categoryId); + $statement->bindParam(2, $startDate); + $statement->bindParam(3, $endDate); + $statement->bindParam(4, $categoryId); + $statement->bindParam(5, $startDate); + $statement->bindParam(6, $endDate); + $statement->execute(); + return (int) $statement->fetch(FetchMode::COLUMN); + } }