diff --git a/src/backend/migrations/Version20210901131245.php b/src/backend/migrations/Version20210901131245.php new file mode 100644 index 0000000..37cc8a9 --- /dev/null +++ b/src/backend/migrations/Version20210901131245.php @@ -0,0 +1,61 @@ +addSql('CREATE SEQUENCE person_id_seq INCREMENT BY 1 MINVALUE 1 START 1'); + $this->addSql('CREATE TABLE person (id INT NOT NULL, country_id INT DEFAULT NULL, state_id INT DEFAULT NULL, name VARCHAR(255) NOT NULL, surname VARCHAR(255) NOT NULL, city VARCHAR(255) DEFAULT NULL, zip VARCHAR(7) DEFAULT NULL, avatar VARCHAR(255) DEFAULT NULL, PRIMARY KEY(id))'); + $this->addSql('CREATE INDEX IDX_34DCD176F92F3E70 ON person (country_id)'); + $this->addSql('CREATE INDEX IDX_34DCD1765D83CC1 ON person (state_id)'); + $this->addSql('ALTER TABLE person ADD CONSTRAINT FK_34DCD176F92F3E70 FOREIGN KEY (country_id) REFERENCES country (id) NOT DEFERRABLE INITIALLY IMMEDIATE'); + $this->addSql('ALTER TABLE person ADD CONSTRAINT FK_34DCD1765D83CC1 FOREIGN KEY (state_id) REFERENCES country_region (id) NOT DEFERRABLE INITIALLY IMMEDIATE'); + $this->addSql('ALTER TABLE "user" DROP CONSTRAINT fk_8d93d649f92f3e70'); + $this->addSql('ALTER TABLE "user" DROP CONSTRAINT fk_8d93d6495d83cc1'); + $this->addSql('DROP INDEX idx_8d93d649f92f3e70'); + $this->addSql('DROP INDEX idx_8d93d6495d83cc1'); + $this->addSql('ALTER TABLE "user" ADD person_id INT DEFAULT NULL'); + $this->addSql('ALTER TABLE "user" DROP country_id'); + $this->addSql('ALTER TABLE "user" DROP state_id'); + $this->addSql('ALTER TABLE "user" DROP name'); + $this->addSql('ALTER TABLE "user" DROP surname'); + $this->addSql('ALTER TABLE "user" DROP avatar'); + $this->addSql('ALTER TABLE "user" ADD CONSTRAINT FK_8D93D649217BBB47 FOREIGN KEY (person_id) REFERENCES person (id) NOT DEFERRABLE INITIALLY IMMEDIATE'); + $this->addSql('CREATE UNIQUE INDEX UNIQ_8D93D649217BBB47 ON "user" (person_id)'); + } + + public function down(Schema $schema): void + { + // this down() migration is auto-generated, please modify it to your needs + $this->addSql('CREATE SCHEMA public'); + $this->addSql('ALTER TABLE "user" DROP CONSTRAINT FK_8D93D649217BBB47'); + $this->addSql('DROP SEQUENCE person_id_seq CASCADE'); + $this->addSql('DROP TABLE person'); + $this->addSql('DROP INDEX UNIQ_8D93D649217BBB47'); + $this->addSql('ALTER TABLE "user" ADD state_id INT DEFAULT NULL'); + $this->addSql('ALTER TABLE "user" ADD name VARCHAR(255) NOT NULL'); + $this->addSql('ALTER TABLE "user" ADD surname VARCHAR(255) NOT NULL'); + $this->addSql('ALTER TABLE "user" ADD avatar VARCHAR(255) DEFAULT NULL'); + $this->addSql('ALTER TABLE "user" RENAME COLUMN person_id TO country_id'); + $this->addSql('ALTER TABLE "user" ADD CONSTRAINT fk_8d93d649f92f3e70 FOREIGN KEY (country_id) REFERENCES country (id) NOT DEFERRABLE INITIALLY IMMEDIATE'); + $this->addSql('ALTER TABLE "user" ADD CONSTRAINT fk_8d93d6495d83cc1 FOREIGN KEY (state_id) REFERENCES country_region (id) NOT DEFERRABLE INITIALLY IMMEDIATE'); + $this->addSql('CREATE INDEX idx_8d93d649f92f3e70 ON "user" (country_id)'); + $this->addSql('CREATE INDEX idx_8d93d6495d83cc1 ON "user" (state_id)'); + } +} diff --git a/src/backend/src/Controller/AuthController.php b/src/backend/src/Controller/AuthController.php index 98aaf38..826e1dd 100644 --- a/src/backend/src/Controller/AuthController.php +++ b/src/backend/src/Controller/AuthController.php @@ -3,6 +3,7 @@ namespace App\Controller; use App\Entity\User; +use App\Entity\Person; use App\Traits\JsonResponseTrait; use App\Repository\UserRepository; use Symfony\Component\HttpFoundation\Request; @@ -37,13 +38,20 @@ class AuthController extends AbstractController return $this->notAcceptable("User email exists"); } + $person = new Person(); + $person->setName($name); + $person->setSurname($surname); + $person->generateAvatar($email); + $em->persist($person); + $em->flush(); + $user = new User(); $user->setPassword($encoder->hashPassword($user, $password)); $user->setEmail($email); - $user->setName($name); - $user->setSurname($surname); + $user->setPerson($person); $em->persist($user); $em->flush(); + return $this->created($user); } @@ -60,7 +68,7 @@ class AuthController extends AbstractController if (!$user) { return $this->unauthorized([]); } - $user->generateAvatar(); + $user->getPerson()->generateAvatar($user->getEmail()); return $this->ok($user); } } diff --git a/src/backend/src/Controller/PersonController.php b/src/backend/src/Controller/PersonController.php new file mode 100644 index 0000000..1bdf482 --- /dev/null +++ b/src/backend/src/Controller/PersonController.php @@ -0,0 +1,23 @@ +personRepository->get($personId); + return $this->ok($person); + } +} diff --git a/src/backend/src/Entity/Abstraction/BaseEntity.php b/src/backend/src/Entity/Abstraction/BaseEntity.php index bddfee2..358789b 100644 --- a/src/backend/src/Entity/Abstraction/BaseEntity.php +++ b/src/backend/src/Entity/Abstraction/BaseEntity.php @@ -15,7 +15,7 @@ class BaseEntity { $output = []; $defaultContext = [ AbstractNormalizer::CIRCULAR_REFERENCE_HANDLER => function ($object, $format, $context) { - return $object->getName(); + return '$' . basename(str_replace('\\', '/', $object::class)); }, ]; $normalizers = [new ObjectNormalizer(defaultContext: $defaultContext)]; diff --git a/src/backend/src/Entity/Person.php b/src/backend/src/Entity/Person.php new file mode 100644 index 0000000..0284a7b --- /dev/null +++ b/src/backend/src/Entity/Person.php @@ -0,0 +1,150 @@ +id; + } + + public function getName(): ?string + { + return $this->name; + } + + public function setName(string $name): self + { + $this->name = $name; + + return $this; + } + + public function getSurname(): ?string + { + return $this->surname; + } + + public function setSurname(string $surname): self + { + $this->surname = $surname; + + return $this; + } + + public function getCountry(): ?Country + { + return $this->country; + } + + public function setCountry(?Country $country): self + { + $this->country = $country; + + return $this; + } + + public function getState(): ?CountryRegion + { + return $this->state; + } + + public function setState(?CountryRegion $state): self + { + $this->state = $state; + + return $this; + } + + public function getCity(): ?string + { + return $this->city; + } + + public function setCity(string $city): self + { + $this->city = $city; + + return $this; + } + + public function getZip(): ?string + { + return $this->zip; + } + + public function setZip(string $zip): self + { + $this->zip = $zip; + + return $this; + } + + public function getAvatar(): ?string + { + return $this->avatar; + } + + public function setAvatar(string $avatar): self + { + $this->avatar = $avatar; + + return $this; + } + + public function generateAvatar($email) { + if (!$this->avatar) { + $this->avatar = 'https://www.gravatar.com/avatar/' . md5($email) . '?s=512&d=robohash'; + } + } +} diff --git a/src/backend/src/Entity/User.php b/src/backend/src/Entity/User.php index 2832e91..25dce6c 100644 --- a/src/backend/src/Entity/User.php +++ b/src/backend/src/Entity/User.php @@ -2,10 +2,11 @@ namespace App\Entity; +use App\Entity\Person; +use App\Enums\UserRoleEnum; use Doctrine\ORM\Mapping as ORM; use App\Repository\UserRepository; use App\Entity\Abstraction\BaseEntity; -use App\Enums\UserRoleEnum; use Symfony\Component\Security\Core\User\UserInterface; use Lexik\Bundle\JWTAuthenticationBundle\Security\User\JWTUserInterface; use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface; @@ -42,29 +43,9 @@ class User extends BaseEntity implements UserInterface, PasswordAuthenticatedUse private $email; /** - * @ORM\Column(type="string", length=255) + * @ORM\OneToOne(targetEntity=Person::class, cascade={"persist", "remove"}) */ - private $name; - - /** - * @ORM\Column(type="string", length=255) - */ - private $surname; - - /** - * @ORM\Column(type="string", length=255, nullable=true) - */ - private $avatar; - - /** - * @ORM\ManyToOne(targetEntity=Country::class) - */ - private $country; - - /** - * @ORM\ManyToOne(targetEntity=CountryRegion::class) - */ - private $state; + private $person; public static function createFromPayload($username, array $payload) @@ -174,68 +155,14 @@ class User extends BaseEntity implements UserInterface, PasswordAuthenticatedUse return $this; } - public function getName(): ?string + public function getPerson(): ?Person { - return $this->name; + return $this->person; } - public function setName(string $name): self + public function setPerson(?Person $person): self { - $this->name = $name; - - return $this; - } - - public function getSurname(): ?string - { - return $this->surname; - } - - public function setSurname(string $surname): self - { - $this->surname = $surname; - - return $this; - } - - public function getAvatar(): ?string - { - return $this->avatar; - } - - public function setAvatar(?string $avatar): self - { - $this->avatar = $avatar; - - return $this; - } - - public function generateAvatar() { - if (!$this->avatar) { - $this->avatar = 'https://www.gravatar.com/avatar/' . md5($this->email) . '?s=514&d=robohash'; - } - } - - public function getCountry(): ?Country - { - return $this->country; - } - - public function setCountry(?Country $country): self - { - $this->country = $country; - - return $this; - } - - public function getState(): ?CountryRegion - { - return $this->state; - } - - public function setState(?CountryRegion $state): self - { - $this->state = $state; + $this->person = $person; return $this; } diff --git a/src/backend/src/Repository/PersonRepository.php b/src/backend/src/Repository/PersonRepository.php new file mode 100644 index 0000000..8b2496c --- /dev/null +++ b/src/backend/src/Repository/PersonRepository.php @@ -0,0 +1,48 @@ +createQueryBuilder('p') + ->andWhere('p.exampleField = :val') + ->setParameter('val', $value) + ->orderBy('p.id', 'ASC') + ->setMaxResults(10) + ->getQuery() + ->getResult() + ; + } + */ + + public function get(int $personId): ?Person + { + return $this->createQueryBuilder('p') + ->andWhere('p.id = :id') + ->setParameter('id', $personId) + ->getQuery() + ->getOneOrNullResult() + ; + } +} diff --git a/src/frontend/src/app/app.component.html b/src/frontend/src/app/app.component.html index c702d4b..fd12d2b 100644 --- a/src/frontend/src/app/app.component.html +++ b/src/frontend/src/app/app.component.html @@ -41,8 +41,8 @@