diff --git a/submissions/edwinNgoma/php_slim/.env.sample b/submissions/edwinNgoma/php_slim/.env.sample new file mode 100644 index 000000000..731c0a633 --- /dev/null +++ b/submissions/edwinNgoma/php_slim/.env.sample @@ -0,0 +1,6 @@ + # Database + + DB_HOSTNAME ="" + DB_USERNAME ="" + DB_PASSWORD ="" + DB_NAME ="" diff --git a/submissions/edwinNgoma/php_slim/.gitignore b/submissions/edwinNgoma/php_slim/.gitignore new file mode 100644 index 000000000..7b558b026 --- /dev/null +++ b/submissions/edwinNgoma/php_slim/.gitignore @@ -0,0 +1,5 @@ +.env + +composer.lock + +vendor \ No newline at end of file diff --git a/submissions/edwinNgoma/php_slim/API(s) collection & examples.txt b/submissions/edwinNgoma/php_slim/API(s) collection & examples.txt new file mode 100644 index 000000000..ddab396ef --- /dev/null +++ b/submissions/edwinNgoma/php_slim/API(s) collection & examples.txt @@ -0,0 +1 @@ +https://www.postman.com/skytechuniversal/workspace/edy-at-dufuna/collection/19239115-e62da646-2f4e-4835-98b4-7cd50af400b7?action=share&creator=19239115 \ No newline at end of file diff --git a/submissions/edwinNgoma/php_slim/app/controllers/ArticlesController.php b/submissions/edwinNgoma/php_slim/app/controllers/ArticlesController.php new file mode 100644 index 000000000..acfbb3b3a --- /dev/null +++ b/submissions/edwinNgoma/php_slim/app/controllers/ArticlesController.php @@ -0,0 +1,151 @@ +db = (new DB())->connect(); + } + + public function published(Request $request, Response $response){ + + try{ + $query = $this->db->prepare("SELECT * FROM articles WHERE status = 1"); + $query ->execute(); + $articles = $query->fetchAll(); + $response->getBody()->write(json_encode($articles)); + return $response->withHeader('CONTENT-TYPE', 'application/json')->withStatus(200); + } catch (PDOException $ex) { + $response->getBody()->write(json_encode(['error'=>$ex->getMessage()])); + return $response->withHeader('CONTENT-TYPE', 'application/json')->withStatus(500); + } + } + + public function article(Request $request, Response $response, $args){ + + $id = $args['id']; + + try{ + $query = $this->db->prepare("SELECT * FROM articles WHERE id = :id"); + $query->bindParam(':id',$id); + $query->execute(); + $articles = $query->fetch(); + if(!$articles){ + $response->getBody()->write(json_encode(['error'=>'Sorry, article not found!'])); + return $response->withHeader('CONTENT-TYPE', 'application/json')->withStatus(404); + } + $response->getBody()->write(json_encode($articles)); + return $response->withHeader('CONTENT-TYPE', 'application/json')->withStatus(200); + } catch (PDOException $ex) { + $response->getBody()->write(json_encode(['error'=>$ex->getMessage()])); + return $response->withHeader('CONTENT-TYPE', 'application/json')->withStatus(500); + } + } + + public function Add_article(Request $request, Response $response, $args){ + + $requestData = $request->getParsedBody(); + + $title = $requestData['title']; + $discription = $requestData['discription']; + $status = 0; + $created_by = $requestData['created_by']; + $created_at = Date('Y-m-d H:l:s'); + + $sql = "INSERT INTO articles (title, discription, status, created_by, created_at) VALUES (:title, :discription, :status, :created_by, :created_at)"; + + + try{ + $query = $this->db->prepare($sql); + $query->bindParam(':title',$title); + $query->bindParam(':discription',$discription); + $query->bindParam(':status',$status); + $query->bindParam(':created_by',$created_by); + $query->bindParam(':created_at',$created_at); + $query->execute(); + $response->getBody()->write(json_encode(['message'=>'Successfull!'])); + return $response->withHeader('CONTENT-TYPE', 'application/json')->withStatus(200); + } catch (PDOException $ex) { + $response->getBody()->write(json_encode(['error'=>$ex->getMessage()])); + return $response->withHeader('CONTENT-TYPE', 'application/json')->withStatus(500); + } + } + + public function modify(Request $request, Response $response, $args){ + + $requestData = $request->getParsedBody(); + + $id = $args['id']; + + $title = $requestData['title']; + $discription = $requestData['discription']; + $created_by = $requestData['created_by']; + $created_at = Date('Y-m-d H:l:s'); + + try{ + $query = $this->db->prepare("UPDATE articles SET title = :title, discription = :discription, created_by = :created_by, created_at = :created_at WHERE id = :id"); + $query->bindParam(':id',$id); + $query->bindParam(':title',$title); + $query->bindParam(':discription',$discription); + $query->bindParam(':created_by',$created_by); + $query->bindParam(':created_at',$created_at); + $query->execute(); + $response->getBody()->write(json_encode(['message'=>'Successfull!'])); + return $response->withHeader('CONTENT-TYPE', 'application/json')->withStatus(200); + } catch (PDOException $ex) { + $response->getBody()->write(json_encode(['error'=>$ex->getMessage()])); + return $response->withHeader('CONTENT-TYPE', 'application/json')->withStatus(500); + } + } + + public function publish(Request $request, Response $response, $args){ + + $requestData = $request->getParsedBody(); + + $id = $args['id']; + + $status = $requestData['status']; + + + try{ + $query = $this->db->prepare("UPDATE articles SET status = :status WHERE id=:id"); + $query->bindParam(':id',$id); + $query->bindParam(':status',$status); + $query->execute(); + $response->getBody()->write(json_encode(['message'=>'Successfull!'])); + return $response->withHeader('CONTENT-TYPE', 'application/json')->withStatus(200); + } catch (PDOException $ex) { + $response->getBody()->write(json_encode(['error'=>$ex->getMessage()])); + return $response->withHeader('CONTENT-TYPE', 'application/json')->withStatus(500); + } + } + + public function del(Request $request, Response $response, $args){ + + $id = $args['id']; + + try{ + $query = $this->db->prepare("DELETE FROM articles WHERE id = $id"); + $query->execute(); + $articles = $query; + if(!$articles){ + $response->getBody()->write(json_encode(['error'=>'Sorry, article not found!'])); + return $response->withHeader('CONTENT-TYPE', 'application/json')->withStatus(404); + } + $response->getBody()->write(json_encode(['message'=>'article was successfully deleted'])); + return $response->withHeader('CONTENT-TYPE', 'application/json')->withStatus(200); + } catch (PDOException $ex) { + $response->getBody()->write(json_encode(['error'=>$ex->getMessage()])); + return $response->withHeader('CONTENT-TYPE', 'application/json')->withStatus(500); + } + } + } \ No newline at end of file diff --git a/submissions/edwinNgoma/php_slim/app/middleware/jsonBody.php b/submissions/edwinNgoma/php_slim/app/middleware/jsonBody.php new file mode 100644 index 000000000..04860901c --- /dev/null +++ b/submissions/edwinNgoma/php_slim/app/middleware/jsonBody.php @@ -0,0 +1,28 @@ +getHeaderLine('Content-Type'); + + if (strstr($contentType, "application/json")){ + + $contents = json_decode(file_get_contents('php://input'), true); + + if(json_last_error() === JSON_ERROR_NONE){ + + $request = $request->withParsedBody($contents); + } + } + + return $handler->handle($request); + } + }; \ No newline at end of file diff --git a/submissions/edwinNgoma/php_slim/bootstrap/app.php b/submissions/edwinNgoma/php_slim/bootstrap/app.php new file mode 100644 index 000000000..ff2e53b66 --- /dev/null +++ b/submissions/edwinNgoma/php_slim/bootstrap/app.php @@ -0,0 +1,15 @@ +load(); + + # Insternsiasion + $app = AppFactory::create(); + + + require __DIR__ . './../routes/api.php'; \ No newline at end of file diff --git a/submissions/edwinNgoma/php_slim/composer.json b/submissions/edwinNgoma/php_slim/composer.json new file mode 100644 index 000000000..2e767119a --- /dev/null +++ b/submissions/edwinNgoma/php_slim/composer.json @@ -0,0 +1,17 @@ +{ + "name": "dufunaslim/task", + "description": "php slim api task", + "require": { + "slim/slim": "4.*", + "slim/psr7": "^1.5", + "vlucas/phpdotenv": "^5.4" + }, + + "autoload":{ + "psr-4": { + "DB\\":"database", + "App\\Controller\\": "app/Controllers", + "App\\Middleware\\": "app/middleware" + } + } +} diff --git a/submissions/edwinNgoma/php_slim/database/DB.php b/submissions/edwinNgoma/php_slim/database/DB.php new file mode 100644 index 000000000..c9376fb49 --- /dev/null +++ b/submissions/edwinNgoma/php_slim/database/DB.php @@ -0,0 +1,34 @@ +dbHostname = $_ENV['DB_HOSTNAME']; + + $this->dbUsername = $_ENV['DB_USERNAME']; + + $this->dbPassword = $_ENV['DB_PASSWORD']; + + $this->dbName = $_ENV['DB_NAME']; + } + + public function connect(){ + + $connectionString = "mysql:host={$this->dbHostname};dbname={$this->dbName}"; + $pdo = new PDO($connectionString, $this->dbUsername, $this->dbPassword); + $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); + return $pdo; + + } + + } \ No newline at end of file diff --git a/submissions/edwinNgoma/php_slim/public/index.php b/submissions/edwinNgoma/php_slim/public/index.php new file mode 100644 index 000000000..edce7bb9f --- /dev/null +++ b/submissions/edwinNgoma/php_slim/public/index.php @@ -0,0 +1,18 @@ +get('/', function ( Request $request, Response $response ){ + $response->getBody()->write("
+

WELCOME

+

to

+

Edy's Articles

+ It works thus far!!!
"); + return $response; + }); + + $app->run(); \ No newline at end of file diff --git a/submissions/edwinNgoma/php_slim/routes/api.php b/submissions/edwinNgoma/php_slim/routes/api.php new file mode 100644 index 000000000..56a58a293 --- /dev/null +++ b/submissions/edwinNgoma/php_slim/routes/api.php @@ -0,0 +1,22 @@ +group('/api', function(proxy $group){ + $group->get('/articles', ArticlesController::class . ":published"); + + $group->get('/articles/{id}', ArticlesController::class . ":article"); + + $group->post('/add', ArticlesController::class . ":Add_article") + ->add(new jsonBody()); + + $group->put('/modify/{id}', ArticlesController::class . ":modify") + ->add(new jsonBody()); + + $group->put('/publish/{id}', ArticlesController::class . ":publish") + ->add(new jsonBody()); + + $group->delete('/delete/{id}', ArticlesController::class . ":del"); + }); \ No newline at end of file