From d0a44ff5cfad5d063929426e2420f6f0d55b1dbe Mon Sep 17 00:00:00 2001 From: Vidhu Kant Sharma Date: Tue, 9 Jul 2024 07:58:34 +0530 Subject: added custom fields support --- invoice/controller.go | 124 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 123 insertions(+), 1 deletion(-) (limited to 'invoice/controller.go') diff --git a/invoice/controller.go b/invoice/controller.go index d3dd51c..ad6df3e 100644 --- a/invoice/controller.go +++ b/invoice/controller.go @@ -210,7 +210,6 @@ func handleGetInvoiceItems (ctx *gin.Context) { userId := uId.(uint) - err = checkInvoiceOwnership(uint(id), userId) if err != nil { ctx.Error(err) @@ -315,3 +314,126 @@ func removeItem (ctx *gin.Context) { "data": item, }) } + +// get custom fields belonging to a certain invoice +func handleGetInvoiceCustomFields (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) + + err = checkInvoiceOwnership(uint(id), userId) + if err != nil { + ctx.Error(err) + ctx.Abort() + return + } + + var cf []CustomField + err = getInvoiceCustomFields(&cf, uint(id)) + if err != nil { + ctx.Error(err) + ctx.Abort() + return + } + + ctx.JSON(http.StatusOK, gin.H{ + "message": "success", + "data": cf, + }) +} + +func addCustomField (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 cf CustomField + ctx.Bind(&cf) + + cf.InvoiceID = uint(id) + + err = checkInvoiceOwnership(cf.InvoiceID, userId) + if err != nil { + ctx.Error(err) + ctx.Abort() + return + } + + err = cf.upsert() + if err != nil { + ctx.Error(err) + ctx.Abort() + return + } + + ctx.JSON(http.StatusOK, gin.H{ + "message": "success", + "data": cf, + }) +} + +func removeCustomField (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 cf CustomField + cf.ID = uint(id) + + invoiceId, err := getCustomFieldInvoice(cf.ID, userId) + if err != nil { + ctx.Error(err) + ctx.Abort() + return + } + + cf.InvoiceID = invoiceId + + err = cf.del() + if err != nil { + ctx.Error(err) + ctx.Abort() + return + } + + ctx.JSON(http.StatusOK, gin.H{ + "message": "success", + "data": cf, + }) +} -- cgit v1.2.3