-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
169 lines (135 loc) · 4.2 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package main
import (
"fmt"
"net/http"
_ "main/docs"
"main/Config"
"github.com/gin-gonic/gin"
"github.com/jinzhu/gorm"
_ "github.com/go-sql-driver/mysql"
swaggerFiles "github.com/swaggo/files"
ginSwagger "github.com/swaggo/gin-swagger"
)
type book struct {
ID string
Title string
Author string
Price string
}
var err error
func connectDB() {
Config.DB, err = gorm.Open("mysql", Config.DbURL(Config.BuildDB()))
if err != nil {
fmt.Println("Status: ", err)
}
Config.DB.AutoMigrate(&book{})
}
// @title Book Service API
// @version v1.0.0
// @description A Book-Service API which contains all the basic CRUD functionality for any book-service application using Go and Gin Framework.
// @host localhost:3000
// @BasePath /
func main () {
connectDB()
router := gin.Default()
// docs route
router.GET("/docs/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
router.GET("/books", getAllBooks)
router.GET("/books/:id", getBookById)
router.POST("/books", postBooks)
router.PUT("/books/:id", patchBooks)
router.DELETE("/books/:id", deleteBookById)
defer Config.DB.Close()
}
// GetAllBooks is a swagger annotation for the GET request to fetch all the books from the server.
// @Summary Get all books.
// @ID get-all-books
// @Description This endpoint fetches all the books available on the server.
// @Produce json
// @Success 200 {array} book "Successful operation"
// @Failure 404 {object} gin.H
// @Failure 500 {object} gin.H
// @Router books [get]
func getAllBooks (r *gin.Context) {
var books []book
err = Config.DB.Find(&books).Error;
if err != nil {
r.AbortWithStatus(http.StatusNotFound)
} else {
r.JSON(http.StatusOK, books)
}
}
// GetBookByID is a swagger annotation for the GET request to fetch a single book by its ID from the server.
// @Summary Get a single book.
// @ID get-book-by-id
// @Description This endpoint fetches the book with the ID that matches the parameter.
// @Param id path string true "The ID of the book to fetch"
// @Produce json
// @Success 200 {object} book "Successful operation"
// @Failure 404 {object} gin.H
// @Failure 500 {object} gin.H
// @Router books/{id} [get]
func getBookById (r *gin.Context) {
id := r.Param("id")
var oneBook book
err := Config.DB.Where("ID = ?", id).First(&oneBook).Error
if err != nil {
r.AbortWithStatus(http.StatusNotFound)
} else {
r.JSON(http.StatusOK, oneBook)
}
}
// PostBooks is a swagger annotation for the POST request to add a new book to the server.
// @Summary Add a new book.
// @ID post-book
// @Description This endpoint adds a new book to the server.
// @Accept json
// @Produce json
// @Param newBook body book true "New book details"
// @Success 201 {object} book "Successful operation"
// @Failure 400 {object} gin.H
// @Failure 500 {object} gin.H
// @Router books [post]
func postBooks (r *gin.Context) {
var newBook book
r.BindJSON(&newBook)
err := Config.DB.Create(newBook).Error
if err != nil {
r.AbortWithStatus(http.StatusNotFound)
} else {
r.JSON(http.StatusOK, newBook)
}
}
// @Summary Update an existing book.
// @ID update-book-by-id
// @Description Update the book with the ID that matches the parameter.
// @Param id path string true "The ID of the book to update"
// @Param book body book true "The updated book"
// @Produce json
// @Tags books
// @Success 200 {object} book "Successful operation"
// @Failure 404 {object} gin.H
// @Router books/{id} [patch]
func patchBooks (r *gin.Context) {
//id := r.Param("id")
var newBook book
r.BindJSON(&newBook)
//err := nil
Config.DB.Save(&newBook)
r.JSON(http.StatusOK, newBook)
}
// @Summary Delete an existing book.
// @ID delete-book-by-id
// @Description Delete the book with the ID that matches the parameter.
// @Param id path string true "The ID of the book to delete"
// @Produce json
// @Tags books
// @Success 200 {object} gin.H "Successful operation"
// @Failure 404 {object} gin.H
// @Router books/{id} [delete]
func deleteBookById (r *gin.Context) {
id := r.Param("id")
var delBook book
Config.DB.Where("ID = ?", id).Delete(&delBook)
r.JSON(http.StatusOK, gin.H{"message" : "Book with given ID deleted!!"})
}