transaction

package
v0.0.0-...-5e69f72 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 2, 2022 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

transaction provides a model for a single user transaction. It also provides a simple implementation of a sqlite table for storing and querying transactions.

Index

Constants

View Source
const (
	TableName  = "transactions"
	IDCol      = "ID"
	EntityCol  = "Entity"
	AmountCol  = "Amount"
	DateCol    = "Date"
	NoteCol    = "Note"
	DateLayout = "1/2/2006"
	// TODO: this should probably be configurable, but I currently only use US dollars
	Currency  = "$"
	Point     = "."
	Thousands = ","
)

Variables

This section is empty.

Functions

func Unix

func Unix(date string) (int64, error)

Unix converts a string of format M/D/YYYY and converts it to the appropriate Unix time in seconds. This function is useful for working with the "Transaction" type.

Types

type CSVReader

type CSVReader struct {
	*csv.Reader
}

func NewCSVReader

func NewCSVReader(r io.Reader) *CSVReader

func (*CSVReader) Read

func (cr *CSVReader) Read() (Transaction, error)

? Should I consider allowing headers to set the order?

func (*CSVReader) ReadAll

func (cr *CSVReader) ReadAll() ([]Transaction, error)

type CSVWriter

type CSVWriter struct {
	*csv.Writer
}

func NewCSVWriter

func NewCSVWriter(w io.Writer) *CSVWriter

func (*CSVWriter) Write

func (cw *CSVWriter) Write(tx Transaction) error

func (*CSVWriter) WriteAll

func (cw *CSVWriter) WriteAll(txs []Transaction) error

type Cent

type Cent int

Cent represents 1/100th of a Dollar

func GetCents

func GetCents(currency string) (Cent, error)

GetCents takes a currency string formatted as [$]X.XX and returns the number of cents that it represents

func (Cent) String

func (c Cent) String() string

String returns a string that represents the value of the given number of "cents".

type Rows

type Rows struct{ *sql.Rows }

Rows wraps *sql.Rows to easily scan Transactions from a DB

func (*Rows) Scan

func (r *Rows) Scan() (Transaction, error)

Scan scans a transaction from the current result set.

func (*Rows) ScanSet

func (r *Rows) ScanSet() ([]Transaction, error)

ScanSet scans up to "limit" transactions from a result set into a slice. Do not use ScanSet if you expect that that your result set will be very large.

type Table

type Table struct{ DB *sql.DB }

Table is the transactions table in a database

func (*Table) Init

func (t *Table) Init() error

Init creates the transactions table if it doesn't exist.

func (*Table) Insert

func (t *Table) Insert(tx Transaction) error

Insert inserts a transaction into the transactions table. The ID provided by "tx" is ignored, as the database determines the ID.

func (*Table) Range

func (t *Table) Range(start, end time.Time, limit int) (*Rows, error)

Range returns the transactions that occurred within the give range of time. It returns, at most, "limit" transactions, and returns them in chronological order. A negative "limit" will return as many transactions as are available.

func (*Table) RangeTotal

func (t *Table) RangeTotal(start, end time.Time) (Cent, error)

RangeTotal returns the cost of the transactions that occurred within the give range of time.

It uses, at most, "limit" transactions. A negative "limit" will use as many transactions as are available.

func (*Table) Remove

func (t *Table) Remove(transactionID int) error

Remove deletes the given transaction from the table.

func (*Table) Search

func (t *Table) Search(query string, limit int) (*Rows, error)

Search returns the most recent transactions that include the given "query". It returns, at most, "limit" transactions, and returns more recent transactions first. A negative "limit" will return as many transactions as are available.

func (*Table) Total

func (t *Table) Total() (Cent, error)

Total returns the total of all the transactions in the database ? will this become slow over time?

type Transaction

type Transaction struct {
	ID int
	// Entity is the person or company the transaction was made with.
	Entity string
	// Amount is the cost of the transaction in cents
	Amount Cent
	// Date is the Unix Time the transaction occurred in seconds.
	Date int64
	// Note is any note the user wants to add about the transaction.
	Note string
}

TODO: add a String() function Transaction represents a single transaction in a person's budget

func (Transaction) DateString

func (t Transaction) DateString() string

DateString returns the Transaction's date in M/D/YYYY format.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL