api

package
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Oct 12, 2023 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const TransactionKey contextKey = 0

TransactionKey is the context key for the database transaction for a request

View Source
const UserKey contextKey = 1

UserKey is the context key for the user for a request

Variables

View Source
var DeviceEventLocation = EventLocation{
	Type:    "Device",
	Table:   "device_log",
	IDField: "device_id",
}

DeviceEventLocation is the EventLocation for the Device type

Functions

func CreateCreatedEvent

func CreateCreatedEvent(ctx context.Context, id int64, el EventLocation, c *CreatedContent) (eventID int64, err error)

CreateCreatedEvent creates a new Created Event for the given type, id, and content

func CreateDevice

func CreateDevice(ctx context.Context, device *Device) (id int64, err error)

CreateDevice creates a new Device with the given fields (ID and Events are ignored and created) and returns its ID, or an error if one occurred

func CreateEvent

func CreateEvent(ctx context.Context, id int64, el EventLocation, event *Event) (eventID int64, err error)

CreateEvent creates a new Event for the given type and id with the given fields (ID is ignored and created) and returns its ID or an error if one occurred

func CreateModel

func CreateModel(ctx context.Context, model *Model) (id int64, err error)

CreateModel creates a new Model with the given fields (ID and Events are ignored and created) and returns its ID, or an error if one occurred

func CreateModifiedEvent

func CreateModifiedEvent(ctx context.Context, id int64, el EventLocation, c *ModifiedContent) (eventID int64, err error)

CreateModifiedEvent creates a new Modified Event for the given type, id, and content

func CreateNoteEvent

func CreateNoteEvent(ctx context.Context, id int64, el EventLocation, note string) (eventID int64, err error)

CreateNoteEvent creates a new Note Event for the given type and id with the given note text

func CreateUser

func CreateUser(ctx context.Context, user *User) (id int64, err error)

CreateUser creates a new User with the given fields (ID is ignored and created) and returns its ID, or an error if one occurred

func CreateUserWithCredentials

func CreateUserWithCredentials(ctx context.Context, email, password, name string) (id int64, err error)

CreateUserWithCredentials creates a new User with the given information and returns it, or an error if one occurred

func UpdateDevice

func UpdateDevice(ctx context.Context, device *Device) error

UpdateDevice updates the fields for the given Device (using the ID field, Events are ignored), or returns an error if one occurred

func UpdateModel

func UpdateModel(ctx context.Context, model *Model) error

UpdateModel updates the fields for the given Model (using the ID field, Events are ignored), or returns an error if one occurred

func UpdateUser

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

UpdateUser updates the fields for the given User (using the ID field), or returns an error if one occurred

func ValidateString

func ValidateString(field, value string, max int) error

ValidateString returns an error if the given value is not within the parameters

Types

type CreatedContent

type CreatedContent struct {
	Fields []*CreatedField `json:"fields"`
}

CreatedContent represents content for a created event

type CreatedField

type CreatedField struct {
	Name  string      `json:"name"`
	Value interface{} `json:"value"`
	Model *Model      `json:"_model,omitempty"`
}

CreatedField represents a field for a CreatedContent. If Name is model_id, Model will be populated.

type Device

type Device struct {
	ID           int64    `json:"id"`
	SerialNumber string   `json:"serial_number"`
	ModelID      int64    `json:"model_id,omitempty"`
	Status       Status   `json:"status"`
	Location     Location `json:"location"`
	Model        *Model   `json:"model,omitempty"`
	Events       []*Event `json:"events,omitempty"`
}

Device represents an inventoried device. ModelID is populated for Create, Read, and Update. Model is populated for Queries.

func QueryDevice

func QueryDevice(ctx context.Context, serialNumber, manufacturer, model, status, location string) ([]*Device, error)

QueryDevice returns all Devices matching the given serial number, manufacturer, model, status, or location, or an error if one occurred.

func ReadDevice

func ReadDevice(ctx context.Context, id int64, includeEvents bool) (*Device, error)

ReadDevice returns the Device with the given id, or an error if one occurred. If includeEvents is true the Events field will be populated

func ReadDeviceBySerialNumber

func ReadDeviceBySerialNumber(ctx context.Context, serialNumber string, includeEvents bool) (*Device, error)

ReadDeviceBySerialNumber returns the Device with the given Serial Number, or an error if one occurred. If includeEvents is true the Events field will be populated

func SimpleQueryDevice

func SimpleQueryDevice(ctx context.Context, search string) ([]*Device, error)

SimpleQueryDevice returns all Devices matching the given search (searching all fields), or an error if one occurred.

func (*Device) ReadModel

func (d *Device) ReadModel(ctx context.Context) (*Model, error)

ReadModel resolves the ModelID field to a Model.

func (*Device) Validate

func (d *Device) Validate(ctx context.Context) error

Validate cleans and validates the given Device

type Error

type Error struct {
	Description string
	Type        ErrorType
	Err         error
	DuplicateID int64
}

Error wraps errors in the API

func (*Error) Error

func (e *Error) Error() string

type ErrorType

type ErrorType int

ErrorType are APIError types

const (
	ErrorTypeUser ErrorType = iota
	ErrorTypeServer
	ErrorTypeDuplicate
)

ErrorTypes

type Event

type Event struct {
	ID      int64       `json:"-"`
	Date    time.Time   `json:"date"`
	UserID  int64       `json:"user_id"`
	User    *User       `json:"_user,omitempty"`
	Type    string      `json:"type"`
	Content interface{} `json:"content"`
}

Event represents an event that has happened. UserID should be used when creating and Event and User is used when reading and Event.

func ReadEvents

func ReadEvents(ctx context.Context, id int64, el EventLocation) ([]*Event, error)

ReadEvents returns the events for the given type and id, or an error if one occurred

type EventLocation

type EventLocation struct {
	Type    string
	Table   string
	IDField string
}

EventLocation contains information needed to add events for the given type

type Location

type Location string

Location is an allowed location

func ReadLocations

func ReadLocations(ctx context.Context) ([]Location, error)

ReadLocations returns all Locations, or an error if one occurred

func (*Location) Scan

func (s *Location) Scan(value interface{}) error

Scan implements the Scanner interface.

func (Location) Value

func (s Location) Value() (driver.Value, error)

Value implements the driver Valuer interface.

type Model

type Model struct {
	ID           int64  `json:"id"`
	Manufacturer string `json:"manufacturer"`
	Model        string `json:"model"`
}

Model represents a device model

func QueryModel

func QueryModel(ctx context.Context, manufacturer, model string) ([]*Model, error)

QueryModel returns all Models matching the given manufacturer and model or an error if one occurred.

func ReadModel

func ReadModel(ctx context.Context, id int64) (*Model, error)

ReadModel returns the Model with the given id, or an error if one occurred.

func ReadModelByManufacturerAndModel

func ReadModelByManufacturerAndModel(ctx context.Context, manufacturer, model string) (*Model, error)

ReadModelByManufacturerAndModel returns the Model with the given Manufacturer and Model, or an error if one occurred.

func (*Model) Validate

func (m *Model) Validate() error

Validate cleans and validates the given Model

type ModifiedContent

type ModifiedContent struct {
	Fields []*ModifiedField `json:"fields"`
}

ModifiedContent represents content for a modified event

type ModifiedField

type ModifiedField struct {
	Name     string      `json:"name"`
	OldValue interface{} `json:"old_value"`
	OldModel *Model      `json:"_old_model,omitempty"`
	NewValue interface{} `json:"new_value"`
	NewModel *Model      `json:"_new_model,omitempty"`
}

ModifiedField represents a field for a ModifiedContent. If Name is model_id, OldModel and NewModel will be populated.

type NoteContent

type NoteContent struct {
	Note string `json:"note"`
}

NoteContent represents content for a note event

type Stats

type Stats struct {
	Locations     []*StatsLocation `json:"locations"`
	Models        []*StatsModel    `json:"models"`
	Statuses      []*StatsStatus   `json:"statuses"`
	DeviceCount   int              `json:"device_count"`
	ModelCount    int              `json:"model_count"`
	LocationCount int              `json:"location_count"`
	Devices       []*Device        `json:"devices"`
}

Stats represents device statistics (top 10, etc)

func ReadStats

func ReadStats(ctx context.Context) (*Stats, error)

ReadStats returns Stats, or an error if one occurred.

type StatsLocation

type StatsLocation struct {
	Location string `json:"location"`
	Count    int    `json:"count"`
}

StatsLocation represents Location Stats

type StatsModel

type StatsModel struct {
	ID           int64  `json:"id"`
	Manufacturer string `json:"manufacturer"`
	Model        string `json:"model"`
	Count        int    `json:"count"`
}

StatsModel represents Model Stats

type StatsStatus

type StatsStatus struct {
	Status string `json:"status"`
	Count  int    `json:"count"`
}

StatsStatus represents Status Stats

type Status

type Status string

Status is an allowed status

func ReadStatuses

func ReadStatuses(ctx context.Context) ([]Status, error)

ReadStatuses returns all Statuses, or an error if one occurred

func (*Status) Scan

func (s *Status) Scan(value interface{}) error

Scan implements the Scanner interface.

func (Status) Value

func (s Status) Value() (driver.Value, error)

Value implements the driver Valuer interface.

type User

type User struct {
	ID    int64  `json:"id"`
	Email string `json:"email"`
	Hash  []byte `json:"-"`
	Name  string `json:"name"`
}

User represents an authencatable user

func ReadUser

func ReadUser(ctx context.Context, id int64) (*User, error)

ReadUser returns the User with the given id, or an error if one occurred

func ReadUserByEmail

func ReadUserByEmail(ctx context.Context, email string) (*User, error)

ReadUserByEmail returns the User with the given email, or an error if one occurred

func (*User) Authenticate

func (u *User) Authenticate(_ context.Context, password string) error

Authenticate authenticates against the database with the given credentials and returns nil if success or error on failure

func (*User) ChangePassword

func (u *User) ChangePassword(ctx context.Context, oldPassword, newPassword string) error

ChangePassword updates the password hash to the given password

func (*User) Validate

func (u *User) Validate() error

Validate validates the given User

Jump to

Keyboard shortcuts

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