models

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2020 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidPaginationQuery = errors.New("invalid pagination query")

ErrInvalidPaginationQuery defines a sentinel error when an invalid pagination query is provided.

Functions

func GetAllKeywords

func GetAllKeywords(db *gorm.DB, pq httputil.PaginationQuery) ([]Keyword, Paginator, error)

GetAllKeywords returns a slice of Keyword objects paginated by an offset, order and limit. An error is returned upon database query failure.

func GetAllModules

func GetAllModules(db *gorm.DB, pq httputil.PaginationQuery) ([]Module, Paginator, error)

GetAllModules returns a slice of Module objects paginated by an offset, order and limit. An error is returned upon database query failure.

func GetAllUsers

func GetAllUsers(db *gorm.DB, pq httputil.PaginationQuery) ([]User, Paginator, error)

GetAllUsers returns a slice of User objects paginated by an offset, order and limit. An error is returned upon database query failure.

func NewNullInt64

func NewNullInt64(i int64) sql.NullInt64

func NewNullString

func NewNullString(s string) sql.NullString

func SearchModules

func SearchModules(db *gorm.DB, query string, pq httputil.PaginationQuery) ([]Module, Paginator, error)

SearchModules performs a paginated query for a set of modules by name, team, description and set of keywords. If not matching modules exist, an empty slice is returned.

Note, we used offset-based pagination even though this approach doesn't scale well. We presume that number of records stored and even returned won't approach the scale where offset pagination would drastically impact performance. This allows us to provide custom sorting.

Types

type BugTracker

type BugTracker struct {
	gorm.Model

	URL      sql.NullString `json:"url"`
	Contact  sql.NullString `json:"contact"`
	ModuleID uint           `json:"module_id"`
}

BugTracker defines the metadata information for reporting bug reports on a given Module type.

func (BugTracker) MarshalJSON

func (bt BugTracker) MarshalJSON() ([]byte, error)

MarshalJSON implements custom JSON marshaling for the BugTracker model.

func (BugTracker) NewBugTrackerJSON added in v0.0.2

func (bt BugTracker) NewBugTrackerJSON() BugTrackerJSON

type BugTrackerJSON

type BugTrackerJSON struct {
	GormModelJSON

	URL      interface{} `json:"url"`
	Contact  interface{} `json:"contact"`
	ModuleID uint        `json:"module_id"`
}

BugTrackerJSON defines the JSON-encodeable type for a BugTracker.

type GormModelJSON

type GormModelJSON struct {
	ID        uint      `json:"id"`
	CreatedAt time.Time `json:"created_at"`
	UpdatedAt time.Time `json:"updated_at"`
}

GormModelJSON defines a wrapper around a gorm.Model object that is used for JSON marshaling.

type Keyword

type Keyword struct {
	gorm.Model

	Name string `json:"name"`
}

Keyword defines a module keyword, where a module can have one or more keywords.

func (Keyword) MarshalJSON

func (k Keyword) MarshalJSON() ([]byte, error)

MarshalJSON implements custom JSON marshaling for the Keyword model.

func (Keyword) NewKeywordJSON added in v0.0.2

func (k Keyword) NewKeywordJSON() KeywordJSON

func (Keyword) Query

func (k Keyword) Query(db *gorm.DB) (Keyword, error)

Query performs a query for a Keyword record where the search criteria is defined by the receiver object. The resulting record, if it exists, is returned. If the query fails or the record does not exist, an error is returned.

type KeywordJSON

type KeywordJSON struct {
	GormModelJSON

	Name string `json:"name"`
}

KeywordJSON defines the JSON-encodeable type for a Keyword.

type Module

type Module struct {
	gorm.Model

	Name        string `gorm:"not null;default:null"`
	Team        string `gorm:"not null;default:null"`
	Homepage    string
	Description string
	Stars       int64
	BugTracker  BugTracker      `gorm:"foreignKey:module_id"`
	Keywords    []Keyword       `gorm:"many2many:module_keywords"`
	Authors     []User          `gorm:"many2many:module_authors"`
	Owners      []User          `gorm:"many2many:module_owners"`
	Versions    []ModuleVersion `gorm:"foreignKey:module_id"`

	Version ModuleVersion `gorm:"-"` // current version in manifest
}

Module defines a Cosmos SDK module.

func GetModuleByID

func GetModuleByID(db *gorm.DB, id uint) (Module, error)

GetModuleByID returns a module by ID. If the module doesn't exist or if the query fails, an error is returned.

func GetUserModules

func GetUserModules(db *gorm.DB, name string) ([]Module, error)

GetUserModules returns a set of Module's authored by a given User by name.

func QueryModule

func QueryModule(db *gorm.DB, query map[string]interface{}) (Module, error)

QueryModule performs a query for a Module record. The resulting record, if it exists, is returned. If the query fails or the record does not exist, an error is returned.

func (Module) AddOwner

func (m Module) AddOwner(db *gorm.DB, owner User) (Module, error)

AddOwner adds a given User as an owner to a Module and deletes the corresponding ModuleOwnerInvite record. It returns an error upon failure.

func (*Module) BeforeSave

func (m *Module) BeforeSave(tx *gorm.DB) error

BeforeSave implements a GORM hook for updating a Module record before it is created or updated.

func (Module) GetLatestVersion

func (m Module) GetLatestVersion(db *gorm.DB) (ModuleVersion, error)

GetLatestVersion returns a module's latest version record, if the module exists.

func (Module) MarshalJSON

func (m Module) MarshalJSON() ([]byte, error)

MarshalJSON implements custom JSON marshaling for the Module model.

func (Module) Star

func (m Module) Star(db *gorm.DB, userID uint) (int64, error)

Star favorites a module by ID for a given userID. It returns an error upon failure, otherwise it returns the total nubmer of favorites for the module.

func (Module) UnStar

func (m Module) UnStar(db *gorm.DB, userID uint) (int64, error)

UnStar removes a favorite for a module by ID for a given userID. It returns an error upon failure, otherwise it returns the total nubmer of favorites for the module.

func (Module) Upsert

func (m Module) Upsert(db *gorm.DB) (Module, error)

Upsert will attempt to either create a new Module record or update an existing record. A Module record is considered unique by a (name, team) index. In the case of the record existing, all primary and one-to-one fields will be updated, where authors and keywords are replaced. If the provided Version does not exist, it will be appended to the existing set of version relations. An error is returned upon failure. Upon success, the created or updated record will be returned.

func (Module) UserStarred

func (m Module) UserStarred(db *gorm.DB, userID uint) (bool, error)

Starred returns a boolean defining if a given user by ID has starred a module. It returns an error upon query failure.

type ModuleAuthors

type ModuleAuthors struct {
	ModuleID uint `json:"module_id"`
	UserID   uint `json:"user_id"`
}

ModuleAuthors defines the type relationship between a module and all the associated authors.

type ModuleJSON

type ModuleJSON struct {
	GormModelJSON

	Name        string              `json:"name"`
	Team        string              `json:"team"`
	Description string              `json:"description"`
	Homepage    string              `json:"homepage"`
	Stars       int64               `json:"stars"`
	BugTracker  BugTrackerJSON      `json:"bug_tracker"`
	Keywords    []KeywordJSON       `json:"keywords"`
	Authors     []UserJSON          `json:"authors"`
	Owners      []UserJSON          `json:"owners"`
	Versions    []ModuleVersionJSON `json:"versions"`
}

ModuleJSON defines the JSON-encodeable type for a Module.

type ModuleKeywords

type ModuleKeywords struct {
	ModuleID  uint `json:"module_id"`
	KeywordID uint `json:"keyword_id"`
}

ModuleKeywords defines the type relationship between a module and all the associated keywords.

type ModuleOwnerInvite

type ModuleOwnerInvite struct {
	CreatedAt time.Time
	UpdatedAt time.Time

	ModuleID        uint
	InvitedUserID   uint
	InvitedByUserID uint
	Token           uuid.UUID
}

ModuleOwnerInvite defines the a module owner invitation relationship.

func QueryModuleOwnerInvite

func QueryModuleOwnerInvite(db *gorm.DB, query map[string]interface{}) (ModuleOwnerInvite, error)

QueryModuleOwnerInvite performs a query for a ModuleOwnerInvite record. The resulting record, if it exists, is returned. If the query fails or the record does not exist, an error is returned.

func (*ModuleOwnerInvite) BeforeSave

func (moi *ModuleOwnerInvite) BeforeSave(_ *gorm.DB) error

BeforeSave will create and set the ModuleOwnerInvite UUID.

func (ModuleOwnerInvite) Upsert

func (moi ModuleOwnerInvite) Upsert(db *gorm.DB) (ModuleOwnerInvite, error)

Upsert creates or updates a ModuleOwnerInvite record. If no record exists, a new record with a UUID token is created. Otherwise, the existing ModuleOwnerInvite record's UUID token is updated/regenerated. It returns an error up database failure.

type ModuleVersion

type ModuleVersion struct {
	gorm.Model

	Documentation string
	Repo          string `gorm:"not null;default:null"`
	Version       string
	SDKCompat     sql.NullString
	ModuleID      uint
	PublishedBy   uint
}

ModuleVersion defines a version associated with a unique module.

func (ModuleVersion) MarshalJSON

func (mv ModuleVersion) MarshalJSON() ([]byte, error)

MarshalJSON implements custom JSON marshaling for the ModuleVersion model.

func (ModuleVersion) NewModuleVersionJSON added in v0.0.2

func (mv ModuleVersion) NewModuleVersionJSON() ModuleVersionJSON

type ModuleVersionJSON

type ModuleVersionJSON struct {
	GormModelJSON

	Version       string      `json:"version"`
	Documentation string      `json:"documentation"`
	Repo          string      `json:"repo"`
	SDKCompat     interface{} `json:"sdk_compat"`
	ModuleID      uint        `json:"module_id"`
	PublishedBy   uint        `json:"published_by"`
}

BugTrackerJSON defines the JSON-encodeable type for a ModuleVersion.

type Paginator

type Paginator struct {
	PrevPage int64
	NextPage int64
	Total    int64
}

Paginator defines pagination result cursor metadata to determine how to make subsequent pagination calls.

type User

type User struct {
	gorm.Model

	Name              string
	FullName          string
	URL               string
	GravatarID        string
	AvatarURL         string
	GithubUserID      sql.NullInt64
	GithubAccessToken sql.NullString
	Email             sql.NullString
	EmailConfirmed    bool

	// many-to-many relationships
	Modules []Module `gorm:"many2many:module_owners"`

	// one-to-many relationships
	Tokens []UserToken `gorm:"foreignKey:user_id"`

	Stars []uint `gorm:"-"`
}

User defines an entity that contributes to a Module type.

func GetUserByID

func GetUserByID(db *gorm.DB, id uint) (User, error)

GetUserByID returns a User by ID. If the user doesn't exist or if the query fails, an error is returned.

func QueryUser

func QueryUser(db *gorm.DB, query map[string]interface{}) (User, error)

QueryUser performs a query for a User record. The resulting record, if it exists, is returned. If the query fails or the record does not exist, an error is returned.

func (*User) AfterFind

func (u *User) AfterFind(tx *gorm.DB) error

AfterFind implements a GORM hook for updating a User record after it has been queried for.

func (User) ConfirmEmail

func (u User) ConfirmEmail(db *gorm.DB, uec UserEmailConfirmation) (User, error)

ConfirmEmail confirms a user email confirmation by updating the User record's EmailConfirmed column and removing the associated UserEmailConfirmation record. It returns an error upon database failure.

func (User) CountTokens

func (u User) CountTokens(db *gorm.DB) int64

CountTokens returns the total number of API tokens belonging to a User.

func (User) CreateToken

func (u User) CreateToken(db *gorm.DB, name string) (UserToken, error)

CreateToken creates a new UserToken for a given User model. It returns an error upon failure.

func (User) Equal

func (u User) Equal(other User) bool

Equal implements an equality check for two User records.

func (User) GetTokens

func (u User) GetTokens(db *gorm.DB) ([]UserToken, error)

GetTokens returns all UserToken records for a given User record. It returns an error upon failure.

func (User) MarshalJSON

func (u User) MarshalJSON() ([]byte, error)

MarshalJSON implements custom JSON marshaling for the User model.

func (User) NewUserJSON added in v0.0.2

func (u User) NewUserJSON() UserJSON

func (User) Upsert

func (u User) Upsert(db *gorm.DB) (User, error)

Upsert creates or updates a User record. Note, this should only be called when authenticating a user. When authors are associated with a Module, they are either fetched or created by their name and email.

type UserEmailConfirmation

type UserEmailConfirmation struct {
	CreatedAt time.Time
	UpdatedAt time.Time

	Email  string
	UserID uint
	Token  uuid.UUID
}

UserEmailConfirmation defines a relation for confirming user email addresses.

func QueryUserEmailConfirmation

func QueryUserEmailConfirmation(db *gorm.DB, query map[string]interface{}) (UserEmailConfirmation, error)

QueryUserEmailConfirmation performs a query for a UserEmailConfirmation record. The resulting record, if it exists, is returned. If the query fails or the record does not exist, an error is returned.

func (*UserEmailConfirmation) BeforeSave

func (uec *UserEmailConfirmation) BeforeSave(_ *gorm.DB) error

BeforeSave will create and set the UserEmailConfirmation UUID.

func (UserEmailConfirmation) Upsert

Upsert creates or updates a UserEmailConfirmation record. If no record exists for a given unique user ID, a new record with a UUID token is created. Otherwise, the existing UserEmailConfirmation record's UUID token is updated/regenerated. It returns an error up database failure.

type UserJSON

type UserJSON struct {
	GormModelJSON

	Name           string      `json:"name"`
	FullName       string      `json:"full_name"`
	URL            string      `json:"url"`
	AvatarURL      string      `json:"avatar_url"`
	GravatarID     string      `json:"gravatar_id"`
	Email          interface{} `json:"email"`
	EmailConfirmed bool        `json:"email_confirmed"`
	Stars          []uint      `json:"stars"`
}

UserJSON defines the JSON-encodeable type for a User.

type UserModuleFavorite

type UserModuleFavorite struct {
	UserID   uint `json:"user_id"`
	ModuleID uint `json:"module_id"`
}

UserModuleFavorite defines the behavior of a user staring a module record.

type UserToken

type UserToken struct {
	gorm.Model

	Name    string
	UserID  uint
	Count   uint
	Token   uuid.UUID
	Revoked bool
}

UserToken defines a user created API token.

func QueryUserToken

func QueryUserToken(db *gorm.DB, query map[string]interface{}) (UserToken, error)

QueryUserToken performs a query for a UserToken record. The resulting record, if it exists, is returned. If the query fails or the record does not exist, an error is returned.

func (*UserToken) BeforeCreate

func (ut *UserToken) BeforeCreate(_ *gorm.DB) error

BeforeCreate will create and set the UserToken UUID.

func (UserToken) IncrCount

func (ut UserToken) IncrCount(db *gorm.DB) (UserToken, error)

IncrCount increments a token's count. It returns an error upon failure.

func (UserToken) MarshalJSON

func (ut UserToken) MarshalJSON() ([]byte, error)

MarshalJSON implements custom JSON marshaling for the UserToken model.

func (UserToken) Revoke

func (ut UserToken) Revoke(db *gorm.DB) (UserToken, error)

Revoke revokes a token. It returns an error upon failure.

type UserTokenJSON

type UserTokenJSON struct {
	GormModelJSON

	Name    string    `json:"name"`
	UserID  uint      `json:"user_id"`
	Count   uint      `json:"count"`
	Token   uuid.UUID `json:"token"`
	Revoked bool      `json:"revoked"`
}

UserTokenJSON defines the JSON-encodeable type for a UserToken.

Jump to

Keyboard shortcuts

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