Sort projects by ebook title

This commit is contained in:
Alex Cabal 2024-12-18 14:27:02 -06:00
parent b2191d1219
commit a1da50fc1a
3 changed files with 74 additions and 3 deletions

View file

@ -49,6 +49,7 @@ use function Safe\shell_exec;
*/ */
class Ebook{ class Ebook{
use Traits\Accessor; use Traits\Accessor;
use Traits\FromRow;
public int $EbookId; public int $EbookId;
public string $Identifier; public string $Identifier;

View file

@ -24,6 +24,7 @@ use Safe\DateTimeImmutable;
*/ */
class Project{ class Project{
use Traits\Accessor; use Traits\Accessor;
use Traits\FromRow;
use Traits\PropertyFromHttp; use Traits\PropertyFromHttp;
public int $ProjectId; public int $ProjectId;
@ -606,20 +607,37 @@ class Project{
* @return array<Project> * @return array<Project>
*/ */
public static function GetAllByStatus(Enums\ProjectStatusType $status): array{ public static function GetAllByStatus(Enums\ProjectStatusType $status): array{
return Db::Query('SELECT * from Projects where Status = ? order by Started desc', [$status], Project::class); return Db::Query('SELECT * from Projects inner join Ebooks using (EbookId) where Projects.Status = ? order by Title asc', [$status], Project::class);
} }
/** /**
* @return array<Project> * @return array<Project>
*/ */
public static function GetAllByManagerUserId(int $userId): array{ public static function GetAllByManagerUserId(int $userId): array{
return Db::Query('SELECT * from Projects where ManagerUserId = ? and Status in (?, ?) order by Started desc', [$userId, Enums\ProjectStatusType::InProgress, Enums\ProjectStatusType::Stalled], Project::class); return Db::Query('SELECT * from Projects inner join Ebooks using (EbookId) where ManagerUserId = ? and Status in (?, ?) order by Title asc', [$userId, Enums\ProjectStatusType::InProgress, Enums\ProjectStatusType::Stalled], Project::class);
} }
/** /**
* @return array<Project> * @return array<Project>
*/ */
public static function GetAllByReviewerUserId(int $userId): array{ public static function GetAllByReviewerUserId(int $userId): array{
return Db::Query('SELECT * from Projects where ReviewerUserId = ? and Status in (?, ?) order by Started desc', [$userId, Enums\ProjectStatusType::InProgress, Enums\ProjectStatusType::Stalled], Project::class); return Db::Query('SELECT * from Projects inner join Ebooks using (EbookId) where ReviewerUserId = ? and Status in (?, ?) order by Title asc', [$userId, Enums\ProjectStatusType::InProgress, Enums\ProjectStatusType::Stalled], Project::class);
}
/**
* Creates a `Project` from a multi table array containing a `Project` and an `Ebook`.
*
* @param array<string, stdClass> $row
*/
public static function FromMultiTableRow(array $row): Project{
$object = Project::FromRow($row['Projects']);
// The Action may be null if it's a Scribophile adjustment. In that case, don't initialize the Action object.
if($row['Ebooks']->EbookId !== null){
$row['Ebooks']->Ebookid = $object->EbookId;
$object->Ebook = Ebook::FromRow($row['Ebooks']);
}
return $object;
} }
} }

52
lib/Traits/FromRow.php Normal file
View file

@ -0,0 +1,52 @@
<?
namespace Traits;
/**
* Normally, the `DbConnection` class fills in an object itself, using reflection to decide on enums. Sometimes, we want to define an explicit `FromRow()` method on a class. This trait provides a default `FromRow()` method that assigns columns to object properties, and attemps to figure out enum types. The object can override this method if necessary.
*/
trait FromRow{
/**
* Fill an object with the given database row.
* This is useful when a subclass has to fill its object in a `FromRow()` method, but its parent class also has a `FromRow()` method defined. For example, `CompoundAction` and `Action`.
*
* @template T of self
*
* @param T $object
* @return T
*/
public static function FillObject(object $object, \stdClass $row): object{
foreach(get_object_vars($row) as $property => $value){
if(is_string($value)){
try{
$type = (new \ReflectionProperty($object, $property))->getType();
if($type instanceof \ReflectionNamedType){
$typeName = $type->getName();
if($typeName != 'string' && is_a($typeName, 'BackedEnum', true)){
$object->$property = $typeName::from($value);
}
else{
$object->$property = $value;
}
}
}
catch(\Exception){
$object->$property = $value;
}
}
else{
$object->$property = $value;
}
}
return $object;
}
/**
* Return an object based on a database row result. This is a fallback object filler for when the parent class hasn't defined their own.
*/
public static function FromRow(\stdClass $row): static{
return self::FillObject(new static(), $row);
}
}