Skip to content

Commit 91f1455

Browse files
committed
Documentation of API routes using swagger
1 parent 7cbebb3 commit 91f1455

11 files changed

+1778
-18
lines changed

Makefile

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
run:
2-
CompileDaemon -command="./homelab-backend"
2+
swag init --parseDependency && CompileDaemon -command="./homelab-backend"
33

44
run-linux:
5-
/home/kaizer/go/bin/CompileDaemon -command="./homelab-backend"
5+
/home/kaizer/go/bin/CompileDaemon -command="./homelab-backend"
6+
7+
swag:
8+
swag init --parseDependency

controllers/Analytics.controller.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@ import (
77
"github.com/kaizerpwn/homelab-backend/initializers"
88
)
99

10+
// @Summary Get Number of All Devices
11+
// @Description Retrieves the total number of devices in the database.
12+
// @Tags analytics
13+
// @Accept json
14+
// @Produce json
15+
// @Success 200 {integer} int64
16+
// @Router /analytics/devices [get]
1017
func GetNumberOfAllDevices(c *gin.Context) {
1118
// >> Get number of all devices
1219
var deviceCount int64
@@ -15,6 +22,13 @@ func GetNumberOfAllDevices(c *gin.Context) {
1522
c.IndentedJSON(http.StatusOK, deviceCount)
1623
}
1724

25+
// @Summary Get Number of All Rooms
26+
// @Description Retrieves the total number of rooms in the database.
27+
// @Tags analytics
28+
// @Accept json
29+
// @Produce json
30+
// @Success 200 {integer} int64
31+
// @Router /analytics/rooms [get]
1832
func GetNumberOfAllRooms(c *gin.Context) {
1933
// >> Get number of all rooms
2034
var deviceCount int64
@@ -23,6 +37,13 @@ func GetNumberOfAllRooms(c *gin.Context) {
2337
c.IndentedJSON(http.StatusOK, deviceCount)
2438
}
2539

40+
// @Summary Get Number of Active Devices
41+
// @Description Retrieves the total number of active devices.
42+
// @Tags analytics
43+
// @Accept json
44+
// @Produce json
45+
// @Success 200 {integer} int64
46+
// @Router /analytics/activedevices [get]
2647
func GetNumberOfActiveDevices(c *gin.Context) {
2748
// >> Get number of all inactive devices
2849
var deviceCount int64

controllers/Devices.controller.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,29 @@ import (
88
"github.com/kaizerpwn/homelab-backend/models"
99
)
1010

11+
// @Summary Get all Devices
12+
// @Description Fetches a list of all devices from the database.
13+
// @Tags devices
14+
// @Accept json
15+
// @Produce json
16+
// @Success 200 {array} models.Device
17+
// @Router /devices [get]
1118
func GetAllDevices(c *gin.Context) {
1219
// Get all devices from database
1320
var devices []models.Device
1421
initializers.DB.Find(&devices)
1522
c.IndentedJSON(http.StatusOK, devices)
1623
}
1724

25+
// @Summary Get a Device by ID
26+
// @Description Retrieve a device's information by its unique ID.
27+
// @Tags devices
28+
// @Accept json
29+
// @Produce json
30+
// @Param id path int true "Device ID" Format(int64)
31+
// @Success 200 {object} models.Device
32+
// @Failure 404 {string} string "Device with that ID doesn't exist."
33+
// @Router /devices/{id} [get]
1834
func GetDeviceById(c *gin.Context) {
1935
// >> Get specific device with id given
2036

controllers/Rooms.controller.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@ import (
88
"github.com/kaizerpwn/homelab-backend/models"
99
)
1010

11+
// @Summary Get all Rooms
12+
// @Description Fetches a list of all rooms from the database.
13+
// @Tags rooms
14+
// @Accept json
15+
// @Produce json
16+
// @Success 200 {array} models.Room
17+
// @Router /rooms [get]
1118
func GetAllRooms(c *gin.Context) {
1219

1320
// >> Get all rooms from database
@@ -17,6 +24,15 @@ func GetAllRooms(c *gin.Context) {
1724
c.IndentedJSON(http.StatusOK, rooms)
1825
}
1926

27+
// @Summary Get a Room by ID
28+
// @Description Retrieve a room's information by its unique ID.
29+
// @Tags rooms
30+
// @Accept json
31+
// @Produce json
32+
// @Param id path int true "Room ID" Format(int64)
33+
// @Success 200 {object} models.Room
34+
// @Failure 404 {string} string "Room with that ID doesn't exist."
35+
// @Router /rooms/{id} [get]
2036
func GetAllRoomsByID(c *gin.Context) {
2137

2238
// >> Get all rooms from database

controllers/User.controller.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,15 @@ import (
99
"golang.org/x/crypto/bcrypt"
1010
)
1111

12+
// @BasePath /api
13+
14+
// @Summary Get all users from the database
15+
// @Description Fetch all users from the database (administrator permission needed)
16+
// @Tags users
17+
// @Accept json
18+
// @Produce json
19+
// @Success 200 {array} models.User
20+
// @Router /users [get]
1221
func GetAllUsers(c *gin.Context) {
1322

1423
// >> Get all users from database
@@ -18,6 +27,15 @@ func GetAllUsers(c *gin.Context) {
1827
c.IndentedJSON(http.StatusOK, users)
1928
}
2029

30+
// @Summary Get a User by ID
31+
// @Description Retrieve a user's information by their unique ID.
32+
// @Tags users
33+
// @Accept json
34+
// @Produce json
35+
// @Param id path int true "User ID" Format(int64)
36+
// @Success 200 {object} models.User
37+
// @Failure 404 {string} string "User with that ID doesn't exist"
38+
// @Router /users/{id} [get]
2139
func GetUserByID(c *gin.Context) {
2240
id := c.Param("id")
2341

@@ -34,6 +52,18 @@ func GetUserByID(c *gin.Context) {
3452
c.IndentedJSON(http.StatusOK, user)
3553
}
3654

55+
// @Summary User Login
56+
// @Description Authenticates a user based on provided email and password.
57+
// @Tags users
58+
// @Accept json
59+
// @Produce json
60+
// @Param email body string true "User's email address" Format(email)
61+
// @Param password body string true "User's password"
62+
// @Success 200 {object} models.User
63+
// @Failure 400 {string} string "Bad Request"
64+
// @Failure 401 {string} string "Invalid Password"
65+
// @Failure 404 {string} string "User with that credentials doesn't exist."
66+
// @Router /users/login [post]
3767
func Login(c *gin.Context) {
3868
var body struct {
3969
Email string `json:"email" binding:"required"`
@@ -70,6 +100,22 @@ func Login(c *gin.Context) {
70100
}
71101
}
72102

103+
// @Summary User Registration
104+
// @Description Creates a new user account with the provided information.
105+
// @Tags users
106+
// @Accept json
107+
// @Produce json
108+
// @Param name body string true "User's first name"
109+
// @Param surname body string true "User's last name"
110+
// @Param email body string true "User's email address" Format(email)
111+
// @Param password body string true "User's password"
112+
// @Param city body string true "User's city"
113+
// @Success 200 {string} string "Account successfully registered."
114+
// @Failure 400 {string} string "Invalid request data"
115+
// @Failure 400 {string} string "All fields are required"
116+
// @Failure 409 {string} string "User already exists."
117+
// @Failure 500 {string} string "Internal server error."
118+
// @Router /users/register [post]
73119
func Register(c *gin.Context) {
74120
var body struct {
75121
Name string `json:"name" binding:"required"`

0 commit comments

Comments
 (0)