Skip to content

Commit

Permalink
Merge pull request #363 from liberu-accounting/sweep/Implement-Transa…
Browse files Browse the repository at this point in the history
…ction-API-and-Exchange-Rate-Endpoint-with-Sanctum-Authentication

Implement Transaction API and Exchange Rate Endpoint with Sanctum Authentication
  • Loading branch information
curtisdelicata authored Dec 24, 2024
2 parents 319ec95 + aebb15f commit 3e617d3
Show file tree
Hide file tree
Showing 5 changed files with 205 additions and 3 deletions.
59 changes: 59 additions & 0 deletions app/Http/Controllers/Api/TransactionController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@


<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Http\Resources\TransactionResource;
use App\Models\Transaction;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
use Illuminate\Http\Response;

class TransactionController extends Controller
{
public function index(): AnonymousResourceCollection
{
return TransactionResource::collection(
Transaction::paginate(15)
);
}

public function show(Transaction $transaction): TransactionResource
{
return new TransactionResource($transaction);
}

public function store(Request $request): TransactionResource
{
$validated = $request->validate([
'account_id' => 'required|exists:accounts,id',
'amount' => 'required|numeric',
'transaction_date' => 'required|date',
'description' => 'required|string'
]);

$transaction = Transaction::create($validated);
return new TransactionResource($transaction);
}

public function update(Request $request, Transaction $transaction): TransactionResource
{
$validated = $request->validate([
'account_id' => 'sometimes|exists:accounts,id',
'amount' => 'sometimes|numeric',
'transaction_date' => 'sometimes|date',
'description' => 'sometimes|string'
]);

$transaction->update($validated);
return new TransactionResource($transaction);
}

public function destroy(Transaction $transaction): Response
{
$transaction->delete();
return response()->noContent();
}
}
2 changes: 1 addition & 1 deletion app/Http/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class Kernel extends HttpKernel
],

'api' => [
// \Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class,
\Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class,
\Illuminate\Routing\Middleware\ThrottleRequests::class.':api',
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],
Expand Down
24 changes: 24 additions & 0 deletions app/Http/Resources/TransactionResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@


<?php

namespace App\Http\Resources;

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

class TransactionResource extends JsonResource
{
public function toArray(Request $request): array
{
return [
'id' => $this->id,
'account_id' => $this->account_id,
'amount' => $this->amount,
'transaction_date' => $this->transaction_date,
'description' => $this->description,
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
];
}
}
110 changes: 110 additions & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@


# Accounting API Documentation

## Authentication
This API uses Laravel Sanctum for authentication. To access the API endpoints, you need to:

1. Create an API token through the dashboard
2. Include the token in your requests:
```
Authorization: Bearer <your-token>
```

## Rate Limiting
API requests are limited to 60 per minute per user.

## Endpoints

### Transactions

#### GET /api/transactions
List all transactions (paginated)

Response:
```json
{
"data": [
{
"id": 1,
"account_id": 1,
"amount": 1000.00,
"transaction_date": "2024-03-15",
"description": "Sample transaction",
"created_at": "2024-03-15T10:00:00Z",
"updated_at": "2024-03-15T10:00:00Z"
}
],
"links": {
"first": "http://example.com/api/transactions?page=1",
"last": "http://example.com/api/transactions?page=1",
"prev": null,
"next": null
},
"meta": {
"current_page": 1,
"from": 1,
"last_page": 1,
"path": "http://example.com/api/transactions",
"per_page": 15,
"to": 1,
"total": 1
}
}
```

#### GET /api/transactions/{id}
Get a specific transaction

Response:
```json
{
"data": {
"id": 1,
"account_id": 1,
"amount": 1000.00,
"transaction_date": "2024-03-15",
"description": "Sample transaction",
"created_at": "2024-03-15T10:00:00Z",
"updated_at": "2024-03-15T10:00:00Z"
}
}
```

#### POST /api/transactions
Create a new transaction

Required fields:
- account_id: integer
- amount: numeric
- transaction_date: date
- description: string

#### PUT /api/transactions/{id}
Update an existing transaction

Optional fields:
- account_id: integer
- amount: numeric
- transaction_date: date
- description: string

#### DELETE /api/transactions/{id}
Delete a transaction

### Exchange Rates

#### GET /api/exchange-rates
Get latest exchange rates

Response:
```json
{
"base": "USD",
"rates": {
"EUR": 0.92,
"GBP": 0.79,
"JPY": 110.86
},
"timestamp": "2024-03-15T10:00:00Z"
}
13 changes: 11 additions & 2 deletions routes/api.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use App\Http\Controllers\Api\TransactionController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;

Expand All @@ -14,6 +15,14 @@
|
*/

Route::middleware('auth:sanctum')->get('/user', function (Request $request) {
return $request->user();
Route::middleware('auth:sanctum')->group(function () {
Route::get('/user', function (Request $request) {
return $request->user();
});

Route::apiResource('transactions', TransactionController::class);

Route::get('/exchange-rates', function () {
return app(App\Services\ExchangeRateService::class)->getLatestRates();
})->middleware('throttle:60,1');
});

0 comments on commit 3e617d3

Please sign in to comment.