Skip to content

Commit

Permalink
feat: An Api controller was created to manage the products.
Browse files Browse the repository at this point in the history
  • Loading branch information
Josemprog committed Nov 28, 2020
1 parent 260a043 commit 540ac65
Show file tree
Hide file tree
Showing 11 changed files with 139 additions and 28 deletions.
6 changes: 6 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,9 @@ PUSHER_APP_CLUSTER=mt1

MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"

FILESYSTEM_DRIVER=

P2P_ENDPOINT_BASE=
P2P_LOGIN=
P2P_SECRET_KEY=
8 changes: 7 additions & 1 deletion .env.testing
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ DB_PASSWORD=password

BROADCAST_DRIVER=log
CACHE_DRIVER=file
QUEUE_CONNECTION=sync
QUEUE_CONNECTION=redis
SESSION_DRIVER=file
SESSION_LIFETIME=120

Expand Down Expand Up @@ -44,3 +44,9 @@ PUSHER_APP_CLUSTER=mt1

MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"

FILESYSTEM_DRIVER=public

P2P_ENDPOINT_BASE=https://test.placetopay.com/redirection
P2P_LOGIN=6dd490faf9cb87a9862245da41170ff2
P2P_SECRET_KEY=024h1IlD
23 changes: 7 additions & 16 deletions app/Http/Controllers/Admin/ProductController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,15 @@
namespace App\Http\Controllers\Admin;

use App\Product;

use App\Http\Requests\ProductsRequest;
use App\Http\Controllers\Controller;

use Illuminate\Http\Request;
use App\Exports\ProductsExport;
use App\Imports\ProductsImport;

use App\Jobs\NotifyUserOfCompletedExport;

use Maatwebsite\Excel\Facades\Excel;
use App\Http\Controllers\Controller;
use Intervention\Image\Facades\Image;

use Illuminate\Http\Request;

use App\Http\Requests\ProductsRequest;
use Illuminate\Support\Facades\Storage;

use Maatwebsite\Excel\Facades\Excel;
use App\Jobs\NotifyUserOfCompletedExport;

class ProductController extends Controller
{
Expand Down Expand Up @@ -168,9 +161,9 @@ public function destroy(Product $product): \Illuminate\Http\RedirectResponse
public function export(): \Illuminate\Http\RedirectResponse
{
$user = auth()->user();
$filePath = asset('storage/products.csv');
$filePath = asset('storage/products.xlsx');

(new ProductsExport())->store('products.csv', 'public')->chain([
(new ProductsExport())->store('products.xlsx', 'public')->chain([
new NotifyUserOfCompletedExport($user, $filePath)
]);

Expand All @@ -185,8 +178,6 @@ public function export(): \Illuminate\Http\RedirectResponse
*/
public function import(Request $request): \Illuminate\Http\RedirectResponse
{
// dd($request->file('file'));

$file = $request->file('file');

Excel::import(new ProductsImport, $file, 'public');
Expand Down
106 changes: 106 additions & 0 deletions app/Http/Controllers/Api/ProductController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php

namespace App\Http\Controllers\Api;

use App\Product;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Requests\ProductsRequest;
use Illuminate\Support\Facades\Storage;

class ProductController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index(Request $request)
{
$products = $request;

$products = Product::orderBy('id', 'ASC')
->brand($products->brand)
->name($products->name)
->price($products->price)
->paginate(10);

return $products;
}

private function cargarArchivo($file)
{
$nombreArchivo = time() . "." . $file->getClientOriginalExtension();
$file->move(storage_path('app/public/'), $nombreArchivo);

return $nombreArchivo;
}

/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\ProductsRequest $request
* @return \Illuminate\Http\Response
*/
public function store(ProductsRequest $request)
{
$product = new Product($request->validated());
if ($request->has('image')) {
$product->image = $this->cargarArchivo($request->image);
}
$product->save();

return response()->json([
'res' => true,
'message' => 'The product has been created successfully'
], 200);
}

/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show(Product $product)
{
return $product;
}

/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param Product $product
* @return \Illuminate\Http\Response
*/
public function update(Product $product, ProductsRequest $request, $id)
{
if ($request->has('image')) {
Storage::disk('public')->delete($product->image);
Storage::delete($product->image);
$product->fill($request->validated());
$product->image = $this->cargarArchivo($request->image);
$product->save();
}

$product->update($request->validated());


return response()->json([
'res' => true,
'message' => 'The product was successfully updated'
], 200);
}

/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
}
1 change: 0 additions & 1 deletion app/Http/Controllers/ProductCartController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use App\Cart;
use App\Product;
use App\Services\CartService;
use Illuminate\Http\Request;

class ProductCartController extends Controller
{
Expand Down
12 changes: 6 additions & 6 deletions app/Imports/ProductsImport.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ class ProductsImport implements ToModel
public function model(array $row)
{
return new Product([
'brand' => $row[1],
'name' => $row[2],
'price' => $row[3],
'quantity' => $row[4],
'description' => $row[5],
'image' => $row[6],
'brand' => $row[1],
'name' => $row[2],
'price' => $row[3],
'quantity' => $row[4],
'description' => $row[5],
'image' => $row[6],
'enabled' => $row[7],
]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function up()
$table->string('brand', 9);
$table->string('name', 20);
$table->unsignedBigInteger('price');
$table->bigInteger('quantity');
$table->unsignedBigInteger('quantity');
$table->string('description', 255);
$table->string('image');
$table->boolean('enabled')->default(true);
Expand Down
2 changes: 1 addition & 1 deletion resources/views/admin/products/edit.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
@if (substr($product->image, 0, 5) == 'https')
<img src="{{$product->image}}" class="img-fluid" alt="Responsive image">
@else
<img src="/storage/{{$product->image}}" class="img-fluid" alt="Responsive image">
<img src="{{ '/img/images/' . $product->image}}" class="img-fluid" alt="Responsive image">
@endif
<div class="custom-file">
<input name="image" type="file" class="custom-file-input" id="customFile">
Expand Down
2 changes: 1 addition & 1 deletion resources/views/admin/products/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
@if (substr($product->image, 0, 5) == 'https')
<img src="{{$product->image}}" class="img-fluid" alt="Responsive image">
@else
<img src="/storage/{{$product->image}}" class="img-fluid" alt="Responsive image">
<img src="{{ '/img/images/' . $product->image}}" class="img-fluid" alt="Responsive image">
@endif
</div>
<div>
Expand Down
3 changes: 2 additions & 1 deletion resources/views/admin/products/show.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,11 @@
</div>
<div class="col-5 d-flex flex-column">
<div class="image-show">
{{-- {{dd(substr($product->image, 0, 5) == 'https' )}} --}}
@if (substr($product->image, 0, 5) == 'https')
<img src="{{$product->image}}" class="img-fluid" alt="Responsive image">
@else
<img src="/storage/{{$product->image}}" class="img-fluid" alt="Responsive image">
<img src="/storage/images/{{$product->image}}" class="img-fluid" alt="Responsive image">
@endif
</div>
{{-- Footer Buttons --}}
Expand Down
2 changes: 2 additions & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@
Route::middleware('auth:api')->get('/user', function (Request $request) {
return $request->user();
});

Route::apiResource('products', 'Api\ProductController');

0 comments on commit 540ac65

Please sign in to comment.