Added basic API to access and edit manufacturer entities

This commit is contained in:
Jan Böhmer 2023-08-06 20:18:51 +02:00
parent f5a15b23d6
commit 676c8eeefb
11 changed files with 96 additions and 21 deletions

View file

@ -43,21 +43,21 @@ abstract class AbstractCompany extends AbstractPartsContainingDBElement
/**
* @var string The address of the company
*/
#[Groups(['full'])]
#[Groups(['full', 'company:read', 'company:write'])]
#[ORM\Column(type: Types::STRING)]
protected string $address = '';
/**
* @var string The phone number of the company
*/
#[Groups(['full'])]
#[Groups(['full', 'company:read', 'company:write'])]
#[ORM\Column(type: Types::STRING)]
protected string $phone_number = '';
/**
* @var string The fax number of the company
*/
#[Groups(['full'])]
#[Groups(['full', 'company:read', 'company:write'])]
#[ORM\Column(type: Types::STRING)]
protected string $fax_number = '';
@ -65,7 +65,7 @@ abstract class AbstractCompany extends AbstractPartsContainingDBElement
* @var string The email address of the company
*/
#[Assert\Email]
#[Groups(['full'])]
#[Groups(['full', 'company:read', 'company:write'])]
#[ORM\Column(type: Types::STRING)]
protected string $email_address = '';
@ -73,7 +73,7 @@ abstract class AbstractCompany extends AbstractPartsContainingDBElement
* @var string The website of the company
*/
#[Assert\Url]
#[Groups(['full'])]
#[Groups(['full', 'company:read', 'company:write'])]
#[ORM\Column(type: Types::STRING)]
protected string $website = '';

View file

@ -37,6 +37,7 @@ use App\Entity\Attachments\ProjectAttachment;
use App\Entity\Attachments\StorelocationAttachment;
use App\Entity\Attachments\SupplierAttachment;
use App\Entity\Attachments\UserAttachment;
use App\Entity\Parameters\AbstractParameter;
use App\Entity\Parts\Category;
use App\Entity\ProjectSystem\Project;
use App\Entity\ProjectSystem\ProjectBOMEntry;
@ -66,14 +67,14 @@ use Symfony\Component\Serializer\Annotation\Groups;
* Every database table which are managed with this class (or a subclass of it)
* must have the table row "id"!! The ID is the unique key to identify the elements.
*/
#[DiscriminatorMap(typeProperty: 'type', mapping: ['attachment_type' => AttachmentType::class, 'attachment' => Attachment::class, 'attachment_type_attachment' => AttachmentTypeAttachment::class, 'category_attachment' => CategoryAttachment::class, 'currency_attachment' => CurrencyAttachment::class, 'footprint_attachment' => FootprintAttachment::class, 'group_attachment' => GroupAttachment::class, 'label_attachment' => LabelAttachment::class, 'manufacturer_attachment' => ManufacturerAttachment::class, 'measurement_unit_attachment' => MeasurementUnitAttachment::class, 'part_attachment' => PartAttachment::class, 'project_attachment' => ProjectAttachment::class, 'storelocation_attachment' => StorelocationAttachment::class, 'supplier_attachment' => SupplierAttachment::class, 'user_attachment' => UserAttachment::class, 'category' => Category::class, 'project' => Project::class, 'project_bom_entry' => ProjectBOMEntry::class, 'footprint' => Footprint::class, 'group' => Group::class, 'manufacturer' => Manufacturer::class, 'orderdetail' => Orderdetail::class, 'part' => Part::class, 'pricedetail' => 'App\Entity\PriceInformation\Pricedetail', 'storelocation' => Storelocation::class, 'part_lot' => PartLot::class, 'currency' => Currency::class, 'measurement_unit' => MeasurementUnit::class, 'parameter' => 'App\Entity\Parts\AbstractParameter', 'supplier' => Supplier::class, 'user' => User::class])]
#[DiscriminatorMap(typeProperty: 'type', mapping: ['attachment_type' => AttachmentType::class, 'attachment' => Attachment::class, 'attachment_type_attachment' => AttachmentTypeAttachment::class, 'category_attachment' => CategoryAttachment::class, 'currency_attachment' => CurrencyAttachment::class, 'footprint_attachment' => FootprintAttachment::class, 'group_attachment' => GroupAttachment::class, 'label_attachment' => LabelAttachment::class, 'manufacturer_attachment' => ManufacturerAttachment::class, 'measurement_unit_attachment' => MeasurementUnitAttachment::class, 'part_attachment' => PartAttachment::class, 'project_attachment' => ProjectAttachment::class, 'storelocation_attachment' => StorelocationAttachment::class, 'supplier_attachment' => SupplierAttachment::class, 'user_attachment' => UserAttachment::class, 'category' => Category::class, 'project' => Project::class, 'project_bom_entry' => ProjectBOMEntry::class, 'footprint' => Footprint::class, 'group' => Group::class, 'manufacturer' => Manufacturer::class, 'orderdetail' => Orderdetail::class, 'part' => Part::class, 'pricedetail' => 'App\Entity\PriceInformation\Pricedetail', 'storelocation' => Storelocation::class, 'part_lot' => PartLot::class, 'currency' => Currency::class, 'measurement_unit' => MeasurementUnit::class, 'parameter' => AbstractParameter::class, 'supplier' => Supplier::class, 'user' => User::class])]
#[ORM\MappedSuperclass(repositoryClass: DBElementRepository::class)]
abstract class AbstractDBElement implements JsonSerializable
{
/** @var int|null The Identification number for this part. This value is unique for the element in this table.
* Null if the element is not saved to DB yet.
*/
#[Groups(['full'])]
#[Groups(['full', 'api:basic:read'])]
#[ORM\Column(type: Types::INTEGER)]
#[ORM\Id]
#[ORM\GeneratedValue]

View file

@ -43,7 +43,7 @@ abstract class AbstractNamedDBElement extends AbstractDBElement implements Named
* @var string the name of this element
*/
#[Assert\NotBlank]
#[Groups(['simple', 'extended', 'full', 'import'])]
#[Groups(['simple', 'extended', 'full', 'import', 'api:basic:read', 'api:basic:write'])]
#[ORM\Column(type: Types::STRING)]
protected string $name = '';

View file

@ -22,6 +22,10 @@ declare(strict_types=1);
namespace App\Entity\Parts;
use ApiPlatform\Metadata\ApiProperty;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Link;
use App\Entity\Attachments\Attachment;
use App\Entity\Attachments\AttachmentTypeAttachment;
use App\Repository\Parts\ManufacturerRepository;
@ -32,6 +36,7 @@ use App\Entity\Base\AbstractCompany;
use App\Entity\Parameters\ManufacturerParameter;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
/**
@ -43,10 +48,24 @@ use Symfony\Component\Validator\Constraints as Assert;
#[ORM\Table('`manufacturers`')]
#[ORM\Index(name: 'manufacturer_name', columns: ['name'])]
#[ORM\Index(name: 'manufacturer_idx_parent_name', columns: ['parent_id', 'name'])]
#[ApiResource(
normalizationContext: ['groups' => ['manufacturer:read', 'company:read', 'api:basic:read']],
denormalizationContext: ['groups' => ['manufacturer:write', 'company:write', 'api:basic:write']],
)]
#[ApiResource(
uriTemplate: '/manufacturers/{id}/children.{_format}',
operations: [new GetCollection()],
uriVariables: [
'id' => new Link(fromClass: Manufacturer::class, fromProperty: 'children')
],
normalizationContext: ['groups' => ['manufacturer:read', 'company:read', 'api:basic:read']]
)]
class Manufacturer extends AbstractCompany
{
#[ORM\ManyToOne(targetEntity: self::class, inversedBy: 'children')]
#[ORM\JoinColumn(name: 'parent_id')]
#[Groups(['manufacturer:read', 'manufacturer:write'])]
#[ApiProperty(readableLink: false, writableLink: false)]
protected ?AbstractStructuralDBElement $parent = null;
#[ORM\OneToMany(targetEntity: self::class, mappedBy: 'parent')]
@ -59,10 +78,14 @@ class Manufacturer extends AbstractCompany
#[Assert\Valid]
#[ORM\OneToMany(targetEntity: ManufacturerAttachment::class, mappedBy: 'element', cascade: ['persist', 'remove'], orphanRemoval: true)]
#[ORM\OrderBy(['name' => 'ASC'])]
#[Groups(['manufacturer:read', 'manufacturer:write'])]
#[ApiProperty(readableLink: false, writableLink: true)]
protected Collection $attachments;
#[ORM\ManyToOne(targetEntity: ManufacturerAttachment::class)]
#[ORM\JoinColumn(name: 'id_preview_attachment', onDelete: 'SET NULL')]
#[Groups(['manufacturer:read', 'manufacturer:write'])]
#[ApiProperty(readableLink: false, writableLink: true)]
protected ?Attachment $master_picture_attachment = null;
/** @var Collection<int, ManufacturerParameter>

View file

@ -22,6 +22,7 @@ declare(strict_types=1);
namespace App\Entity\UserSystem;
use ApiPlatform\Metadata\ApiResource;
use App\Entity\Attachments\Attachment;
use App\Entity\Attachments\AttachmentTypeAttachment;
use App\Validator\Constraints\NoLockout;

View file

@ -22,6 +22,14 @@ declare(strict_types=1);
namespace App\Entity\UserSystem;
use ApiPlatform\Doctrine\Orm\Filter\SearchFilter;
use ApiPlatform\Metadata\ApiFilter;
use ApiPlatform\Metadata\ApiProperty;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Post;
use ApiPlatform\Serializer\Filter\PropertyFilter;
use App\Entity\Attachments\Attachment;
use App\Entity\Attachments\AttachmentTypeAttachment;
use App\Repository\UserRepository;
@ -72,7 +80,16 @@ use Jbtronics\TFAWebauthn\Model\TwoFactorInterface as WebauthnTwoFactorInterface
#[ORM\AttributeOverrides([
new ORM\AttributeOverride(name: 'name', column: new ORM\Column(type: Types::STRING, length: 180, unique: true))
])]
#[ApiResource(
shortName: 'User',
operations: [
new Get(),
new GetCollection(),
],
normalizationContext: ['groups' => ['user:read']],
)]
#[ApiFilter(PropertyFilter::class)]
#[ApiFilter(SearchFilter::class, properties: ['name' => 'exact', 'email' => 'exact'])]
#[NoLockout()]
class User extends AttachmentContainingDBElement implements UserInterface, HasPermissionsInterface, TwoFactorInterface,
BackupCodeInterface, TrustedDeviceInterface, WebauthnTwoFactorInterface, PreferredProviderInterface, PasswordAuthenticatedUserInterface, SamlUserInterface
@ -84,17 +101,26 @@ class User extends AttachmentContainingDBElement implements UserInterface, HasPe
*/
final public const ID_ANONYMOUS = 1;
#[Groups(['user:read'])]
protected ?int $id;
#[Groups(['user:read'])]
protected ?\DateTimeInterface $lastModified = null;
#[Groups(['user:read'])]
protected ?\DateTimeInterface $createdAt = null;
/**
* @var bool Determines if the user is disabled (user can not log in)
*/
#[Groups(['extended', 'full', 'import'])]
#[Groups(['extended', 'full', 'import', 'user:read'])]
#[ORM\Column(type: Types::BOOLEAN)]
protected bool $disabled = false;
/**
* @var string|null The theme
*/
#[Groups(['full', 'import'])]
#[Groups(['full', 'import', 'user:read'])]
#[ORM\Column(type: Types::STRING, name: 'config_theme', nullable: true)]
#[ValidTheme()]
protected ?string $theme = null;
@ -112,9 +138,9 @@ class User extends AttachmentContainingDBElement implements UserInterface, HasPe
protected string $instock_comment_w = '';
/**
* @var string A self-description of the user
* @var string A self-description of the user as markdown text
*/
#[Groups(['full', 'import'])]
#[Groups(['full', 'import', 'user:read'])]
#[ORM\Column(type: Types::TEXT)]
protected string $aboutMe = '';
@ -133,10 +159,11 @@ class User extends AttachmentContainingDBElement implements UserInterface, HasPe
* @var Group|null the group this user belongs to
* DO NOT PUT A fetch eager here! Otherwise, you can not unset the group of a user! This seems to be some kind of bug in doctrine. Maybe this is fixed in future versions.
*/
#[Groups(['extended', 'full', 'import'])]
#[Groups(['extended', 'full', 'import', 'user:read'])]
#[ORM\ManyToOne(targetEntity: Group::class, inversedBy: 'users')]
#[ORM\JoinColumn(name: 'group_id')]
#[Selectable]
#[ApiProperty(readableLink: true, writableLink: false)]
protected ?Group $group = null;
/**
@ -149,7 +176,7 @@ class User extends AttachmentContainingDBElement implements UserInterface, HasPe
* @var string|null The timezone the user prefers
*/
#[Assert\Timezone]
#[Groups(['full', 'import'])]
#[Groups(['full', 'import', 'user:read'])]
#[ORM\Column(type: Types::STRING, name: 'config_timezone', nullable: true)]
protected ?string $timezone = '';
@ -157,7 +184,7 @@ class User extends AttachmentContainingDBElement implements UserInterface, HasPe
* @var string|null The language/locale the user prefers
*/
#[Assert\Language]
#[Groups(['full', 'import'])]
#[Groups(['full', 'import', 'user:read'])]
#[ORM\Column(type: Types::STRING, name: 'config_language', nullable: true)]
protected ?string $language = '';
@ -165,7 +192,7 @@ class User extends AttachmentContainingDBElement implements UserInterface, HasPe
* @var string|null The email address of the user
*/
#[Assert\Email]
#[Groups(['simple', 'extended', 'full', 'import'])]
#[Groups(['simple', 'extended', 'full', 'import', 'user:read'])]
#[ORM\Column(type: Types::STRING, length: 255, nullable: true)]
protected ?string $email = '';
@ -173,33 +200,34 @@ class User extends AttachmentContainingDBElement implements UserInterface, HasPe
* @var bool True if the user wants to show his email address on his (public) profile
*/
#[ORM\Column(type: Types::BOOLEAN, options: ['default' => false])]
#[Groups(['full', 'import', 'user:read'])]
protected bool $show_email_on_profile = false;
/**
* @var string|null The department the user is working
*/
#[Groups(['simple', 'extended', 'full', 'import'])]
#[Groups(['simple', 'extended', 'full', 'import', 'user:read'])]
#[ORM\Column(type: Types::STRING, length: 255, nullable: true)]
protected ?string $department = '';
/**
* @var string|null The last name of the User
*/
#[Groups(['simple', 'extended', 'full', 'import'])]
#[Groups(['simple', 'extended', 'full', 'import', 'user:read'])]
#[ORM\Column(type: Types::STRING, length: 255, nullable: true)]
protected ?string $last_name = '';
/**
* @var string|null The first name of the User
*/
#[Groups(['simple', 'extended', 'full', 'import'])]
#[Groups(['simple', 'extended', 'full', 'import', 'user:read'])]
#[ORM\Column(type: Types::STRING, length: 255, nullable: true)]
protected ?string $first_name = '';
/**
* @var bool True if the user needs to change password after log in
*/
#[Groups(['extended', 'full', 'import'])]
#[Groups(['extended', 'full', 'import', 'user:read'])]
#[ORM\Column(type: Types::BOOLEAN)]
protected bool $need_pw_change = true;
@ -211,6 +239,7 @@ class User extends AttachmentContainingDBElement implements UserInterface, HasPe
#[Assert\NotBlank]
#[Assert\Regex('/^[\w\.\+\-\$]+$/', message: 'user.invalid_username')]
#[Groups(['user:read'])]
protected string $name = '';
/**
@ -224,10 +253,13 @@ class User extends AttachmentContainingDBElement implements UserInterface, HasPe
*/
#[ORM\OneToMany(mappedBy: 'element', targetEntity: UserAttachment::class, cascade: ['persist', 'remove'], orphanRemoval: true)]
#[ORM\OrderBy(['name' => 'ASC'])]
#[Groups(['user:read'])]
#[ApiProperty(readableLink: false, writableLink: false)]
protected Collection $attachments;
#[ORM\ManyToOne(targetEntity: UserAttachment::class)]
#[ORM\JoinColumn(name: 'id_preview_attachment', onDelete: 'SET NULL')]
#[Groups(['user:read'])]
protected ?Attachment $master_picture_attachment = null;
/** @var \DateTimeInterface|null The time when the backup codes were generated
@ -501,6 +533,7 @@ class User extends AttachmentContainingDBElement implements UserInterface, HasPe
*
* @return string a string with the full name of this user
*/
#[Groups(['user:read'])]
public function getFullName(bool $including_username = false): string
{
$tmp = $this->getFirstName();

View file

@ -49,6 +49,11 @@ class StructuralElementDenormalizer implements DenormalizerInterface
public function supportsDenormalization($data, string $type, string $format = null, array $context = []): bool
{
//Only denormalize if we are doing a file import operation
if (!($context['partdb_import'] ?? false)) {
return false;
}
return is_array($data)
&& is_subclass_of($type, AbstractStructuralDBElement::class)
//Only denormalize if we are doing a file import operation

View file

@ -39,6 +39,11 @@ class StructuralElementFromNameDenormalizer implements DenormalizerInterface
public function supportsDenormalization($data, string $type, string $format = null, array $context = []): bool
{
//Only denormalize if we are doing a file import operation
if (!($context['partdb_import'] ?? false)) {
return false;
}
return is_string($data) && is_subclass_of($type, AbstractStructuralDBElement::class);
}

View file

@ -43,6 +43,11 @@ class StructuralElementNormalizer implements NormalizerInterface
public function supportsNormalization($data, string $format = null, array $context = []): bool
{
//Only normalize if we are doing a file export operation
if (!($context['partdb_export'] ?? false)) {
return false;
}
return $data instanceof AbstractStructuralDBElement;
}

View file

@ -95,6 +95,7 @@ class EntityExporter
'as_collection' => true,
'csv_delimiter' => $options['csv_delimiter'],
'xml_root_node_name' => 'PartDBExport',
'partdb_export' => true,
]
);
}

View file

@ -156,6 +156,7 @@ class EntityImporter
'csv_delimiter' => $options['csv_delimiter'],
'create_unknown_datastructures' => $options['create_unknown_datastructures'],
'path_delimiter' => $options['path_delimiter'],
'partdb_import' => true,
]);
//Ensure we have an array of entity elements.