- GIN
- GORM (SQLite)
To get started, follow these steps:
-
Clone the Repository:
git clone https://github.com/BoomNooB/SimpleGoCRUD.git cd SimpleGoCRUD
-
Install Dependencies:
Ensure you have Go installed. Then, run the following command to install the project's dependencies:
go mod tidy
-
Running a makefile:
Simply just runmake
then it will do a couple of thing here- Create a
.env
file in the root directory and set theAPI_PORT
variable to22345
the port on which the API will run: - Initialize the SQLite database and put
20
example of customer info into database that namecustomer.db
- Copy
.env
andcustomer.db
to./api
- Create a
-
Run the Application:
Start the server by executing:go run cmd/main/main.go
or you can run withair
(I've already provide the air config file) The server will start athttp://localhost:22345
Check if the API is running.
- URL:
/
- Method:
GET
- Response:
- Status Code: 200 OK
- Body:
{
"data": "API is running"
}
Create a new customer.
- URL:
/customers
- Method:
POST
- Request Body:
{
"name": "New Customer",
"age": 25
}
- Response:
- Status Code: 201 Created
- Body:
{
"id": 2,
"name": "New Customer",
"age": 25
}
Update customer information by ID.
- URL:
/customers/:id
- Method:
PUT
- URL Parameters:
id
(uint, required) - The ID of the customer to update.
- Request Body:
{
"name": "Updated Customer",
"age": 30
}
- Response:
- Status Code: 200 OK
- Body:
{
"id": 1,
"name": "Updated Customer",
"age": 30
}
Retrieve customer information by ID.
- URL:
/customers/:id
- Method:
GET
- URL Parameters:
id
(uint, required) - The ID of the customer to retrieve.
- Response:
- Status Code: 200 OK
- Body:
{
"id": 1,
"name": "John Doe",
"age": 30
}
Delete a customer by ID.
- URL:
/customers/:id
- Method:
DELETE
- URL Parameters:
id
(uint, required) - The ID of the customer to delete.
- Response:
- Status Code: 200 OK
Assume that you've run the makefile since installation step
Before running the test please ensure that the variable record_id
contains an existing ID from the database in the test file api/customer_test.go
- Run the tests using the following command:
go test ./...
orgo test ./... -cover
if you want to see percent of coverage
The test coverage of this suite is 83.3%.