Skip to content

Commit

Permalink
Merge pull request #7 from robiningelbrecht/fix-medal-count
Browse files Browse the repository at this point in the history
Fix medal count
  • Loading branch information
robiningelbrecht authored Feb 2, 2024
2 parents bfd1e68 + df62a05 commit 0368c62
Show file tree
Hide file tree
Showing 12 changed files with 134 additions and 109 deletions.
15 changes: 15 additions & 0 deletions docs/openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -957,6 +957,21 @@ components:
type: integer
format: int64
example: 35643
medals:
type: object
properties:
bronze:
type: integer
format: int64
example: 10
silver:
type: integer
format: int64
example: 3
gold:
type: integer
format: int64
example: 23
results:
type: object
properties:
Expand Down
24 changes: 12 additions & 12 deletions src/Domain/Competition/Competition.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,18 @@ public static function fromState(
string $externalWebsite = null,
): self {
return new self(
$id,
$name,
$city,
$country,
$date,
$isCanceled,
$events,
$wcaDelegates,
$venue,
$organisers,
$information,
$externalWebsite,
id: $id,
name: $name,
city: $city,
country: $country,
date: $date,
isCanceled: $isCanceled,
events: $events,
wcaDelegates: $wcaDelegates,
venue: $venue,
organisers: $organisers,
information: $information,
externalWebsite: $externalWebsite,
);
}

Expand Down
24 changes: 12 additions & 12 deletions src/Domain/Competition/CompetitionRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,18 +158,18 @@ private function buildResult(array $result): Competition
}

return Competition::fromState(
$result['id'],
$result['name'],
$result['cityName'],
Iso2Code::fromString($result['iso2']),
DateRange::fromFromDateAndTillDate(
id: $result['id'],
name: $result['name'],
city: $result['cityName'],
country: Iso2Code::fromString($result['iso2']),
date: DateRange::fromFromDateAndTillDate(
SerializableDateTime::fromString($result['year'].'-'.$result['month'].'-'.$result['day']),
SerializableDateTime::fromString($result['year'].'-'.$result['endMonth'].'-'.$result['endDay']),
),
$result['cancelled'],
explode(' ', $result['eventSpecs']),
$wcaDelegates,
Venue::fromValues(
isCanceled: $result['cancelled'],
events: explode(' ', $result['eventSpecs']),
wcaDelegates: $wcaDelegates,
venue: Venue::fromValues(
$result['venue'],
$result['venueAddress'],
$result['venueDetails'],
Expand All @@ -178,9 +178,9 @@ private function buildResult(array $result): Competition
$result['longitude'],
)
),
$organisers,
$result['information'],
$result['external_website'],
organisers: $organisers,
information: $result['information'],
externalWebsite: $result['external_website'],
);
}
}
6 changes: 3 additions & 3 deletions src/Domain/Event/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ public static function fromState(
string $format,
): self {
return new self(
$id,
$name,
$format
id: $id,
name: $name,
format: $format
);
}

Expand Down
6 changes: 3 additions & 3 deletions src/Domain/Event/EventRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ public function findAll(): Overview
$overview = Overview::empty(Pagination::default(), $total);
foreach ($results as $result) {
$overview->addItem(Event::fromState(
$result['id'],
$result['name'],
$result['format'],
id: $result['id'],
name: $result['name'],
format: $result['format'],
));
}

Expand Down
20 changes: 10 additions & 10 deletions src/Domain/Person/Person.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ public static function fromState(
array $championshipIds,
): self {
return new self(
$id,
$name,
$country,
$competitionIds,
$ranks,
$results,
$championshipIds
id: $id,
name: $name,
country: $country,
competitionIds: $competitionIds,
ranks: $ranks,
results: $results,
championshipIds: $championshipIds
);
}

Expand Down Expand Up @@ -114,9 +114,9 @@ public function jsonSerialize(): array
],
'results' => $results,
'medals' => [
'gold' => count(array_filter($this->results, fn (Result $result) => 1 == $result->getPosition())),
'silver' => count(array_filter($this->results, fn (Result $result) => 2 == $result->getPosition())),
'bronze' => count(array_filter($this->results, fn (Result $result) => 3 == $result->getPosition())),
'gold' => count(array_filter($this->results, fn (Result $result) => $result->isFinalRound() && 1 == $result->getPosition())),
'silver' => count(array_filter($this->results, fn (Result $result) => $result->isFinalRound() && 2 == $result->getPosition())),
'bronze' => count(array_filter($this->results, fn (Result $result) => $result->isFinalRound() && 3 == $result->getPosition())),
],
];
}
Expand Down
14 changes: 7 additions & 7 deletions src/Domain/Person/PersonRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@ public function findOneBy(

foreach ($results as $result) {
$overview->addItem(Person::fromState(
$result['id'],
$result['name'],
Iso2Code::fromString($result['iso2']),
$this->competitionRepository->findCompetitionIdsByPerson($result['id']),
$this->rankRepository->findByPerson($result['id']),
$this->resultRepository->findByPerson($result['id']),
$this->championshipRepository->findChampionshipIdsByPerson($result['id']),
id: $result['id'],
name: $result['name'],
country: Iso2Code::fromString($result['iso2']),
competitionIds: $this->competitionRepository->findCompetitionIdsByPerson($result['id']),
ranks: $this->rankRepository->findByPerson($result['id']),
results: $this->resultRepository->findByPerson($result['id']),
championshipIds: $this->championshipRepository->findChampionshipIdsByPerson($result['id']),
));
}

Expand Down
14 changes: 7 additions & 7 deletions src/Domain/Rank/Rank.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ public static function fromState(
int $countryRank,
): self {
return new self(
$rankType,
$personId,
$eventId,
$best,
$worldRank,
$continentRank,
$countryRank,
rankType: $rankType,
personId: $personId,
eventId: $eventId,
best: $best,
worldRank: $worldRank,
continentRank: $continentRank,
countryRank: $countryRank,
);
}

Expand Down
28 changes: 14 additions & 14 deletions src/Domain/Rank/RankRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,13 @@ public function findOneBy(

foreach ($results as $result) {
$overview->addItem(Rank::fromState(
$rankType,
$result['personId'],
$result['eventId'],
$result['best'],
$result['worldRank'],
$result['continentRank'],
$result['countryRank'],
rankType: $rankType,
personId: $result['personId'],
eventId: $result['eventId'],
best: $result['best'],
worldRank: $result['worldRank'],
continentRank: $result['continentRank'],
countryRank: $result['countryRank'],
));
}

Expand All @@ -96,13 +96,13 @@ public function findByPerson(string $personId): array
])->fetchAllAssociative();

return array_map(fn (array $result) => Rank::fromState(
RankType::from($result['rankType']),
$result['personId'],
$result['eventId'],
$result['best'],
$result['worldRank'],
$result['continentRank'],
$result['countryRank'],
rankType: RankType::from($result['rankType']),
personId: $result['personId'],
eventId: $result['eventId'],
best: $result['best'],
worldRank: $result['worldRank'],
continentRank: $result['continentRank'],
countryRank: $result['countryRank'],
), $results);
}
}
26 changes: 17 additions & 9 deletions src/Domain/Result/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ private function __construct(
private string $personId,
private string $eventId,
private string $round,
private bool $isFinalRound,
private int $position,
private int $best,
private int $average,
Expand All @@ -28,22 +29,24 @@ public static function fromState(
string $personId,
string $eventId,
string $round,
bool $isFinalRound,
int $position,
int $best,
int $average,
string $format,
array $solves,
): self {
return new self(
$competitionId,
$personId,
$eventId,
$round,
$position,
$best,
$average,
$format,
$solves,
competitionId: $competitionId,
personId: $personId,
eventId: $eventId,
round: $round,
isFinalRound: $isFinalRound,
position: $position,
best: $best,
average: $average,
format: $format,
solves: $solves,
);
}

Expand All @@ -62,6 +65,11 @@ public function getRound(): string
return $this->round;
}

public function isFinalRound(): bool
{
return $this->isFinalRound;
}

public function getPosition(): int
{
return $this->position;
Expand Down
42 changes: 22 additions & 20 deletions src/Domain/Result/ResultRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function findOneBy(
string $eventId = null,
): Overview {
$queryBuilder = $this->connection->createQueryBuilder();
$queryBuilder->select('r.*, rt.name as roundName, f.name as formatName')
$queryBuilder->select('r.*, rt.name as roundName, rt.final as isFinalRound, f.name as formatName')
->from('Results', 'r')
->innerJoin('r', 'RoundTypes', 'rt', 'r.roundTypeId = rt.id')
->innerJoin('r', 'Formats', 'f', 'r.formatId = f.id')
Expand Down Expand Up @@ -53,15 +53,16 @@ public function findOneBy(

foreach ($results as $result) {
$overview->addItem(Result::fromState(
$result['competitionId'],
$result['personId'],
$result['eventId'],
$result['roundName'],
$result['pos'],
$result['best'],
$result['average'],
$result['formatName'],
[
competitionId: $result['competitionId'],
personId: $result['personId'],
eventId: $result['eventId'],
round: $result['roundName'],
isFinalRound: !empty($result['isFinalRound']),
position: $result['pos'],
best: $result['best'],
average: $result['average'],
format: $result['formatName'],
solves: [
$result['value1'],
$result['value2'],
$result['value3'],
Expand All @@ -80,7 +81,7 @@ public function findOneBy(
public function findByPerson(string $personId): array
{
$query = '
SELECT r.*, rt.name as roundName, f.name as formatName
SELECT r.*, rt.name as roundName, rt.final as isFinalRound, f.name as formatName
FROM Results r
INNER JOIN RoundTypes rt ON r.roundTypeId = rt.id
INNER JOIN Formats f ON r.formatId = f.id
Expand All @@ -95,15 +96,16 @@ public function findByPerson(string $personId): array
])->fetchAllAssociative();

return array_map(fn (array $result) => Result::fromState(
$result['competitionId'],
$result['personId'],
$result['eventId'],
$result['roundName'],
$result['pos'],
$result['best'],
$result['average'],
$result['formatName'],
[
competitionId: $result['competitionId'],
personId: $result['personId'],
eventId: $result['eventId'],
round: $result['roundName'],
isFinalRound: !empty($result['isFinalRound']),
position: $result['pos'],
best: $result['best'],
average: $result['average'],
format: $result['formatName'],
solves: [
$result['value1'],
$result['value2'],
$result['value3'],
Expand Down
Loading

0 comments on commit 0368c62

Please sign in to comment.