Skip to content

Commit f5aefcb

Browse files
committed
Improved loading time by not always loading samples
1 parent c6b4614 commit f5aefcb

28 files changed

+372
-86
lines changed

Diff for: app/Application/Buddies/ViewModels/BuddyDetailViewModel.php

+11-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010

1111
final class BuddyDetailViewModel extends ViewModel
1212
{
13-
protected array $visible = ['buddy_id', 'text', 'color', 'dive_count', 'last_dive', 'email', 'buddy_user_id'];
13+
protected array $visible = [
14+
'buddy_id', 'text', 'color', 'dive_count', 'last_dive', 'email', 'buddy_user_id', 'updated'
15+
];
1416

1517
public function __construct(
1618
private int $buddyId,
@@ -20,6 +22,7 @@ public function __construct(
2022
private int $buddyUserId,
2123
private int $diveCount,
2224
private ?DateTimeInterface $lastDive,
25+
private DateTimeInterface $updated,
2326
) {
2427
}
2528

@@ -31,8 +34,9 @@ public static function fromDetailBuddy(DetailBuddy $buddy): self
3134
color: $buddy->getColor(),
3235
email: $buddy->getEmail(),
3336
buddyUserId: 0,
34-
lastDive:$buddy->getLastDive(),
3537
diveCount: $buddy->getDiveCount(),
38+
lastDive: $buddy->getLastDive(),
39+
updated: $buddy->getUpdated(),
3640
);
3741
}
3842

@@ -70,4 +74,9 @@ public function getBuddyUserId(): ?int
7074
{
7175
return $this->buddyUserId;
7276
}
77+
78+
public function getUpdated(): string
79+
{
80+
return $this->updated->format(\DateTimeInterface::ATOM);
81+
}
7382
}

Diff for: app/Application/Dives/Services/DiveCreator.php

+6-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use App\Application\Dives\DataTransferObjects\DiveData;
88
use App\Domain\Dives\Entities\Dive;
9+
use App\Domain\Dives\ValueObjects\DiveId;
910
use App\Domain\Users\Entities\User;
1011

1112
final class DiveCreator
@@ -15,7 +16,7 @@ public function __construct(
1516
) {
1617
}
1718

18-
public function create(User $user, DiveData $diveData): Dive
19+
public function create(User $user, DiveData $diveData): DiveId
1920
{
2021
$dive = Dive::new(
2122
userId: $user->getId(),
@@ -25,8 +26,10 @@ public function create(User $user, DiveData $diveData): Dive
2526
fingerprint: $diveData->getFingerprint(),
2627
);
2728

28-
$this->diveUpdater->update($dive, $diveData);
29+
if ($diveData->getSamples() !== null) {
30+
$dive->setSamples($diveData->getSamples());
31+
}
2932

30-
return $dive;
33+
return $this->diveUpdater->update($dive, $diveData);
3134
}
3235
}

Diff for: app/Application/Dives/Services/DiveUpdater.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use App\Domain\Dives\Entities\Dive;
1616
use App\Domain\Dives\Entities\DiveTank;
1717
use App\Domain\Dives\Repositories\DiveRepository;
18+
use App\Domain\Dives\ValueObjects\DiveId;
1819
use App\Domain\Dives\ValueObjects\GasMixture;
1920
use App\Domain\Dives\ValueObjects\TankPressures;
2021
use App\Domain\Support\Arrg;
@@ -32,7 +33,7 @@ public function __construct(
3233
) {
3334
}
3435

35-
public function update(Dive $dive, DiveData $diveData): void
36+
public function update(Dive $dive, DiveData $diveData): DiveId
3637
{
3738
$user = $this->userRepository->getCurrentUser();
3839

@@ -85,6 +86,6 @@ public function update(Dive $dive, DiveData $diveData): void
8586
);
8687
$dive->setTanks($tanks);
8788

88-
$this->diveRepository->save($dive);
89+
return $this->diveRepository->save($dive);
8990
}
9091
}

Diff for: app/Application/Dives/Services/Mergers/DiveMergerImpl.php

+12-5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use App\Application\Dives\Exceptions\CannotMergeDivesException;
88
use App\Domain\Dives\Entities\Dive;
9+
use App\Domain\Dives\Entities\DiveWithSamples;
910
use App\Domain\Support\ArrayUtil;
1011
use App\Domain\Support\Arrg;
1112

@@ -34,18 +35,24 @@ public function merge(array $dives): Dive
3435

3536
$divesWithComputerPreferred = $this->preferDivesWithComputer($dives);
3637

37-
return Dive::new(
38+
$dive = Dive::new(
3839
userId: $dives[0]->getUserId(),
39-
divetime: array_sum(Arrg::call($divesWithComputerPreferred, 'getDivetime')),
4040
date: min(Arrg::call($divesWithComputerPreferred, 'getDate')),
41+
divetime: array_sum(Arrg::call($divesWithComputerPreferred, 'getDivetime')),
4142
maxDepth: max(Arrg::call($divesWithComputerPreferred, 'getMaxDepth')),
42-
place: Arrg::firstNotNull(Arrg::call($dives, 'getPlace')),
4343
computer: Arrg::firstNotNull(Arrg::call($divesWithComputerPreferred, 'getComputer')),
44+
place: Arrg::firstNotNull(Arrg::call($dives, 'getPlace')),
4445
tanks: $this->diveTankMerger->mergeForDives($dives),
4546
tags: $this->diveEntityMerger->unique(ArrayUtil::flatten(Arrg::call($dives, 'getTags'))),
46-
buddies: $this->diveEntityMerger->unique(ArrayUtil::flatten(Arrg::call($dives, 'getBuddies'))),
47-
samples: $this->diveSampleStitcher->combine($dives),
47+
buddies: $this->diveEntityMerger->unique(ArrayUtil::flatten(Arrg::call($dives, 'getBuddies')))
4848
);
49+
50+
$samples = $this->diveSampleStitcher->combine($dives);
51+
if (empty($samples)) {
52+
return $dive;
53+
}
54+
55+
return DiveWithSamples::addSamples($dive, $samples);
4956
}
5057

5158
/**

Diff for: app/Application/Dives/Services/Mergers/DiveSampleCombinerImpl.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public function combine(array $dives): array
2020

2121
$divesWithSamples = Arrg::filter(
2222
$dives,
23-
fn (Dive $dive) => !empty($dive->getSamples())
23+
fn (Dive $dive) => $dive->hasSamples()
2424
);
2525
$orderedDives = $this->orderDives($divesWithSamples);
2626

Diff for: app/Application/Dives/ViewModels/DiveDetailViewModel.php

+4-5
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use App\Domain\Buddies\Entities\Buddy;
1212
use App\Domain\Dives\Entities\Dive;
1313
use App\Domain\Dives\Entities\DiveTank;
14+
use App\Domain\Dives\ValueObjects\DiveId;
1415
use App\Domain\Support\Arrg;
1516
use App\Domain\Tags\Entities\Tag;
1617

@@ -19,7 +20,7 @@ final class DiveDetailViewModel extends ViewModel
1920
protected array $visible = [
2021
'dive_id', 'date', 'divetime',
2122
'max_depth', 'place', 'buddies',
22-
'tags', 'tanks', 'samples', 'updated'
23+
'tags', 'tanks', 'updated'
2324
];
2425

2526
/**
@@ -28,15 +29,14 @@ final class DiveDetailViewModel extends ViewModel
2829
* @param DiveTankViewModel[] $diveTanks
2930
*/
3031
public function __construct(
31-
private int $diveId,
32+
private DiveId $diveId,
3233
private ?\DateTimeInterface $date,
3334
private ?int $divetime,
3435
private ?float $maxDepth,
3536
private ?PlaceViewModel $place,
3637
private array $tags,
3738
private array $buddies,
3839
private array $diveTanks,
39-
private array $samples,
4040
private \DateTimeInterface $updated,
4141
) {
4242
}
@@ -52,14 +52,13 @@ public static function fromDive(Dive $dive): self
5252
Arrg::map($dive->getTags(), fn (Tag $tag) => ShortTagViewModel::fromTag($tag)),
5353
Arrg::map($dive->getBuddies(), fn (Buddy $buddy) => ShortBuddyViewModel::fromBuddy($buddy)),
5454
Arrg::map($dive->getTanks(), fn (DiveTank $diveTank) => DiveTankViewModel::fromDiveTank($diveTank)),
55-
$dive->getSamples(),
5655
$dive->getUpdated(),
5756
);
5857
}
5958

6059
public function getDiveId()
6160
{
62-
return $this->diveId;
61+
return $this->diveId->value();
6362
}
6463

6564
public function getDivetime(): ?int

Diff for: app/Domain/Buddies/Entities/DetailBuddy.php

+6
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public function __construct(
1616
private ?string $email,
1717
private ?DateTimeInterface $lastDive,
1818
private int $diveCount,
19+
private ?DateTimeInterface $updated,
1920
) {
2021
}
2122

@@ -53,4 +54,9 @@ public function getId(): ?int
5354
{
5455
return $this->id;
5556
}
57+
58+
public function getUpdated(): DateTimeInterface
59+
{
60+
return $this->updated;
61+
}
5662
}

Diff for: app/Domain/Dives/Entities/Dive.php

+33-22
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,22 @@
66

77
use App\Domain\Buddies\Entities\Buddy;
88
use App\Domain\Computers\Entities\Computer;
9+
use App\Domain\Dives\ValueObjects\DiveId;
910
use App\Domain\Places\Entities\Place;
1011
use App\Domain\Tags\Entities\Tag;
1112
use DateTimeInterface;
1213
use Webmozart\Assert\Assert;
1314

1415
final class Dive
1516
{
16-
public function __construct(
17-
private ?int $diveId,
17+
private function __construct(
18+
private DiveId $diveId,
1819
private ?int $userId,
1920
private DateTimeInterface $updated,
2021
private ?DateTimeInterface $date,
2122
private ?int $divetime,
2223
private ?float $maxDepth,
23-
private ?Computer $computerId,
24+
private ?Computer $computer,
2425
private ?string $fingerprint,
2526
private ?Place $place,
2627
private array $tanks,
@@ -34,7 +35,7 @@ public function __construct(
3435
}
3536

3637
public static function existing(
37-
int $diveId,
38+
DiveId $diveId,
3839
int $userId,
3940
DateTimeInterface $updated,
4041
?DateTimeInterface $date,
@@ -46,16 +47,16 @@ public static function existing(
4647
array $tanks = [],
4748
array $tags = [],
4849
array $buddies = [],
49-
?array $samples = [],
50+
?array $samples = null
5051
): self {
51-
return new self(
52+
return new static(
5253
diveId: $diveId,
5354
userId: $userId,
5455
updated: $updated,
5556
date: $date,
5657
divetime: $divetime,
5758
maxDepth: $maxDepth,
58-
computerId: $computer,
59+
computer: $computer,
5960
fingerprint: $fingerprint,
6061
place: $place,
6162
tanks: $tanks,
@@ -76,16 +77,16 @@ public static function new(
7677
array $tanks = [],
7778
array $tags = [],
7879
array $buddies = [],
79-
?array $samples = [],
80+
?array $samples = null,
8081
): self {
8182
return new self(
82-
diveId: null,
83+
diveId: DiveId::new(),
8384
userId: $userId,
8485
updated: new \DateTimeImmutable(),
8586
date: $date,
8687
divetime: $divetime,
8788
maxDepth: $maxDepth,
88-
computerId: $computer,
89+
computer: $computer,
8990
fingerprint: $fingerprint,
9091
place: $place,
9192
tanks: $tanks,
@@ -95,12 +96,12 @@ public static function new(
9596
);
9697
}
9798

98-
public function getDiveId(): ?int
99+
public function getDiveId(): DiveId
99100
{
100101
return $this->diveId;
101102
}
102103

103-
public function setDiveId(?int $diveId): void
104+
public function setDiveId(DiveId $diveId): void
104105
{
105106
$this->diveId = $diveId;
106107
}
@@ -147,12 +148,12 @@ public function setMaxDepth(?float $maxDepth): void
147148

148149
public function getComputer(): ?Computer
149150
{
150-
return $this->computerId;
151+
return $this->computer;
151152
}
152153

153154
public function setComputer(?Computer $computerId): void
154155
{
155-
$this->computerId = $computerId;
156+
$this->computer = $computerId;
156157
}
157158

158159
public function getFingerprint(): ?string
@@ -213,23 +214,33 @@ public function setBuddies(array $buddies): void
213214
$this->buddies = $buddies;
214215
}
215216

216-
public function getSamples(): array
217+
public function isExisting(): bool
217218
{
218-
return $this->samples ?? [];
219+
return !$this->diveId->isNew();
219220
}
220221

221-
public function setSamples(array $samples): void
222+
public function getUpdated(): DateTimeInterface
222223
{
223-
$this->samples = $samples;
224+
return $this->updated;
224225
}
225226

226-
public function isExisting(): bool
227+
public function setUpdated(DateTimeInterface $updated): void
227228
{
228-
return $this->diveId !== null;
229+
$this->updated = $updated;
229230
}
230231

231-
public function getUpdated(): DateTimeInterface
232+
public function getSamples(): array
232233
{
233-
return $this->updated;
234+
return $this->samples;
235+
}
236+
237+
public function setSamples(array $samples): void
238+
{
239+
$this->samples = $samples;
240+
}
241+
242+
public function hasSamples(): bool
243+
{
244+
return !empty($this->samples);
234245
}
235246
}

Diff for: app/Domain/Dives/Entities/DiveSamples.php

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Domain\Dives\Entities;
6+
7+
use App\Domain\Dives\ValueObjects\DiveId;
8+
9+
final class DiveSamples
10+
{
11+
public function __construct(
12+
private DiveId $diveId,
13+
private array $samples = [],
14+
) {
15+
}
16+
17+
public static function create(DiveId $diveId, mixed $samples)
18+
{
19+
return new self($diveId, $samples);
20+
}
21+
22+
public function samples(): array
23+
{
24+
return $this->samples;
25+
}
26+
27+
public function diveId(): DiveId
28+
{
29+
return $this->diveId;
30+
}
31+
}

0 commit comments

Comments
 (0)