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 --- errors/errors.go | 47 +++++++++++++++++++++++++---------------------- errors/status.go | 5 +++++ invoice/controller.go | 25 +++++++++++++++++++++++++ invoice/hooks.go | 15 +++++++++++++++ invoice/invoice.go | 13 +++++++++++-- invoice/router.go | 2 ++ invoice/service.go | 8 ++++++++ invoice/validators.go | 26 +++++++++++++++++++++----- item/item.go | 21 ++++++++++----------- main.go | 2 +- 10 files changed, 123 insertions(+), 41 deletions(-) diff --git a/errors/errors.go b/errors/errors.go index 9fed3a3..c16bd52 100644 --- a/errors/errors.go +++ b/errors/errors.go @@ -23,35 +23,38 @@ import ( var ( // 204 - ErrEmptyResponse = errors.New("No Records Found") + ErrEmptyResponse = errors.New("No Records Found") // 400 - ErrNoWhereCondition = errors.New("No Where Condition") - ErrInvalidID = errors.New("Invalid ID") - ErrEmptyContactName = errors.New("Contact Name Cannot Be Empty") - ErrInvalidGSTIN = errors.New("Invalid GSTIN") - ErrInvalidEmail = errors.New("Invalid E-Mail Address") - ErrInvalidPhone = errors.New("Invalid Phone Number") - ErrInvalidWebsite = errors.New("Invalid Website URL") - ErrEmptyBrandName = errors.New("Brand Name Cannot Be Empty") - ErrInvalidUnitPrice = errors.New("Invalid Unit Price") - ErrInvalidGSTPercentage = errors.New("Invalid GST Percentage") - ErrPasswordTooShort = errors.New("Password Is Too Short") - ErrPasswordTooLong = errors.New("Password Is Too Long") - ErrInvalidLoginMethod = errors.New("Login Method Can Only Be 'email' Or 'username'") + ErrNoWhereCondition = errors.New("No Where Condition") + ErrInvalidID = errors.New("Invalid ID") + ErrEmptyContactName = errors.New("Contact Name Cannot Be Empty") + ErrInvalidGSTIN = errors.New("Invalid GSTIN") + ErrInvalidEmail = errors.New("Invalid E-Mail Address") + ErrInvalidPhone = errors.New("Invalid Phone Number") + ErrInvalidWebsite = errors.New("Invalid Website URL") + ErrEmptyBrandName = errors.New("Brand Name Cannot Be Empty") + ErrInvalidUnitPrice = errors.New("Invalid Unit Price") + ErrInvalidGSTPercentage = errors.New("Invalid GST Percentage") + ErrPasswordTooShort = errors.New("Password Is Too Short") + ErrPasswordTooLong = errors.New("Password Is Too Long") + ErrInvalidLoginMethod = errors.New("Login Method Can Only Be 'email' Or 'username'") // 401 - ErrWrongPassword = errors.New("Wrong Password") - ErrInvalidAuthHeader = errors.New("Invalid Authorization Header") - ErrUnauthorized = errors.New("Unauthorized") - ErrSessionExpired = errors.New("Session Expired") + ErrWrongPassword = errors.New("Wrong Password") + ErrInvalidAuthHeader = errors.New("Invalid Authorization Header") + ErrUnauthorized = errors.New("Unauthorized") + ErrSessionExpired = errors.New("Session Expired") // 403 - ErrForbidden = errors.New("You Are Not Authorized To Access This Resource") + ErrForbidden = errors.New("You Are Not Authorized To Access This Resource") // 404 - ErrNotFound = errors.New("Not Found") - ErrBrandNotFound = errors.New("This Brand Does Not Exist") + ErrNotFound = errors.New("Not Found") + ErrBrandNotFound = errors.New("This Brand Does Not Exist") + + // 405 + ErrCannotEditInvoice = errors.New("This Invoice Cannot Be Edited Now") // 409 ErrNonUniqueGSTIN = errors.New("GSTIN Must Be Unique") @@ -64,5 +67,5 @@ var ( ErrNonUniqueInvoiceNumber = errors.New("Invoice Number Must Be Unique") // 500 - ErrInternalServerError = errors.New("Internal Server Error") + ErrInternalServerError = errors.New("Internal Server Error") ) diff --git a/errors/status.go b/errors/status.go index c9113a9..47820b5 100644 --- a/errors/status.go +++ b/errors/status.go @@ -64,6 +64,11 @@ func StatusCodeFromErr(err error) int { return http.StatusNotFound } + // 405 + if errors.Is(err, ErrCannotEditInvoice) { + return http.StatusMethodNotAllowed + } + // 409 if errors.Is(err, ErrNonUniqueGSTIN) || errors.Is(err, ErrNonUniquePhone) || 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. diff --git a/item/item.go b/item/item.go index 02e568e..3f911fa 100644 --- a/item/item.go +++ b/item/item.go @@ -38,20 +38,19 @@ type Brand struct { } type Item struct { - UserID uint `json:"-"` - User user.User `json:"-"` - BrandID uint - Brand Brand - UnitOfMeasure string // TODO: probably has to be a custom type - HasDecimalQuantity bool - Name string - Description string - HSN string - UnitPrice string // float - GSTPercentage string // float + Name string + Description string + HSN string + UnitOfMeasure string // TODO: probably has to be a custom type + UnitPrice string // float + GSTPercentage string // float } type SavedItem struct { gorm.Model Item + BrandID uint + Brand Brand + UserID uint `json:"-"` + User user.User `json:"-"` } diff --git a/main.go b/main.go index 91b2128..a1bd010 100644 --- a/main.go +++ b/main.go @@ -38,7 +38,7 @@ import ( "log" ) -const OPENBILLS_VERSION = "v0.0.11" +const OPENBILLS_VERSION = "v0.0.12" func init() { if viper.GetBool("production_mode") { -- cgit v1.2.3