[CLEAR-29] Adding product
This commit is contained in:
parent
4406f430da
commit
c3d2c3330c
23
api/app/Http/Controllers/ProductsController.php
Normal file
23
api/app/Http/Controllers/ProductsController.php
Normal file
@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Http\Requests\AddProductRequest;
|
||||
use App\Repositories\ProductsRepositoryInterface;
|
||||
|
||||
class ProductsController extends Controller
|
||||
{
|
||||
private ProductsRepositoryInterface $productsRepository;
|
||||
|
||||
public function __construct(ProductsRepositoryInterface $productsRepository)
|
||||
{
|
||||
$this->productsRepository = $productsRepository;
|
||||
}
|
||||
|
||||
public function add(AddProductRequest $request)
|
||||
{
|
||||
$product = $this->productsRepository->create($request->all());
|
||||
|
||||
return response()->json(['success' => true, 'data' => ['product' => $product]], 200);
|
||||
}
|
||||
}
|
37
api/app/Http/Requests/AddProductRequest.php
Normal file
37
api/app/Http/Requests/AddProductRequest.php
Normal file
@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class AddProductRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function authorize()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function rules()
|
||||
{
|
||||
return [
|
||||
'name' => 'required',
|
||||
];
|
||||
}
|
||||
|
||||
public function messages()
|
||||
{
|
||||
return [
|
||||
'name.required' => 'Product name is required',
|
||||
];
|
||||
}
|
||||
}
|
@ -33,6 +33,7 @@ class LoginRequest extends FormRequest
|
||||
{
|
||||
return [
|
||||
'email.required' => 'Email is required',
|
||||
'email.email' => 'Email is in wrong format',
|
||||
'password.required' => 'Password is required',
|
||||
];
|
||||
}
|
||||
|
@ -33,6 +33,7 @@ class RegisterRequest extends FormRequest
|
||||
{
|
||||
return [
|
||||
'email.required' => 'Email is required',
|
||||
'email.email' => 'Email is in wrong format',
|
||||
'password.required' => 'Password is required',
|
||||
];
|
||||
}
|
||||
|
15
api/app/Models/Product.php
Normal file
15
api/app/Models/Product.php
Normal file
@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class Product extends Model
|
||||
{
|
||||
|
||||
protected $guarded = ['id'];
|
||||
|
||||
protected $hidden = [
|
||||
'created_at', 'updated_at'
|
||||
];
|
||||
}
|
@ -2,6 +2,8 @@
|
||||
|
||||
namespace App\Providers;
|
||||
|
||||
use App\Repositories\ProductsRepository;
|
||||
use App\Repositories\ProductsRepositoryInterface;
|
||||
use App\Repositories\UsersRepository;
|
||||
use App\Repositories\UsersRepositoryInterface;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
@ -26,5 +28,6 @@ class RepositoriesServiceProvider extends ServiceProvider
|
||||
public function boot()
|
||||
{
|
||||
$this->app->bind(UsersRepositoryInterface::class, UsersRepository::class);
|
||||
$this->app->bind(ProductsRepositoryInterface::class, ProductsRepository::class);
|
||||
}
|
||||
}
|
||||
|
15
api/app/Repositories/ProductsRepository.php
Normal file
15
api/app/Repositories/ProductsRepository.php
Normal file
@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace App\Repositories;
|
||||
|
||||
|
||||
use App\Models\Product;
|
||||
|
||||
class ProductsRepository implements ProductsRepositoryInterface
|
||||
{
|
||||
public function create($data)
|
||||
{
|
||||
return Product::create($data);
|
||||
}
|
||||
}
|
9
api/app/Repositories/ProductsRepositoryInterface.php
Normal file
9
api/app/Repositories/ProductsRepositoryInterface.php
Normal file
@ -0,0 +1,9 @@
|
||||
<?php
|
||||
|
||||
namespace App\Repositories;
|
||||
|
||||
|
||||
interface ProductsRepositoryInterface
|
||||
{
|
||||
public function create($data);
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
|
||||
class CreateProductsTable extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::create('products', function (Blueprint $table) {
|
||||
$table->bigIncrements('id');
|
||||
$table->string('name')->unique();
|
||||
$table->integer('kcal')->nullable();
|
||||
$table->float('carbohydrates')->nullable();
|
||||
$table->float('protein')->nullable();
|
||||
$table->float('fat')->nullable();
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
Schema::dropIfExists('products');
|
||||
}
|
||||
}
|
@ -7,3 +7,7 @@ Route::group(['prefix' => 'user', 'middleware' => ['assign.guard:users']], funct
|
||||
Route::post('/login', 'UsersController@login');
|
||||
Route::get('/me', 'UsersController@me');
|
||||
});
|
||||
|
||||
Route::group(['prefix' => 'product', 'middleware' => ['assign.guard:users']], function () {
|
||||
Route::post('/', 'ProductsController@add');
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user