db

package
v0.0.0-...-a940e1d Latest Latest
Warning

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

Go to latest
Published: Sep 30, 2018 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// DefaultDonationRow is used if the user has not set a donation row pattern
	DefaultDonationRow = "%CHARACTER% just donated %AMOUNT% ISK!"

	// DefaultContractRow is used if the user has not set a contract row pattern
	DefaultContractRow = "%CHARACTER% just contracted %ITEMS% items worth" +
		" %AMOUNT% ISK!"
)

Variables

View Source
var (
	// RePreferences ensures the row pattern has at least some content
	RePreferences = regexp.MustCompile(`[^( \t)]+`)
)

Functions

func Connect

func Connect(ctx context.Context) *sqlx.DB

Connect returns a new connection to the postgres db

func DeleteUser

func DeleteUser(ctx context.Context, charID int32) error

DeleteUser removes a user (auth/tracked character)

func GetCharStandingISK

func GetCharStandingISK(ctx context.Context, charID int32) (float64, error)

GetCharStandingISK returns the amount donated towards improving standing

func GetContractItems

func GetContractItems(
	ctx context.Context,
	contracts Contracts,
) error

GetContractItems fills in the Items of each Contract passed

func GetName

func GetName(ctx context.Context, id int32) (string, error)

GetName returns the name for a single character ID from the DB

func GetNames

func GetNames(ctx context.Context, ids ...int32) (map[int32]string, error)

GetNames returns the names for the IDs from the db

func GetOutstandingContracts

func GetOutstandingContracts(ctx context.Context, c int32) ([]int32, error)

GetOutstandingContracts returns a list of outstanding contract IDs

func GetStatements

func GetStatements(ctx context.Context) map[cx.Key]*sqlx.NamedStmt

GetStatements prepares all queries for the global context

func NewCharacter

func NewCharacter(ctx context.Context, char *CharacterRow) error

NewCharacter adds a new character to the characters table

func PruneContract

func PruneContract(ctx context.Context, c *Contract) error

PruneContract removes a contract and deducts from the 30d totals

func PruneDonation

func PruneDonation(ctx context.Context, donation *Donation) error

PruneDonation removes a donation by ID

func SaveCharacter

func SaveCharacter(ctx context.Context, char *Character) error

SaveCharacter saves a single character

func SaveCharacterContracts

func SaveCharacterContracts(
	ctx context.Context,
	donations Contracts,
	affiliations []*Affiliation,
	addition bool,
) error

SaveCharacterContracts updates all totals in the characters table

func SaveCharacterDonations

func SaveCharacterDonations(
	ctx context.Context,
	donations []*Donation,
	affiliations []*Affiliation,
	addition bool,
) error

SaveCharacterDonations updates all totals in the characters table

func SaveContract

func SaveContract(ctx context.Context, contract *Contract) error

SaveContract saves the contract and associated items in the db

func SaveDonation

func SaveDonation(ctx context.Context, donation *Donation) error

SaveDonation stores a donation in the database

func SaveNames

func SaveNames(ctx context.Context, affiliations []*Affiliation) error

SaveNames stores the map of ids:names in the db

func SaveUser

func SaveUser(ctx context.Context, user *User) error

SaveUser attempts to save the User in the db

func SetPreferences

func SetPreferences(ctx context.Context, charID int32, p *Preferences) error

SetPreferences sets the Preferences for the logged in user

func UpdateContracts

func UpdateContracts(
	ctx context.Context,
	contracts []*Contract,
	aff []*Affiliation,
) error

UpdateContracts sets the contract as accepted in the db, if it has been

Types

type Affiliation

type Affiliation struct {
	Character   *Name
	Corporation *Name
	Alliance    *Name
}

Affiliation links a character with a corporation and maybe alliance

type CharDetails

type CharDetails struct {
	Character *Character `json:"character"`

	// ISK IN
	Donations Donations `json:"donations,omitempty"`
	Contracts Contracts `json:"contracts,omitempty"`

	// ISK OUT
	Donated    Donations `json:"donated,omitempty"`
	Contracted Contracts `json:"contracted,omitempty"`
}

CharDetails is the api return for a character

func GetCharDetails

func GetCharDetails(ctx context.Context, charID int32) (*CharDetails, error)

GetCharDetails returns details for the character from pg

type Character

type Character struct {
	// ID is the characterID of this donator/recipient
	ID int32 `json:"id"`

	// Name is the last checked name of the character
	Name string `json:"name,omitempty"`

	// CorporationID is the last checked corporation ID of the character
	CorporationID int32 `json:"corporation,omitempty"`

	// CorporationName is the last checked name of the corporation
	CorporationName string `json:"corporation_name,omitempty"`

	// AllianceID is the last checked alliance ID of the character
	AllianceID int32 `json:"alliance,omitempty"`

	// AllianceName is the last checked name of the alliance
	AllianceName string `json:"alliance_name,omitempty"`

	// Received donations and/or contracts
	Received int64 `json:"received,omitempty"`

	// ReceivedISK value of all donations plus contracts
	ReceivedISK float64 `json:"received_isk,omitempty"`

	// Received donations and/or contracts in the last 30 days
	Received30 int64 `json:"received_30,omitempty"`

	// ReceivedISK30 value of all donations plus contracts in the last 30 days
	ReceivedISK30 float64 `json:"received_isk_30,omitempty"`

	// Donated is the number of times this character has donated to someone else
	Donated int64 `json:"donated,omitempty"`

	// DonatedISK is the value of all ISK donated
	DonatedISK float64 `json:"donated_isk,omitempty"`

	// Donated30 is the number of donations in the last 30 days
	Donated30 int64 `json:"donated_30,omitempty"`

	// DonatedISK30 is the value of all ISK donated in the last 30 days
	DonatedISK30 float64 `json:"donated_isk_30,omitempty"`

	// LastDonated timestamp
	LastDonated time.Time `json:"last_donated,omitempty"`

	// LastReceived timestamp
	LastReceived time.Time `json:"last_received,omitempty"`

	// GoodStanding boolean
	GoodStanding bool `json:"good_standing"`
}

Character describes the output format of known characters

func GetCharacter

func GetCharacter(ctx context.Context, charID int32) (*Character, error)

GetCharacter pulls a single character from the db

func GetTopDonators

func GetTopDonators(ctx context.Context) ([]*Character, error)

GetTopDonators returns the top character IDs and isk values

func GetTopRecipients

func GetTopRecipients(ctx context.Context) ([]*Character, error)

GetTopRecipients returns the top character IDs and isk values

func (*Character) MarshalJSON

func (c *Character) MarshalJSON() ([]byte, error)

MarshalJSON implementation to omit our null timestamps

type CharacterRow

type CharacterRow struct {
	// ID is the characterID of this donator/recipient
	ID int32 `db:"character_id"`

	// CorporationID is the last checked corporation ID of the character
	CorporationID int32 `db:"corporation_id"`

	// AllianceID is the last checked alliance ID of the character
	AllianceID int32 `db:"alliance_id"`

	// Received donations and/or contracts
	Received int64 `db:"received"`

	// ReceivedISK value of all donations plus contracts
	ReceivedISK float64 `db:"received_isk"`

	// Received donations and/or contracts in the last 30 days
	Received30 int64 `db:"received_30"`

	// ReceivedISK30 value of all donations plus contracts in the last 30 days
	ReceivedISK30 float64 `db:"received_isk_30"`

	// Donated is the number of times this character has donated to someone else
	Donated int64 `db:"donated"`

	// DonatedISK is the value of all ISK donated
	DonatedISK float64 `db:"donated_isk"`

	// Donated30 is the number of donations in the last 30 days
	Donated30 int64 `db:"donated_30"`

	// DonatedISK30 is the value of all ISK donated in the last 30 days
	DonatedISK30 float64 `db:"donated_isk_30"`

	// LastDonated timestamp
	LastDonated pq.NullTime `db:"last_donated"`

	// LastReceived timestamp
	LastReceived pq.NullTime `db:"last_received"`

	// GoodStanding boolean
	GoodStanding bool `db:"good_standing"`
}

CharacterRow describes Character as stored in the characters table

type Contract

type Contract struct {
	// ID is the contract ID
	ID int32 `db:"contract_id" json:"id"`

	// Donator who sent the recipient the contract
	Donator int32 `db:"donator" json:"donator"`

	// Receiver is who received the contract
	Receiver int32 `db:"receiver" json:"receiver"`

	// Location is the station or structure ID
	Location int64 `db:"location" json:"location"`

	// Issued timestamp
	Issued time.Time `db:"issued" json:"issued"`

	// Expires timestamp
	Expires time.Time `db:"expires" json:"expires"`

	// Accepted boolean
	Accepted bool `db:"accepted" json:"accepted"`

	// Value is an estimated value of the contract items
	Value float64 `db:"value" json:"value"`

	// Note is the title of the contract
	Note string `db:"note" json:"note"`

	// Items is an array of items in the contract
	Items []*Item `json:"items"`
}

Contract describes zero ISK donation contracts

type Contracts

type Contracts []*Contract

Contracts are time sorted

func GetStaleContracts

func GetStaleContracts(ctx context.Context) (Contracts, error)

GetStaleContracts returns contracts issued more than 30 days ago

func (Contracts) Len

func (c Contracts) Len() int

func (Contracts) Less

func (c Contracts) Less(i, j int) bool

func (Contracts) Swap

func (c Contracts) Swap(i, j int)

type Donation

type Donation struct {
	// ID is the transaction ID
	ID int64 `db:"transaction_id" json:"id"`

	// Donator who sent the recipient this isk
	Donator int32 `db:"donator" json:"donator"`

	// Recipient is who received the donation
	Recipient int32 `db:"receiver" json:"receiver"`

	// Timestamp of when this tranfer occurred
	Timestamp time.Time `db:"timestamp" json:"timestamp"`

	// Note or memo that came with the transfer
	Note string `db:"note" json:"note,omitempty"`

	// Amount of ISK transferred
	Amount float64 `db:"amount" json:"amount"`
}

Donation describes a one time ISK transfer

type Donations

type Donations []*Donation

Donations are time sorted

func GetCharDonated

func GetCharDonated(ctx context.Context, charID int32) (Donations, error)

GetCharDonated returns donations FROM the character

func GetCharDonations

func GetCharDonations(ctx context.Context, charID int32) (Donations, error)

GetCharDonations returns donations FOR the character

func GetStaleDonations

func GetStaleDonations(ctx context.Context) (Donations, error)

GetStaleDonations returns donations from more than 30 days ago

func (Donations) Len

func (d Donations) Len() int

func (Donations) Less

func (d Donations) Less(i, j int) bool

func (Donations) Swap

func (d Donations) Swap(i, j int)

type Item

type Item struct {
	// ID is a record ID
	ID int64 `db:"id" json:"-"`

	// ContractID links this item to a contract
	ContractID int32 `db:"contract_id" json:"-"`

	// TypeID of the item in the contract
	TypeID int32 `db:"type_id" json:"type_id"`

	// Quantity of items given
	Quantity int32 `db:"quantity" json:"quantity"`

	// ItemID of the item in the contract (if possible to determine)
	ItemID int64 `db:"item_id" json:"item_id,omitempty"`
}

Item are sourced from the contractItems table by id

type Name

type Name struct {
	ID   int32  `db:"id"`
	Name string `db:"name"`
}

Name represents an ID -> name mapping

type Preferences

type Preferences struct {
	Donations *Prefs `json:"donations"`
	Contracts *Prefs `json:"contracts"`
}

Preferences exports Prefs for donations, contracts, or both NB: the JSON form of this is only used for the combined view

func GetPreferences

func GetPreferences(ctx context.Context, t string, charID int32) (
	*Preferences,
	error,
)

GetPreferences returns the Preferences for the logged in user

type Prefs

type Prefs struct {
	Header     string  `json:"header,omitempty"`
	Footer     string  `json:"footer,omitempty"`
	Pattern    string  `json:"pattern"`
	Passphrase string  `json:"passphrase,omitempty"`
	Rows       int     `json:"rows"`
	MaxAge     int     `json:"max_age,omitempty"` // seconds
	Minimum    float64 `json:"minimum"`
}

Prefs exports preferences for either donations or contracts

func (*Prefs) Sanity

func (p *Prefs) Sanity(ctx context.Context) error

Sanity ensures our attribute lengths are acceptable

type Profile

type Profile struct {
	// CharacterID recipient
	CharacterID int32

	// Private is if we should hide this profile for everyone else
	Private bool

	// Passphrase can be set by the profile owner for minimal security
	Passphrase string
}

Profile describes someone setup to receive ISK

type User

type User struct {
	RefreshToken   string        `db:"refresh_token"`
	AccessToken    string        `db:"access_token"`
	OwnerHash      string        `db:"owner_hash"`
	CharacterID    int32         `db:"character_id"`
	LastJournalID  sql.NullInt64 `db:"last_journal_id"`
	LastContractID sql.NullInt64 `db:"last_contract_id"`
	AccessExpires  time.Time     `db:"access_expires"`
	LastProcessed  *time.Time    `db:"last_processed"`
}

User describes a mapping between a user and a character

func GetUsersToProcess

func GetUsersToProcess(ctx context.Context) ([]*User, error)

GetUsersToProcess returns all characters needing to be processed

type UserError

type UserError struct {
	Msg  []byte
	Code int
}

UserError can bubble up http errors to the api package

func (UserError) Error

func (e UserError) Error() string

Jump to

Keyboard shortcuts

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