diff options
author | Michiel Schuurmans <michielschuurmans@gmail.com> | 2020-10-08 19:27:48 +0200 |
---|---|---|
committer | Michiel Schuurmans <michielschuurmans@gmail.com> | 2020-10-08 19:27:48 +0200 |
commit | aec22abdb0aeb51b4f388d1067ec54aa598b1c88 (patch) | |
tree | c20030f79f9c9a1e5e56c2d2ff90e346e2cb938e | |
parent | 3640cf9ecf0712dc3829f74725370caacaa489ac (diff) |
Signed-off-by: Michiel Schuurmans <michielschuurmans@gmail.com>
-rw-r--r-- | src/products/app/Http/Controllers/UsersController.php | 21 | ||||
-rw-r--r-- | src/products/app/Models/User.php | 39 | ||||
-rw-r--r-- | src/products/app/Models/UserProduct.php | 19 | ||||
-rw-r--r-- | src/products/database/migrations/2020_10_08_170146_user_products_table.php | 37 | ||||
-rw-r--r-- | src/products/routes/web.php | 2 |
5 files changed, 101 insertions, 17 deletions
diff --git a/src/products/app/Http/Controllers/UsersController.php b/src/products/app/Http/Controllers/UsersController.php index f7b6b0c..e633e65 100644 --- a/src/products/app/Http/Controllers/UsersController.php +++ b/src/products/app/Http/Controllers/UsersController.php @@ -3,6 +3,7 @@ namespace App\Http\Controllers; use App\Models\User; +use App\Models\UserProduct; use Illuminate\Http\Request; class UsersController extends Controller @@ -36,4 +37,24 @@ class UsersController extends Controller return response('Deleted successfully', 200); } + + public function get_products($id) + { + return response()->json(User::find($id)->products()); + } + + public function add_product($id, Request $request) + { + $this->validate($request, [ + 'serial' => 'required|unique:user_products', + 'product_id' => 'required' + ]); + + $product = new UserProduct($request->all()); + $product->user_id = $id; + + $result = $product->save(); + + return response()->json($product, 201); + } } diff --git a/src/products/app/Models/User.php b/src/products/app/Models/User.php index 6d48cdd..233e4c2 100644 --- a/src/products/app/Models/User.php +++ b/src/products/app/Models/User.php @@ -11,23 +11,28 @@ use Laravel\Lumen\Auth\Authorizable; class User extends Model implements AuthenticatableContract, AuthorizableContract { - use Authenticatable, Authorizable, HasFactory; + use Authenticatable, Authorizable, HasFactory; - /** - * The attributes that are mass assignable. - * - * @var array - */ - protected $fillable = [ - 'email', 'firstname', 'lastname' - ]; + /** + * The attributes that are mass assignable. + * + * @var array + */ + protected $fillable = [ + 'email', 'firstname', 'lastname' + ]; - /** - * The attributes excluded from the model's JSON form. - * - * @var array - */ - protected $hidden = [ - 'password', - ]; + /** + * The attributes excluded from the model's JSON form. + * + * @var array + */ + protected $hidden = [ + 'password', + ]; + + public function products() + { + return $this->hasMany('\App\Models\UserProduct', 'user_id', 'id')->get(); + } } diff --git a/src/products/app/Models/UserProduct.php b/src/products/app/Models/UserProduct.php new file mode 100644 index 0000000..d78501f --- /dev/null +++ b/src/products/app/Models/UserProduct.php @@ -0,0 +1,19 @@ +<?php + +namespace App\Models; + +use Illuminate\Database\Eloquent\Model; + +class UserProduct extends Model +{ + protected $fillable = [ + 'serial', 'product_id', 'user_id' + ]; + + protected $hidden = []; + + public function products() + { + return $this->hasMany('\App\Models\Product')->get(); + } +} diff --git a/src/products/database/migrations/2020_10_08_170146_user_products_table.php b/src/products/database/migrations/2020_10_08_170146_user_products_table.php new file mode 100644 index 0000000..452d7b6 --- /dev/null +++ b/src/products/database/migrations/2020_10_08_170146_user_products_table.php @@ -0,0 +1,37 @@ +<?php + +use Illuminate\Database\Migrations\Migration; +use Illuminate\Database\Schema\Blueprint; +use Illuminate\Support\Facades\Schema; + +class UserProductsTable extends Migration +{ + /** + * Run the migrations. + * + * @return void + */ + public function up() + { + Schema::create('user_products', function (Blueprint $table) { + $table->increments('id'); + $table->string('serial'); + $table->bigInteger('product_id')->unsigned(); + $table->bigInteger('user_id')->unsigned(); + $table->timestamps(); + + $table->foreign('product_id')->references('id')->on('products')->onDelete('cascade'); + $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('user_products'); + } +} diff --git a/src/products/routes/web.php b/src/products/routes/web.php index 80505c2..8dea349 100644 --- a/src/products/routes/web.php +++ b/src/products/routes/web.php @@ -31,6 +31,8 @@ $router->group(['prefix' => 'api/v1', 'middleware' => 'auth'], function () use ( $router->get('users', ['middleware' => 'auth:users:read', 'uses' => 'UsersController@get_all']); $router->get('users/{id}', ['middleware' => 'auth:users:read', 'uses' => 'UsersController@get_one']); + $router->get('users/{id}/products', ['middleware' => 'auth:users:read', 'uses' => 'UsersController@get_products']); + $router->post('users/{id}/products', ['middleware' => 'auth:users:read', 'uses' => 'UsersController@add_product']); $router->post('users', ['middleware' => 'auth:users:create', 'uses' => 'UsersController@create']); $router->delete('users/{id}', ['middleware' => 'auth:users:delete', 'uses' => 'UsersController@delete']); }); |