This document provides information about the endpoints available in the PayIt API, as well as the data models/entities used in the API.
-
HTTP Method: GET
-
Endpoint:
/accounts/:id
-
Description: Retrieve an account by its ID.
Response (JSON):
{ "id": "812f2e44-6b34-4e26-a418-9affc416c032", "name": "John Doe", "balance": 1000.0 }
- Note: The
balance
field in the request body is of type string, but in the response, it will be of type float. (Why? Example data is supplied with Balance as a string, but to facilitate transfers it was changed to float)
- Note: The
- HTTP Method: GET
- Endpoint:
/accounts
- Description: Retrieve a list of all accounts.
-
HTTP Method: POST
-
Endpoint:
/accounts
-
Description: Create a new account.
Request Body (JSON):
{ "name": "John Doe", "balance": "1000.00" }
-
HTTP Method: PUT
-
Endpoint:
/accounts/:id
-
Description: Update an account by its ID.
Request Body (JSON):
{ "name": "Updated Name", "balance": "1500.00" }
- Note: The
id
field in the request URL specifies the account to update.
- Note: The
-
HTTP Method: GET
-
Endpoint:
/transfers/:id
-
Description: Retrieve a transfer by its ID.
Response (JSON):
{ "id": "33f47cae-580f-11ee-8c99-0242ac120002", "senderID": "7d4b886d-dd4d-4133-9bc7-26f0c6233e8c", "receiverID": "53f30a2e-f9c8-4442-a278-b622a42e4054", "amount": 500.0, "succeeded": true }
- HTTP Method: GET
- Endpoint:
/transfers
- Description: Retrieve a list of all transfers.
-
HTTP Method: POST
-
Endpoint:
/transfers
-
Description: Make a new transfer.
Request Body (JSON):
{ "id": "33f47cae-580f-11ee-8c99-0242ac120002", "senderID": "7d4b886d-dd4d-4133-9bc7-26f0c6233e8c", "receiverID": "53f30a2e-f9c8-4442-a278-b622a42e4054", "amount": 500.0, }
- A collection of requests is provided.
- For Postman
- For ThunderClient
To run the PayIt program, follow these steps:
-
Using source code
go run main.go
-
Build and run executable
go build -o payit ./payit
-
Server starts on port
8080
- Note: The application loads
accounts.json
data into the memory, if successful this message will be logged:Ready to receive requests
. - Note: Optionally replace
accounts.json
with your ownaccounts.json
file, but it should follow the same format, before running.
To run unit tests run:
go test ./...
Go's sync.Map
is used as the main structure for holding Accounts & Transfers entities.
There is a separate for each kind of entity.
sync.Map
facilitates concurrent read and write access to the data .
The transfer must carefully modify the sender and receiver accounts.
This is done by introducing a mutex.Lock
for each account. This means that the locking granularity is on the row level.
Transfers include locking the sender's account, inspecting their balance, and then deducting the transfer amount, and finally and unlocking the sender's account.
As for the receiver, their account is locked until their balance is updated, to ensure that there are no write after write issues, and finally ublocking the receiver's account.
As for fetching and creating accounts and transfers, the concurrency is handled by the sync.Map