aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
-rw-r--r--item/controller.go20
-rw-r--r--item/router.go1
-rw-r--r--item/service.go15
-rw-r--r--main.go2
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)
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") {