Same but for tags.

This commit is contained in:
James Cole 2019-12-30 21:04:22 +01:00
parent e1186b48ec
commit a331b2d661
No known key found for this signature in database
GPG Key ID: C16961E655E74B5E
5 changed files with 61 additions and 32 deletions

View File

@ -23,8 +23,6 @@ declare(strict_types=1);
namespace FireflyIII\Api\V1\Requests;
use FireflyIII\Models\Tag;
/**
* Class TagStoreRequest
*
@ -52,13 +50,19 @@ class TagStoreRequest extends Request
*/
public function getAll(): array
{
$hasLocation = false;
if ($this->has('longitude') && $this->has('latitude') && $this->has('zoom_level')) {
$hasLocation = true;
}
return [
'tag' => $this->string('tag'),
'date' => $this->date('date'),
'description' => $this->string('description'),
'latitude' => '' === $this->string('latitude') ? null : $this->string('latitude'),
'longitude' => '' === $this->string('longitude') ? null : $this->string('longitude'),
'zoom_level' => $this->integer('zoom_level'),
'tag' => $this->string('tag'),
'date' => $this->date('date'),
'description' => $this->string('description'),
'has_location' => $hasLocation,
'longitude' => $this->string('longitude'),
'latitude' => $this->string('latitude'),
'zoom_level' => $this->integer('zoom_level'),
];
}

View File

@ -51,13 +51,19 @@ class TagUpdateRequest extends Request
*/
public function getAll(): array
{
$updateLocation = false;
if ($this->has('longitude') && $this->has('latitude') && $this->has('zoom_level')) {
$updateLocation = true;
}
return [
'tag' => $this->string('tag'),
'date' => $this->date('date'),
'description' => $this->string('description'),
'latitude' => '' === $this->string('latitude') ? null : $this->string('latitude'),
'longitude' => '' === $this->string('longitude') ? null : $this->string('longitude'),
'zoom_level' => $this->integer('zoom_level'),
'tag' => $this->string('tag'),
'date' => $this->date('date'),
'description' => $this->string('description'),
'has_location' => $updateLocation,
'longitude' => '' === $this->string('longitude') ? null : $this->string('longitude'),
'latitude' => '' === $this->string('latitude') ? null : $this->string('latitude'),
'zoom_level' => '' === $this->string('zoom_level') ? null : $this->integer('zoom_level'),
];
}

View File

@ -405,20 +405,28 @@ class TagRepository implements TagRepositoryInterface
$tag->save();
// update, delete or create location:
$hasLocation = $data['has_location'] ?? false;
if (false === $hasLocation) {
$tag->locations()->delete();
}
if (true === $hasLocation) {
$location = $this->getLocation($tag);
if (null === $location) {
$location = new Location;
$location->locatable()->associate($tag);
$updateLocation = $data['has_location'] ?? false;
// location must be updated?
if (true === $updateLocation) {
// if all set to NULL, delete
if (null === $data['latitude'] && null === $data['longitude'] && null === $data['zoom_level']) {
$tag->locations()->delete();
}
// otherwise, update or create.
if (!(null === $data['latitude'] && null === $data['longitude'] && null === $data['zoom_level'])) {
$location = $this->getLocation($tag);
if (null === $location) {
$location = new Location;
$location->locatable()->associate($tag);
}
$location->latitude = $data['latitude'] ?? config('firefly.default_location.latitude');
$location->longitude = $data['longitude'] ?? config('firefly.default_location.longitude');
$location->zoom_level = $data['zoom_level'] ?? config('firefly.default_location.zoom_level');
$location->save();
}
$location->latitude = $data['latitude'] ?? config('firefly.default_location.latitude');
$location->longitude = $data['longitude'] ?? config('firefly.default_location.longitude');
$location->zoom_level = $data['zoom_level'] ?? config('firefly.default_location.zoom_level');
$location->save();
}
return $tag;

View File

@ -106,12 +106,12 @@ class AccountUpdateService
// location must be updated?
if (true === $updateLocation) {
// if all set to NULL, delete
if(null === $data['latitude'] && null === $data['longitude'] && null === $data['zoom_level']) {
if (null === $data['latitude'] && null === $data['longitude'] && null === $data['zoom_level']) {
$account->locations()->delete();
}
// otherwise, update or create.
if(!(null === $data['latitude'] && null === $data['longitude'] && null === $data['zoom_level'])) {
if (!(null === $data['latitude'] && null === $data['longitude'] && null === $data['zoom_level'])) {
$location = $this->accountRepository->getLocation($account);
if (null === $location) {
$location = new Location;

View File

@ -24,6 +24,7 @@ declare(strict_types=1);
namespace FireflyIII\Transformers;
use FireflyIII\Models\Location;
use FireflyIII\Models\Tag;
use Log;
@ -56,6 +57,16 @@ class TagTransformer extends AbstractTransformer
public function transform(Tag $tag): array
{
$date = null === $tag->date ? null : $tag->date->format('Y-m-d');
/** @var Location $location */
$location = $tag->locations()->first();
$latitude = null;
$longitude = null;
$zoomLevel = null;
if (null !== $location) {
$latitude = $location->latitude;
$longitude = $location->longitude;
$zoomLevel = $location->zoom_level;
}
$data = [
'id' => (int)$tag->id,
'created_at' => $tag->created_at->toAtomString(),
@ -63,9 +74,9 @@ class TagTransformer extends AbstractTransformer
'tag' => $tag->tag,
'date' => $date,
'description' => '' === $tag->description ? null : $tag->description,
'latitude' => null === $tag->latitude ? null : (float)$tag->latitude,
'longitude' => null === $tag->longitude ? null : (float)$tag->longitude,
'zoom_level' => null === $tag->zoomLevel ? null : (int)$tag->zoomLevel,
'longitude' => $longitude,
'latitude' => $latitude,
'zoom_level' => $zoomLevel,
'links' => [
[
'rel' => 'self',