gorm

package
v0.0.0-...-35a8602 Latest Latest
Warning

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

Go to latest
Published: Dec 21, 2023 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// DBHost ..
	DBHost = "POSTGRES_HOST"
	// DBPort ...
	DBPort = "POSTGRES_PORT"
	// DBUser ...
	DBUser = "POSTGRES_USER"
	// DBPASSWORD ...
	DBPASSWORD = "POSTGRES_PASSWORD"
	// DBName ...
	DBName = "POSTGRES_DB"
)

Variables

View Source
var (
	// DefaultResidence is the default residence for a user incase none is specified
	DefaultResidence = helpers.MustGetEnvVar("DEFAULT_RESIDENCE_ID")
)

Functions

This section is empty.

Types

type Base

type Base struct {
	CreatedAt time.Time `gorm:"column:created_at"`
	CreatedBy *string   `gorm:"column:created_by"`
	UpdatedAt time.Time `gorm:"column:updated_at"`
	UpdatedBy *string   `gorm:"column:updated_by"`
}

Base is the base table for all tables

type Bill

type Bill struct {
	Base

	ID      string  `gorm:"column:id"`
	Active  bool    `gorm:"column:active"`
	Type    string  `gorm:"column:type"`
	Amount  float64 `gorm:"column:amount"`
	Penalty float64 `gorm:"column:penalty"`
	UserID  string  `gorm:"column:user_id"`
}

Bill represents a billing model

func (*Bill) BeforeCreate

func (b *Bill) BeforeCreate(tx *gorm.DB) (err error)

BeforeCreate is a hook run before creating a bill

func (Bill) TableName

func (Bill) TableName() string

TableName customizes how the table name is generated

type Contact

type Contact struct {
	Base

	ID           string        `gorm:"column:id"`
	Active       bool          `gorm:"column:active"`
	ContactType  string        `gorm:"column:contact_type"`
	ContactValue string        `gorm:"column:contact_value"`
	Flavour      enums.Flavour `gorm:"column:flavour"`
	UserID       *string       `gorm:"column:user_id"`
}

Contact is a contact model for a user

func (*Contact) BeforeCreate

func (c *Contact) BeforeCreate(tx *gorm.DB) (err error)

BeforeCreate is a hook run before creating user contact

func (Contact) TableName

func (Contact) TableName() string

TableName customizes how the table name is generated

type Create

type Create interface {
	RegisterUser(ctx context.Context, user *User, contact *Contact, identifier *Identifier) error
	SaveOTP(ctx context.Context, otp *OTP) error
	SavePIN(ctx context.Context, pinData *UserPIN) (bool, error)
	CreateResidence(ctx context.Context, payload Residence) (*Residence, error)
	CreateHouse(ctx context.Context, house *House) error
}

Create holds all the database record creation methods

type House

type House struct {
	Base

	ID          string  `gorm:"column:id;primary_key"`
	Active      bool    `gorm:"column:active"`
	Number      string  `gorm:"column:number"`
	Category    string  `gorm:"column:category"`
	Class       string  `gorm:"column:class"` // applicable where houses maybe charged differently due to size
	RentValue   float64 `gorm:"column:rent_value"`
	State       string  `gorm:"column:state"`
	ResidenceID string  `gorm:"column:residence_id"`
}

House models the datastore entity for a house

func (*House) BeforeCreate

func (h *House) BeforeCreate(tx *gorm.DB) (err error)

BeforeCreate is a hook run before creating a house

func (House) TableName

func (House) TableName() string

TableName customizes how the table name is generated

type HouseClient

type HouseClient struct {
	ID       string `gorm:"column:id"`
	HouseID  string `gorm:"column:house_id"`
	TenantID string `gorm:"column:tenant_id"`
}

House models the relationship between a tenant and the living house

func (*HouseClient) BeforeCreate

func (h *HouseClient) BeforeCreate(tx *gorm.DB) (err error)

BeforeCreate is a hook run before creating a house client

func (HouseClient) TableName

func (HouseClient) TableName() string

TableName customizes how the table name is generated

type Identifier

type Identifier struct {
	Base

	ID              string               `gorm:"column:id"`
	Active          bool                 `gorm:"column:active"`
	IdentifierType  enums.IdentifierType `gorm:"column:identifier_type"`
	IdentifierValue string               `gorm:"column:identifier_value"`
	UserID          *string              `gorm:"column:user_id"`
}

Identifiers models the identifier that may be used in the system

func (*Identifier) BeforeCreate

func (i *Identifier) BeforeCreate(tx *gorm.DB) (err error)

BeforeCreate is a hook run before creating an identifier

func (Identifier) TableName

func (Identifier) TableName() string

TableName customizes how the table name is generated

type OTP

type OTP struct {
	Base

	ID          string        `gorm:"column:id"`
	IsValid     bool          `gorm:"column:is_valid"`
	ValidUntil  time.Time     `gorm:"column:valid_until"`
	PhoneNumber string        `gorm:"column:phone_number"`
	OTP         string        `gorm:"column:otp"`
	Flavour     enums.Flavour `gorm:"column:flavour"`
	Medium      string        `gorm:"column:medium"`
	UserID      string        `gorm:"column:user_id"`
}

OTP is model for one time password

func (*OTP) BeforeCreate

func (o *OTP) BeforeCreate(tx *gorm.DB) (err error)

BeforeCreate is a hook run before creating an OTP

func (OTP) TableName

func (OTP) TableName() string

TableName customizes how the table name is generated

type PGInstance

type PGInstance struct {
	DB *gorm.DB
}

PGInstance box for postgres client. We use this instead of a global variable

func NewPGInstance

func NewPGInstance() (*PGInstance, error)

NewPGInstance creates a new instance of postgres client

func (*PGInstance) CreateHouse

func (db *PGInstance) CreateHouse(ctx context.Context, house *House) error

CreateHouse creates a new house

func (*PGInstance) CreateResidence

func (db *PGInstance) CreateResidence(ctx context.Context, payload Residence) (*Residence, error)

CreateResidence creates a new residence

func (*PGInstance) GetHouseByNumber

func (db *PGInstance) GetHouseByNumber(ctx context.Context, houseNumber string) (*House, error)

GetHouseByNumber fetches a house using the house number

func (*PGInstance) GetResidenceByID

func (db *PGInstance) GetResidenceByID(ctx context.Context, residenceID string) (*Residence, error)

GetResidenceByID fetches a residence using the residence ID

func (*PGInstance) GetUserPINByUserID

func (db *PGInstance) GetUserPINByUserID(ctx context.Context, userID string, flavour enums.Flavour) (*UserPIN, error)

GetUserPINByUserID fetches a user's pin using the user ID and Flavour

func (*PGInstance) GetUserProfileByPhoneNumber

func (db *PGInstance) GetUserProfileByPhoneNumber(ctx context.Context, phoneNumber string, flavour enums.Flavour) (*User, error)

GetUserProfileByPhoneNumber fetches a user profile using the phone number

func (*PGInstance) GetUserProfileByUserID

func (db *PGInstance) GetUserProfileByUserID(ctx context.Context, userID *string) (*User, error)

GetUserProfileByUserID fetches a user profile using the user ID

func (*PGInstance) GetUserResidencesByUserID

func (db *PGInstance) GetUserResidencesByUserID(ctx context.Context, userID string) ([]*UserResidence, error)

GetUserResidencesByUserID fetches a user's residences using the user ID

func (*PGInstance) InvalidatePIN

func (db *PGInstance) InvalidatePIN(ctx context.Context, userID string, flavour enums.Flavour) (bool, error)

InvalidatePIN invalidates a pin that is linked to the user profile when a new one is created

func (*PGInstance) ListHousesInResidence

func (db *PGInstance) ListHousesInResidence(ctx context.Context, residenceID string) ([]*House, error)

ListHousesInResidence lists all the houses in a residence

func (*PGInstance) RegisterUser

func (db *PGInstance) RegisterUser(ctx context.Context, user *User, contact *Contact, identifier *Identifier) error

RegisterUser creates a new user record. The user can be a resident or a staff member

func (*PGInstance) SaveOTP

func (db *PGInstance) SaveOTP(ctx context.Context, otp *OTP) error

SaveOTP saves an OTP in the database

func (*PGInstance) SavePIN

func (db *PGInstance) SavePIN(ctx context.Context, pinData *UserPIN) (bool, error)

SavePIN saves a pin in the database

func (*PGInstance) SearchUser

func (db *PGInstance) SearchUser(ctx context.Context, searchTerm string) ([]*User, error)

SearchUser searches for a user using the search term

func (*PGInstance) UpdateUser

func (db *PGInstance) UpdateUser(ctx context.Context, user *User, updateData map[string]interface{}) (bool, error)

UpdateUser updates a user record

type Query

type Query interface {
	GetUserProfileByUserID(ctx context.Context, userID *string) (*User, error)
	GetUserProfileByPhoneNumber(ctx context.Context, phoneNumber string, flavour enums.Flavour) (*User, error)
	GetUserPINByUserID(ctx context.Context, userID string, flavour enums.Flavour) (*UserPIN, error)
	GetUserResidencesByUserID(ctx context.Context, userID string) ([]*UserResidence, error)
	GetResidenceByID(ctx context.Context, residenceID string) (*Residence, error)
	SearchUser(ctx context.Context, searchTerm string) ([]*User, error)
	GetHouseByNumber(ctx context.Context, houseNumber string) (*House, error)
	ListHousesInResidence(ctx context.Context, residenceID string) ([]*House, error)
}

Query holds all the database record query methods

type Residence

type Residence struct {
	Base

	ID                 string `gorm:"column:id"`
	Active             bool   `gorm:"column:active"`
	Name               string `gorm:"column:name"`
	RegistrationNumber string `gorm:"column:registration_number"`
	Location           string `gorm:"column:location"`
	LivingRoomsCount   int    `gorm:"column:living_rooms_count"`
	Owner              string `gorm:"column:owner"`
}

Residence models the residence's table

func (*Residence) BeforeCreate

func (r *Residence) BeforeCreate(tx *gorm.DB) (err error)

BeforeCreate is a hook run before creating a residence

func (Residence) TableName

func (Residence) TableName() string

TableName customizes how the table name is generated

type Update

type Update interface {
	InvalidatePIN(ctx context.Context, userID string, flavour enums.Flavour) (bool, error)
	UpdateUser(ctx context.Context, user *User, updateData map[string]interface{}) (bool, error)
}

Update holds all the database record update methods

type User

type User struct {
	Base

	ID               *string       `gorm:"column:id"`
	FirstName        string        `gorm:"column:first_name"`
	MiddleName       string        `gorm:"column:middle_name"`
	LastName         string        `gorm:"column:last_name"`
	Active           bool          `gorm:"column:active"`
	Flavour          enums.Flavour `gorm:"column:flavour"`
	UserName         string        `gorm:"column:username"`
	UserType         string        `gorm:"column:user_type"`
	DeviceToken      string        `gorm:"column:device_token"`
	Residence        string        `gorm:"column:residence"`
	UserContact      Contact       `gorm:"ForeignKey:UserID;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;not null"`
	UserIdentifier   Identifier    `gorm:"ForeignKey:UserID;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;not null"`
	CurrentResidence *string       `gorm:"column:current_residence"`
	CurrentHouse     *string       `gorm:"column:current_house"`
}

User models the system user

func (*User) BeforeCreate

func (u *User) BeforeCreate(tx *gorm.DB) (err error)

BeforeCreate is a hook run before creating a user

func (User) TableName

func (User) TableName() string

TableName customizes how the table name is generated

type UserPIN

type UserPIN struct {
	Base

	ID        string        `gorm:"column:id"`
	Active    bool          `gorm:"column:active"`
	Flavour   enums.Flavour `gorm:"column:flavour"`
	ValidFrom time.Time     `gorm:"column:valid_from"`
	ValidTo   time.Time     `gorm:"column:valid_to"`
	HashedPIN string        `gorm:"column:hashed_pin"`
	Salt      string        `gorm:"column:salt"`
	UserID    string        `gorm:"column:user_id"`
}

UserPIN models the user's PIN table

func (*UserPIN) BeforeCreate

func (u *UserPIN) BeforeCreate(tx *gorm.DB) (err error)

BeforeCreate is a hook run before creating user PIN

func (UserPIN) TableName

func (UserPIN) TableName() string

TableName customizes how the table name is generated

type UserResidence

type UserResidence struct {
	ID          string `gorm:"column:id"`
	UserID      string `gorm:"column:user_id"`
	ResidenceID string `gorm:"column:residence_id"`
}

UserResidence models the relationship between a user and the residence they live in

func (*UserResidence) BeforeCreate

func (ur *UserResidence) BeforeCreate(tx *gorm.DB) (err error)

BeforeCreate is a hook run before creating a house client

func (UserResidence) TableName

func (UserResidence) TableName() string

TableName customizes how the table name is generated

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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