gorm

package
v0.0.0-...-4605876 Latest Latest
Warning

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

Go to latest
Published: Jun 25, 2023 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const (

	// GoogleProject ...
	GoogleProject = "GOOGLE_CLOUD_PROJECT_ID"
	// DatabaseRegion ...
	DatabaseRegion = "DATABASE_REGION"
	// DatabasesInstance ...
	DatabasesInstance = "DATABASE_INSTANCE"

	// DBHost ..
	DBHost = "POSTGRES_HOST"
	// DBPort ...
	DBPort = "POSTGRES_PORT"
	// DBUser ...
	DBUser = "POSTGRES_USER"
	// DBPASSWORD ...
	DBPASSWORD = "POSTGRES_PASSWORD"
	// DBName ...
	DBName = "POSTGRES_DB"
)

Variables

This section is empty.

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 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) (*User, error)
	SaveOTP(ctx context.Context, otp *OTP) (*OTP, error)
	SavePIN(ctx context.Context, pinData *UserPIN) (*UserPIN, error)

	AddProduct(ctx context.Context, product *Product) (*Product, error)
	AddSaleRecord(ctx context.Context, sale *Sale) (*Sale, error)
}

Create holds all the database record creation methods

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"`
	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) AddProduct

func (db *PGInstance) AddProduct(ctx context.Context, product *Product) (*Product, error)

Adds a product into the database

func (*PGInstance) AddSaleRecord

func (db *PGInstance) AddSaleRecord(ctx context.Context, sale *Sale) (*Sale, error)

AddSaleRecord adds sale record in the database

func (*PGInstance) GetDailySale

func (db *PGInstance) GetDailySale(ctx context.Context) ([]*Sale, error)

GetDailySale retrieves daily sales

func (*PGInstance) GetProductByID

func (db *PGInstance) GetProductByID(ctx context.Context, id string) (*Product, error)

GetProductByID retrieves a product using its 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) InvalidatePIN

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

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

func (*PGInstance) RegisterUser

func (db *PGInstance) RegisterUser(ctx context.Context, user *User, contact *Contact) (*User, 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) (*OTP, error)

SaveOTP saves an OTP in the database

func (*PGInstance) SavePIN

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

SavePIN saves a pin in the database

func (*PGInstance) SearchProduct

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

SearchProduct searches a product using the term provided by the user

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) UpdateProduct

func (db *PGInstance) UpdateProduct(ctx context.Context, product *Product, updateData map[string]interface{}) error

UpdateProduct updates product details

func (*PGInstance) UpdateUser

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

UpdateUser updates a user record

type Product

type Product struct {
	Base

	ID           string  `gorm:"column:id"`
	Active       bool    `gorm:"column:active"`
	Name         string  `gorm:"column:name"`
	Category     string  `gorm:"column:category"`
	Quantity     float64 `gorm:"column:quantity"`
	Unit         string  `gorm:"column:unit"`
	Price        float64 `gorm:"column:price"`
	VAT          float64 `gorm:"column:vat"`
	Description  string  `gorm:"column:description"`
	Manufacturer string  `gorm:"column:manufacturer"`
	InStock      bool    `gorm:"column:in_stock"`
}

Product is used to display product info

func (*Product) BeforeCreate

func (p *Product) BeforeCreate(tx *gorm.DB) (err error)

BeforeCreate is a hook run before creating an OTP

func (Product) TableName

func (Product) TableName() string

TableName customizes how the table name is generated

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)
	SearchUser(ctx context.Context, searchTerm string) ([]*User, error)

	GetProductByID(ctx context.Context, id string) (*Product, error)
	GetDailySale(ctx context.Context) ([]*Sale, error)
	SearchProduct(ctx context.Context, searchTerm string) ([]*Product, error)
}

Query holds all the database record query methods

type Sale

type Sale struct {
	Base

	ID        string  `gorm:"column:id"`
	Active    bool    `gorm:"column:active"`
	ProductID string  `gorm:"column:product_id"`
	Quantity  float64 `gorm:"column:quantity"`
	Unit      string  `gorm:"column:unit"`
	Price     float64 `gorm:"column:price"`
	Product   Product `gorm:"ForeignKey:product_id;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;not null"`
}

Sale is used to show sales data

func (*Sale) BeforeCreate

func (s *Sale) BeforeCreate(tx *gorm.DB) (err error)

BeforeCreate is a hook run before creating an OTP

func (Sale) TableName

func (Sale) TableName() string

TableName customizes how the table name is generated

type Update

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

	UpdateProduct(ctx context.Context, product *Product, updateData map[string]interface{}) 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"`
	LastName  string  `gorm:"column:last_name"`
	Active    bool    `gorm:"column:active"`
	UserName  string  `gorm:"column:username"`
	UserType  string  `gorm:"column:user_type"`
	PushToken string  `gorm:"column:push_token"`
	Email     string  `gorm:"column:email"`
	Contacts  Contact `gorm:"ForeignKey:user_id;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;not null"`
}

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"`
	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

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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