-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
26 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,38 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
// album represents data about a record album. | ||
type album struct { | ||
ID string `json:"id"` | ||
Title string `json:"title"` | ||
Artist string `json:"artist"` | ||
Price float64 `json:"price"` | ||
} | ||
|
||
// albums slice to seed record album data. | ||
var albums = []album{ | ||
{ID: "1", Title: "Blue Train", Artist: "John Coltrane", Price: 56.99}, | ||
{ID: "2", Title: "Jeru", Artist: "Gerry Mulligan", Price: 17.99}, | ||
{ID: "3", Title: "Sarah Vaughan and Clifford Brown", Artist: "Sarah Vaughan", Price: 39.99}, | ||
} | ||
|
||
// getAlbums responds with the list of all albums as JSON. | ||
func getAlbums(c *gin.Context) { | ||
c.IndentedJSON(http.StatusOK, albums) | ||
func generatePreorderRelation(set []int) map[string]bool { | ||
preorderRelation := make(map[string]bool) | ||
for _, element := range set { | ||
preorderRelation[fmt.Sprintf("[%d,%d]", element, element)] = true | ||
for _, otherElement := range set { | ||
if element <= otherElement { | ||
preorderRelation[fmt.Sprintf("[%d,%d]", element, otherElement)] = true | ||
} | ||
} | ||
} | ||
return preorderRelation | ||
} | ||
|
||
// postAlbums adds an album from JSON received in the request body. | ||
func postAlbums(c *gin.Context) { | ||
var newAlbum album | ||
func main() { | ||
r := gin.Default() | ||
|
||
// Call BindJSON to bind the received JSON to | ||
// newAlbum. | ||
if err := c.BindJSON(&newAlbum); err != nil { | ||
r.POST("/preorder", func(c *gin.Context) { | ||
var set []int | ||
if err := c.BindJSON(&set); err != nil { | ||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request body"}) | ||
return | ||
} | ||
} | ||
|
||
// Add the new album to the slice. | ||
albums = append(albums, newAlbum) | ||
c.IndentedJSON(http.StatusCreated, newAlbum) | ||
} | ||
preorderRelation := generatePreorderRelation(set) | ||
c.JSON(http.StatusOK, preorderRelation) | ||
}) | ||
|
||
func main() { | ||
router := gin.Default() | ||
router.GET("/albums", getAlbums) | ||
router.POST("/albums", postAlbums) | ||
|
||
router.Run("localhost:8080") | ||
} | ||
r.Run() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters