Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Legacy App Rewrite to Laravel #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 15 additions & 0 deletions bootstrap_laravel.php
@@ -0,0 +1,15 @@
<?php

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

$app = require_once __DIR__.'/laravel/bootstrap/app.php';

$request = Illuminate\Http\Request::capture();
$app->instance('request', $request);
Illuminate\Support\Facades\Facade::clearResolvedInstance('request');

$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);
$kernel->bootstrap();

// below you could set global configuration values, etc.
config(['global_configuration_value' => 'value']);
13 changes: 13 additions & 0 deletions laravel/app/Features/Products/Fetch/GetAllProducts.php
@@ -0,0 +1,13 @@
<?php

namespace App\Features\Products\Fetch;

use App\Models\Products;

class GetAllProducts
{
public function execute()
{
return Products::all();
}
}
@@ -0,0 +1,26 @@
<?php

namespace App\Features\Products\Update;

use Illuminate\Http\Request;
use App\Models\Products;

class UpdateDisplayInShopAttribute
{
public function execute()
{
$request = app('request');
$input = $request->input();
$validatedData = $request->validate([
'product_id' => 'required|exists:products,id',
'display_in_shop' => 'required|in:yes,no'
]);
$product_id = $validatedData['product_id'];
$display_in_shop = $validatedData['display_in_shop'] === 'yes' ? 1 : 0;

// update database
$product = Products::find($product_id);
$product->display_in_shop = $display_in_shop;
$product->save();
}
}
29 changes: 29 additions & 0 deletions laravel/app/Http/Controllers/ProductsListController.php
@@ -0,0 +1,29 @@
<?php

namespace App\Http\Controllers;

use App\Features\Products\Fetch\GetAllProducts;
use App\Features\Products\Update\UpdateDisplayInShopAttribute;

class ProductsListController {

public function index()
{
$products = (new GetAllProducts)->execute();

return view('products_list', compact('products'));
}

public function update()
{
(new UpdateDisplayInShopAttribute)->execute();

// Once we run Laravel as a standalone app,
// we can use Laravel's redirect helpers, like so:
// redirect('/products_list.php');
// but for now we have to redirect back to the /products_list.php file
// to bootstrap Laravel
header('Location: /products_list.php');
exit(0);
}
}
43 changes: 43 additions & 0 deletions laravel/app/Models/Products.php
@@ -0,0 +1,43 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Products extends Model
{
/**
* Table definition variable.
*
* @var string
*/
protected $table = 'products';

/**
* Mass-assign guarded keys.
*
* @var array
*/
protected $guarded = ['id'];

/**
* Set primary key for table.
*
* @var string
*/
protected $primaryKey = 'id';

/**
* Auto-increment primary key.
*
* @var bool
*/
public $incrementing = true;

/**
* Toggle insertion of timestamps.
*
* @var bool
*/
public $timestamps = false;
}
46 changes: 46 additions & 0 deletions laravel/resources/views/products_list.blade.php
@@ -0,0 +1,46 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Product List</title>
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
</head>
<body>
<div class="container">
<div class="row">
<div class="col">
<h1>Product List</h1>
<table class="table">
<thead>
<tr>
<th>Product ID</th>
<th>Product Name</th>
<th>Display in Shop?</th>
</tr>
</thead>
<tbody>
@foreach ($products as $product)
<tr>
<td>{{ $product->id }}</td>
<td>{{ $product->name }}</td>
<td>
<form method="post" action="products_list.php">
@csrf
@method('PUT')
<input type="hidden" name="action" value="update_display_in_shop">
<input type="hidden" name="product_id" value="{{ $product->id }}">
<input type="radio" name="display_in_shop" value="yes"{{ $product->display_in_shop === 1 ? ' checked' : '' }}> Yes
<input type="radio" name="display_in_shop" value="no"{{ $product->display_in_shop !== 1 ? ' checked' : '' }}> No
<button type="submit" class="btn btn-info">Update!</button>
</form>
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
</div>
</div>
</body>
</html>
3 changes: 3 additions & 0 deletions laravel/routes/web.php
Expand Up @@ -14,3 +14,6 @@
Route::get('/', function () {
return view('welcome');
});

Route::get('products_list.php', 'ProductsListController@index');
Route::put('products_list.php', 'ProductsListController@update');
19 changes: 19 additions & 0 deletions laravel/tests/Feature/Products/Fetch/GetAllProductsTest.php
@@ -0,0 +1,19 @@
<?php

namespace Tests\Feature\Products\Fetch;

use Tests\TestCase;
use App\Features\Products\Fetch\GetAllProducts;

class GetAllProductsTest extends TestCase
{
public function testGetAllProductsTest()
{
$products = (new GetAllProducts)->execute();

$this->assertSame(3, count($products));
$this->assertSame('Product A', $products[0]->name);
$this->assertSame('Product B', $products[1]->name);
$this->assertSame('Product C', $products[2]->name);
}
}
@@ -0,0 +1,48 @@
<?php

namespace Tests\Feature\Products\Update;

use Tests\TestCase;
use App\Features\Products\Update\UpdateDisplayInShopAttribute;
use App\Models\Products;

class UpdateDisplayInShopAttributeTest extends TestCase
{
public function testUpdateDisplayAttributeSuccess()
{
$request = app('request');
$request->replace([
'product_id' => 1,
'display_in_shop' => 'yes'
]);
(new UpdateDisplayInShopAttribute)->execute();

$this->assertSame(1, Products::find(1)->display_in_shop);
}

public function testUpdateFailWithInvalidId()
{
// assert exception
$this->expectException(\Illuminate\Validation\ValidationException::class);

$request = app('request');
$request->replace([
'product_id' => 9999,
'display_in_shop' => 'yes'
]);
(new UpdateDisplayInShopAttribute)->execute();
}

public function testUpdateFailWithInvalidDisplayInShop()
{
// assert exception
$this->expectException(\Illuminate\Validation\ValidationException::class);

$request = app('request');
$request->replace([
'product_id' => 1,
'display_in_shop' => 'invalid-value'
]);
(new UpdateDisplayInShopAttribute)->execute();
}
}
90 changes: 2 additions & 88 deletions products_list.php
@@ -1,90 +1,4 @@
<?php

require_once './lib/database.php';

// check if it's an update request, if so, perform update operations
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if ($_POST['action'] === 'update_display_in_shop') {
if ($_POST['display_in_shop'] !== 'yes' && $_POST['display_in_shop'] !== 'no') {
exit(1);
}
$sql = 'UPDATE products SET display_in_shop=' . ($_POST['display_in_shop'] === 'yes' ? '1' : '0') . ' WHERE id=' . intval($_POST['product_id']);
if ($mysqli->query($sql) !== TRUE) {
exit(1);
}
}
header('Location: /products_list.php');
exit(0);
}

// get products
$sql = "SELECT * FROM products";

// error
if (!$result = $mysqli->query($sql)) {
echo "Sorry, the website is experiencing problems.";
echo "Error: Our query failed to execute and here is why: \n";
echo "Query: " . $sql . "\n";
echo "Errno: " . $mysqli->errno . "\n";
echo "Error: " . $mysqli->error . "\n";
exit;
}

// results
if ($result->num_rows === 0) {
echo "Your product catalogue is empty.";
exit;
}


$products = [];
while ($product = $result->fetch_object()) {
$products[] = $product;
}

// html
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Product List</title>
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
</head>
<body>
<div class="container">
<div class="row">
<div class="col">
<h1>Product List</h1>
<table class="table">
<thead>
<tr>
<th>Product ID</th>
<th>Product Name</th>
<th>Display in Shop?</th>
</tr>
</thead>
<tbody>
<?php foreach ($products as $product) : ?>
<tr>
<td><?php echo $product->id;?></td>
<td><?php echo $product->name;?></td>
<td>
<form method="post" action="products_list.php">
<input type="hidden" name="action" value="update_display_in_shop">
<input type="hidden" name="product_id" value="<?php echo $product->id;?>">
<input type="radio" name="display_in_shop" value="yes"<?php echo $product->display_in_shop === '1' ? ' checked' : '';?>> Yes
<input type="radio" name="display_in_shop" value="no"<?php echo $product->display_in_shop !== '1' ? ' checked' : '';?>> No
<button type="submit" class="btn btn-info">Update!</button>
</form>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
</div>
</body>
</html>
$_SERVER['SCRIPT_FILENAME'] = '';
require_once './laravel/public/index.php';