From 029fe2c7a3532bc3bf435f257cdade240c8f3568 Mon Sep 17 00:00:00 2001 From: Vidhu Kant Sharma Date: Sun, 12 Oct 2025 23:28:54 +0530 Subject: added get single item route --- item/controller.go | 20 ++++++++++++++++++++ item/router.go | 1 + item/service.go | 15 +++++++++++++++ main.go | 2 +- 4 files changed, 37 insertions(+), 1 deletion(-) 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) diff --git a/main.go b/main.go index 26982d8..3cfb23f 100644 --- a/main.go +++ b/main.go @@ -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") { -- cgit v1.2.3