A locally ran program that uses gin to create api endpoints that perform CRUD Operations.
For this project I'm simulating a bikshop database that keeps a table of invoices.
-
This program requires that both the go programming language
and PostgreSQL be installed on your local machine.
It also requires a program like Postman to send http requests to the database. -
Make sure to import the psql dump file into PostgreSQL.
Use the following command 'psql --username= Bikeshop < .sql'.
Note: replace placeholder info within the angle brackets with your own. -
Inside the bikeshop.go file replace the substring 'username' within the uri variable.
Provide the username you used to create the database in PostgreSQL instead.
The program is ran using the terminal. Typing go run .
or by running its binary ./conch
within in its directory, starts the program.
A prompt will show up asking for outside internet connection, always deny it.
While the programs is still running, open Postman.
Then, send a request using the following options below to run the crud operation.
some CRUD Operations will require you to pass JSON to the responsebody
('Body' in PostMan) along with the request. In that case, where ever you see <struct>
in 'CRUD Operations' replace it with the expected json format for the object listed below.
Also, do not send the id attribute as part of the json object, its created along with the invoice.
For routes that end in :usr_id
or :id
replace it with an integer number.
To end the program in the terminal, type ^c
(ctrl-c).
{
"username": string,
"fname": string,
"lname": string,
"address": string,
"password": string
}
{
"id": int,
"username": string,
"fname": string,
"lname": string,
"address": string
}
{
"id": int,
"usr_id": int,
"product": string,
"category": string,
"price": float,
"quantity": int
}
{
"username": string,
"pswd": []byte
}
Although you don't directly pass a json struct for a user object in the request body,
you still need to provide its id for other routes. i.e :usr_id
.
User and Invoice structs' id properties :usr_id
and :id
respectfully are assigned after creation.
They're incremented after insertion(pass or fail) by the database.
After sending a Post request to create an account, the password is encrypted and stored in the database.
- Add a user account to the table
POST
localhost:8080/create/Account/
<account>
- Login into your account
POST
localhost:8080/account/login
<login-creds>
- Read all the users from the table
GET
localhost:8080/users
- Read all the invoices from the table
GET
localhost:8080/users/invoices
- Read a specific user from the table
GET
localhost:8080/user/:usr_id
- Read all the invoices for a specific user
GET
localhost:8080/user/:usr_id/invoices
- Read an invoice for a specific user
GET
localhost:8080/user/:usr_id/invoice/:id
- Add an invoice to a specific user
POST
localhost:8080/user/:usr_id/invoices
- Update all the fields on an existing invoice
PUT
localhost:8080/user/:usr_id/invoice/:id
<invoice>
- Patch one or more fields on an existing invoice
PATCH
localhost:8080/user/:usr_id/invoice/:id
<invoice>
- Delete an existing account
DELETE
localhost:8080/account
<login-creds>
- Delete an existing invoice
DELETE
localhost:8080/user/:usr_id/invoice/:id