Microservice for managing user balances
Our company has many microservices. Many of them somehow need to interact with user balance. The architectural committee has decided to centralize the work with user balance in a separate service.
Implement a microservice for working with user balances (credit, debit, transfer, and getting user balance). The service should provide an HTTP API with requests and responses in JSON format.
Main task (minimum): 👍
- Method for crediting funds to the balance. Accepts user ID and the amount of funds to credit.
- Method for reserving funds from the main balance to a separate account. Accepts user ID, service ID, order ID, and cost.
- Method for recognizing revenue. Deducts money from the reserve and adds data to the accounting report. Accepts user ID, service ID, order ID, and amount.
- Method for getting the user balance. Accepts user ID.
Optional: ✅
- Code coverage with tests.
- Swagger file for your API.
- Implementation of a scenario for unreserving funds if the service cannot be applied.
Full task text, swagger.yaml here
First of all, clone this repo and download dependencies. Requirements: go 1.20
, git
git clone [email protected]:pershin-daniil/internship_backend_2022.git
cd internship_backend_2022
go mod tidy
make up
- to start docker container, and then you need databases. So, here file to create tables.
Run command to start server. This command up docker container and run main.go
.
make run
Now you can try commands in your shell and see the results. Don't forget to create tables in database. File to create tables here
To check tests 👇 This command up docker container, then run tests, and finally remove docker container.
make test
Other make command you can check here. There is lint
, up
, down
to manage project more satisfying.
curl --location 'localhost:8080/api/v1/addFunds' \
--header 'Content-Type: application/json' \
--data '{
"transactionID":"transaction-uuid-1",
"userID":1,
"balance":100
}'
{"id":3,"userID":1,"balance":100,"reserved":0,"updatedAt":"2023-03-28T17:52:16.152192+03:00"}
curl --location 'localhost:8080/api/v1/reserveFunds' \
--header 'Content-Type: application/json' \
--data '{
"transactionID":"transaction-uuid-2",
"walletID":1,
"serviceID":1,
"orderID":1,
"price":15
}'
{"id":4,"walletID":1,"serviceID":1,"orderID":1,"price":15,"status":"","dateTime":"2023-03-28T17:57:41.681074+03:00"}
curl --location 'localhost:8080/api/v1/recognizeRevenue' \
--header 'Content-Type: application/json' \
--data '{
"transactionID":"transaction-uuid-3",
"walletID":1,
"serviceID":1,
"orderID":1,
"status":"DONE"
}'
{"id":4,"walletID":1,"serviceID":1,"orderID":1,"price":15,"status":"DONE","dateTime":"2023-03-28T17:57:41.681074+03:00"}
curl --location --request GET 'localhost:8080/api/v1/getUserBalance' \
--header 'Content-Type: application/json' \
--data '{
"userID":1
}'
{"id":3,"userID":1,"balance":100,"reserved":0,"updatedAt":"2023-03-28T17:52:16.152192+03:00"}