Skip to content

Commit 5667f0b

Browse files
committed
API Documentation updated
- Added /api in routes - Added new route for houses
1 parent 91f1455 commit 5667f0b

10 files changed

+263
-47
lines changed

controllers/Analytics.controller.go

+17-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
// @Accept json
1414
// @Produce json
1515
// @Success 200 {integer} int64
16-
// @Router /analytics/devices [get]
16+
// @Router /api/analytics/devices [get]
1717
func GetNumberOfAllDevices(c *gin.Context) {
1818
// >> Get number of all devices
1919
var deviceCount int64
@@ -28,7 +28,7 @@ func GetNumberOfAllDevices(c *gin.Context) {
2828
// @Accept json
2929
// @Produce json
3030
// @Success 200 {integer} int64
31-
// @Router /analytics/rooms [get]
31+
// @Router /api/analytics/rooms [get]
3232
func GetNumberOfAllRooms(c *gin.Context) {
3333
// >> Get number of all rooms
3434
var deviceCount int64
@@ -43,11 +43,25 @@ func GetNumberOfAllRooms(c *gin.Context) {
4343
// @Accept json
4444
// @Produce json
4545
// @Success 200 {integer} int64
46-
// @Router /analytics/activedevices [get]
46+
// @Router /api/analytics/activedevices [get]
4747
func GetNumberOfActiveDevices(c *gin.Context) {
4848
// >> Get number of all inactive devices
4949
var deviceCount int64
5050
initializers.DB.Count(&deviceCount)
5151

5252
c.IndentedJSON(http.StatusOK, deviceCount)
5353
}
54+
55+
// @Summary Get Number of All Houses
56+
// @Description Retrieves the total number of houses.
57+
// @Tags analytics
58+
// @Accept json
59+
// @Produce json
60+
// @Success 200 {integer} int64
61+
// @Router /api/analytics/houses [get]
62+
func GetNumberOfAllHouses(c *gin.Context) {
63+
var housesCount int64
64+
initializers.DB.Count(&housesCount)
65+
66+
c.IndentedJSON(http.StatusOK, housesCount)
67+
}

controllers/Devices.controller.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
// @Accept json
1515
// @Produce json
1616
// @Success 200 {array} models.Device
17-
// @Router /devices [get]
17+
// @Router /api/devices [get]
1818
func GetAllDevices(c *gin.Context) {
1919
// Get all devices from database
2020
var devices []models.Device
@@ -30,7 +30,7 @@ func GetAllDevices(c *gin.Context) {
3030
// @Param id path int true "Device ID" Format(int64)
3131
// @Success 200 {object} models.Device
3232
// @Failure 404 {string} string "Device with that ID doesn't exist."
33-
// @Router /devices/{id} [get]
33+
// @Router /api/devices/{id} [get]
3434
func GetDeviceById(c *gin.Context) {
3535
// >> Get specific device with id given
3636

controllers/Houses.controller.go

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package controllers
2+
3+
import (
4+
"net/http"
5+
6+
"github.com/gin-gonic/gin"
7+
"github.com/kaizerpwn/homelab-backend/initializers"
8+
"github.com/kaizerpwn/homelab-backend/models"
9+
)
10+
11+
// @Summary Get House by ID
12+
// @Description Retrieve a house's information by its unique ID
13+
// @Tags houses
14+
// @Accept json
15+
// @Produce json
16+
// @Param id path int true "House ID" Format(int64)
17+
// @Success 200 {object} models.House
18+
// @Failure 404 {string} string "Device with that ID doesn't exist."
19+
// @Router /api/houses/{id} [get]
20+
func GetHouseById(c *gin.Context) {
21+
id := c.Param("id")
22+
var house models.House
23+
24+
result := initializers.DB.First(&house, id)
25+
26+
if result.Error != nil {
27+
c.JSON(http.StatusNotFound, gin.H{
28+
"message": "House with that ID doesn't exist.",
29+
})
30+
}
31+
}

controllers/Rooms.controller.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
// @Accept json
1515
// @Produce json
1616
// @Success 200 {array} models.Room
17-
// @Router /rooms [get]
17+
// @Router /api/rooms [get]
1818
func GetAllRooms(c *gin.Context) {
1919

2020
// >> Get all rooms from database
@@ -32,7 +32,7 @@ func GetAllRooms(c *gin.Context) {
3232
// @Param id path int true "Room ID" Format(int64)
3333
// @Success 200 {object} models.Room
3434
// @Failure 404 {string} string "Room with that ID doesn't exist."
35-
// @Router /rooms/{id} [get]
35+
// @Router /api/rooms/{id} [get]
3636
func GetAllRoomsByID(c *gin.Context) {
3737

3838
// >> Get all rooms from database

controllers/User.controller.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717
// @Accept json
1818
// @Produce json
1919
// @Success 200 {array} models.User
20-
// @Router /users [get]
20+
// @Router /api/users [get]
2121
func GetAllUsers(c *gin.Context) {
2222

2323
// >> Get all users from database
@@ -35,7 +35,7 @@ func GetAllUsers(c *gin.Context) {
3535
// @Param id path int true "User ID" Format(int64)
3636
// @Success 200 {object} models.User
3737
// @Failure 404 {string} string "User with that ID doesn't exist"
38-
// @Router /users/{id} [get]
38+
// @Router /api/users/{id} [get]
3939
func GetUserByID(c *gin.Context) {
4040
id := c.Param("id")
4141

@@ -63,7 +63,7 @@ func GetUserByID(c *gin.Context) {
6363
// @Failure 400 {string} string "Bad Request"
6464
// @Failure 401 {string} string "Invalid Password"
6565
// @Failure 404 {string} string "User with that credentials doesn't exist."
66-
// @Router /users/login [post]
66+
// @Router /api/users/login [post]
6767
func Login(c *gin.Context) {
6868
var body struct {
6969
Email string `json:"email" binding:"required"`
@@ -115,7 +115,7 @@ func Login(c *gin.Context) {
115115
// @Failure 400 {string} string "All fields are required"
116116
// @Failure 409 {string} string "User already exists."
117117
// @Failure 500 {string} string "Internal server error."
118-
// @Router /users/register [post]
118+
// @Router /api/users/register [post]
119119
func Register(c *gin.Context) {
120120
var body struct {
121121
Name string `json:"name" binding:"required"`

docs/docs.go

+73-11
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const docTemplate = `{
1515
"host": "{{.Host}}",
1616
"basePath": "{{.BasePath}}",
1717
"paths": {
18-
"/analytics/activedevices": {
18+
"/api/analytics/activedevices": {
1919
"get": {
2020
"description": "Retrieves the total number of active devices.",
2121
"consumes": [
@@ -38,7 +38,7 @@ const docTemplate = `{
3838
}
3939
}
4040
},
41-
"/analytics/devices": {
41+
"/api/analytics/devices": {
4242
"get": {
4343
"description": "Retrieves the total number of devices in the database.",
4444
"consumes": [
@@ -61,7 +61,30 @@ const docTemplate = `{
6161
}
6262
}
6363
},
64-
"/analytics/rooms": {
64+
"/api/analytics/houses": {
65+
"get": {
66+
"description": "Retrieves the total number of houses.",
67+
"consumes": [
68+
"application/json"
69+
],
70+
"produces": [
71+
"application/json"
72+
],
73+
"tags": [
74+
"analytics"
75+
],
76+
"summary": "Get Number of All Houses",
77+
"responses": {
78+
"200": {
79+
"description": "OK",
80+
"schema": {
81+
"type": "integer"
82+
}
83+
}
84+
}
85+
}
86+
},
87+
"/api/analytics/rooms": {
6588
"get": {
6689
"description": "Retrieves the total number of rooms in the database.",
6790
"consumes": [
@@ -84,7 +107,7 @@ const docTemplate = `{
84107
}
85108
}
86109
},
87-
"/devices": {
110+
"/api/devices": {
88111
"get": {
89112
"description": "Fetches a list of all devices from the database.",
90113
"consumes": [
@@ -110,7 +133,7 @@ const docTemplate = `{
110133
}
111134
}
112135
},
113-
"/devices/{id}": {
136+
"/api/devices/{id}": {
114137
"get": {
115138
"description": "Retrieve a device's information by its unique ID.",
116139
"consumes": [
@@ -149,7 +172,46 @@ const docTemplate = `{
149172
}
150173
}
151174
},
152-
"/rooms": {
175+
"/api/houses/{id}": {
176+
"get": {
177+
"description": "Retrieve a house's information by its unique ID",
178+
"consumes": [
179+
"application/json"
180+
],
181+
"produces": [
182+
"application/json"
183+
],
184+
"tags": [
185+
"houses"
186+
],
187+
"summary": "Get House by ID",
188+
"parameters": [
189+
{
190+
"type": "integer",
191+
"format": "int64",
192+
"description": "House ID",
193+
"name": "id",
194+
"in": "path",
195+
"required": true
196+
}
197+
],
198+
"responses": {
199+
"200": {
200+
"description": "OK",
201+
"schema": {
202+
"$ref": "#/definitions/models.House"
203+
}
204+
},
205+
"404": {
206+
"description": "Device with that ID doesn't exist.",
207+
"schema": {
208+
"type": "string"
209+
}
210+
}
211+
}
212+
}
213+
},
214+
"/api/rooms": {
153215
"get": {
154216
"description": "Fetches a list of all rooms from the database.",
155217
"consumes": [
@@ -175,7 +237,7 @@ const docTemplate = `{
175237
}
176238
}
177239
},
178-
"/rooms/{id}": {
240+
"/api/rooms/{id}": {
179241
"get": {
180242
"description": "Retrieve a room's information by its unique ID.",
181243
"consumes": [
@@ -214,7 +276,7 @@ const docTemplate = `{
214276
}
215277
}
216278
},
217-
"/users": {
279+
"/api/users": {
218280
"get": {
219281
"description": "Fetch all users from the database (administrator permission needed)",
220282
"consumes": [
@@ -240,7 +302,7 @@ const docTemplate = `{
240302
}
241303
}
242304
},
243-
"/users/login": {
305+
"/api/users/login": {
244306
"post": {
245307
"description": "Authenticates a user based on provided email and password.",
246308
"consumes": [
@@ -302,7 +364,7 @@ const docTemplate = `{
302364
}
303365
}
304366
},
305-
"/users/register": {
367+
"/api/users/register": {
306368
"post": {
307369
"description": "Creates a new user account with the provided information.",
308370
"consumes": [
@@ -391,7 +453,7 @@ const docTemplate = `{
391453
}
392454
}
393455
},
394-
"/users/{id}": {
456+
"/api/users/{id}": {
395457
"get": {
396458
"description": "Retrieve a user's information by their unique ID.",
397459
"consumes": [

0 commit comments

Comments
 (0)