models

package
v0.0.0-...-b44a069 Latest Latest
Warning

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

Go to latest
Published: Aug 20, 2015 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNegativePence is returned when a Pence with a negative values is
	// given to a function where it must not be negative
	ErrNegativePence = errors.New("Pence must not be negative")

	// ErrInvalidMoneyStr indicates that a string used to represent money
	// could not be parsed and converted to Pence
	ErrInvalidMoneyStr = errors.New("Invalid string passed to PenceFromString")

	// ErrStructNotSaved is returned when an operation is performed on a struct
	// that must first be saved to the database
	ErrStructNotSaved = errors.New("Invalid operation: struct must be saved first")

	ErrMustAssignToUsers = errors.New("There must be a positive number of users to assign an expense")
)
View Source
var (
	ErrAlreadySaved = errors.New("Cannot insert as model as already saved")
)

Functions

This section is empty.

Types

type ByExpense

type ByExpense []*ExpenseAssignment

func (ByExpense) Len

func (a ByExpense) Len() int

func (ByExpense) Less

func (a ByExpense) Less(i, j int) bool

func (ByExpense) Swap

func (a ByExpense) Swap(i, j int)

type Category

type Category int

Category is used to group expenses

const (
	// CategoryGroceries indicates an expense spend on groceries
	// e.g. Boursin
	CategoryGroceries Category = iota
	// CategoryAlcohol indicates an expense spend on alcohol e.g. K Cider
	CategoryAlcohol
	// CategoryDrugs indicates an expense spend on medicine e.g cough
	// medicine
	CategoryDrugs
	// CategoryHouseholdItems indicates an expense spend on household items
	// e.g. An iron
	CategoryHouseholdItems
	// CategoryBills indicates an expense spend to on bill e.g. Netflix
	CategoryBills
	// CategoryPresents indicates an expense spent on presents e.g. singing
	// lessons
	CategoryPresents
	// CategoryTickets indicates an expense spent on tickets e.g. Balls Deep
	CategoryTickets
	// CategoryMisc is used for expenses that do not fit into any of the
	// other categories
	CategoryMisc
	// CategoryUnknown is not a category that should be used and indicates
	// a logic error.
	CategoryUnknown //Should always be last
)

func AllCategories

func AllCategories() []Category

AllCategories returns a slice containing all the valid categories

func StringToCategory

func StringToCategory(s string) Category

StringToCategory converts a string to the appropriate category. This performs a lower case compare and strips any surrounding double quote characters.

func (*Category) Scan

func (c *Category) Scan(src interface{}) error

func (Category) String

func (c Category) String() string

func (Category) Validate

func (c Category) Validate() error

Validate ensures the Category is valid

func (Category) Value

func (c Category) Value() (driver.Value, error)

type Expense

type Expense struct {
	ID          int64                `db:"id" json:"id"`
	Amount      Pence                `db:"amount" json:"amount"`
	PayerID     int64                `db:"payer_id" json:"payerId"`
	GroupID     int64                `db:"group_id" json:"groupId"`
	Category    Category             `db:"category" json:"category"`
	Description string               `db:"description" json:"category"`
	CreatedAt   time.Time            `db:"created_at" json:"category"`
	Assignments []*ExpenseAssignment `db:"-" json:"assignments"`
}

Expense represents an expense made that is to be shared with the group

func (*Expense) Assign

func (e *Expense) Assign(userIds []int64) ([]*ExpenseAssignment, error)

Assign assigns an expense to the users given. If the amount is not equally divisable by the number of users the expense applies to, then the remaining amount is assigned at random.

type ExpenseAssignment

type ExpenseAssignment struct {
	ID        int64 `db:"id" json:"id"`
	UserID    int64 `db:"user_id" json:"userId"`
	Amount    Pence `db:"amount" json:"amount"`
	ExpenseID int64 `db:"expense_id" json:"expenseId"`
	GroupID   int64 `db:"group_id" json:"groupId"`
}

ExpenseAssignment represents the amount of money assigned to each user when an expense is made.

type Group

type Group struct {
	ID   int64  `db:"id" json:"id"`
	Name string `db:"name" json:"name"`
}

Group represents a group of users in which the expenses are shared. An example of this would be housemates sharing the expenses incurred while living together, such as shared meals and communal home items.

type Manager

type Manager struct {
	// contains filtered or unexported fields
}

Manager contains the methods that are available to the models in the. The manager needs to be created with a Storer interface, which deals with the persistence of the structs. Actions built on these persistence methods are available for use, for example in HTTP handlers.

func NewManager

func NewManager(s Storer) *Manager

NewManager creates a new instance of the Manager object.

func (Manager) AddUserToGroup

func (m Manager) AddUserToGroup(g *Group, u *auth.User, admin bool) error

AddUserToGroup associates a user to the group. This is done internally by creating a mapping between the user and the group.

func (Manager) AllGroups

func (m Manager) AllGroups() ([]*Group, error)

AllGroups retrieves all the groups in the database.

func (Manager) DeleteExpense

func (m Manager) DeleteExpense(e *Expense) error

DeleteExpense removes an expense and any assignments associated with the expense. This must be transactional.

func (Manager) DeleteGroup

func (m Manager) DeleteGroup(g *Group) error

DeleteGroup removes the group from storage and any mappings to the members of the group.

func (Manager) DeletePayment

func (m Manager) DeletePayment(p *Payment) error

DeletePayment removes a Payment from storage

func (Manager) GroupByID

func (m Manager) GroupByID(id int64) (*Group, error)

GroupByID retrieves a group from persistence by the ID supplied.

func (Manager) GroupExpenses

func (m Manager) GroupExpenses(g *Group) ([]*Expense, error)

func (Manager) InsertPayment

func (m Manager) InsertPayment(g *Group, giver, receiver int64, amount Pence) (*Payment, error)

InsertPayment persists a payment of money from one person to another within a group.

func (Manager) NewExpense

func (m Manager) NewExpense(g *Group, amount Pence, payer int64, cat Category, desc string, users []int64) (*Expense, error)

NewExpense creates, assigns and persists a new expense. The assignments should be created using AssignExpense. For consistency, the expense and the assignments need to occur transactionally. i.e. they must all be persisted, or all rollback. This is to ensure a consisent state in the database. This is the reason that AssignExpense cannot be used to persist the assignments, as this must be called within the transaction. This can only be guaranteed at the storage driver level (i.e. the implementation of the Storer interface)

func (Manager) NewGroup

func (m Manager) NewGroup(name string) (*Group, error)

NewGroup creates and persists a new group with the name supplied.

func (Manager) PaymentByID

func (m Manager) PaymentByID(id int64) (*Payment, error)

PaymentByID returns a payment object with the given ID.

func (Manager) RemoveUserFromGroup

func (m Manager) RemoveUserFromGroup(g *Group, u *auth.User) error

RemoveUserFromGroup dissociates a user from the group. Any expense assignments are deleted. Payments remain as these apply to other in the group, but this user must not be taken into account in any calculations

func (Manager) UpdateExpense

func (m Manager) UpdateExpense(e *Expense, users []int64) error

UpdateExpense saves any changes to the expense. If there are changes made to the amount or number of people, then all previous assignments must be removed and this must be reassigned. This must all happen within a transaction.

func (Manager) UpdateGroup

func (m Manager) UpdateGroup(g *Group) error

UpdateGroup saves any changes made to the group object. If the ID has been changed then this will fail or over-write another existing group!

func (Manager) UpdatePayment

func (m Manager) UpdatePayment(p *Payment) error

UpdatePayment saves any modifications to the payment

func (Manager) UserGroups

func (m Manager) UserGroups(u *auth.User) ([]*Group, error)

type Payment

type Payment struct {
	ID         int64     `db:"id" json:"id"`
	GroupID    int64     `db:"group_id" json:"groupId"`
	Amount     Pence     `db:"amount" json:"amount"`
	GiverID    int64     `db:"giver_id" json:"giverId"`
	ReceiverID int64     `db:"receiver_id" json:"receieverId"`
	CreatedAt  time.Time `db:"created_at" json:"createdAt"`
}

Payment represent a transfer of money from one person to another in the group. This is typically performed when one person is at a deficit overall to the group and another has paid a surplus with expenses.

type Pence

type Pence int64

Pence is an amount of money used in Payments & Expenses. There are 100 Pence in a Pound (Sterling)

func PenceFromString

func PenceFromString(s string) (Pence, error)

PenceFromString parses a string and returns the amount of pence.

func (*Pence) Scan

func (p *Pence) Scan(src interface{}) error

func (Pence) String

func (p Pence) String() string

String formats pence into a human readable string

func (Pence) Validate

func (p Pence) Validate() error

Validate ensures that the pence is positive.

func (Pence) Value

func (p Pence) Value() (driver.Value, error)

type Storer

type Storer interface {
	// Group storage functions
	InsertGroup(*Group) error
	UpdateGroup(*Group) error
	DeleteGroup(*Group) error
	GroupByID(int64) (*Group, error)
	AddUserToGroup(*Group, *auth.User, bool) error
	RemoveUserFromGroup(*Group, *auth.User) error
	ExpensesByGroup(*Group) ([]*Expense, error)
	GroupsByUser(*auth.User) ([]*Group, error)
	AllGroups() ([]*Group, error)

	// Expense storage functions
	InsertExpense(*Expense, []int64) error // Need to fill in Id and Assignments
	UpdateExpense(*Expense, []int64) error
	ExpenseByID(int64) (*Expense, error)
	DeleteExpense(*Expense) error

	// Payment storage functions
	InsertPayment(*Payment) error
	UpdatePayment(*Payment) error
	DeletePayment(*Payment) error
	PaymentByID(int64) (*Payment, error)
}

Storer is the interface required in order to perform the actions required to store persist the models defined in the package. Any particular behavior that is not described by the type system will be explained in the comments above each method.

type UserGroupMap

type UserGroupMap struct {
	ID      int64 `db:"id" json:"id"`
	GroupID int64 `db:"group_id" json:"groupId"`
	UserID  int64 `db:"user_id" json:"userId"`
	Admin   bool  `db:"admin" json:"admin"`
}

UserGroupMap represents the database structure mapping users and groups. This is a many-to-many relationship. The Admin flag represents whether the particular user is an admin of the group. An admin has the ability (soon) to be able to add and remove people from the group.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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