diff options
Diffstat (limited to 'invoice/controller.go')
-rw-r--r-- | invoice/controller.go | 124 |
1 files changed, 123 insertions, 1 deletions
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, + }) +} |