Skip to content

Commit

Permalink
feat: implement recent sales
Browse files Browse the repository at this point in the history
  • Loading branch information
bhimbho committed Aug 7, 2024
1 parent 592cce9 commit 5cdd7c7
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 1 deletion.
15 changes: 15 additions & 0 deletions app/Http/Controllers/Api/V1/User/DashboardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Http\Controllers\Api\V1\User;

use App\Http\Controllers\Controller;
use App\Models\Order;
use Illuminate\Http\Request;
use Illuminate\Http\Response;

Expand Down Expand Up @@ -106,6 +107,20 @@ public function index()
]);
}

public function recent_sales()
{
$user = auth()->user();
$orders = Order::whereHas('product', function ($query) use ($user) {
$query->where('user_id', $user->id);
})->with('user')->get();

return response()->json([
'message' => 'Recent sales retrieved successfully',
'status_code' => Response::HTTP_OK,
'data' => $orders,
]);
}

/**
* Show the form for creating a new resource.
*/
Expand Down
11 changes: 11 additions & 0 deletions app/Models/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,20 @@
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Concerns\HasUuids;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class Order extends Model
{
use HasUuids;
use HasFactory;

public function product(): BelongsTo
{
return $this->belongsTo(Product::class, 'product_id');
}

public function user(): BelongsTo
{
return $this->belongsTo(User::class, 'user_id');
}
}
2 changes: 1 addition & 1 deletion database/seeders/OrderSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ class OrderSeeder extends Seeder
{
public function run()
{
// Order::factory()->count(10)->create();
Order::factory()->count(10)->create();
}
}
1 change: 1 addition & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@
Route::get('/user/preferences', [PreferenceController::class, 'index']);
Route::delete('/user/preferences/{id}', [PreferenceController::class, 'delete']);
Route::get('/user-statistics', [DashboardController::class, 'index']);
Route::get('/user-sales', [DashboardController::class, 'recent_sales']);

});

Expand Down

0 comments on commit 5cdd7c7

Please sign in to comment.