Documentation ¶
Index ¶
- Variables
- type ByExpense
- type Category
- type Expense
- type ExpenseAssignment
- type Group
- type Manager
- func (m Manager) AddUserToGroup(g *Group, u *auth.User, admin bool) error
- func (m Manager) AllGroups() ([]*Group, error)
- func (m Manager) DeleteExpense(e *Expense) error
- func (m Manager) DeleteGroup(g *Group) error
- func (m Manager) DeletePayment(p *Payment) error
- func (m Manager) GroupByID(id int64) (*Group, error)
- func (m Manager) GroupExpenses(g *Group) ([]*Expense, error)
- func (m Manager) InsertPayment(g *Group, giver, receiver int64, amount Pence) (*Payment, error)
- func (m Manager) NewExpense(g *Group, amount Pence, payer int64, cat Category, desc string, users []int64) (*Expense, error)
- func (m Manager) NewGroup(name string) (*Group, error)
- func (m Manager) PaymentByID(id int64) (*Payment, error)
- func (m Manager) RemoveUserFromGroup(g *Group, u *auth.User) error
- func (m Manager) UpdateExpense(e *Expense, users []int64) error
- func (m Manager) UpdateGroup(g *Group) error
- func (m Manager) UpdatePayment(p *Payment) error
- func (m Manager) UserGroups(u *auth.User) ([]*Group, error)
- type Payment
- type Pence
- type Storer
- type UserGroupMap
Constants ¶
This section is empty.
Variables ¶
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") )
var (
ErrAlreadySaved = errors.New("Cannot insert as model as already saved")
)
Functions ¶
This section is empty.
Types ¶
type ByExpense ¶
type ByExpense []*ExpenseAssignment
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 ¶
StringToCategory converts a string to the appropriate category. This performs a lower case compare and strips any surrounding double quote characters.
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
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 ¶
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 ¶
NewManager creates a new instance of the Manager object.
func (Manager) AddUserToGroup ¶
AddUserToGroup associates a user to the group. This is done internally by creating a mapping between the user and the group.
func (Manager) DeleteExpense ¶
DeleteExpense removes an expense and any assignments associated with the expense. This must be transactional.
func (Manager) DeleteGroup ¶
DeleteGroup removes the group from storage and any mappings to the members of the group.
func (Manager) DeletePayment ¶
DeletePayment removes a Payment from storage
func (Manager) InsertPayment ¶
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) PaymentByID ¶
PaymentByID returns a payment object with the given ID.
func (Manager) RemoveUserFromGroup ¶
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 ¶
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 ¶
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 ¶
UpdatePayment saves any modifications to the payment
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 ¶
PenceFromString parses a string and returns the amount of pence.
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.