diff options
author | Vidhu Kant Sharma <vidhukant@vidhukant.com> | 2025-10-12 23:28:54 +0530 |
---|---|---|
committer | Vidhu Kant Sharma <vidhukant@vidhukant.com> | 2025-10-12 23:28:54 +0530 |
commit | 029fe2c7a3532bc3bf435f257cdade240c8f3568 (patch) | |
tree | e4b0488af6b867e3065a11af8d706d139c7176a9 | |
parent | fd863aadf34b9837bd77587c1d36eb49000a8de7 (diff) |
added get single item routev0.21.0
-rw-r--r-- | item/controller.go | 20 | ||||
-rw-r--r-- | item/router.go | 1 | ||||
-rw-r--r-- | item/service.go | 15 | ||||
-rw-r--r-- | main.go | 2 |
4 files changed, 37 insertions, 1 deletions
diff --git a/item/controller.go b/item/controller.go index b68f2aa..3df95c3 100644 --- a/item/controller.go +++ b/item/controller.go @@ -24,6 +24,26 @@ import ( "strconv" ) +func handleGetSingleItem (ctx *gin.Context) { + id, err := strconv.ParseUint(ctx.Param("id"), 10, 64) + if err != nil { + ctx.Error(e.ErrInvalidID) + return + } + + var item SavedItem + err = getItem(&item, uint(id)) + if err != nil { + ctx.Error(err) + ctx.Abort() + return + } + + ctx.JSON(http.StatusOK, gin.H{ + "data": item, + }) +} + func handleGetItems (ctx *gin.Context) { var items []SavedItem diff --git a/item/router.go b/item/router.go index a8a3f16..7e06019 100644 --- a/item/router.go +++ b/item/router.go @@ -25,6 +25,7 @@ func Routes(route *gin.RouterGroup) { i := route.Group("/item") { i.GET("/", handleGetItems) + i.GET("/:id", handleGetSingleItem) i.POST("/", handleSaveItem) i.DELETE("/:id", handleDelItem) } diff --git a/item/service.go b/item/service.go index b7d3490..7a622cb 100644 --- a/item/service.go +++ b/item/service.go @@ -21,6 +21,21 @@ import ( "vidhukant.com/openbills/errors" ) +func getItem(item *SavedItem, id uint) error { + res := db.Find(&item, id) + + // TODO: handle potential errors + if res.Error != nil { + return res.Error + } + + if res.RowsAffected == 0 { + return errors.ErrNotFound + } + + return nil +} + func getItems(items *[]SavedItem) error { res := db.Find(&items) @@ -38,7 +38,7 @@ import ( "log" ) -const OPENBILLS_VERSION = "v0.20.0" +const OPENBILLS_VERSION = "v0.21.0" func init() { if !viper.GetBool("debug_mode") { |