Skip to content

Commit

Permalink
add list view on grud api
Browse files Browse the repository at this point in the history
  • Loading branch information
philipphomberger committed Mar 29, 2024
1 parent e2f0ca5 commit 8ed7d9d
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
60 changes: 60 additions & 0 deletions controllers/album.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,63 @@ func GetAlbum() gin.HandlerFunc {
c.JSON(http.StatusCreated, responses.AlbumResponse{Status: http.StatusCreated, Message: "success", Data: map[string]interface{}{"data": result}})
}
}

func DelAlbum() gin.HandlerFunc {
return func(c *gin.Context) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
var result models.Album
objectId, err := primitive.ObjectIDFromHex(c.Param("id"))
filter := bson.D{{"_id", objectId}}
err = albumsCollection.FindOneAndDelete(ctx, filter).Decode(&result)
fmt.Println(result)
if err != nil {
if err == mongo.ErrNoDocuments {
return
}
panic(err)
}
c.JSON(http.StatusCreated, responses.AlbumResponse{Status: http.StatusCreated, Message: "success delete", Data: map[string]interface{}{"data": result}})
}
}

func PutAlbum() gin.HandlerFunc {
return func(c *gin.Context) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
var result models.Album
var album = models.Album{}

//validate the request body
if err := c.BindJSON(&album); err != nil {
c.JSON(http.StatusBadRequest, responses.AlbumResponse{Status: http.StatusBadRequest, Message: "error", Data: map[string]interface{}{"data": err.Error()}})
return
}

//use the validator library to validate required fields
if validationErr := validate.Struct(&album); validationErr != nil {
c.JSON(http.StatusBadRequest, responses.AlbumResponse{Status: http.StatusBadRequest, Message: "error", Data: map[string]interface{}{"data": validationErr.Error()}})
return
}

newAlbum := models.Album{
ID: primitive.NewObjectID(),
Title: album.Title,
Artist: album.Artist,
Price: album.Price,
}

objectId, err := primitive.ObjectIDFromHex(c.Param("id"))
filter := bson.D{{"_id", objectId}}
err = albumsCollection.FindOneAndReplace(ctx, filter, newAlbum).Decode(&result)

if err != nil {
if err == mongo.ErrNoDocuments {
return
}
panic(err)
}

c.JSON(http.StatusCreated, responses.AlbumResponse{Status: http.StatusCreated, Message: "success replaced", Data: map[string]interface{}{"data": newAlbum}})
}
}
2 changes: 2 additions & 0 deletions routes/albums.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ func AlbumsRoute(router *gin.Engine) {
router.POST("/albums", controllers.CreateAlbum())
router.GET("/albums", controllers.GetAlbums())
router.GET("/albums/:id", controllers.GetAlbum())
router.DELETE("/albums/:id", controllers.DelAlbum())
router.PUT("/albums/:id", controllers.PutAlbum())
}

0 comments on commit 8ed7d9d

Please sign in to comment.