Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions submissions/edwinNgoma/php_slim/.env.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Database

DB_HOSTNAME =""
DB_USERNAME =""
DB_PASSWORD =""
DB_NAME =""
5 changes: 5 additions & 0 deletions submissions/edwinNgoma/php_slim/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.env

composer.lock

vendor
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
https://www.postman.com/skytechuniversal/workspace/edy-at-dufuna/collection/19239115-e62da646-2f4e-4835-98b4-7cd50af400b7?action=share&creator=19239115
151 changes: 151 additions & 0 deletions submissions/edwinNgoma/php_slim/app/controllers/ArticlesController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
<?php

namespace App\Controller;

use DB\DB;
// use PDOException;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;

class ArticlesController{

private $db;

public function __construct(){

$this->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);
}
}
}
28 changes: 28 additions & 0 deletions submissions/edwinNgoma/php_slim/app/middleware/jsonBody.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace App\Middleware;

use Psr\Http\Message\ResponseInterface as response;
use Psr\Http\Message\ServerRequestInterface as request;
use Psr\Http\Server\MiddlewareInterface as Middleware;
use Psr\Http\Server\RequestHandlerInterface as Rhandler;

class jsonBody implements Middleware{

public function process(request $request, Rhandler $handler): response{

$contentType = $request->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);
}
};
15 changes: 15 additions & 0 deletions submissions/edwinNgoma/php_slim/bootstrap/app.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

use Slim\Factory\AppFactory;
use Dotenv\Dotenv;

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

$dotenv = Dotenv::createImmutable (__DIR__ . '../../');
$dotenv->load();

# Insternsiasion
$app = AppFactory::create();


require __DIR__ . './../routes/api.php';
17 changes: 17 additions & 0 deletions submissions/edwinNgoma/php_slim/composer.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
34 changes: 34 additions & 0 deletions submissions/edwinNgoma/php_slim/database/DB.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace DB;

use PDO;

class DB {
private $dbHostname;
private $dbUsername;
private $dbPassword;
private $dbName;

public function __construct(){

$this->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;

}

}
18 changes: 18 additions & 0 deletions submissions/edwinNgoma/php_slim/public/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;

require __DIR__ . '/../bootstrap/app.php';

$app->get('/', function ( Request $request, Response $response ){
$response->getBody()->write("<div style=' height:100vh; position: relative; top: 25em; display: flex;
flex-direction: column; align-items: center; position: relative; top: 13em;' >
<h1>WELCOME</h1>
<p>to</p>
<h2>Edy's Articles</h2>
It works thus far!!!</div>");
return $response;
});

$app->run();
22 changes: 22 additions & 0 deletions submissions/edwinNgoma/php_slim/routes/api.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

use Slim\Routing\RouteCollectorProxy as proxy;
use App\Controller\ArticlesController;
use App\Middleware\jsonBody;

$app->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");
});