# README
Go-Crud
This is a comprehensive guide for the "Go-Crud" project in Go, an example application that implements the basic CRUD (Create, Read, Update, Delete) operations for users. The project includes a Dockerfile to facilitate running it in containers.
Information
- Title: Go-Crud | Diego Dev Tech
- Version: 1.0
- Host: localhost:8080
Prerequisites
Before getting started, make sure you have the following prerequisites installed on your system:
- Go: The Go programming language.
- Docker: Docker is required if you wish to run the application in a container.
Installation
Follow the steps below to install the project in your development environment:
-
Clone the repository:
git clone https://github.com/diegodevtech/go-crud.git
-
Navigate to the project directory:
cd go-crud
-
Make sure you have a .env file containing the following variables:
MONGODB_URL="mongodb://localhost:27017"
MONGODB_USER_DB=<"mongodb_name">
MONGODB_USER_COLLECTION=<"mongodb_collection_name">
JWT_SECRET_KEY=<"a random value">
-
Build the application using Docker Compose:
docker compose up
The application will be accessible at http://localhost:8080
Testing the Application
If you prefer, after running the project, visit: http://localhost:8080/swagger/index.html# to see and test all the route contracts.
The Go-Crud application offers REST endpoints for creating, listing, updating, and deleting users. You can use tools like curl or Postman to test the endpoints. Here are some curl
command examples for testing the endpoints:
-
Create a user:
curl -X POST -H "Content-Type: application/json" -d '{"name": "João", "email": "[email protected]", "age": 30, "password": "password$#@$#323"}' http://localhost:8080/createUser
-
Update a user:
curl -X PUT -H "Content-Type: application/json" -d '{"name": "João Silva"}' http://localhost:8080/updateUser/{userId}
-
Delete a user:
curl -X DELETE http://localhost:8080/deleteUser/{userID}
Remember to adjust the commands according to your needs and requirements.
Data Models
request.Login
Structure containing the necessary fields for user login.
email
(string, required): The user's email (must be a valid email address).password
(string, required): The user's password (must be at least 8 characters and contain at least one of the characters: !@#$%&*()_-=+).
request.UserRequest
Structure containing the required fields for creating a new user.
age
(integer, required): The user's age (must be between 18 and 100).email
(string, required): The user's email (must be a valid email address).name
(string, required): The user's name (must be at least 4 characters and at most 100 characters).password
(string, required): The user's password (must be at least 8 characters and contain at least one of the characters: !@#$%&*()_-=+).
request.UserUpdateRequest
Structure containing fields to update user information.
age
(integer, required): The user's age (must be between 18 and 100).name
(string, required): The user's name (must be at least 4 characters and at most 100 characters).
response.UserResponse
Response structure containing user information.
age
(integer): The user's age.email
(string): The user's email.id
(string): The user's unique ID.name
(string): The user's name.
rest_err.Causes
Structure representing the causes of an error.
field
(string): The field associated with the error cause.message
(string): Error message describing the cause.
rest_err.RestErr
Structure describing why an error occurred.
causes
(array of rest_err.Causes): Error causes.code
(integer): Error code.error
(string): Error description.message
(string): Error message.
Endpoints
Note
- For authentication, you should include the access token in the
Authorization
header as "Bearer " for protected endpoints.
The API offers the following endpoints:
-
POST /createUser
- Description: Create a new user with the provided user information.
- Parameters:
userRequest
(body, required): User information for registration.
- Responses:
- 200: OK (User created successfully)
- 400: Bad Request (Request error)
- 500: Internal Server Error (Internal server error)
-
DELETE /deleteUser/{userId}
- Description: Delete a user based on the provided ID parameter.
- Parameters:
userId
(path, required): ID of the user to be deleted.
- Responses:
- 200: OK (User deleted successfully)
- 400: Bad Request (Request error)
- 500: Internal Server Error (Internal server error)
-
GET /getUserByEmail/{userEmail}
- Description: Retrieve user details based on the email provided as a parameter.
- Parameters:
userEmail
(path, required): Email of the user to be retrieved.
- Responses:
- 200: User information retrieved successfully
- 400: Error: Invalid user ID
- 404: User not found
-
GET /getUserById/{userId}
- Description: Retrieve user details based on the user ID provided as a parameter.
- Parameters:
userId
(path, required): ID of the user to be retrieved.
- Responses:
- 200: User information retrieved successfully
- 400: Error: Invalid user ID
- 404: User not found
-
POST /login
- Description: Allow a user to log in and receive an authentication token.
- Parameters:
Login
(body, required): User login credentials.
- Responses:
- 200: Login successful, authentication token provided
- 401: Error: Invalid login credentials
-
PUT /updateUser/{userId}
- Description: Update user details based on the ID provided as a parameter.
- Parameters:
userId
(path, required): ID of the user to be updated.userRequest
(body, required): User information for update.
- Responses:
- 200: OK (User updated successfully)
- 400: Bad Request (Request error)
- 500: Internal Server Error (Internal server error)
Contributing
If you wish to contribute to the Go-Crud project, feel free to submit pull requests or report issues on the official repository.
License
This project is distributed under the MIT license. Please refer to the LICENSE file for more details.
I hope this documentation has been helpful in understanding and interacting with the API of the Go-Crud. If you have any questions or need additional support, please don't hesitate to reach out. Enjoy using the API!