Example data - Added sample data generation for RoomCategories.
CI / Docker Lint (push) Successful in 4s

This commit is contained in:
2025-02-04 20:57:51 +01:00
parent dcd0b5f064
commit 4cb3971940
5 changed files with 191 additions and 4 deletions
+4
View File
@@ -24,5 +24,9 @@ services:
arguments:
$targetDirectory: 'images/'
App\Service\ExampleDataService:
arguments:
$sampleDirectory: '../samples/'
# add more service definitions when explicit configuration is needed
# please note that last definitions always *replace* previous ones
+10
View File
@@ -0,0 +1,10 @@
[
{
"name": "Double Bed",
"iconUrl": "icon-bed-double"
},
{
"name": "Single Bed",
"iconUrl": "icon-bed-single"
}
]
+41
View File
@@ -0,0 +1,41 @@
[
{
"name": "Double room (double bed)",
"description": "Room with double bed and shared bathroom",
"maxGuestNum": 2,
"orderHomePage": null,
"roomCategoryPhotos": [
{
"path": "https://images.unsplash.com/photo-1559841644-08984562005a",
"altText": "Room",
"photoOrder": 1
}
]
},
{
"name": "Double w/ bath (double bed)",
"description": "Room with double bed and private bathroom",
"maxGuestNum": 2,
"orderHomePage": null,
"roomCategoryPhotos": [
{
"path": "https://images.unsplash.com/photo-1559841644-08984562005a",
"altText": "Room",
"photoOrder": 1
}
]
},
{
"name": "Double w/ bath (single bed)",
"description": "Room with 2 single beds and private bathroom",
"maxGuestNum": 2,
"orderHomePage": null,
"roomCategoryPhotos": [
{
"path": "https://images.unsplash.com/photo-1559841644-08984562005a",
"altText": "Room",
"photoOrder": 1
}
]
}
]
+12
View File
@@ -0,0 +1,12 @@
[
{
"amount": 1,
"roomCategory": 0,
"bedType": 0
},
{
"amount": 1,
"roomCategory": 2,
"bedType": 1
}
]
+124 -4
View File
@@ -4,9 +4,11 @@ namespace App\Service;
use App\DTO\AppSetupConfig;
use App\DTO\ExampleDataConfig;
use App\Entity\BedType;
use App\Entity\ConfigVariable;
use App\Entity\FareTable;
use App\Entity\RoomCategory;
use App\Entity\RoomCategoryBedType;
use App\Entity\RoomFare;
use App\Entity\User;
use App\Enum\UserRoleEnum;
@@ -15,24 +17,54 @@ use App\Repository\FareTableRepository;
use App\Repository\RoomAvailabilityRepository;
use App\Repository\RoomCategoryRepository;
use App\Repository\RoomFareRepository;
use App\Serializer\Normalizer\EntityByIdDenormalizer;
use DateTime;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
use stdClass;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer;
use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Serializer\SerializerInterface;
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader;
/**
* Service for setup proccess.
*/
class ExampleDataService
{
private Serializer $sampleSerializer;
private FileLocator $fileLocator;
public function __construct(
private string $sampleDirectory,
private LoggerInterface $logger,
private EntityManagerInterface $entityManager,
private RoomCategoryRepository $roomCategoryRepository,
private RoomAvailabilityRepository $roomAvailabilityRepository,
private FareTableRepository $fareTableRepository,
private RoomFareRepository $roomFareRepository,
) {}
private RoomFareRepository $roomFareRepository
) {
$this->sampleSerializer = new Serializer(
array(
new ObjectNormalizer(
null,
null,
null,
new PhpDocExtractor()
),
new ArrayDenormalizer(),
),
array('json' => new JsonEncoder())
);
$this->fileLocator = new FileLocator($this->sampleDirectory);
}
public function AddExampleData(ExampleDataConfig $config): string
{
@@ -64,12 +96,53 @@ class ExampleDataService
return "OK";
}
private function GenerateRoomCategories(): bool
public function GenerateRoomCategories(): bool
{
//TODOME.
//Add Example BedTypes.
$bedTypeList = $this->DeserializeSampleFile("SampleBedTypes.json", BedType::class . '[]');
$this->PersistEntityArray($bedTypeList);
//Add Example RoomCategories
$roomCategoryList = $this->DeserializeSampleFile("SampleRoomCategories.json", RoomCategory::class . '[]');
$this->PersistEntityArray($roomCategoryList);
//Add Example RoomCategoryBedTypes
$sampleRcBtData = $this->GetFileData("SampleRoomCategoryBedTypes.json");
$sampleRcBtContent = json_decode($sampleRcBtData);
foreach ($sampleRcBtContent as $contentElement) {
$bedType = null;
if (count($bedTypeList) > $contentElement->bedType) {
$bedType = $bedTypeList[$contentElement->bedType];
}
$roomCategory = null;
if (count($roomCategoryList) > $contentElement->roomCategory) {
$roomCategory = $roomCategoryList[$contentElement->roomCategory];
}
if (!$roomCategory || !$bedType) {
$this->logger->error(
"Invalid RoomCategoryBedType: RoomCategory or BedType not found!",
["json" => $contentElement, "bedtype" => $bedType, "roomcategory" => $roomCategory]
);
continue;
}
$item = new RoomCategoryBedType();
$item->setAmount($contentElement->amount);
$item->setBedType($bedType);
$item->setRoomCategory($roomCategory);
$this->entityManager->persist($item);
}
$this->entityManager->flush();
return true;
}
#region Generate Ramdom Data
private function GenerateAvailability(ExampleDataConfig $config): bool
{
$roomCategoryList = $this->roomCategoryRepository->findAll();
@@ -125,4 +198,51 @@ class ExampleDataService
return true;
}
#endregion
#region Load Sample Data
//TODO
#endregion
#region Utils
private function DeserializeSampleFile(string $fileName, string $class)
{
$data = $this->GetFileData($fileName);
if (!$data) return;
$itemList = $this->sampleSerializer->deserialize($data, $class, "json");
return $itemList;
}
private function GetFileData(string $fileName): string|null
{
$file = $this->fileLocator->locate($fileName, null);
$data = file_get_contents($file);
if (!$data) {
$this->logger->error(
"ExampleDataService - DeserializeSampleFile - Unable to get file contents.",
["file" => $fileName, "fileData" => $data]
);
return null;
}
return $data;
}
private function PersistEntityArray(array $itemList): void
{
foreach ($itemList as $item) {
try {
$this->entityManager->persist($item);
} catch (\Throwable $th) {
$this->logger->error("Item can't be persisted", ["item" => $item, "exception" => $th]);
}
}
}
#endregion
}