Add transaction sum endpoints
Signed-off-by: William Brawner <me@wbrawner.com>
This commit is contained in:
parent
683fb07e48
commit
9be135d138
3 changed files with 154 additions and 4 deletions
|
@ -1,4 +1,5 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create your routes in here. The name is the lowercase name of the controller
|
* Create your routes in here. The name is the lowercase name of the controller
|
||||||
* without the controller part, the stuff after the hash is the method.
|
* without the controller part, the stuff after the hash is the method.
|
||||||
|
@ -9,11 +10,20 @@
|
||||||
*/
|
*/
|
||||||
return [
|
return [
|
||||||
'resources' => [
|
'resources' => [
|
||||||
'budget' => ['url' => '/budgets'],
|
'budget' => ['url' => "/api/v1.0/budgets"],
|
||||||
'category' => ['url' => '/categories'],
|
'category' => ['url' => "/api/v1.0/categories"],
|
||||||
'transaction' => ['url' => '/transactions'],
|
'transaction' => ['url' => "/api/v1.0/transactions"],
|
||||||
],
|
],
|
||||||
'routes' => [
|
'routes' => [
|
||||||
['name' => 'page#index', 'url' => '/', 'verb' => 'GET'],
|
[
|
||||||
|
'name' => 'page#index',
|
||||||
|
'url' => '/',
|
||||||
|
'verb' => 'GET',
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'name' => 'transaction#sum',
|
||||||
|
'url' => '/api/v1.0/transactions/sum',
|
||||||
|
'verb' => 'POST',
|
||||||
|
]
|
||||||
]
|
]
|
||||||
];
|
];
|
||||||
|
|
|
@ -209,4 +209,83 @@ class TransactionController extends Controller
|
||||||
}
|
}
|
||||||
return new DataResponse($this->transactionMapper->delete($transaction));
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
namespace OCA\Twigs\Db;
|
namespace OCA\Twigs\Db;
|
||||||
|
|
||||||
|
use Doctrine\DBAL\FetchMode;
|
||||||
use OCP\IDbConnection;
|
use OCP\IDbConnection;
|
||||||
use OCP\AppFramework\Db\QBMapper;
|
use OCP\AppFramework\Db\QBMapper;
|
||||||
|
|
||||||
|
@ -54,4 +55,64 @@ class TransactionMapper extends QBMapper
|
||||||
->where('budget_id', $budgetId);
|
->where('budget_id', $budgetId);
|
||||||
return $qb->execute();
|
return $qb->execute();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function sumByBudgetId(int $budgetId, int $startDate, int $endDate)
|
||||||
|
{
|
||||||
|
$sql = <<<EOD
|
||||||
|
SELECT (COALESCE((
|
||||||
|
SELECT SUM(amount)
|
||||||
|
FROM `*PREFIX*twigs_transactions`
|
||||||
|
WHERE budget_id = ?
|
||||||
|
AND expense = 0
|
||||||
|
AND date >= ?
|
||||||
|
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 = <<<EOD
|
||||||
|
SELECT (COALESCE((
|
||||||
|
SELECT SUM(amount)
|
||||||
|
FROM `*PREFIX*twigs_transactions`
|
||||||
|
WHERE category_id = ?
|
||||||
|
AND expense = 0
|
||||||
|
AND date >= ?
|
||||||
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue