Basic CRUD operations with MongoDB for study.
Start a MongoDB instance using docker:
docker run --name mongodb -d -p 27017:27017 mongo
Clone the repository:
git clone [email protected]:gabrielmvas/user-api-golang.git
Change the current directory to the repository:
cd user-api-golang
Install the dependencies:
go get ./...
Finally, run the app on port 9080
:
go run .
GET /users
GET /users/:email
POST /users
PUT /users/:email
DELETE /users/:email
This endpoint retrieves a user given the email.
Send a GET
request to /users/:email
:
curl -X GET 'http://127.0.0.1:9080/users/[email protected]'
Response:
{
"user": {
"id": "<user_id>",
"first_name": "Test",
"last_name": "User",
"email": "[email protected]",
"password": "testpassword"
}
}
This endpoint inserts a document in the users
collection of the users
database.
Send a POST
request to /users
:
curl -X POST 'http://127.0.0.1:9080/users' -H "Content-Type: application/json" -d '{"first_name": "Test", "last_name": "User" "email": "[email protected]", "password": "testpassword"}'
Response:
{
"user": {
"id": "<user_id>",
"name": "TestUser",
"email": "[email protected]",
"password": "testpassword"
}
}
This endpoint updates the provided fields within the specified document filtered by email.
Send a PUT
request to /users/:email
:
curl -X PUT 'http://127.0.0.1:9080/users/[email protected]' -H "Content-Type: application/json" -d '{"password": "testpassword"}'
Response:
{
"user": {
"id": "<user_id>",
"first_name": "Test",
"last_name": "User",
"email": "[email protected]",
"password": "testpassword"
}
}
This endpoint deletes the user from database given the email.
Send a DELETE
request to /users/:email
:
curl -X DELETE 'http://127.0.0.1:9080/users/[email protected]'
Response:
{}
All of the endpoints return an error in json format with a proper http status code, if something goes wrong:
{
"error": "User not found"
}