diff options
Diffstat (limited to 'invoice/service.go')
-rw-r--r-- | invoice/service.go | 66 |
1 files changed, 64 insertions, 2 deletions
diff --git a/invoice/service.go b/invoice/service.go index 91579c6..163b21e 100644 --- a/invoice/service.go +++ b/invoice/service.go @@ -52,7 +52,7 @@ func getNewInvoiceNumber(userId uint) (uint, error) { } func getInvoice(invoice *Invoice, id uint) error { - res := db.Preload("BillingAddress").Preload("ShippingAddress").Preload("Items").Find(&invoice, id) + res := db.Preload("BillingAddress").Preload("ShippingAddress").Preload("Items").Preload("CustomFields").Find(&invoice, id) // TODO: handle potential errors if res.Error != nil { @@ -96,7 +96,20 @@ func getInvoiceItems(items *[]InvoiceItem, invoiceId uint) error { return nil } -// TODO: route to only get the invouce's items +func getInvoiceCustomFields(customFields *[]CustomField, invoiceId uint) error { + res := db.Where("invoice_id = ?", invoiceId).Find(&customFields) + + // TODO: handle potential errors + if res.Error != nil { + return res.Error + } + + if res.RowsAffected == 0 { + return e.ErrEmptyResponse + } + + return nil +} func (i *Invoice) upsert() error { res := db.Save(i) @@ -147,6 +160,33 @@ func getItemInvoice(itemId, userId uint) (uint, error) { return invoiceId, nil } +// also checks for ownership +func getCustomFieldInvoice(fieldId, userId uint) (uint, error) { + var invoiceId uint + res := db. + Model(&CustomField{}). + Select("invoice_id"). + Where("id = ?", fieldId). + 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) @@ -163,8 +203,30 @@ func (i *InvoiceItem) del() error { return nil } +func (c *CustomField) del() error { + res := db.Delete(c) + + // 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 return res.Error } + +func (c *CustomField) upsert() error { + res := db.Save(c) + // TODO: handle potential errors + return res.Error +} |