Skip to content

leoiensen/user-api-golang

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Basic CRUD operations with MongoDB for study.

Run the project

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 .

Endpoints:

GET    /users
GET    /users/:email
POST   /users
PUT    /users/:email
DELETE /users/:email

Get User

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"
  }
}

Create User

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"
  }
}

Update User

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"
  }
}

Delete User

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:

{}

Errors

All of the endpoints return an error in json format with a proper http status code, if something goes wrong:

{
  "error": "User not found"
}

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Go 100.0%