models

package
v0.0.0-...-df1273a Latest Latest
Warning

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

Go to latest
Published: May 1, 2020 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetAllDevices

func GetAllDevices() ([]*Device, *JSONError)

GetAllDevices returns all the `Device` from the database

func GetAllUsers

func GetAllUsers() (users []*User, jsonErr *JSONError)

GetAllUsers returns all the `User` from the database

func GetDevice

func GetDevice(deviceID int64) (*Device, *JSONError)

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

func GetUser

func GetUser(uid int64) (*User, *JSONError)

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

func SwitchDevicePower

func SwitchDevicePower(device *Device) (*Device, *JSONError)

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

func UpdateUser

func UpdateUser(uid int64, uu *User) (u *User, jsonErr *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" orm:"pk"`
	Hostname string `json:"hostname"`
	IsTurnOn bool   `json:"isTurnOn"`
	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(device *Device, uid int64) *JSONError

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

func AddDevice

func AddDevice(d *Device) *JSONError

AddDevice inserts a new `Device` into the database

func AddDeviceMulti

func AddDeviceMulti(devices *[]Device) *JSONError

AddDeviceMulti insters multiple `Device` into the database

func AddUser

func AddUser(u *User) *JSONError

AddUser inserts a new `User` into the database

func DeleteDevice

func DeleteDevice(uid int64) *JSONError

DeleteDevice removes the given Rubus `Device` from the database

func DeleteUser

func DeleteUser(uid int64) *JSONError

DeleteUser removes the given Rubus `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(device *Device) *JSONError

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

type NewUser

type NewUser struct {
	Username string `json:"username"`
	Email    string `json:"email"`
	Password string `json:"password"`
	Role     Role   `json:"role"`
}

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

type PutUser

type PutUser struct {
	Username string `json:"username"`
	Email    string `json:"email"`
	Password string `json:"password"`
}

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

func Login

func Login(username, password string) (*int64, *Role, bool)

Login checks if the given credentials are valid or not

type User

type User struct {
	ID           int64  `json:"id" orm:"pk;auto"`
	Username     string `json:"username" orm:"unique"`
	Email        string `json:"email" orm:"unique"`
	Role         Role   `json:"role"`
	PasswordHash string `json:"-"`
}

User is the `User` model in the database

func (*User) Bind

func (u *User) Bind(requestBody []byte) *JSONError

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

func (*User) BindWithEmptyFields

func (u *User) BindWithEmptyFields(requestBody []byte) *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