Skip to content

Commit c32cdaf

Browse files
committed
feat: poc working
1 parent 90ddeb1 commit c32cdaf

16 files changed

Lines changed: 321 additions & 93 deletions

.editorconfig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[*]
2+
tab_width = 2
3+
indent_size = 2
4+
indent_style = space
5+
insert_final_newline = true
6+
trim_trailing_whitespace = true
7+
8+
[*.md]
9+
trim_trailing_whitespace = false

.eslintignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
templates/
2+
scaffolds/

.prettierignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
templates/
2+
scaffolds/

.prettierrc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"printWidth": 100,
3+
"semi": false,
4+
"singleQuote": true,
5+
"trailingComma": "all",
6+
"overrides": [
7+
{
8+
"files": "*.md",
9+
"options": {
10+
"printWidth": 100,
11+
"proseWrap": "always"
12+
}
13+
}
14+
]
15+
}

.vscode/settings.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"cSpell.words": [
3+
"Cospend",
4+
"projectid"
5+
]
6+
}

appinfo/info.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,7 @@ It will use the default currency as the base, and will fill existing currencies
2525
<route>autocurrency.page.index</route>
2626
</navigation>
2727
</navigations>
28+
<background-jobs>
29+
<job>OCA\AutoCurrency\Cron\FetchCurrenciesJob</job>
30+
</background-jobs>
2831
</info>

lib/Controller/PageController.php

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,24 @@
99
use OCP\AppFramework\Controller;
1010
use OCP\AppFramework\Http\TemplateResponse;
1111
use OCP\IRequest;
12+
use OCP\BackgroundJob\IJobList;
1213
use OCP\Util;
1314

1415
class PageController extends Controller {
15-
public function __construct(IRequest $request) {
16-
parent::__construct(Application::APP_ID, $request);
17-
}
16+
private IJobList $jobList;
1817

19-
/**
20-
* @NoAdminRequired
21-
* @NoCSRFRequired
22-
*/
23-
public function index(): TemplateResponse {
24-
Util::addScript(Application::APP_ID, 'autocurrency-main');
18+
public function __construct(IRequest $request, IJobList $jobList) {
19+
parent::__construct(Application::APP_ID, $request);
20+
$this->jobList = $jobList;
21+
}
2522

26-
return new TemplateResponse(Application::APP_ID, 'main');
27-
}
23+
/**
24+
* @NoAdminRequired
25+
* @NoCSRFRequired
26+
*/
27+
public function index(): TemplateResponse {
28+
Util::addScript(Application::APP_ID, 'autocurrency-main');
29+
// $this->jobList->add('OCA\AutoCurrency\BackgroundJob\FetchCurrenciesJob');
30+
return new TemplateResponse(Application::APP_ID, 'main');
31+
}
2832
}

lib/Cron/FetchCurrenciesJob.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
namespace OCA\AutoCurrency\Cron;
3+
4+
use OCA\AutoCurrency\Service\FetchCurrenciesService;
5+
use OCP\BackgroundJob\TimedJob;
6+
use OCP\AppFramework\Utility\ITimeFactory;
7+
use OCP\ILogger;
8+
9+
class FetchCurrenciesJob extends TimedJob {
10+
private FetchCurrenciesService $service;
11+
private ILogger $logger;
12+
13+
public function __construct(ITimeFactory $time, FetchCurrenciesService $service, ILogger $logger) {
14+
parent::__construct($time);
15+
$this->service = $service;
16+
$this->logger = $logger;
17+
18+
// Run once a day
19+
$this->setInterval(60);
20+
// $this->setTimeSensitivity(\OCP\BackgroundJob\IJob::TIME_INSENSITIVE);
21+
}
22+
23+
protected function run($arguments) {
24+
$this->logger->info('Running cron job for FetchCurrenciesTask - args: ' . json_encode($arguments));
25+
$this->service->doCron();
26+
}
27+
}

lib/Db/CospendProjectMapper.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
declare(strict_types=1);
3+
// SPDX-FileCopyrightText: Chen Asraf <contact@casraf.dev>
4+
// SPDX-License-Identifier: AGPL-3.0-or-later
5+
6+
namespace OCA\AutoCurrency\Db;
7+
8+
use OCA\Cospend\Db\Project;
9+
use OCP\AppFramework\Db\DoesNotExistException;
10+
use OCP\AppFramework\Db\Entity;
11+
use OCP\AppFramework\Db\QBMapper;
12+
use OCP\DB\QueryBuilder\IQueryBuilder;
13+
use OCP\IDBConnection;
14+
15+
/**
16+
* @template-extends QBMapper<Project>
17+
*/
18+
class CospendProjectMapper extends QBMapper {
19+
public function __construct(IDBConnection $db) {
20+
parent::__construct($db, 'cospend_projects', Project::class);
21+
}
22+
23+
/**
24+
* @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException
25+
* @throws DoesNotExistException
26+
*/
27+
public function find(int $id): Project {
28+
/* @var $qb IQueryBuilder */
29+
$qb = $this->db->getQueryBuilder();
30+
$qb->select('*')
31+
->from('cospend_projects')
32+
->where($qb->expr()->eq('id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT)));
33+
return $this->findEntity($qb);
34+
}
35+
36+
/**
37+
* @param string $projectId
38+
* @return array
39+
*/
40+
public function findAll(): array {
41+
/* @var $qb IQueryBuilder */
42+
$qb = $this->db->getQueryBuilder();
43+
$qb->select('*')
44+
->from('cospend_projects');
45+
return $this->findEntities($qb);
46+
}
47+
}

lib/Db/Currency.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
declare(strict_types=1);
3+
// SPDX-FileCopyrightText: Chen Asraf <contact@casraf.dev>
4+
// SPDX-License-Identifier: AGPL-3.0-or-later
5+
6+
namespace OCA\AutoCurrency\Db;
7+
8+
use JsonSerializable;
9+
10+
use OCP\AppFramework\Db\Entity;
11+
12+
/**
13+
* @method getId(): int
14+
* @method getName(): string
15+
* @method setName(string $name): void
16+
* @method getExchangeRate(): string
17+
* @method setExchangeRate(string $exchangeRate): void
18+
* @method getProjectId(): string
19+
* @method setProjectId(string $exchangeRate): void
20+
*/
21+
class Currency extends Entity implements JsonSerializable {
22+
protected string $name = '';
23+
protected string $exchangeRate = '';
24+
protected string $projectid = '';
25+
26+
public function jsonSerialize(): array {
27+
return [
28+
'id' => $this->id,
29+
'name' => $this->name,
30+
'exchange_rate' => $this->exchangeRate,
31+
'projectid' => $this->projectid,
32+
];
33+
}
34+
}

0 commit comments

Comments
 (0)