From f73b042cc091be82abdbbca8992080875d526972 Mon Sep 17 00:00:00 2001
From: Yassine Doghri <yassine@doghri.fr>
Date: Fri, 29 May 2020 16:25:17 +0000
Subject: [PATCH] feat(categories): create model, entity, migrations and seeds

---
 Dockerfile                                    |   2 +
 README.md                                     |  17 +++
 src/app/Config/Autoload.php                   |   8 +-
 src/app/Controllers/Migrate.php               |  20 +++
 .../2020-05-29-152000_add_categories.php      |  41 ++++++
 src/app/Database/Seeds/CategorySeeder.php     | 128 ++++++++++++++++++
 src/app/Entities/.gitkeep                     |   0
 src/app/Entities/CategoryEntity.php           |  14 ++
 src/app/Models/CategoryModel.php              |  20 +++
 9 files changed, 248 insertions(+), 2 deletions(-)
 create mode 100644 src/app/Controllers/Migrate.php
 create mode 100644 src/app/Database/Migrations/2020-05-29-152000_add_categories.php
 create mode 100644 src/app/Database/Seeds/CategorySeeder.php
 create mode 100644 src/app/Entities/.gitkeep
 create mode 100644 src/app/Entities/CategoryEntity.php
 create mode 100644 src/app/Models/CategoryModel.php

diff --git a/Dockerfile b/Dockerfile
index c5a24ff76b..2187cb653b 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -10,3 +10,5 @@ WORKDIR /castopod
 RUN apt-get update && apt-get install -y \
     libicu-dev \
     && docker-php-ext-install intl
+
+RUN docker-php-ext-install mysqli && docker-php-ext-enable mysqli
diff --git a/README.md b/README.md
index e1442e771c..1556683169 100644
--- a/README.md
+++ b/README.md
@@ -82,6 +82,23 @@ docker ps -a
 >
 > _NB._ `./mariadb`, `./phpmyadmin` folders will be mounted in the project's root directory to persist data and logs.
 
+
+### Initialize and populate database
+
+Build the database with the migrate command:
+
+```bash
+# loads the database schema during first migration
+docker-compose run --rm app php spark migrate
+```
+
+Populate the database with the required data:
+
+```bash
+# Populates all categories
+docker-compose run --rm app php spark db:seed CategorySeeder
+```
+
 ### Start hacking
 
 You're all set! Start working your magic by updating the project's files! Help yourself to the [CodeIgniter4 User Guide](https://codeigniter.com/user_guide/index.html) for more insights.
diff --git a/src/app/Config/Autoload.php b/src/app/Config/Autoload.php
index 6a673d31eb..8462d8d50e 100644
--- a/src/app/Config/Autoload.php
+++ b/src/app/Config/Autoload.php
@@ -1,4 +1,6 @@
-<?php namespace Config;
+<?php
+
+namespace Config;
 
 require_once SYSTEMPATH . 'Config/AutoloadConfig.php';
 
@@ -11,7 +13,9 @@ require_once SYSTEMPATH . 'Config/AutoloadConfig.php';
  */
 class Autoload extends \CodeIgniter\Config\AutoloadConfig
 {
-	public $psr4 = [];
+	public $psr4 = [
+		'App'       => APPPATH,
+	];
 
 	public $classmap = [];
 
diff --git a/src/app/Controllers/Migrate.php b/src/app/Controllers/Migrate.php
new file mode 100644
index 0000000000..6a5db34e61
--- /dev/null
+++ b/src/app/Controllers/Migrate.php
@@ -0,0 +1,20 @@
+<?php
+
+namespace App\Controllers;
+
+use \CodeIgniter\Controller;
+
+class Migrate extends Controller
+{
+
+    public function index()
+    {
+        $migrate = \Config\Services::migrations();
+
+        try {
+            $migrate->latest();
+        } catch (\Exception $e) {
+            // Do something with the error here...
+        }
+    }
+}
diff --git a/src/app/Database/Migrations/2020-05-29-152000_add_categories.php b/src/app/Database/Migrations/2020-05-29-152000_add_categories.php
new file mode 100644
index 0000000000..661bb8f4cd
--- /dev/null
+++ b/src/app/Database/Migrations/2020-05-29-152000_add_categories.php
@@ -0,0 +1,41 @@
+<?php
+
+namespace App\Database\Migrations;
+
+use \CodeIgniter\Database\Migration;
+
+class AddCategories extends Migration
+{
+
+    public function up()
+    {
+        $this->forge->addField([
+            'id'          => [
+                'type'           => 'INT',
+                'constraint'     => 10,
+                'unsigned'       => TRUE,
+            ],
+            'parent_id'          => [
+                'type'           => 'INT',
+                'constraint'     => 10,
+                'unsigned'       => TRUE
+            ],
+            'apple_category'       => [
+                'type'           => 'VARCHAR',
+                'constraint'     => '1024',
+            ],
+            'google_category' => [
+                'type'           => 'VARCHAR',
+                'constraint'           => '1024',
+            ],
+        ]);
+        $this->forge->addKey('id', TRUE);
+        $this->forge->addForeignKey('parent_id', 'categories', 'id');
+        $this->forge->createTable('categories');
+    }
+
+    public function down()
+    {
+        $this->forge->dropTable('categories');
+    }
+}
diff --git a/src/app/Database/Seeds/CategorySeeder.php b/src/app/Database/Seeds/CategorySeeder.php
new file mode 100644
index 0000000000..2dd08c0dee
--- /dev/null
+++ b/src/app/Database/Seeds/CategorySeeder.php
@@ -0,0 +1,128 @@
+<?php
+
+namespace App\Database\Seeds;
+
+use CodeIgniter\Database\Seeder;
+
+
+class CategorySeeder extends Seeder
+{
+    public function run()
+    {
+        $data = array(
+            array('parent_id' => 0, 'id' => 0, 'apple_category' => 'uncategorized', 'google_category' => 'uncategorized'),
+            array('parent_id' => 0, 'id' => 1, 'apple_category' => 'Arts', 'google_category' => 'Arts'),
+            array('parent_id' => 0, 'id' => 2, 'apple_category' => 'Business', 'google_category' => 'Business'),
+            array('parent_id' => 0, 'id' => 3, 'apple_category' => 'Comedy', 'google_category' => 'Comedy'),
+            array('parent_id' => 0, 'id' => 4, 'apple_category' => 'Education', 'google_category' => 'Education'),
+            array('parent_id' => 0, 'id' => 5, 'apple_category' => 'Fiction', 'google_category' => ''),
+            array('parent_id' => 0, 'id' => 6, 'apple_category' => 'Government', 'google_category' => 'Government &amp; Organizations'),
+            array('parent_id' => 0, 'id' => 7, 'apple_category' => 'Health &amp; Fitness', 'google_category' => 'Health'),
+            array('parent_id' => 0, 'id' => 8, 'apple_category' => 'History', 'google_category' => ''),
+            array('parent_id' => 0, 'id' => 9, 'apple_category' => 'Kids &amp; Family', 'google_category' => 'Kids &amp; Family'),
+            array('parent_id' => 0, 'id' => 10, 'apple_category' => 'Leisure', 'google_category' => 'Games &amp; Hobbies'),
+            array('parent_id' => 0, 'id' => 11, 'apple_category' => 'Music', 'google_category' => 'Music'),
+            array('parent_id' => 0, 'id' => 12, 'apple_category' => 'News', 'google_category' => 'News &amp; Politics'),
+            array('parent_id' => 0, 'id' => 13, 'apple_category' => 'Religion &amp; Spirituality', 'google_category' => 'Religion &amp; Spirituality'),
+            array('parent_id' => 0, 'id' => 14, 'apple_category' => 'Science', 'google_category' => 'Science &amp; Medicine'),
+            array('parent_id' => 0, 'id' => 15, 'apple_category' => 'Society &amp; Culture', 'google_category' => 'Society &amp; Culture'),
+            array('parent_id' => 0, 'id' => 16, 'apple_category' => 'Sports', 'google_category' => 'Sports &amp; Recreation'),
+            array('parent_id' => 0, 'id' => 17, 'apple_category' => 'Technology', 'google_category' => 'Technology'),
+            array('parent_id' => 0, 'id' => 18, 'apple_category' => 'True Crime', 'google_category' => ''),
+            array('parent_id' => 0, 'id' => 19, 'apple_category' => 'TV &amp; Film', 'google_category' => 'TV &amp; Film'),
+            array('parent_id' => 1, 'id' => 20, 'apple_category' => 'Books', 'google_category' => ''),
+            array('parent_id' => 1, 'id' => 21, 'apple_category' => 'Design', 'google_category' => ''),
+            array('parent_id' => 1, 'id' => 22, 'apple_category' => 'Fashion &amp; Beauty', 'google_category' => ''),
+            array('parent_id' => 1, 'id' => 23, 'apple_category' => 'Food', 'google_category' => ''),
+            array('parent_id' => 1, 'id' => 24, 'apple_category' => 'Performing Arts', 'google_category' => ''),
+            array('parent_id' => 1, 'id' => 25, 'apple_category' => 'Visual Arts', 'google_category' => ''),
+            array('parent_id' => 2, 'id' => 26, 'apple_category' => 'Careers', 'google_category' => ''),
+            array('parent_id' => 2, 'id' => 27, 'apple_category' => 'Entrepreneurship', 'google_category' => ''),
+            array('parent_id' => 2, 'id' => 28, 'apple_category' => 'Investing', 'google_category' => ''),
+            array('parent_id' => 2, 'id' => 29, 'apple_category' => 'Management', 'google_category' => ''),
+            array('parent_id' => 2, 'id' => 30, 'apple_category' => 'Marketing', 'google_category' => ''),
+            array('parent_id' => 2, 'id' => 31, 'apple_category' => 'Non-Profit', 'google_category' => ''),
+            array('parent_id' => 3, 'id' => 32, 'apple_category' => 'Comedy Interviews', 'google_category' => ''),
+            array('parent_id' => 3, 'id' => 33, 'apple_category' => 'Improv', 'google_category' => ''),
+            array('parent_id' => 3, 'id' => 34, 'apple_category' => 'Stand-Up', 'google_category' => ''),
+            array('parent_id' => 4, 'id' => 35, 'apple_category' => 'Courses', 'google_category' => ''),
+            array('parent_id' => 4, 'id' => 36, 'apple_category' => 'How To', 'google_category' => ''),
+            array('parent_id' => 4, 'id' => 37, 'apple_category' => 'Language Learning', 'google_category' => ''),
+            array('parent_id' => 4, 'id' => 38, 'apple_category' => 'Self-Improvement', 'google_category' => ''),
+            array('parent_id' => 5, 'id' => 39, 'apple_category' => 'Comedy Fiction', 'google_category' => ''),
+            array('parent_id' => 5, 'id' => 40, 'apple_category' => 'Drama', 'google_category' => ''),
+            array('parent_id' => 5, 'id' => 41, 'apple_category' => 'Science Fiction', 'google_category' => ''),
+            array('parent_id' => 7, 'id' => 42, 'apple_category' => 'Alternative Health', 'google_category' => ''),
+            array('parent_id' => 7, 'id' => 43, 'apple_category' => 'Fitness', 'google_category' => ''),
+            array('parent_id' => 7, 'id' => 44, 'apple_category' => 'Medicine', 'google_category' => ''),
+            array('parent_id' => 7, 'id' => 45, 'apple_category' => 'Mental Health', 'google_category' => ''),
+            array('parent_id' => 7, 'id' => 46, 'apple_category' => 'Nutrition', 'google_category' => ''),
+            array('parent_id' => 7, 'id' => 47, 'apple_category' => 'Sexuality', 'google_category' => ''),
+            array('parent_id' => 9, 'id' => 48, 'apple_category' => 'Education for Kids', 'google_category' => ''),
+            array('parent_id' => 9, 'id' => 49, 'apple_category' => 'Parenting', 'google_category' => ''),
+            array('parent_id' => 9, 'id' => 50, 'apple_category' => 'Pets &amp; Animals', 'google_category' => ''),
+            array('parent_id' => 9, 'id' => 51, 'apple_category' => 'Stories for Kids', 'google_category' => ''),
+            array('parent_id' => 10, 'id' => 52, 'apple_category' => 'Animation &amp; Manga', 'google_category' => ''),
+            array('parent_id' => 10, 'id' => 53, 'apple_category' => 'Automotive', 'google_category' => ''),
+            array('parent_id' => 10, 'id' => 54, 'apple_category' => 'Aviation', 'google_category' => ''),
+            array('parent_id' => 10, 'id' => 55, 'apple_category' => 'Crafts', 'google_category' => ''),
+            array('parent_id' => 10, 'id' => 56, 'apple_category' => 'Games', 'google_category' => ''),
+            array('parent_id' => 10, 'id' => 57, 'apple_category' => 'Hobbies', 'google_category' => ''),
+            array('parent_id' => 10, 'id' => 58, 'apple_category' => 'Home &amp; Garden', 'google_category' => ''),
+            array('parent_id' => 10, 'id' => 59, 'apple_category' => 'Video Games', 'google_category' => ''),
+            array('parent_id' => 11, 'id' => 60, 'apple_category' => 'Music Commentary', 'google_category' => ''),
+            array('parent_id' => 11, 'id' => 61, 'apple_category' => 'Music History', 'google_category' => ''),
+            array('parent_id' => 11, 'id' => 62, 'apple_category' => 'Music Interviews', 'google_category' => ''),
+            array('parent_id' => 12, 'id' => 63, 'apple_category' => 'Business News', 'google_category' => ''),
+            array('parent_id' => 12, 'id' => 64, 'apple_category' => 'Daily News', 'google_category' => ''),
+            array('parent_id' => 12, 'id' => 65, 'apple_category' => 'Entertainment News', 'google_category' => ''),
+            array('parent_id' => 12, 'id' => 66, 'apple_category' => 'News Commentary', 'google_category' => ''),
+            array('parent_id' => 12, 'id' => 67, 'apple_category' => 'Politics', 'google_category' => ''),
+            array('parent_id' => 12, 'id' => 68, 'apple_category' => 'Sports News', 'google_category' => ''),
+            array('parent_id' => 12, 'id' => 69, 'apple_category' => 'Tech News', 'google_category' => ''),
+            array('parent_id' => 13, 'id' => 70, 'apple_category' => 'Buddhism', 'google_category' => ''),
+            array('parent_id' => 13, 'id' => 71, 'apple_category' => 'Christianity', 'google_category' => ''),
+            array('parent_id' => 13, 'id' => 72, 'apple_category' => 'Hinduism', 'google_category' => ''),
+            array('parent_id' => 13, 'id' => 73, 'apple_category' => 'Islam', 'google_category' => ''),
+            array('parent_id' => 13, 'id' => 74, 'apple_category' => 'Judaism', 'google_category' => ''),
+            array('parent_id' => 13, 'id' => 75, 'apple_category' => 'Religion', 'google_category' => ''),
+            array('parent_id' => 13, 'id' => 76, 'apple_category' => 'Spirituality', 'google_category' => ''),
+            array('parent_id' => 14, 'id' => 77, 'apple_category' => 'Astronomy', 'google_category' => ''),
+            array('parent_id' => 14, 'id' => 78, 'apple_category' => 'Chemistry', 'google_category' => ''),
+            array('parent_id' => 14, 'id' => 79, 'apple_category' => 'Earth Sciences', 'google_category' => ''),
+            array('parent_id' => 14, 'id' => 80, 'apple_category' => 'Life Sciences', 'google_category' => ''),
+            array('parent_id' => 14, 'id' => 81, 'apple_category' => 'Mathematics', 'google_category' => ''),
+            array('parent_id' => 14, 'id' => 82, 'apple_category' => 'Natural Sciences', 'google_category' => ''),
+            array('parent_id' => 14, 'id' => 83, 'apple_category' => 'Nature', 'google_category' => ''),
+            array('parent_id' => 14, 'id' => 84, 'apple_category' => 'Physics', 'google_category' => ''),
+            array('parent_id' => 14, 'id' => 85, 'apple_category' => 'Social Sciences', 'google_category' => ''),
+            array('parent_id' => 15, 'id' => 86, 'apple_category' => 'Documentary', 'google_category' => ''),
+            array('parent_id' => 15, 'id' => 87, 'apple_category' => 'Personal Journals', 'google_category' => ''),
+            array('parent_id' => 15, 'id' => 88, 'apple_category' => 'Philosophy', 'google_category' => ''),
+            array('parent_id' => 15, 'id' => 89, 'apple_category' => 'Places &amp; Travel', 'google_category' => ''),
+            array('parent_id' => 15, 'id' => 90, 'apple_category' => 'Relationships', 'google_category' => ''),
+            array('parent_id' => 16, 'id' => 91, 'apple_category' => 'Baseball', 'google_category' => ''),
+            array('parent_id' => 16, 'id' => 92, 'apple_category' => 'Basketball', 'google_category' => ''),
+            array('parent_id' => 16, 'id' => 93, 'apple_category' => 'Cricket', 'google_category' => ''),
+            array('parent_id' => 16, 'id' => 94, 'apple_category' => 'Fantasy Sports', 'google_category' => ''),
+            array('parent_id' => 16, 'id' => 95, 'apple_category' => 'Football', 'google_category' => ''),
+            array('parent_id' => 16, 'id' => 96, 'apple_category' => 'Golf', 'google_category' => ''),
+            array('parent_id' => 16, 'id' => 97, 'apple_category' => 'Hockey', 'google_category' => ''),
+            array('parent_id' => 16, 'id' => 98, 'apple_category' => 'Rugby', 'google_category' => ''),
+            array('parent_id' => 16, 'id' => 99, 'apple_category' => 'Running', 'google_category' => ''),
+            array('parent_id' => 16, 'id' => 100, 'apple_category' => 'Soccer', 'google_category' => ''),
+            array('parent_id' => 16, 'id' => 101, 'apple_category' => 'Swimming', 'google_category' => ''),
+            array('parent_id' => 16, 'id' => 102, 'apple_category' => 'Tennis', 'google_category' => ''),
+            array('parent_id' => 16, 'id' => 103, 'apple_category' => 'Volleyball', 'google_category' => ''),
+            array('parent_id' => 16, 'id' => 104, 'apple_category' => 'Wilderness', 'google_category' => ''),
+            array('parent_id' => 16, 'id' => 105, 'apple_category' => 'Wrestling', 'google_category' => ''),
+            array('parent_id' => 19, 'id' => 106, 'apple_category' => 'After Shows', 'google_category' => ''),
+            array('parent_id' => 19, 'id' => 107, 'apple_category' => 'Film History', 'google_category' => ''),
+            array('parent_id' => 19, 'id' => 108, 'apple_category' => 'Film Interviews', 'google_category' => ''),
+            array('parent_id' => 19, 'id' => 109, 'apple_category' => 'Film Reviews', 'google_category' => ''),
+            array('parent_id' => 19, 'id' => 110, 'apple_category' => 'TV Reviews', 'google_category' => ''),
+        );
+
+        $this->db->table('categories')->insertBatch($data);
+    }
+}
diff --git a/src/app/Entities/.gitkeep b/src/app/Entities/.gitkeep
new file mode 100644
index 0000000000..e69de29bb2
diff --git a/src/app/Entities/CategoryEntity.php b/src/app/Entities/CategoryEntity.php
new file mode 100644
index 0000000000..6ef0bd3262
--- /dev/null
+++ b/src/app/Entities/CategoryEntity.php
@@ -0,0 +1,14 @@
+<?php
+
+namespace App\Entities;
+
+use CodeIgniter\Entity;
+
+class Category extends Entity
+{
+    protected $casts = [
+        'parent_id' => 'integer',
+        'apple_category' => 'string',
+        'google_category' => 'string',
+    ];
+}
diff --git a/src/app/Models/CategoryModel.php b/src/app/Models/CategoryModel.php
new file mode 100644
index 0000000000..c2921c4f7b
--- /dev/null
+++ b/src/app/Models/CategoryModel.php
@@ -0,0 +1,20 @@
+<?php
+
+namespace App\Models;
+
+use CodeIgniter\Model;
+
+class CategoryModel extends Model
+{
+    protected $table      = 'categories';
+    protected $primaryKey = 'id';
+
+    protected $allowedFields = [
+        'apple_category', 'google_category'
+    ];
+
+    protected $returnType    = 'App\Entities\Category';
+    protected $useSoftDeletes = false;
+
+    protected $useTimestamps = true;
+}
-- 
GitLab