|
1 | | -# golang-test-api |
2 | | - |
| 1 | +- # golang-test-api |
3 | 2 | A simple CRUD API made with Go, Postgres, FIber, Gorm and Docker. |
4 | 3 |
|
5 | 4 | - ## Cloning the repository |
6 | 5 | To clone the repository run the following command: |
7 | | - ``` |
| 6 | + ```bash |
8 | 7 | $ git clone https://github.com/WeDias/golang-test-api.git |
9 | 8 | ``` |
10 | 9 |
|
11 | 10 | - ## How to install and run (docker) |
12 | | - Inside the newly cloned project folder run the following command: |
13 | | - ``` |
14 | | - $ docker compose up |
15 | | - ``` |
| 11 | + Inside the newly cloned project folder run the following command: |
| 12 | + ```bash |
| 13 | + $ docker compose up |
| 14 | + # or |
| 15 | + $ docker-compose up |
| 16 | + ``` |
16 | 17 |
|
17 | 18 | - ## How to install and run (manually) |
18 | 19 |
|
19 | 20 | - ### Installing the dependencies and setup env |
20 | 21 | Inside the newly cloned project folder run the following command to install the dependencies: |
21 | | - ``` |
| 22 | + ```bash |
22 | 23 | $ go mod download |
23 | 24 | ``` |
24 | 25 | To configure the environment run this command: |
25 | | - ``` |
| 26 | + ```bash |
26 | 27 | $ bash setup-env.sh |
27 | 28 | ``` |
28 | 29 | It will generate a .env file with the variables below, you can edit to adapt with your database settings. |
29 | | - ``` |
| 30 | + ```bash |
30 | 31 | PG_HOST=localhost |
31 | 32 | PG_PASS=postgres |
32 | 33 | PG_USER=postgres |
|
39 | 40 |
|
40 | 41 | - ### Running the application |
41 | 42 | Inside the project, run the following command to run the application: |
42 | | - ``` |
| 43 | + ```bash |
43 | 44 | $ go run main.go |
44 | 45 | ``` |
45 | 46 |
|
46 | 47 | - ## API |
| 48 | + #### Create a new product: |
| 49 | + ```bash |
| 50 | + # POST /api/v1/product |
| 51 | + |
| 52 | + $ curl --request POST \ |
| 53 | + --url http://localhost:3000/api/v1/product \ |
| 54 | + --header 'Content-Type: application/json' \ |
| 55 | + --data '{ |
| 56 | + "name": "product1", |
| 57 | + "price": 10.99, |
| 58 | + "stock": 10 |
| 59 | + }' |
| 60 | + |
| 61 | + > {"id":1,"name":"product1","price":10.99,"stock":10} |
47 | 62 | ``` |
48 | | - # POST /api/v1/product |
49 | | -
|
50 | | - $ curl --request POST \ |
51 | | - --url http://localhost:3000/api/v1/product \ |
52 | | - --header 'Content-Type: application/json' \ |
53 | | - --data '{ |
54 | | - "name": "product1", |
55 | | - "price": 10.99, |
56 | | - "stock": 10 |
57 | | - }' |
58 | 63 |
|
59 | | - > {"id":1,"name":"product1","price":10.99,"stock":10} |
60 | | - ``` |
| 64 | + #### Get all products: |
| 65 | + ```bash |
| 66 | + # GET /api/v1/product |
61 | 67 |
|
62 | | - ``` |
63 | | - # GET /api/v1/product |
| 68 | + $ curl --request GET \ |
| 69 | + --url http://localhost:3000/api/v1/product |
64 | 70 |
|
65 | | - $ curl --request GET \ |
66 | | - --url http://localhost:3000/api/v1/product |
67 | | -
|
68 | | - > [{"id":1,"name":"product1","price":10.99,"stock":10}] |
69 | | - ``` |
70 | | -
|
71 | | - ``` |
72 | | - # GET /api/v1/product/1 |
73 | | -
|
74 | | - $ curl --request GET \ |
75 | | - --url http://localhost:3000/api/v1/product/1 |
76 | | -
|
77 | | - > {"id":1,"name":"product1","price":10.99,"stock":10} |
78 | | - ``` |
| 71 | + > [{"id":1,"name":"product1","price":10.99,"stock":10}] |
| 72 | + ``` |
79 | 73 |
|
80 | | - ``` |
81 | | - # PUT /api/v1/product/1 |
| 74 | + #### Get a product by id: |
| 75 | + ```bash |
| 76 | + # GET /api/v1/product/1 |
82 | 77 |
|
83 | | - $ curl --request PUT \ |
84 | | - --url http://localhost:3000/api/v1/product/1 \ |
85 | | - --header 'Content-Type: application/json' \ |
86 | | - --data '{ |
87 | | - "name": "a product", |
88 | | - "price": 8.99, |
89 | | - "stock": 5 |
90 | | - }' |
| 78 | + $ curl --request GET \ |
| 79 | + --url http://localhost:3000/api/v1/product/1 |
91 | 80 |
|
92 | | - > {"id":1,"name":"a product","price":8.99,"stock":5} |
93 | | - ``` |
| 81 | + > {"id":1,"name":"product1","price":10.99,"stock":10} |
| 82 | + ``` |
| 83 | + |
| 84 | + #### Update a product by id: |
| 85 | + ```bash |
| 86 | + # PUT /api/v1/product/1 |
| 87 | + |
| 88 | + $ curl --request PUT \ |
| 89 | + --url http://localhost:3000/api/v1/product/1 \ |
| 90 | + --header 'Content-Type: application/json' \ |
| 91 | + --data '{ |
| 92 | + "name": "a product", |
| 93 | + "price": 8.99, |
| 94 | + "stock": 5 |
| 95 | + }' |
| 96 | + |
| 97 | + > {"id":1,"name":"a product","price":8.99,"stock":5} |
| 98 | + ``` |
94 | 99 |
|
95 | | - ``` |
96 | | - # DELETE /api/v1/product/1 |
| 100 | + #### Delete a product by id: |
| 101 | + ```bash |
| 102 | + # DELETE /api/v1/product/1 |
97 | 103 |
|
98 | | - $ curl --request DELETE \ |
99 | | - --url http://localhost:3000/api/v1/product/1 |
| 104 | + $ curl --request DELETE \ |
| 105 | + --url http://localhost:3000/api/v1/product/1 |
100 | 106 |
|
101 | | - > {"id":1,"name":"a product","price":8.99,"stock":5} |
| 107 | + > {"id":1,"name":"a product","price":8.99,"stock":5} |
102 | 108 | ``` |
0 commit comments