diff options
Diffstat (limited to 'invoice/service.go')
| -rw-r--r-- | invoice/service.go | 42 | 
1 files changed, 10 insertions, 32 deletions
diff --git a/invoice/service.go b/invoice/service.go index 163b21e..55b1319 100644 --- a/invoice/service.go +++ b/invoice/service.go @@ -22,18 +22,12 @@ import (  )  // returns greatest invoice number + 1 -func getNewInvoiceNumber(userId uint) (uint, error) { -	var i uint - +func getNewInvoiceNumber() (uint, error) {  	// check if number of invoices is 0  	var count int64 -	err := db.Model(&Invoice{}). -		Where("user_id = ?", userId). -		Count(&count). -		Error - +	err := db.Model(&Invoice{}).Count(&count).Error  	if err != nil { -		return i, err +		return 0, err  	}  	// if no records exist, then invoice number should be 1 @@ -45,7 +39,8 @@ func getNewInvoiceNumber(userId uint) (uint, error) {  	// NOTE: if there are gaps in invoice numbers,  	// they won't be filled and the series would continue  	// from the greatest invoice number. -	row := db.Model(&Invoice{}).Where("user_id = ?", userId).Select("max(invoice_number)").Row() +	var i uint +	row := db.Model(&Invoice{}).Select("max(invoice_number)").Row()  	err = row.Scan(&i)  	return i + 1, err @@ -66,8 +61,8 @@ func getInvoice(invoice *Invoice, id uint) error {  	return nil  } -func getInvoices(invoices *[]Invoice, userId uint, isDraft bool) error { -	res := db.Where("user_id = ? and is_draft = ?", userId, isDraft).Find(&invoices) +func getInvoices(invoices *[]Invoice, isDraft bool) error { +	res := db.Where("is_draft = ?", isDraft).Find(&invoices)  	// TODO: handle potential errors  	if res.Error != nil { @@ -118,14 +113,13 @@ func (i *Invoice) upsert() error {  }  func (i *Invoice) del() error { -	res := db.Where("id = ? and user_id = ?", i.ID, i.UserID).Delete(i) +	res := db.Where("id = ?", i.ID).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  	} @@ -133,8 +127,7 @@ func (i *Invoice) del() error {  	return nil  } -// also checks for ownership -func getItemInvoice(itemId, userId uint) (uint, error) { +func getItemInvoice(itemId uint) (uint, error) {  	var invoiceId uint  	res := db.  		Model(&InvoiceItem{}). @@ -151,17 +144,10 @@ func getItemInvoice(itemId, userId uint) (uint, error) {  		return invoiceId, e.ErrNotFound  	} -	err := checkInvoiceOwnership(invoiceId, userId) - -	if err != nil { -		return invoiceId, err -	} -  	return invoiceId, nil  } -// also checks for ownership -func getCustomFieldInvoice(fieldId, userId uint) (uint, error) { +func getCustomFieldInvoice(fieldId uint) (uint, error) {  	var invoiceId uint  	res := db.  		Model(&CustomField{}). @@ -178,12 +164,6 @@ func getCustomFieldInvoice(fieldId, userId uint) (uint, error) {  		return invoiceId, e.ErrNotFound  	} -	err := checkInvoiceOwnership(invoiceId, userId) - -	if err != nil { -		return invoiceId, err -	} -  	return invoiceId, nil  } @@ -195,7 +175,6 @@ func (i *InvoiceItem) del() error {  		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  	} @@ -211,7 +190,6 @@ func (c *CustomField) del() error {  		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  	}  |