From 1924bfca2439829253df3598481034e5c586e3e2 Mon Sep 17 00:00:00 2001 From: Vidhu Kant Sharma Date: Mon, 9 Oct 2023 21:07:20 +0530 Subject: added route to add items to invoice --- invoice/controller.go | 25 +++++++++++++++++++++++++ invoice/hooks.go | 15 +++++++++++++++ invoice/invoice.go | 13 +++++++++++-- invoice/router.go | 2 ++ invoice/service.go | 8 ++++++++ invoice/validators.go | 26 +++++++++++++++++++++----- 6 files changed, 82 insertions(+), 7 deletions(-) (limited to 'invoice') diff --git a/invoice/controller.go b/invoice/controller.go index efcaa40..354ae21 100644 --- a/invoice/controller.go +++ b/invoice/controller.go @@ -151,3 +151,28 @@ func handleDelInvoice (ctx *gin.Context) { "message": "success", }) } + +func addItem (ctx *gin.Context) { + id, err := strconv.ParseUint(ctx.Param("id"), 10, 64) + if err != nil { + ctx.Error(e.ErrInvalidID) + return + } + + var item InvoiceItem + ctx.Bind(&item) + + item.InvoiceID = uint(id) + + err = item.upsert() + if err != nil { + ctx.Error(err) + ctx.Abort() + return + } + + ctx.JSON(http.StatusOK, gin.H{ + "message": "success", + "data": item, + }) +} diff --git a/invoice/hooks.go b/invoice/hooks.go index 3d933fb..686500a 100644 --- a/invoice/hooks.go +++ b/invoice/hooks.go @@ -55,3 +55,18 @@ func (i *Invoice) BeforeDelete(tx *gorm.DB) error { return nil } + +func (i *InvoiceItem) BeforeSave(tx *gorm.DB) error { + var err error + + isDraft, err := isDraft(i.InvoiceID) + if err != nil { + return err + } + + if !isDraft { + return errors.ErrCannotEditInvoice + } + + return nil +} diff --git a/invoice/invoice.go b/invoice/invoice.go index 0f4a601..fa8bb08 100644 --- a/invoice/invoice.go +++ b/invoice/invoice.go @@ -22,6 +22,7 @@ import ( d "vidhukant.com/openbills/db" "vidhukant.com/openbills/user" c "vidhukant.com/openbills/customer" + i "vidhukant.com/openbills/item" "time" ) @@ -29,7 +30,7 @@ var db *gorm.DB func init() { db = d.DB - db.AutoMigrate(&Invoice{}, &InvoiceBillingAddress{}, &InvoiceShippingAddress{}) + db.AutoMigrate(&Invoice{}, &InvoiceItem{}, &InvoiceBillingAddress{}, &InvoiceShippingAddress{}) } type InvoiceBillingAddress struct { @@ -44,6 +45,14 @@ type InvoiceShippingAddress struct { InvoiceID uint } +type InvoiceItem struct { + gorm.Model + i.Item + InvoiceID uint + BrandName string + Quantity string // float +} + type Invoice struct { gorm.Model UserID uint `json:"-"` @@ -52,7 +61,7 @@ type Invoice struct { InvoiceNumber uint BillingAddress InvoiceBillingAddress ShippingAddress InvoiceShippingAddress - Draft bool + IsDraft bool // Transporter Transporter // DueDate string diff --git a/invoice/router.go b/invoice/router.go index 5536466..e231c0d 100644 --- a/invoice/router.go +++ b/invoice/router.go @@ -28,5 +28,7 @@ func Routes(route *gin.RouterGroup) { g.GET("/:id", handleGetSingleInvoice) g.POST("/", handleSaveInvoice) g.DELETE("/:id", handleDelInvoice) + g.POST("/:id/item", addItem) + //g.DELETE("/:invoice_id/item/:item_id", handleDelInvoice) } } diff --git a/invoice/service.go b/invoice/service.go index 6b59949..cfb873f 100644 --- a/invoice/service.go +++ b/invoice/service.go @@ -51,6 +51,8 @@ func getInvoices(invoices *[]Invoice, userId uint) error { return nil } +// TODO: route to only get the invouce's items + func (i *Invoice) upsert() error { res := db.Save(i) // TODO: handle potential errors @@ -72,3 +74,9 @@ func (i *Invoice) del() error { return nil } + +func (i *InvoiceItem) upsert() error { + res := db.Save(i) + // TODO: handle potential errors + return res.Error +} diff --git a/invoice/validators.go b/invoice/validators.go index 645bdff..9f145dc 100644 --- a/invoice/validators.go +++ b/invoice/validators.go @@ -18,12 +18,7 @@ package invoice import ( - //"regexp" - //"strings" - //"net/mail" - //"net/url" "vidhukant.com/openbills/errors" - //e "errors" ) func (i *Invoice) validate() error { @@ -44,6 +39,27 @@ func (i *Invoice) validate() error { return nil } +func isDraft(invoiceId uint) (bool, error) { + var invoice Invoice + err := db. + Select("id", "is_draft"). + Where("id = ?", invoiceId). + Find(&invoice). + Error + + // TODO: handle potential errors + if err != nil { + return invoice.IsDraft, err + } + + // invoice doesn't exist + if invoice.ID == 0 { + return invoice.IsDraft, errors.ErrNotFound + } + + return invoice.IsDraft, nil +} + func checkInvoiceOwnership(invoiceId, userId uint) error { var invoice Invoice err := db. -- cgit v1.2.3