Skip to content

Commit

Permalink
add internal views counter, using goroutines'
Browse files Browse the repository at this point in the history
  • Loading branch information
razramon committed Nov 28, 2024
1 parent 7a1abb3 commit 7cf1eda
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions backend/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
type Item struct {
ID int `json:"id"`
Name string `json:"name"`
Views int `json:"-"`
}

var items = []Item{
Expand Down Expand Up @@ -48,12 +49,15 @@ func postItems(c *gin.Context){
func getItemById(c *gin.Context){
id, err := strconv.Atoi(c.Param("id"))
if err != nil {
c.IndentedJSON(http.StatusInternalServerError, "Atoi failed")
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message":"strconv Atoi failed"})
return
}
for _, item := range items {
for indx, item := range items {
if item.ID == id {
c.IndentedJSON(http.StatusOK, item)
go func() {
items[indx].Views += 1
}()
c.IndentedJSON(http.StatusOK, items[indx])
return
}
}
Expand Down

0 comments on commit 7cf1eda

Please sign in to comment.