This is a simple web API that performs basic CRUD using Go and Mysql
- Clone the repository
- Navigate to the project directory:
cd simpleAPI
- Create an
.env
file in the root directory of the project. - Populate the
.env
file with the required environment variables.
Here's an example:
PORT=:8080
DB_HOST={hostname}
DB_PORT={port}
DB_USERNAME={username}
DB_PASSWORD={password}
DB_NAME={simpleapidb}
DB_NETWORK=tcp
- run
docker compose up
- to stop the application run
docker compose down
- run
go build -o {binaryName} .\cmd\app\main.go
to build the binary - run
.\{binary name}
to run the binary (eg .\simpleAPI)
- run
go test -v ./...
to run the unit tests - run
go test -v ./... -coverprofile=c.out
to create the cover profile - run
go tool cover -html c
to see the coverage of the unit tests
This Endpoint Creates a Student
curl --location 'http://localhost:8001/student/' \ --header 'Content-Type: application/json' \ --data '{ "firstname":"Daniel", "lastname":"Riccardo", "year":3 }'
{
"status": "Success",
"data": {
"id": 8,
"firstname": "Daniel",
"lastname": "Riccardo",
"year": 3
},
"message": "Student Created Successfully"
}
This Endpoint Returns all the Students
curl --location 'http://localhost:8001/student/'
{
"status": "Success",
"data": [
{
"id": 1,
"firstname": "Charles",
"lastname": "Leclerc",
"year": 3
},
{
"id": 2,
"firstname": "Carlos",
"lastname": "Sainz",
"year": 1
},
{
"id": 3,
"firstname": "Lewis",
"lastname": "Hamilton",
"year": 3
}
],
"message": "Student Queried Successfully"
}
This Endpoint Returns a specific Student
curl --location 'http://localhost:8001/student/getStudent/3'
{
"status": "Success",
"data": {
"id": 3,
"firstname": "Lewis",
"lastname": "Hamilton",
"year": 3
},
"message": "Student Queried Successfully"
}
This Endpoint Updates Data of a Student
curl --location --request PUT 'http://localhost:8001/student/' \ --header 'Content-Type: application/json' \ --data '{ "id":1, "firstname":"Charles", "lastname":"Leclerc", "year":3 }'
{
"status": "Success",
"data": {
"id": 1,
"firstname": "Charles",
"lastname": "Leclerc",
"year": 3
},
"message": "Student Updated Successfully"
}
This Endpoint Deletes a Student
curl --location --request DELETE 'http://localhost:8001/student/7'
{
"status": "Success",
"data": {
"id": 0,
"firstname": "",
"lastname": "",
"year": 0
},
"message": "Student Deleted Successfully"
}
This Endpoint can be used to search a student based on their firstname or lastname and sort the results. This endpoint give a paginated response
curl --location --request GET 'http://localhost:8001/student/search' \ --header 'Content-Type: application/json' \ --data '{ "searchString":"charl", "sortBy": { "column":"firstname", "direction":"ASC" }, "pagination": { "page":0, "pageSize":2 } }'
{
"status": "Success",
"data": {
"totalElements": 1,
"data": [
{
"id": 1,
"firstname": "Charles",
"lastname": "Leclerc",
"year": 3
}
]
},
"message": "Student Queried Successfully"
}