Browse Source

switch to mysql, add db configuration and basic entities

Olivier Massot 9 months ago
parent
commit
09625b1ab0

+ 1 - 1
api/.env

@@ -22,7 +22,7 @@ APP_SECRET=e5d6295bff11cbca1abbb10deeb7d74c
 # Format described at https://www.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/configuration.html#connecting-using-a-url
 # For a PostgreSQL database, use: "postgresql://db_user:db_password@127.0.0.1:5432/db_name?serverVersion=11&charset=utf8"
 # IMPORTANT: You MUST configure your server version, either here or in config/packages/doctrine.yaml
-DATABASE_URL=postgresql://<username>:Hxb3aMXUPb3m%$Ai*@db:5432/<db>?serverVersion=13&charset=utf8
+DATABASE_URL='mysql://root:Hxb3aMXUPb3m%$Ai*@snc_demo_db:3306/snc_demo?serverVersion=5.7'
 ###< doctrine/doctrine-bundle ###
 
 ###> nelmio/cors-bundle ###

+ 2 - 0
api/config/packages/api_platform.yaml

@@ -1,4 +1,6 @@
 api_platform:
+    title: 'SNC Demo API'
+    version: '1.0'
     mapping:
         paths: ['%kernel.project_dir%/src/Entity']
     patch_formats:

+ 13 - 0
api/public/.htaccess

@@ -0,0 +1,13 @@
+<IfModule mod_rewrite.c>
+    Options -MultiViews
+    RewriteEngine On
+    RewriteCond %{REQUEST_FILENAME} !-f
+    RewriteCond %{REQUEST_FILENAME} !-d
+    RewriteRule ^(.*)$ index.php [QSA,L]
+</IfModule>
+
+<IfModule !mod_rewrite.c>
+    <IfModule mod_alias.c>
+        RedirectMatch 302 ^/$ /index.php/
+    </IfModule>
+</IfModule>

+ 0 - 0
api/src/Entity/.gitignore


+ 69 - 0
api/src/Entity/Author.php

@@ -0,0 +1,69 @@
+<?php
+declare(strict_types=1);
+
+namespace App\Entity;
+
+use ApiPlatform\Metadata\ApiResource;
+use ApiPlatform\Metadata\Get;
+use Doctrine\ORM\Mapping as ORM;
+use Doctrine\Common\Collections\Collection;
+
+#[ORM\Entity]
+#[ApiResource(operations: [
+    new Get()
+])]
+class Author
+{
+    #[ORM\Id]
+    #[ORM\GeneratedValue]
+    #[ORM\Column]
+    private ?int $id = null;
+
+    #[ORM\Column(length: 100, nullable: false)]
+    private string $name;
+
+    #[ORM\OneToMany(targetEntity: Song::class, mappedBy: 'author', cascade: ['persist', 'remove'])]
+    private Collection $songs;
+
+    public function getId(): ?int
+    {
+        return $this->id;
+    }
+
+    public function setId(?int $id): self
+    {
+        $this->id = $id;
+        return $this;
+    }
+
+    public function getName(): string
+    {
+        return $this->name;
+    }
+
+    public function setName(string $name): self
+    {
+        $this->name = $name;
+        return $this;
+    }
+
+    public function getSongs(): Collection
+    {
+        return $this->songs;
+    }
+
+    public function addSong(Song $songs): self
+    {
+        if (!$this->songs->contains($songs)) {
+            $this->songs[] = $songs;
+            $songs->setAuthor($this);
+        }
+
+        return $this;
+    }
+
+    public function removeSong(Song $songs): self {
+        $this->songs->removeElement($songs);
+        return $this;
+    }
+}

+ 57 - 0
api/src/Entity/Song.php

@@ -0,0 +1,57 @@
+<?php
+declare(strict_types=1);
+
+namespace App\Entity;
+
+use ApiPlatform\Metadata\ApiResource;
+use Doctrine\ORM\Mapping as ORM;
+use Symfony\Component\Serializer\Annotation\Groups;
+
+#[ORM\Entity]
+#[ApiResource(operations: [])]
+class Song
+{
+    #[ORM\Id]
+    #[ORM\GeneratedValue]
+    #[ORM\Column]
+    private ?int $id = null;
+
+    #[ORM\Column(length: 100, nullable: false)]
+    private string $title;
+
+    #[ORM\ManyToOne(inversedBy: 'songs')]
+    private Author $author;
+
+    public function getId(): ?int
+    {
+        return $this->id;
+    }
+
+    public function setId(?int $id): self
+    {
+        $this->id = $id;
+        return $this;
+    }
+
+    public function getTitle(): string
+    {
+        return $this->title;
+    }
+
+    public function setTitle(string $title): self
+    {
+        $this->title = $title;
+        return $this;
+    }
+
+    public function getAuthor(): Author
+    {
+        return $this->author;
+    }
+
+    public function setAuthor(Author $author): self
+    {
+        $this->author = $author;
+        return $this;
+    }
+}

+ 6 - 6
docker-compose.yml

@@ -24,13 +24,13 @@ services:
       dockerfile: docker/db/Dockerfile
     restart: always
     ports:
-      - "5433:5432"
+      - "3306:3306"
     environment:
-      - POSTGRES_DB=${POSTGRES_DB}
-      - POSTGRES_USER=${POSTGRES_USER}
-      - POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
+      - MYSQL_DB=${MYSQL_DB}
+      - MYSQL_USER=${MYSQL_USER}
+      - MYSQL_ROOT_PASSWORD=${MYSQL_PASSWORD}
     volumes:
-      - db_data:/var/lib/postgresql/data:rw
+      - mysqldata:/var/lib/mysql
 
   snc_demo_api:
     hostname: snc_demo_api
@@ -71,7 +71,7 @@ services:
       - snc_demo_api
 
 volumes:
-  db_data: ~
+  mysqldata: ~
   appdata5: ~
   caddy_data: ~
   caddy_config: ~

+ 3 - 0
docker/api/Dockerfile

@@ -5,7 +5,10 @@ RUN apt-get update && apt-get install -y --fix-missing \
 
 COPY /docker/api/vhost.conf /etc/apache2/sites-available/vhost.conf
 
+RUN docker-php-ext-install pdo pdo_mysql
+
 RUN a2ensite vhost.conf; \
+    a2enmod rewrite; \
     service apache2 start;
 
 RUN php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"; \

+ 4 - 2
docker/db/Dockerfile

@@ -1,3 +1,5 @@
-FROM postgres:13-alpine
+FROM mariadb:10.4
 
-WORKDIR /usr/src
+COPY /docker/db/my.cnf /etc/mysql/conf.d/
+
+WORKDIR /usr/src

+ 8 - 0
docker/db/my.cnf

@@ -0,0 +1,8 @@
+[mysqld]
+sql-mode="NO_ENGINE_SUBSTITUTION"
+group_concat_max_len=100000
+max_allowed_packet=512M
+connect_timeout=28800
+wait_timeout=28800
+interactive_timeout=28800
+bind-address = 0.0.0.0

+ 14 - 1
readme.md

@@ -31,9 +31,22 @@ Install the API server :
 
     docker exec -it api bash
     composer install --with-all-dependencies
-    bin/console d:s:u
+    bin/console doctrine:database:create
+    bin/console doctrine:schema:update --force
 
 Start the nuxt server:
 
     docker exec -it app bash
+    yarn install
     yarn dev
+
+## Add Fixtures
+
+    INSERT INTO `snc_demo`.`author` (`name`)
+    VALUES ('Georges Brassens');
+    
+    INSERT INTO `snc_demo`.`song` (`title`, `author_id`)
+    VALUES
+        ('Heureux qui comme Ulysse', 1),
+        ('Les passantes', 1),
+        ("Dans l'eau de la claire fontaine", 1);