aboutsummaryrefslogtreecommitdiff
path: root/invoice/service.go
diff options
context:
space:
mode:
authorVidhu Kant Sharma <vidhukant@vidhukant.com>2023-10-09 22:11:16 +0530
committerVidhu Kant Sharma <vidhukant@vidhukant.com>2023-10-09 22:11:16 +0530
commit48845b9e703756471a98f8b1f1edaa2313763df4 (patch)
tree1883095a68148bad0d0e5daff818616e5137a3c3 /invoice/service.go
parent1924bfca2439829253df3598481034e5c586e3e2 (diff)
checking user while adding and removing invoice itemsv0.0.13
Diffstat (limited to 'invoice/service.go')
-rw-r--r--invoice/service.go45
1 files changed, 44 insertions, 1 deletions
diff --git a/invoice/service.go b/invoice/service.go
index cfb873f..099f6a0 100644
--- a/invoice/service.go
+++ b/invoice/service.go
@@ -22,7 +22,7 @@ import (
)
func getInvoice(invoice *Invoice, id uint) error {
- res := db.Preload("BillingAddress").Preload("ShippingAddress").Find(&invoice, id)
+ res := db.Preload("BillingAddress").Preload("ShippingAddress").Preload("Items").Find(&invoice, id)
// TODO: handle potential errors
if res.Error != nil {
@@ -75,6 +75,49 @@ func (i *Invoice) del() error {
return nil
}
+// also checks for ownership
+func getItemInvoice(itemId, userId uint) (uint, error) {
+ var invoiceId uint
+ res := db.
+ Model(&InvoiceItem{}).
+ Select("invoice_id").
+ Where("id = ?", itemId).
+ Find(&invoiceId)
+
+ // TODO: handle potential errors
+ if res.Error != nil {
+ return invoiceId, res.Error
+ }
+
+ if res.RowsAffected == 0 {
+ return invoiceId, e.ErrNotFound
+ }
+
+ err := checkInvoiceOwnership(invoiceId, userId)
+
+ if err != nil {
+ return invoiceId, err
+ }
+
+ return invoiceId, nil
+}
+
+func (i *InvoiceItem) del() error {
+ res := db.Delete(i)
+
+ // TODO: handle potential errors
+ if res.Error != nil {
+ return res.Error
+ }
+
+ // returns 404 if either row doesn't exist or if the user doesn't own it
+ if res.RowsAffected == 0 {
+ return e.ErrNotFound
+ }
+
+ return nil
+}
+
func (i *InvoiceItem) upsert() error {
res := db.Save(i)
// TODO: handle potential errors