diff --git a/backend/main.go b/backend/main.go index 0af0869..89a001b 100644 --- a/backend/main.go +++ b/backend/main.go @@ -2,34 +2,21 @@ package main import ( "net/http" - "strconv" - "sync" - "github.com/gin-gonic/gin" ) -type Item struct { - ID int `json:"id"` - Name string `json:"name"` - ViewCount int `json:"view_count"` +var items = []gin.H{ + {"id": 1, "name": "Galactic Goggles"}, + {"id": 2, "name": "Meteor Muffins"}, + {"id": 3, "name": "Alien Antenna Kit"}, + {"id": 4, "name": "Starlight Lantern"}, + {"id": 5, "name": "Quantum Quill"}, } -var ( - items = []Item{ - {ID: 1, Name: "Galactic Goggles"}, - {ID: 2, Name: "Meteor Muffins"}, - {ID: 3, Name: "Alien Antenna Kit"}, - {ID: 4, Name: "Starlight Lantern"}, - {ID: 5, Name: "Quantum Quill"}, - } - mu sync.Mutex -) - func main() { router := gin.Default() router.GET("/", greet) router.GET("/items", getItems) - router.GET("/items/:id", getItemByID) router.POST("/items", addItem) router.HEAD("/healthcheck", healthcheck) @@ -50,31 +37,6 @@ func getItems(c *gin.Context) { c.JSON(http.StatusOK, items) } -func getItemByID(c *gin.Context) { - id, err := strconv.Atoi(c.Param("id")) - if err != nil { - c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid item ID"}) - return - } - - var item *Item - for i := range items { - if items[i].ID == id { - item = &items[i] - break - } - } - - if item == nil { - c.JSON(http.StatusNotFound, gin.H{"error": "Item not found"}) - return - } - - go incrementViewCount(item) - - c.JSON(http.StatusOK, item) -} - func addItem(c *gin.Context) { var newItem struct { Name string `json:"name" binding:"required"` @@ -86,14 +48,8 @@ func addItem(c *gin.Context) { } id := len(items) + 1 - item := Item{ID: id, Name: newItem.Name} + item := gin.H{"id": id, "name": newItem.Name} items = append(items, item) c.JSON(http.StatusOK, item) } - -func incrementViewCount(item *Item) { - mu.Lock() - defer mu.Unlock() - item.ViewCount++ -}