diff options
author | Vidhu Kant Sharma <vidhukant@vidhukant.com> | 2023-10-09 22:11:16 +0530 |
---|---|---|
committer | Vidhu Kant Sharma <vidhukant@vidhukant.com> | 2023-10-09 22:11:16 +0530 |
commit | 48845b9e703756471a98f8b1f1edaa2313763df4 (patch) | |
tree | 1883095a68148bad0d0e5daff818616e5137a3c3 /invoice/controller.go | |
parent | 1924bfca2439829253df3598481034e5c586e3e2 (diff) |
checking user while adding and removing invoice itemsv0.0.13
Diffstat (limited to 'invoice/controller.go')
-rw-r--r-- | invoice/controller.go | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/invoice/controller.go b/invoice/controller.go index 354ae21..02bbaf5 100644 --- a/invoice/controller.go +++ b/invoice/controller.go @@ -28,6 +28,7 @@ func handleGetSingleInvoice (ctx *gin.Context) { id, err := strconv.ParseUint(ctx.Param("id"), 10, 64) if err != nil { ctx.Error(e.ErrInvalidID) + ctx.Abort() return } @@ -117,6 +118,7 @@ func handleDelInvoice (ctx *gin.Context) { id, err := strconv.ParseUint(ctx.Param("id"), 10, 64) if err != nil { ctx.Error(e.ErrInvalidID) + ctx.Abort() return } @@ -156,14 +158,31 @@ func addItem (ctx *gin.Context) { id, err := strconv.ParseUint(ctx.Param("id"), 10, 64) if err != nil { ctx.Error(e.ErrInvalidID) + ctx.Abort() + return + } + + uId, ok := ctx.Get("UserID") + if !ok { + ctx.Error(e.ErrUnauthorized) + ctx.Abort() return } + userId := uId.(uint) + var item InvoiceItem ctx.Bind(&item) item.InvoiceID = uint(id) + err = checkInvoiceOwnership(item.InvoiceID, userId) + if err != nil { + ctx.Error(err) + ctx.Abort() + return + } + err = item.upsert() if err != nil { ctx.Error(err) @@ -176,3 +195,45 @@ func addItem (ctx *gin.Context) { "data": item, }) } + +func removeItem (ctx *gin.Context) { + id, err := strconv.ParseUint(ctx.Param("id"), 10, 64) + if err != nil { + ctx.Error(e.ErrInvalidID) + ctx.Abort() + return + } + + uId, ok := ctx.Get("UserID") + if !ok { + ctx.Error(e.ErrUnauthorized) + ctx.Abort() + return + } + + userId := uId.(uint) + + var item InvoiceItem + item.ID = uint(id) + + invoiceId, err := getItemInvoice(item.ID, userId) + if err != nil { + ctx.Error(err) + ctx.Abort() + return + } + + item.InvoiceID = invoiceId + + err = item.del() + if err != nil { + ctx.Error(err) + ctx.Abort() + return + } + + ctx.JSON(http.StatusOK, gin.H{ + "message": "success", + "data": item, + }) +} |