Create a service similar to Splitwise, where users can track each expense and the amount that each person in the group owes. Users are not required to pay each bill individually; the service should allow users to sum up all of the expenses that they owe and automatically calculate the total amount owed to others in the group.
This project is a simple API that mimics the functionality of Splitwise, allowing users to track expenses and calculate the amounts owed between users in a group. The API provides endpoints for user registration, adding expenses, and creating groups. The expenses can be divided among multiple users in the group, and the total amount owed is calculated automatically.
- Problem Statement
- Technologies Used
- Setup Instructions
- API Endpoints
- Testing the API
- Sample Requests & Responses
- Contributions
Create a service similar to Splitwise, where users can track each expense and the amount that each person in the group owes. Users are not required to pay each bill individually; the service should allow users to sum up all of the expenses they owe and automatically calculate the total amount owed to others in the group.
-
Register a new user
- Endpoint:
POST /api/users/register - Description: Registers a new user with necessary details (name, email, password).
- Endpoint:
-
Add an expense
- Endpoint:
POST /api/expenses/add - Description: Allows users to add expenses and split the amount among multiple people in the group.
- Endpoint:
-
Create a group
- Endpoint:
POST /api/groups/create - Description: Allows users to create a group and add members to it.
- Endpoint:
- Go (Golang): The primary programming language used for backend development.
- Gorilla Mux: For routing HTTP requests.
- In-memory storage: For simplicity, the data is stored in memory (in future, a database can replace it).
Ensure that Go is installed on your machine. If not, follow these steps:
-
Visit Go's official website.
-
Download and install the latest version of Go for your operating system.
-
Verify the installation by running the following command in your terminal:
go version
To test the API using REST Client in VS Code:
- Install Visual Studio Code.
- Install the REST Client extension by following this link: REST Client Extension.
Clone this repository to your local machine:
git clone https://github.com/your-username/splitwise-api.git
cd splitwise-apiTo run the application, execute the following command:
go run main.goYour application will be running on http://localhost:8000.
- POST
/api/users/register - Description: Registers a new user with name, email, and password.
- Request Body:
{ "name": "John Doe", "email": "john@example.com", "password": "securepassword" } - Response:
{ "message": "User registered successfully", "userId": "391e4413-a833-4d92-bbf1-b3cbfdb9cf87" }
- POST
/api/expenses/add - Description: Adds a new expense and splits it between users.
- Request Body:
{ "amount": 100, "payer_id": "user-id-1", "split_between": [ "user-id-1", "user-id-2" ], "description": "Lunch" } - Response:
{ "expense": { "id": "expense1", "amount": 100, "payer_id": "user-id-1", "split_between": [ "user-id-1", "user-id-2" ], "description": "Lunch" }, "message": "Expense added successfully" }
- POST
/api/groups/create - Description: Creates a group and adds members to it.
- Request Body:
{ "group_name": "Lunch Group", "members": ["user-id-1", "user-id-2"] } - Response:
{ "groupId": "group1", "message": "Group created successfully" }
To test the API using the REST Client in VS Code:
- Install the REST Client extension in VS Code.
- Create a
test.httpfile in the root of the project directory. - Use the following structure to test the endpoints.
Example of User Registration:
### User Registration
POST http://localhost:8000/api/users/register
Content-Type: application/json
{
"name": "John Doe",
"email": "john@example.com",
"password": "securepassword"
}You can add additional tests for other endpoints like Expense Addition and Group Creation in the same file.
Click Send Request to test the API.
-
User Registration Response:
{ "message": "User registered successfully", "userId": "391e4413-a833-4d92-bbf1-b3cbfdb9cf87" } -
Expense Addition Response:
{ "expense": { "id": "expense1", "amount": 100, "payer_id": "user-id-1", "split_between": [ "user-id-1", "user-id-2" ], "description": "Lunch" }, "message": "Expense added successfully" } -
Group Creation Response:
{ "groupId": "group1", "message": "Group created successfully" }
Feel free to fork the repository, make changes, and submit pull requests. All contributions are welcome!