Work
This commit is contained in:
+15
-13
@@ -32,7 +32,7 @@ func (invoicelist *InvoiceList) GetInvoicesWithProject(project string) *InvoiceL
|
||||
// Filter filters the current InvoiceList for the given predicate (a function that takes an invoice as input and returns a
|
||||
// bool), and returns a new InvoiceList. The original InvoiceList is not modified.
|
||||
func (invoicelist *InvoiceList) Filter(predicate func(*Invoice) bool) *InvoiceList {
|
||||
var newList ProjectList
|
||||
var newList InvoiceList
|
||||
for _, t := range invoicelist.Invoices {
|
||||
if predicate(t) {
|
||||
newList.AddInvoice(t)
|
||||
@@ -41,25 +41,27 @@ func (invoicelist *InvoiceList) Filter(predicate func(*Invoice) bool) *InvoiceLi
|
||||
return &newList
|
||||
}
|
||||
|
||||
func (invoicelist *InvoiceList) GetNextId() int {
|
||||
nextId := 0
|
||||
for _, i := range invoicelist.Invoices {
|
||||
if i.ID > nextId {
|
||||
nextId = i.ID
|
||||
}
|
||||
// ForEach runs the given function passing it each invoice in the list
|
||||
func (invoicelist *InvoiceList) ForEach(run func(*Invoice)) {
|
||||
for _, t := range invoicelist.Invoices {
|
||||
run(t)
|
||||
}
|
||||
return nextId + 1
|
||||
}
|
||||
|
||||
// AddInvoice prepe
|
||||
// AddInvoice prepepends an Invoice to the current InvoiceList
|
||||
func (invoicelist *InvoiceList) AddInvoice(invoice *Invoice) {
|
||||
invoicelist.Invoices = append(invoicelist.Invoices, invoice)
|
||||
}
|
||||
func (invoicelist *InvoiceList) AddInvoices(invoices []*Invoice) {
|
||||
for _, v := range invoices {
|
||||
invoicelist.AddInvoice(v)
|
||||
}
|
||||
}
|
||||
func (invoicelist *InvoiceList) Combine(other *InvoiceList) { invoicelist.AddInvoices(other.Invoices) }
|
||||
|
||||
// GetInvoice returns the Invoice with the given id from the invoice list
|
||||
// Returns an error if the invoice could not be found
|
||||
func (invoicelist *InvoiceList) GetInvoice(id int) (*Invoice, error) {
|
||||
func (invoicelist *InvoiceList) GetInvoice(id string) (*Invoice, error) {
|
||||
for i := range invoicelist.Invoices {
|
||||
if invoicelist.Invoices[i].ID == id {
|
||||
return invoicelist.Invoices[i], nil
|
||||
@@ -70,7 +72,7 @@ func (invoicelist *InvoiceList) GetInvoice(id int) (*Invoice, error) {
|
||||
|
||||
// RemoveInvoice removes any Invoice with given Invoice id from the InvoiceList.
|
||||
// Returns an error if no invoice was removed.
|
||||
func (invoicelist *InvoiceList) RemoveInvoice(id int) error {
|
||||
func (invoicelist *InvoiceList) RemoveInvoice(id string) error {
|
||||
var found bool
|
||||
var remIdx int
|
||||
var i *Invoice
|
||||
@@ -94,7 +96,7 @@ func (invoicelist *InvoiceList) ArchiveInvoiceToFile(invoice Invoice, filename s
|
||||
if err := invoicelist.RemoveInvoice(invoice.ID); err != nil {
|
||||
return err
|
||||
}
|
||||
f, err := os.Open(filename, os.O_APPEND|os.O_WRONLY, 0600)
|
||||
f, err := os.Open(filename)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -139,7 +141,7 @@ func (invoicelist *InvoiceList) WriteToFile(file *os.File) error {
|
||||
|
||||
// LoadFromFilename loads an InvoiceList from the filename
|
||||
// (it piggybacks on LoadFromFile)
|
||||
func (invoicelist *InvoiceList) LoadFromFilename(filename strinng) error {
|
||||
func (invoicelist *InvoiceList) LoadFromFilename(filename string) error {
|
||||
file, err := os.Open(filename)
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
Reference in New Issue
Block a user