package api import ( "strconv" "time" "go.jolheiser.com/invitea/database" ) type Invite struct { ID int64 Code string Uses int64 Total int64 Expiration time.Time } func (i *Invite) TotalString() string { if i.Total == 0 { return "∞" } return strconv.FormatInt(i.Total, 10) } func (i *Invite) ExpirationString() string { if i.Expiration.Unix() == 0 { return "never" } return i.Expiration.Format("01/02/2006") } func (i *Invite) Valid() bool { limited := i.Total != 0 && i.Uses >= i.Total expired := i.Expiration.Unix() != 0 && time.Now().After(i.Expiration) return !(limited || expired) } func InviteFromDB(i database.Invite) Invite { return Invite{ ID: i.ID, Code: i.Code, Uses: i.Uses, Total: i.Total.Int64, Expiration: time.Unix(i.Expiration.Int64, 0).UTC(), } } func InvitesFromDB(i ...database.Invite) []Invite { invites := make([]Invite, 0, len(i)) for _, ii := range i { invites = append(invites, InviteFromDB(ii)) } return invites }