Skip to content

Commit

Permalink
add /random
Browse files Browse the repository at this point in the history
  • Loading branch information
kernel64 committed Sep 28, 2024
1 parent dc250be commit a29b311
Show file tree
Hide file tree
Showing 17 changed files with 175 additions and 10 deletions.
Binary file modified .gitignore
Binary file not shown.
Binary file modified README.md
Binary file not shown.
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
"description": "It's a REST API that returns quotes from the Tunisian TV series Choufli 7all.",
"type": "project",
"require": {
"mabslabs/mabs": "^2.1"
"mabslabs/mabs": "^2.1",
"ext-pdo": "*"
},
"license": "GPL-3.0 license",
"autoload": {
"psr-4": {
"Mabslabs\\ApiChoufli7all\\": "src/"
"App\\": "src/"
}
},
"authors": [
Expand Down
1 change: 1 addition & 0 deletions config.simple.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php
30 changes: 30 additions & 0 deletions db/schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/** ensure when create the database to use utf8mb4_unicode_ci
CREATE DATABASE choufli_7all_db CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
**/

CREATE TABLE actors
(
actors_id SERIAL UNIQUE NOT NULL PRIMARY KEY,
actors_slug VARCHAR(100) NOT NULL,
actors_name VARCHAR(100) NOT NULL
) CHARACTER SET utf8mb4
COLLATE utf8mb4_unicode_ci;

CREATE TABLE quotes
(
quotes_id SERIAL UNIQUE NOT NULL PRIMARY KEY,
quotes_text VARCHAR(1000) NOT NULL,
id_author INT NOT NULL REFERENCES actors (authors_id) ON UPDATE CASCADE,
quotes_counter_random INT NOT NULL DEFAULT 0
) CHARACTER SET utf8mb4
COLLATE utf8mb4_unicode_ci;

INSERT INTO actors (actors_slug, actors_name) VALUES ('sboui', 'السبوعي');
INSERT INTO actors (actors_slug, actors_name) VALUES ('slimene', 'سليمان الأبيض');
INSERT INTO actors (actors_slug, actors_name) VALUES ('zeineb', 'زينب');
INSERT INTO actors (actors_slug, actors_name) VALUES ('fadhila', 'لالة فضيلة');
INSERT INTO actors (actors_slug, actors_name) VALUES ('azza', 'عزّة');
INSERT INTO actors (actors_slug, actors_name) VALUES ('dalanda', 'دالندا');
INSERT INTO actors (actors_slug, actors_name) VALUES ('jannet', 'جنّاة');
INSERT INTO actors (actors_slug, actors_name) VALUES ('fouchika', 'فوشيكا');
INSERT INTO actors (actors_slug, actors_name) VALUES ('beji', 'الباجي ماتريكس');
33 changes: 25 additions & 8 deletions index.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,35 @@
<?php
ini_set('display_errors', 'on');
error_reporting(E_ALL);

require_once __DIR__.'/vendor/autoload.php';
require_once __DIR__.'/config.php';

use \Symfony\Component\HttpFoundation\JsonResponse;

$app = new Mabs\Application();
$app = new App\APIChoufli7allApp();
$container = $app->getContainer();

$app->get('random', function () {
$app->get('random', function () use ($container) {
$quote = [];
$stmt = $container['pdo']->query('SELECT COUNT(*) AS count FROM quotes');
$count = $stmt->fetch(PDO::FETCH_ASSOC)['count'];

$data = [
'quote' => 'المريض عندي، ما يخرجش قبل ما يتحسن',
'auhtor' => 'سليمان الأبيض'
];

return new JsonResponse($data);
if ($count > 0) {
// Get a random quote
$randomIndex = rand(0, $count - 1);
$stmt = $container['pdo']->prepare('SELECT quotes.quotes_text, actors.actors_name FROM quotes JOIN actors ON quotes.id_author = actors.actors_id LIMIT 1 OFFSET :offset');
$stmt->bindValue(':offset', $randomIndex, PDO::PARAM_INT);
$stmt->execute();

$row = $stmt->fetch(PDO::FETCH_ASSOC);

$quote = [
'quote' => $row['quotes_text'],
'actor' => $row['actors_name'],
];
}

return new JsonResponse($quote);

})->run();
52 changes: 52 additions & 0 deletions quotes/importQuotes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

require __DIR__.'/../config.php';
require __DIR__.'/../vendor/autoload.php';

$quotesDir = __DIR__.'/raw';
$actorsArray = [];
// Connexion to database and init script
try {

$dsn = 'mysql:host=' . DB_HOST . ';port=' . DB_PORT . ';dbname=' . DB_NAME . ';charset=utf8mb4';
$pdo = new PDO($dsn, DB_USER, DB_PASSWORD);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$stmt = $pdo->query('SELECT actors_id, actors_slug FROM actors');

while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$actorsArray[$row['actors_slug']] = $row['actors_id'];
}

} catch (PDOException $e) {
die('Erreur de connexion : ' . $e->getMessage());
}



// get all .txt files
$files = glob($quotesDir . '/*.txt');

foreach ($files as $file) {

$slug = basename($file, '.txt');

if (isset($actorsArray[$slug])) {
$actorId = $actorsArray[$slug];

$quotes = file($file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

foreach ($quotes as $quote) {

$stmt = $pdo->prepare('INSERT INTO quotes (quotes_text, id_author) VALUES (:quote, :authorId)');
$stmt->execute(['quote' => $quote, 'authorId' => $actorId]);
}

echo "Quotes imported for actor: $slug\n";
} else {
echo "No actor found for slug: $slug\n";
}
}



2 changes: 2 additions & 0 deletions quotes/raw/azza.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
يا السبوعي، شنوّة هالحالة؟ لازم نبدلوا كل شيء في الدار
ياخي أنا تزوجت باش نعيش في القلة؟
2 changes: 2 additions & 0 deletions quotes/raw/dalanda.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
أنا ما نضيعش وقتي في كلام فارغ
دكتور، لازمك تاخذ إجازة، شوف روحك كيفاش وليت!
3 changes: 3 additions & 0 deletions quotes/raw/fadhila.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
الجار قبل الدار
الواحد يلزم يكون في حالو وما يتدخلش في شؤون الناس
الواحد يلزموا يعرف كيفاش يعيش، موش الكل فلوس
2 changes: 2 additions & 0 deletions quotes/raw/fouchika.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
الدنيا هانية، يا سبوعي، الدنيا لعب وفلوس
يا سبوعي، احنا ولاد حومة وحدة، ما فماش اللي يغلبنا
3 changes: 3 additions & 0 deletions quotes/raw/jannet.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
كي تبدأ الزهرة في الميزان، الدنيا تهبط وتطلع!
أنا نقولك الحقيقة، الكواكب ما تكذبش
اليوم القمر في برجك!
4 changes: 4 additions & 0 deletions quotes/raw/sboui.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
نحبك برشا يا دكتووور
الدنيا هانية
والله ما فهمت شيء
دكتور، ما نعرش، برشا حاجات في مخي
3 changes: 3 additions & 0 deletions quotes/raw/slimene.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ما تنجمش تعالج مريض وأنت مريض!
أنا طبيب، مش معالج سحري!
يا ربي، أعطني الصبر!
2 changes: 2 additions & 0 deletions quotes/raw/zeineb.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
المرأة اللي ما تحكمش في دارها، ما تحكمش في حتى شيء
كيفاش باش نمشي نقابل الناس بهالحالة؟
14 changes: 14 additions & 0 deletions src/APIChoufli7allApp.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
namespace App;
use Mabs\Application;

class APIChoufli7allApp extends Application
{
public function getAdapters()
{
return array(
new \Mabs\Adapter\SessionServiceAdapter(),
new \App\Service\DbManager,
);
}
}
29 changes: 29 additions & 0 deletions src/Service/DbManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace App\Service;

use Mabs\Container\Container;
use Mabs\ServiceAdapterInterface;

class DbManager implements ServiceAdapterInterface
{

public function load(Container $container)
{
$container['pdo'] = function () {

$dsn = 'mysql:host=' . DB_HOST . ';port=' . DB_PORT . ';dbname=' . DB_NAME . ';charset=utf8mb4';
$pdo = new \PDO($dsn, DB_USER, DB_PASSWORD);
$pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);

return $pdo;
};

}

public function boot(Container $container)
{
// do nothing
}

}

0 comments on commit a29b311

Please sign in to comment.