Skip to content

Commit

Permalink
Merge pull request #7 from Wilcolab/go-first-issue
Browse files Browse the repository at this point in the history
add popular item route
  • Loading branch information
razramon authored Nov 28, 2024
2 parents 88465cb + ccfc55e commit 8c7a2cb
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion backend/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func main() {
router.GET("/items", getItems)
router.GET("/items/:id", getItemById)
router.POST("/items", postItems)

router.GET("/items/popular", getMostPopularItem)
router.Run()
}

Expand Down Expand Up @@ -64,6 +64,18 @@ func getItemById(c *gin.Context){
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "item not found"})
}

func getMostPopularItem(c *gin.Context){
max_views := -1
id := -1
for indx, item := range items {
if item.Views > max_views{
max_views = item.Views
id = indx
}
}
c.IndentedJSON(http.StatusOK, items[id])
}

func greet(c *gin.Context) {
c.IndentedJSON(http.StatusOK, "Welcome, Go navigator, to the Anythink cosmic catalog.")
}
Expand Down

0 comments on commit 8c7a2cb

Please sign in to comment.