summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichiel Schuurmans <michielschuurmans@gmail.com>2020-10-01 22:07:18 +0200
committerMichiel Schuurmans <michielschuurmans@gmail.com>2020-10-01 22:07:18 +0200
commitadaa761b061bdb1dcfcdfcafa996c770149eb9ff (patch)
treeba362d06b1d23045e7ec5c39edac9d92cd62afa1
parenta7975976780e2370eade4e041e505273c7d1e2a9 (diff)
Brands: Add brand table
Signed-off-by: Michiel Schuurmans <michielschuurmans@gmail.com>
-rw-r--r--src/products/app/Brand.php14
-rw-r--r--src/products/app/Http/Controllers/BrandsController.php37
-rw-r--r--src/products/app/Http/Controllers/ProductsController.php3
-rw-r--r--src/products/app/Product.php3
-rw-r--r--src/products/database/migrations/2020_10_01_194541_brands_create_table.php32
-rw-r--r--src/products/database/migrations/2020_10_01_195720_product_brand_id.php32
-rw-r--r--src/products/routes/web.php5
7 files changed, 124 insertions, 2 deletions
diff --git a/src/products/app/Brand.php b/src/products/app/Brand.php
new file mode 100644
index 0000000..01f1151
--- /dev/null
+++ b/src/products/app/Brand.php
@@ -0,0 +1,14 @@
+<?php
+
+namespace App;
+
+use Illuminate\Database\Eloquent\Model;
+
+class Brand extends Model
+{
+ protected $fillable = [
+ 'name'
+ ];
+
+ protected $hidden = [];
+}
diff --git a/src/products/app/Http/Controllers/BrandsController.php b/src/products/app/Http/Controllers/BrandsController.php
new file mode 100644
index 0000000..62b2f5b
--- /dev/null
+++ b/src/products/app/Http/Controllers/BrandsController.php
@@ -0,0 +1,37 @@
+<?php
+
+namespace App\Http\Controllers;
+
+use App\Brand;
+use Illuminate\Http\Request;
+
+class BrandsController extends Controller
+{
+ public function get_all()
+ {
+ return response()->json(Brand::all());
+ }
+
+ public function create(Request $request)
+ {
+ $this->validate($request, [
+ 'name' => 'required|unique:brands'
+ ]);
+
+ $brand = Brand::create($request->all());
+
+ return response()->json($brand, 201);
+ }
+
+ public function get_one($id)
+ {
+ return response()->json(Brand::find($id));
+ }
+
+ public function delete($id)
+ {
+ Brand::FindOrFail($id)->delete();
+
+ return response('Deleted successfully', 200);
+ }
+}
diff --git a/src/products/app/Http/Controllers/ProductsController.php b/src/products/app/Http/Controllers/ProductsController.php
index 58de828..30816d6 100644
--- a/src/products/app/Http/Controllers/ProductsController.php
+++ b/src/products/app/Http/Controllers/ProductsController.php
@@ -15,7 +15,8 @@ class ProductsController extends Controller
public function create(Request $request)
{
$this->validate($request, [
- 'name' => 'required|unique:products'
+ 'name' => 'required|unique:products',
+ 'brand_id' => 'required'
]);
$product = Product::create($request->all());
diff --git a/src/products/app/Product.php b/src/products/app/Product.php
index 1223533..e66378b 100644
--- a/src/products/app/Product.php
+++ b/src/products/app/Product.php
@@ -7,7 +7,8 @@ use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
protected $fillable = [
- 'name'
+ 'name',
+ 'brand_id'
];
protected $hidden = [];
diff --git a/src/products/database/migrations/2020_10_01_194541_brands_create_table.php b/src/products/database/migrations/2020_10_01_194541_brands_create_table.php
new file mode 100644
index 0000000..b55f151
--- /dev/null
+++ b/src/products/database/migrations/2020_10_01_194541_brands_create_table.php
@@ -0,0 +1,32 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+class BrandsCreateTable extends Migration
+{
+ /**
+ * Run the migrations.
+ *
+ * @return void
+ */
+ public function up()
+ {
+ Schema::create('brands', function (Blueprint $table) {
+ $table->increments('id');
+ $table->string('name');
+ $table->timestamps();
+ });
+ }
+
+ /**
+ * Reverse the migrations.
+ *
+ * @return void
+ */
+ public function down()
+ {
+ Schema::dropIfExists('brands');
+ }
+}
diff --git a/src/products/database/migrations/2020_10_01_195720_product_brand_id.php b/src/products/database/migrations/2020_10_01_195720_product_brand_id.php
new file mode 100644
index 0000000..106bfc1
--- /dev/null
+++ b/src/products/database/migrations/2020_10_01_195720_product_brand_id.php
@@ -0,0 +1,32 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+class ProductBrandId extends Migration
+{
+ /**
+ * Run the migrations.
+ *
+ * @return void
+ */
+ public function up()
+ {
+ Schema::table('products', function (Blueprint $table) {
+ $table->foreignId('brand_id')->constrained('brands')->onDelete('cascade');
+ });
+ }
+
+ /**
+ * Reverse the migrations.
+ *
+ * @return void
+ */
+ public function down()
+ {
+ Schema::table('products', function (Blueprint $table) {
+ $table->dropColumn('brand_id');
+ });
+ }
+}
diff --git a/src/products/routes/web.php b/src/products/routes/web.php
index 28214e1..9cd6f1f 100644
--- a/src/products/routes/web.php
+++ b/src/products/routes/web.php
@@ -22,4 +22,9 @@ $router->group(['prefix' => 'api/v1', 'middleware' => 'auth'], function () use (
$router->get('products/{id}', ['middleware' => 'auth:products:read', 'uses' => 'ProductsController@get_one']);
$router->post('products', ['middleware' => 'auth:products:create', 'uses' => 'ProductsController@create']);
$router->delete('products/{id}', ['middleware' => 'auth:products:delete', 'uses' => 'ProductsController@delete']);
+
+ $router->get('brands', ['middleware' => 'auth:brands:read', 'uses' => 'BrandsController@get_all']);
+ $router->get('brands/{id}', ['middleware' => 'auth:brands:read', 'uses' => 'BrandsController@get_one']);
+ $router->post('brands', ['middleware' => 'auth:brands:create', 'uses' => 'BrandsController@create']);
+ $router->delete('brands/{id}', ['middleware' => 'auth:brands:delete', 'uses' => 'BrandsController@delete']);
});