models

package
v0.0.0-...-3c57ce0 Latest Latest
Warning

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

Go to latest
Published: Jun 10, 2020 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetDevice

func GetDevice(db *pg.DB, deviceID int64) (*Device, *JSONError)

GetDevice returns the `Device` with the given `deviceID` from the database

func GetUser

func GetUser(db *pg.DB, uid int64) (*User, *JSONError)

GetUser returns the `User` with the given `uid` from the database

func SwitchDevicePower

func SwitchDevicePower(db *pg.DB, device *Device) (*Device, *JSONError)

SwitchDevicePower inverse the `isTurnedOn` field of the `Device` with the given `deviceID`

func UpdateUser

func UpdateUser(db *pg.DB, uid int64, uu *User) (*User, *JSONError)

UpdateUser modifies the `User` with the given `uid` in the database, with some validations

Types

type Claims

type Claims struct {
	jwt.StandardClaims
	UserID int64
	Role   Role
}

Claims is the JWT claims

type Device

type Device struct {
	ID         int64  `json:"id" pg:",pk"`
	Hostname   string `json:"hostname"`
	IsTurnedOn bool   `json:"isTurnedOn"`
	Owner      *int64 `json:"owner" orm:"null"`
}

Device contains the information about a device

type JSONError

type JSONError struct {
	Status int    `json:"status"`
	Error  string `json:"error"`
}

JSONError is the struct representing a JSON formatted error

func AcquireDevice

func AcquireDevice(db *pg.DB, device *Device, uid int64) *JSONError

AcquireDevice sets the `User` parameter as the owner of the `Device`

func AddDevice

func AddDevice(db *pg.DB, d *Device) *JSONError

AddDevice inserts a new `Device` into the database

func AddDeviceMulti

func AddDeviceMulti(db *pg.DB, devices *[]Device) *JSONError

AddDeviceMulti insters multiple `Device` into the database

func AddUser

func AddUser(db *pg.DB, user *User) *JSONError

AddUser inserts a new `User` into the database

func DeleteDevice

func DeleteDevice(db *pg.DB, uid int64) *JSONError

DeleteDevice removes the given Rubus `Device` from the database

func DeleteUser

func DeleteUser(db *pg.DB, uid int64) *JSONError

DeleteUser removes the given Rubus `User` from the database

func GetAllDevices

func GetAllDevices(db *pg.DB) (*[]Device, *JSONError)

GetAllDevices returns all the `Device` from the database

func GetAllUsers

func GetAllUsers(db *pg.DB) (*[]User, *JSONError)

GetAllUsers returns all the `User` from the database

func NewBadRequestError

func NewBadRequestError() *JSONError

NewBadRequestError return an error 400 JSON formatted

func NewForbiddenError

func NewForbiddenError() *JSONError

NewForbiddenError return an error 403 JSON formatted

func NewInternalServerError

func NewInternalServerError() *JSONError

NewInternalServerError returns an error 500 JSON formatted

func NewUnauthorizedError

func NewUnauthorizedError() *JSONError

NewUnauthorizedError return an error 401 JSON formatted

func ReleaseDevice

func ReleaseDevice(db *pg.DB, device *Device) *JSONError

ReleaseDevice sets the `owner` of the `Device` as nil, if it is the current `owner` which is modifying it.

type JWT

type JWT struct {
	// contains filtered or unexported fields
}

JWT is used for swagger doc only

type NewUser

type NewUser struct {
	Username   string `json:"username" example:"rubus"`
	Email      string `json:"email" example:"rubus@mail.com"`
	Password   string `json:"password" example:"rubus_secret"`
	Role       Role   `json:"role" example:"administrator"`
	Expiration string `json:"expiration" example:"2020-05-18"`
}

NewUser is the model sent to create a new `User`

type PutUser

type PutUser struct {
	Username string `json:"username" example:"rubus"`
	Email    string `json:"email" example:"rubus@mail.com"`
	Password string `json:"password" example:"rubus_secret"`
}

PutUser is only use to document the PUT `User` endpoint

type Role

type Role string

Role is an enum which spcify the role of a `User`

const (
	EnumRoleAdmin Role = "administrator"
	EnumRoleUser  Role = "user"
)

Values for `Role` enum

type User

type User struct {
	ID           int64     `json:"id" pg:",pk" example:"1"`
	Username     string    `json:"username" pg:",unique, notnull" example:"rubus"`
	Email        string    `json:"email" pg:",unique, notnull" example:"rubus@mail.com"`
	Role         Role      `json:"role" example:"administrator"`
	Expiration   time.Time `json:"expiration" example:"2020-05-18"`
	PasswordHash string    `json:"-" pg:",notnull"`
}

User is the `User` model in the database

func Login

func Login(db *pg.DB, username, password string) *User

Login checks if the given credentials are valid or not

func (*User) Bind

func (u *User) Bind(c echo.Context, cost int) *JSONError

Bind transforms the given payload into a `User`, with some validations

func (*User) BindWithEmptyFields

func (u *User) BindWithEmptyFields(c echo.Context, cost int) *JSONError

BindWithEmptyFields transforms the given payload into a `User`, with some validations, but does not require any field (they should be either nil or valid)

Jump to

Keyboard shortcuts

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