Compare commits
No commits in common. "6171344b65c5c05b8dc8b614c5f45ea9b12d0ca1" and "1517fdaa099a0dcbf2a59e14a8bd2e10aeee5e61" have entirely different histories.
6171344b65
...
1517fdaa09
42
main.go
42
main.go
@ -8,17 +8,17 @@ import (
|
||||
|
||||
// album represents data about a record album.
|
||||
type album struct {
|
||||
ID string `json:"id"`
|
||||
Title string `json:"title"`
|
||||
Description string `json:"description"`
|
||||
Filepath string `json:"filepath"`
|
||||
ID string `json:"id"`
|
||||
Title string `json:"title"`
|
||||
Artist string `json:"artist"`
|
||||
Price string `json:"price"`
|
||||
}
|
||||
|
||||
// albums slice to seed record album data.
|
||||
var albums = []album{
|
||||
{ID: "1", Title: "Blue Train", Description: "John Coltrane", Filepath: "56.99"},
|
||||
{ID: "2", Title: "Jeru", Description: "Gerry Mulligan", Filepath: "17.99"},
|
||||
{ID: "3", Title: "Sarah Vaughan and Clifford Brown", Description: "Sarah Vaughan", Filepath: "39.99"},
|
||||
{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"},
|
||||
}
|
||||
|
||||
func remove(slice []album, s int) []album {
|
||||
@ -27,8 +27,6 @@ func remove(slice []album, s int) []album {
|
||||
|
||||
func main() {
|
||||
router := gin.Default()
|
||||
router.SetTrustedProxies(nil)
|
||||
|
||||
router.GET("/albums", getAlbums)
|
||||
router.GET("/albums/:id", getAlbumByID)
|
||||
router.POST("/albums", postAlbums)
|
||||
@ -41,7 +39,7 @@ func main() {
|
||||
|
||||
// getAlbums responds with the list of all albums as JSON.
|
||||
func getAlbums(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, albums)
|
||||
c.IndentedJSON(http.StatusOK, albums)
|
||||
}
|
||||
|
||||
// postAlbums adds an album from JSON received in the request body.
|
||||
@ -51,20 +49,12 @@ func postAlbums(c *gin.Context) {
|
||||
// Call BindJSON to bind the received JSON to
|
||||
// newAlbum.
|
||||
if err := c.BindJSON(&newAlbum); err != nil {
|
||||
c.Status(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
for _, a := range albums {
|
||||
if a == newAlbum {
|
||||
c.Status(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// Add the new album to the slice.
|
||||
albums = append(albums, newAlbum)
|
||||
c.JSON(http.StatusCreated, newAlbum)
|
||||
c.IndentedJSON(http.StatusCreated, newAlbum)
|
||||
}
|
||||
|
||||
// getAlbumByID locates the album whose ID value matches the id
|
||||
@ -76,16 +66,16 @@ func getAlbumByID(c *gin.Context) {
|
||||
// an album whose ID value matches the parameter.
|
||||
for _, a := range albums {
|
||||
if a.ID == id {
|
||||
c.JSON(http.StatusOK, a)
|
||||
c.IndentedJSON(http.StatusOK, a)
|
||||
return
|
||||
}
|
||||
}
|
||||
c.JSON(http.StatusNotFound, gin.H{"message": "album not found"})
|
||||
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "album not found"})
|
||||
}
|
||||
|
||||
func delAlbums(c *gin.Context) {
|
||||
albums = []album{}
|
||||
c.JSON(http.StatusOK, albums)
|
||||
c.IndentedJSON(http.StatusOK, albums)
|
||||
}
|
||||
|
||||
func delAlbumByID(c *gin.Context) {
|
||||
@ -94,11 +84,11 @@ func delAlbumByID(c *gin.Context) {
|
||||
for s, a := range albums {
|
||||
if a.ID == id {
|
||||
albums = remove(albums, s)
|
||||
c.JSON(http.StatusCreated, albums)
|
||||
c.IndentedJSON(http.StatusOK, albums)
|
||||
return
|
||||
}
|
||||
}
|
||||
c.JSON(http.StatusNotFound, gin.H{"message": "album not found"})
|
||||
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "album not found"})
|
||||
}
|
||||
|
||||
func updateAlbum(c *gin.Context) {
|
||||
@ -114,9 +104,9 @@ func updateAlbum(c *gin.Context) {
|
||||
if a.ID == id {
|
||||
albums = remove(albums, s)
|
||||
albums = append(albums, newAlbum)
|
||||
c.JSON(http.StatusCreated, newAlbum)
|
||||
c.IndentedJSON(http.StatusOK, newAlbum)
|
||||
return
|
||||
}
|
||||
}
|
||||
c.JSON(http.StatusNotFound, gin.H{"message": "album not found"})
|
||||
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "album not found"})
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user