Skip to content

Commit 4dee3b1

Browse files
authored
Merge pull request #20 from w3bdesign/dev
Add API routes
2 parents 1203413 + df79100 commit 4dee3b1

File tree

10 files changed

+181
-15
lines changed

10 files changed

+181
-15
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace App\Http\Controllers\Api;
4+
5+
use App\Http\Controllers\Controller;
6+
use Illuminate\Http\Request;
7+
8+
class CategoryController extends Controller
9+
{
10+
//
11+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace App\Http\Controllers\Api;
4+
5+
use App\Http\Controllers\Controller;
6+
use Illuminate\Http\Request;
7+
use App\Models\Product;
8+
9+
class ProductController extends Controller
10+
{
11+
public function index()
12+
{
13+
return Product::with(['categories' => function ($query) {
14+
$query->select('id', 'name');
15+
}])
16+
->get();
17+
}
18+
19+
public function show(Product $product)
20+
{
21+
$product->load('categories:id,name');
22+
23+
return $product;
24+
}
25+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace App\Http\Controllers\Api;
4+
5+
use App\Http\Controllers\Controller;
6+
use Illuminate\Http\Request;
7+
8+
class UserController extends Controller
9+
{
10+
//
11+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Database\Factories;
4+
5+
use App\Models\Order;
6+
use Illuminate\Database\Eloquent\Factories\Factory;
7+
use Illuminate\Support\Str;
8+
9+
class OrderFactory extends Factory
10+
{
11+
/**
12+
* The name of the factory's corresponding model.
13+
*
14+
* @var string
15+
*/
16+
protected $model = Order::class;
17+
18+
/**
19+
* Define the model's default state.
20+
*
21+
* @return array
22+
*/
23+
public function definition()
24+
{
25+
return [
26+
'user_id' => $this->faker->numberBetween(1, 10),
27+
'transaction_id' => Str::random(16),
28+
'total' => $this->faker->numberBetween(50000, 200000),
29+
];
30+
}
31+
}

database/factories/ProductFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function definition()
3030
'name' => $name,
3131
'slug' => Str::slug($name),
3232
'description' => $this->faker->realText(320),
33-
'price' => $this->faker->numberBetween(10000, 100000),
33+
'price' => $this->faker->numberBetween(100, 1000),
3434
];
3535
}
3636
}

database/seeders/DatabaseSeeder.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ class DatabaseSeeder extends Seeder
1313
*/
1414
public function run()
1515
{
16-
// \App\Models\User::factory(10)->create();
16+
$this->call([
17+
UserSeeder::class,
18+
ProductSeeder::class,
19+
]);
1720
}
1821
}

database/seeders/OrderSeeder.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Database\Seeders;
4+
5+
use Illuminate\Database\Seeder;
6+
use App\Models\Order;
7+
8+
class OrderSeeder extends Seeder
9+
{
10+
/**
11+
* Run the database seeds.
12+
*
13+
* @return void
14+
*/
15+
public function run()
16+
{
17+
Order::factory()
18+
->times(10)
19+
->create();
20+
}
21+
}

database/seeders/ProductSeeder.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace Database\Seeders;
4+
5+
use App\Models\Category;
6+
use App\Models\Order;
7+
use Illuminate\Database\Seeder;
8+
use App\Models\Product;
9+
10+
class ProductSeeder extends Seeder
11+
{
12+
/**
13+
* Run the database seeds.
14+
*
15+
* @return void
16+
*/
17+
public function run()
18+
{
19+
Product::factory()
20+
->times(20)
21+
->create();
22+
23+
Category::factory()
24+
->times(4)
25+
->create();
26+
27+
Order::factory()
28+
->times(10)
29+
->create();
30+
31+
$categories = Category::all();
32+
Product::all()->each(function ($product) use ($categories) {
33+
$product->categories()->attach(
34+
$categories->random(2)->pluck('id')->toArray()
35+
);
36+
});
37+
38+
$orders = Order::all();
39+
Product::all()->each(function ($product) use ($orders) {
40+
$orderIds = $orders->random(3)->pluck('id')->toArray();
41+
foreach ($orderIds as $orderId) {
42+
$product->orders()->attach($orderId, ['quantity' => rand(1, 5)]);
43+
}
44+
});
45+
}
46+
}

database/seeders/UserSeeder.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Database\Seeders;
4+
5+
use Illuminate\Database\Seeder;
6+
use App\Models\User;
7+
8+
class UserSeeder extends Seeder
9+
{
10+
/**
11+
* Run the database seeds.
12+
*
13+
* @return void
14+
*/
15+
public function run()
16+
{
17+
User::factory()
18+
->times(10)
19+
->create();
20+
}
21+
}

routes/api.php

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,14 @@
33
use Illuminate\Http\Request;
44
use Illuminate\Support\Facades\Route;
55

6-
/*
7-
|--------------------------------------------------------------------------
8-
| API Routes
9-
|--------------------------------------------------------------------------
10-
|
11-
| Here is where you can register API routes for your application. These
12-
| routes are loaded by the RouteServiceProvider within a group which
13-
| is assigned the "api" middleware group. Enjoy building your API!
14-
|
15-
*/
6+
use App\Http\Controllers\Api\CategoryController;
7+
use App\Http\Controllers\Api\ProductController;
8+
use App\Http\Controllers\Api\UserController;
169

17-
Route::middleware('auth:api')->get('/user', function (Request $request) {
18-
return $request->user();
19-
});
10+
Route::get('/categories', [CategoryController::class, 'index']);
11+
Route::get('/categories/{category}', [CategoryController::class, 'show']);
12+
13+
Route::get('/products', [ProductController::class, 'index']);
14+
Route::get('/products/{product}', [ProductController::class, 'show']);
15+
16+
Route::post('/purchase', [UserController::class, 'purchase']);

0 commit comments

Comments
 (0)