-
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #363 from liberu-accounting/sweep/Implement-Transa…
…ction-API-and-Exchange-Rate-Endpoint-with-Sanctum-Authentication Implement Transaction API and Exchange Rate Endpoint with Sanctum Authentication
- Loading branch information
Showing
5 changed files
with
205 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters