-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
131 lines (103 loc) · 2.93 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
package main
import (
"fmt"
"net/http"
"github.com/gin-gonic/gin"
)
type Book struct {
ID string `json:"id"`
Title string `json:"title"`
Author string `json:"author"`
Quantity int `json:"quantity"`
}
var BooksList = []Book{
{ID: "1", Title: "Tha DaVinci Code", Author: "Dan Brown", Quantity: 10},
{ID: "2", Title: "Valley Of The Forgotten Wolves", Author: "Amrou Abdelhamid", Quantity: 20},
{ID: "3", Title: "Land Of Zicola", Author: "Amrou Abdelhamid", Quantity: 15},
}
func checkQuery(context *gin.Context) (*Book, error) {
id, ok := context.GetQuery("id")
if !ok {
context.IndentedJSON(http.StatusBadRequest, gin.H{"message": "Missing id query parameter!!"})
// return
}
book, err := getBookById(id)
if err != nil {
context.IndentedJSON(http.StatusNotFound, gin.H{"message": "Book Not Found!!"})
// return
}
return book, err
}
// GET => /books
func getBooks(context *gin.Context) {
context.IndentedJSON(http.StatusOK, BooksList)
}
// GET => /books/:id
func bookById(context *gin.Context) {
// Targeting books by id
id := context.Param("id")
book, err := getBookById(id)
// Return "Book Not Found!!" message if book not found
if err != nil {
context.IndentedJSON(http.StatusNotFound, gin.H{"message": "Book Not Found!!"})
return
}
// Returning the indented JSON of the book
context.IndentedJSON(http.StatusOK, book)
}
func getBookById(id string) (*Book, error) {
// Looping through the books list and returning the book item from the books list
for index, book := range BooksList {
if book.ID == id {
return &BooksList[index], nil
}
}
return nil, fmt.Errorf("Book Not Found")
}
// POST => books/{}
func createBook(context *gin.Context) {
var newBook Book
if err := context.BindJSON(&newBook); err != nil {
return
}
BooksList = append(BooksList, newBook)
context.IndentedJSON(http.StatusCreated, newBook)
}
// CheckOut a book (Query parameter style)
func checkOutBook(context *gin.Context) {
book, err := checkQuery(context)
if err != nil {
context.IndentedJSON(http.StatusNotFound, gin.H{"message": "Book Not Found!!"})
return
}
if book.Quantity <= 0 {
context.IndentedJSON(http.StatusBadRequest, gin.H{"message": "Book Is Not Available!!"})
return
}
book.Quantity -= 1
context.IndentedJSON(http.StatusOK, book)
}
// Returning a book
func returnBook(context *gin.Context) {
book, err := checkQuery(context)
if err != nil {
context.IndentedJSON(http.StatusNotFound, gin.H{"message": "Book Not Found!!"})
return
}
if book.Quantity <= 0 {
context.IndentedJSON(http.StatusBadRequest, gin.H{"message": "Book Is Not Available!!"})
return
}
book.Quantity += 1
context.IndentedJSON(http.StatusOK, book)
}
func main() {
// Setup GIN Router
router := gin.Default()
router.GET("/books", getBooks)
router.GET("/books/:id", bookById)
router.POST("/books", createBook)
router.PATCH("/checkout", checkOutBook)
router.PATCH("/return", returnBook)
router.Run("localhost:8080")
}