-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
149 lines (124 loc) · 3.64 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
package main
import (
"log"
"net/http"
"strconv"
"time"
"github.com/gin-gonic/gin"
)
// Item represents a product with ID, Name, and Price
type Item struct {
ID int `json:"id"`
Name string `json:"name"`
Price float64 `json:"price"`
}
// Sample data
var items = []Item{
{ID: 1, Name: "Item1", Price: 10.5},
{ID: 2, Name: "Item2", Price: 20.0},
}
func main() {
// Create a new Gin router
router := gin.Default()
// Logging middleware
router.Use(loggingMiddleware)
// Define routes
router.GET("/items", getItems)
router.POST("/items", addItem)
router.GET("/items/:id", getItemByID)
router.PUT("/items/:id", updateItemByID)
router.DELETE("/items/:id", deleteItemByID)
// Start the server
log.Println("Server is running on http://localhost:8080")
router.Run(":8080")
}
// loggingMiddleware logs incoming requests and their processing time
func loggingMiddleware(c *gin.Context) {
start := time.Now()
path := c.Request.URL.Path
method := c.Request.Method
c.Next()
statusCode := c.Writer.Status()
log.Printf("[%s] %s %d %v", method, path, statusCode, time.Since(start))
}
// getItems handles GET /items and returns the list of items
func getItems(ctx *gin.Context) {
ctx.JSON(http.StatusOK, items)
log.Println("Returned all items")
}
// addItem handles POST /items and adds a new item
func addItem(ctx *gin.Context) {
var newItem Item
if err := ctx.ShouldBindJSON(&newItem); err != nil {
ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
log.Printf("Invalid input: %v", err)
return
}
newItem.ID = len(items) + 1
items = append(items, newItem)
ctx.JSON(http.StatusCreated, newItem)
log.Printf("Added new item: %+v", newItem)
}
// getItemByID handles GET /items/:id and returns a single item by ID
func getItemByID(ctx *gin.Context) {
id, err := strconv.Atoi(ctx.Param("id"))
if err != nil {
ctx.JSON(http.StatusBadRequest, gin.H{"error": "Invalid ID"})
log.Printf("Invalid ID: %s", ctx.Param("id"))
return
}
for _, item := range items {
if item.ID == id {
ctx.JSON(http.StatusOK, item)
log.Printf("Returned item with ID %d", id)
return
}
}
ctx.JSON(http.StatusNotFound, gin.H{"error": "Item not found"})
log.Printf("Item not found: ID %d", id)
}
// updateItemByID handles PUT /items/:id to update an existing item by ID
func updateItemByID(ctx *gin.Context) {
id, err := strconv.Atoi(ctx.Param("id"))
if err != nil {
ctx.JSON(http.StatusBadRequest, gin.H{"error": "Invalid ID"})
log.Printf("Invalid ID: %s", ctx.Param("id"))
return
}
var updatedItem Item
if err := ctx.ShouldBindJSON(&updatedItem); err != nil {
ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
log.Printf("Invalid input: %v", err)
return
}
for i, item := range items {
if item.ID == id {
updatedItem.ID = item.ID // Preserve the original ID
items[i] = updatedItem
ctx.JSON(http.StatusOK, updatedItem)
log.Printf("Updated item with ID %d: %+v", id, updatedItem)
return
}
}
ctx.JSON(http.StatusNotFound, gin.H{"error": "Item not found"})
log.Printf("Item not found: ID %d", id)
}
// deleteItemByID handles DELETE /items/:id to delete an item by ID
func deleteItemByID(ctx *gin.Context) {
id, err := strconv.Atoi(ctx.Param("id"))
if err != nil {
ctx.JSON(http.StatusBadRequest, gin.H{"error": "Invalid ID"})
log.Printf("Invalid ID: %s", ctx.Param("id"))
return
}
for i, item := range items {
if item.ID == id {
items = append(items[:i], items[i+1:]...)
ctx.JSON(http.StatusOK, gin.H{"message": "Item deleted"})
log.Printf("Deleted item with ID %d", id)
return
}
}
ctx.JSON(http.StatusNotFound, gin.H{"error": "Item not found"})
log.Printf("Item not found: ID %d", id)
}