This is Spring Boot application with H2 Database. Just clone the code and let the Maven manage the dependencies and then start the application. This API allows you to create, retrieve, update, delete, and convert purchase transaction values to different currencies based on exchange rates. Below is an overview of the available endpoints and how to use them.
POST /api/purchases
Creates a new purchase transaction. This requires the transaction to have the state ACCEPT
.
- Body:
{ "state": "ACCEPT", "description": "Laptop purchase", "transactionDate": "2024-11-05T15:30:00", "amountUSD": 1000.00 }
- Body:
{ "transactionId": "uuid", "state": "ACCEPT", "description": "Laptop purchase", "transactionDate": "2024-11-05T15:30:00", "amountUSD": 1000.00 }
GET /api/purchases/{transactionId}
Retrieve a specific purchase transaction by its transactionId
.
- Path Parameter:
transactionId
(UUID): The unique identifier of the purchase transaction.
- 200 OK:
{ "transactionId": "uuid", "state": "ACCEPT", "description": "Laptop purchase", "transactionDate": "2024-11-05T15:30:00", "amountUSD": 1000.00 }
GET /api/purchases
Retrieves a list of all purchase transactions.
- No request body.
-
200 OK: If the transactions are retrieved successfully.
- Body (JSON Array):
[ { "transactionId": "f47ac10b-58cc-4372-a567-0e02b2c3d479", "state": "ACCEPT", "description": "Laptop purchase", "transactionDate": "2024-11-05T15:30:00", "amountUSD": 1000.00 }, { "transactionId": "c72d3e89-d670-45a5-b459-1b43e29d24f7", "state": "ACCEPT", "description": "Smartphone purchase", "transactionDate": "2024-11-06T10:15:00", "amountUSD": 500.00 } ]
- Body (JSON Array):
-
404 Not Found: If no purchase transactions exist.
- Body:
{ "error": "No transactions found." }
- Body:
PUT /api/purchases/{transactionId}
Updates an existing purchase transaction by its transaction ID.
- transactionId (UUID): The ID of the purchase transaction to update.
- purchaseTransactionDetails (JSON Object): The details to update for the purchase transaction.
{ "state": "ACCEPT", "description": "Updated laptop purchase", "transactionDate": "2024-11-06T15:00:00", "amountUSD": 1200.00 }
- 200 OK: If the transaction is updated successfully..
{ "transactionId": "f47ac10b-58cc-4372-a567-0e02b2c3d479", "state": "ACCEPT", "description": "Updated laptop purchase", "transactionDate": "2024-11-06T15:00:00", "amountUSD": 1200.00 }
DELETE /api/purchases/{transactionId}
Deletes a purchase transaction by its transaction ID.
- transactionId (UUID): The ID of the purchase transaction to delete.
-
204 No Content: If the transaction is deleted successfully.
- Body: Empty response body.
-
404 Not Found: If the specified transaction ID does not exist.
- Body:
{ "error": "Transaction not found." }
- Body:
GET /api/transactions/{transactionId}/convert?targetCurrency=XXXXXXX
Converts the purchase transaction amount from USD to a specified target currency using the latest exchange rate available within the last 6 months.
- transactionId (UUID): The ID of the purchase transaction to convert.
- targetCurrency (String): The currency code to which the transaction amount should be converted (e.g., "Canada-Dollar", "Brazil-Real").
-
200 OK: If the currency conversion is successful.
- Body:
{ "transactionId": "uuid", "description": "Transaction description", "transactionDate": "2024-11-06", "originalAmountUSD": 100.00, "exchangeRate": 1.252, "convertedAmount": 125.20 }
- Body:
-
400 Bad Request: If no exchange rate is available within the last 6 months for the specified target currency.
- Body:
{ "error": "Currency conversion rate not available within the last 6 months for the specified currency." }
- Body:
-
404 Not Found: If the specified transaction ID does not exist.
- Body:
{ "error": "Transaction not found." }
- Body:
Represents a purchase transaction in the system.
- transactionId (UUID): The unique identifier of the transaction.
- description (String): The description of the purchase.
- transactionDate (Date): The date when the transaction occurred.
- amountUSD (BigDecimal): The amount of the purchase in USD.
- state (TransactionState): The state of the transaction (ACCEPT or NOT_ACCEPT).
{
"transactionId": "uuid",
"description": "Laptop Purchase",
"transactionDate": "2024-11-06",
"amountUSD": 1000.00,
"state": "ACCEPT"
}
Represents the response for a converted purchase transaction.
- transactionId (UUID): The unique identifier of the transaction.
- description (String): The description of the purchase.
- transactionDate (Date): The date when the transaction occurred.
- originalAmountUSD (BigDecimal): The original purchase amount in USD.
- exchangeRate (BigDecimal): The exchange rate used for conversion.
- convertedAmount (BigDecimal): The amount in the target currency after conversion.
{
"transactionId": "uuid",
"description": "Laptop Purchase",
"transactionDate": "2024-11-06",
"originalAmountUSD": 1000.00,
"exchangeRate": 1.252,
"convertedAmount": 1252.00
}
Represents the exchange rate data returned from the Treasury API.
- countryCurrencyDesc (String): A description of the country and its currency (e.g., "Canada-Dollar", "Mexico-Peso").
- exchangeRate (BigDecimal): The exchange rate for the currency relative to USD.
- recordDate (Date): The date when the exchange rate was recorded.
{
"countryCurrencyDesc": "Canada-Dollar",
"exchangeRate": 1.252,
"recordDate": "2022-03-31"
}