diff --git a/app/Http/Controllers/API/v2/CartController.php b/app/Http/Controllers/API/v2/CartController.php new file mode 100644 index 0000000..5112158 --- /dev/null +++ b/app/Http/Controllers/API/v2/CartController.php @@ -0,0 +1,67 @@ + + */ + public function rules(): array + { + return [ + // + ]; + } +} diff --git a/app/Http/Requests/UpdateCartRequest.php b/app/Http/Requests/UpdateCartRequest.php new file mode 100644 index 0000000..25e0a73 --- /dev/null +++ b/app/Http/Requests/UpdateCartRequest.php @@ -0,0 +1,28 @@ + + */ + public function rules(): array + { + return [ + // + ]; + } +} diff --git a/app/Http/Resources/API/v2/CartCollection.php b/app/Http/Resources/API/v2/CartCollection.php new file mode 100644 index 0000000..7e85eff --- /dev/null +++ b/app/Http/Resources/API/v2/CartCollection.php @@ -0,0 +1,19 @@ + + */ + public function toArray(Request $request): array + { + return parent::toArray($request); + } +} diff --git a/app/Http/Resources/API/v2/CartResource.php b/app/Http/Resources/API/v2/CartResource.php new file mode 100644 index 0000000..dea5291 --- /dev/null +++ b/app/Http/Resources/API/v2/CartResource.php @@ -0,0 +1,28 @@ + + */ + public function toArray(Request $request): array + { + $city_name = $this->city->name; + $company_name = $this->company->name; + $items = $this->items; + + return [ + 'id' => $this->id, + 'cityName' => $city_name, + 'companyName' => $company_name, + 'item' => new ItemCollection($items), + ]; + } +} diff --git a/app/Models/Cart.php b/app/Models/Cart.php new file mode 100644 index 0000000..ca3458d --- /dev/null +++ b/app/Models/Cart.php @@ -0,0 +1,33 @@ +belongsTo(City::class); + } + + public function company(): BelongsTo + { + return $this->belongsTo(Company::class); + } + + public function items(): BelongsToMany + { + return $this->belongsToMany(Item::class, 'carts_items', 'cart_id', 'item_id'); + } +} + diff --git a/app/Models/Item.php b/app/Models/Item.php index 0d9d84f..2ae3448 100644 --- a/app/Models/Item.php +++ b/app/Models/Item.php @@ -5,6 +5,7 @@ namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsTo; +use Illuminate\Database\Eloquent\Relations\BelongsToMany; class Item extends Model { @@ -23,4 +24,10 @@ class Item extends Model { return $this->belongsTo(Category::class); } + + public function carts(): BelongsToMany + { + return $this->belongsToMany(Cart::class, 'carts_items', 'item_id', 'cart_id'); + } } + diff --git a/app/Policies/CartPolicy.php b/app/Policies/CartPolicy.php new file mode 100644 index 0000000..96c331c --- /dev/null +++ b/app/Policies/CartPolicy.php @@ -0,0 +1,66 @@ + + */ +class CartFactory extends Factory +{ + /** + * Define the model's default state. + * + * @return array + */ + public function definition(): array + { + return [ + // + ]; + } +} diff --git a/database/migrations/2023_06_15_134235_create_carts_table.php b/database/migrations/2023_06_15_134235_create_carts_table.php new file mode 100644 index 0000000..197c88c --- /dev/null +++ b/database/migrations/2023_06_15_134235_create_carts_table.php @@ -0,0 +1,38 @@ +id(); + $table->unsignedBiginteger('city_id')->unsigned(); + $table->unsignedBiginteger('company_id')->unsigned(); + $table->unsignedBiginteger('user_id')->unsigned(); + $table->string('status'); // CART | DONE + + $table->foreign('city_id')->references('id') + ->on('cities')->onDelete('cascade'); + $table->foreign('company_id')->references('id') + ->on('companies')->onDelete('cascade'); + $table->foreign('user_id')->references('id') + ->on('users')->onDelete('cascade'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('carts'); + } +}; diff --git a/database/migrations/2023_06_15_134238_create_carts_items_table.php b/database/migrations/2023_06_15_134238_create_carts_items_table.php new file mode 100644 index 0000000..93e9228 --- /dev/null +++ b/database/migrations/2023_06_15_134238_create_carts_items_table.php @@ -0,0 +1,34 @@ +id(); + $table->unsignedBiginteger('cart_id')->unsigned(); + $table->unsignedBiginteger('item_id')->unsigned(); + + $table->foreign('cart_id')->references('id') + ->on('carts')->onDelete('cascade'); + $table->foreign('item_id')->references('id') + ->on('items')->onDelete('cascade'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('carts_items'); + } +}; diff --git a/database/seeders/CartSeeder.php b/database/seeders/CartSeeder.php new file mode 100644 index 0000000..ddbc131 --- /dev/null +++ b/database/seeders/CartSeeder.php @@ -0,0 +1,17 @@ +