Documentation
¶
Overview ¶
Package databaser provides SQLite database connection and operations.
Index ¶
- Variables
- func InTransaction(ctx context.Context, db *DB, f func(tx *sqlx.Tx) error) error
- func SaveManyEventsTx(ctx context.Context, tx *sqlx.Tx, events []*Event) error
- func SaveManyHolidaysTx(ctx context.Context, tx *sqlx.Tx, holidays []Holiday) error
- type DB
- func (db *DB) ApproveUser(ctx context.Context, userID int64) error
- func (db *DB) Close() error
- func (db *DB) DeleteUser(ctx context.Context, userID int64) error
- func (db *DB) GetAllEvents(ctx context.Context, limit, offset int) ([]Event, error)
- func (db *DB) GetApprovedUsers(ctx context.Context) ([]User, error)
- func (db *DB) GetEvents(ctx context.Context, period time.Duration) ([]Event, error)
- func (db *DB) GetHolidays(ctx context.Context, year int, location *time.Location) ([]Holiday, error)
- func (db *DB) GetPendingUsers(ctx context.Context) ([]User, error)
- func (db *DB) GetUser(ctx context.Context, userID int64) (*User, error)
- func (db *DB) GetUsers(ctx context.Context) ([]User, error)
- func (db *DB) Init(ctx context.Context) error
- func (db *DB) RejectUser(ctx context.Context, userID int64) error
- func (db *DB) SaveEvent(ctx context.Context, event Event) error
- func (db *DB) SaveManyEvents(ctx context.Context, events []Event) error
- type DateOnly
- func (d *DateOnly) After(u *DateOnly) bool
- func (d *DateOnly) Before(u *DateOnly) bool
- func (d *DateOnly) Date() (int, time.Month, int)
- func (d *DateOnly) EndOfYear() string
- func (d *DateOnly) Format(layout string) string
- func (d *DateOnly) Scan(value any) error
- func (d *DateOnly) SetLocation(loc *time.Location)
- func (d *DateOnly) StartOfYear() string
- func (d *DateOnly) String() string
- func (d *DateOnly) Time() time.Time
- func (d *DateOnly) Value() (driver.Value, error)
- type Event
- type Holiday
- type User
Constants ¶
This section is empty.
Variables ¶
var ErrUserNotFound = errors.New("user not found")
ErrUserNotFound is returned when a user operation fails because the user doesn't exist.
Functions ¶
func InTransaction ¶
InTransaction executes the given function within a database transaction.
func SaveManyEventsTx ¶
SaveManyEventsTx stores multiple events in the database within a transaction.
Types ¶
type DB ¶
DB wraps sqlx.DB for database operations.
func (*DB) ApproveUser ¶ added in v0.1.0
ApproveUser sets the approved flag to true for a user by ID.
func (*DB) DeleteUser ¶ added in v0.1.0
DeleteUser removes a user by ID from the database.
func (*DB) GetAllEvents ¶ added in v0.0.13
GetAllEvents retrieves all events with pagination.
func (*DB) GetApprovedUsers ¶ added in v0.1.0
GetApprovedUsers retrieves all approved users from the database.
func (*DB) GetHolidays ¶ added in v0.0.12
func (db *DB) GetHolidays(ctx context.Context, year int, location *time.Location) ([]Holiday, error)
GetHolidays retrieves holidays for the specified year and location.
func (*DB) GetPendingUsers ¶ added in v0.1.0
GetPendingUsers retrieves all pending users from the database.
func (*DB) RejectUser ¶ added in v0.1.0
RejectUser sets the approved flag to false for a user by ID.
type DateOnly ¶ added in v0.0.12
DateOnly is a custom type that stores only the date part of time.Time.
func (*DateOnly) EndOfYear ¶ added in v0.0.12
EndOfYear returns the last day of the year in "2006-01-02" format.
func (*DateOnly) SetLocation ¶ added in v0.0.12
SetLocation sets the time zone location while preserving the date.
func (*DateOnly) StartOfYear ¶ added in v0.0.12
StartOfYear returns the first day of the year in "2006-01-02" format.
type Event ¶
type Event struct {
Timestamp time.Time `db:"timestamp"`
Load uint8 `db:"load"`
Predict float64 `db:"-"`
}
Event represents a load event with a timestamp and load percentage.
func NewEventFromCSVRecord ¶
NewEventFromCSVRecord creates an Event from a CSV record.
type Holiday ¶ added in v0.0.12
type Holiday struct {
Created time.Time `db:"created"`
Day *DateOnly `db:"day"`
Title string `db:"title"`
}
Holiday represents a holiday with a date and title.
type User ¶ added in v0.1.0
type User struct {
Created time.Time `db:"created"`
Updated time.Time `db:"updated"`
Username string `db:"username"`
FirstName string `db:"first_name"`
LastName string `db:"last_name"`
ID int64 `db:"id"`
Status uint8 `db:"status"`
}
User represents a user in the database.
func GetOrCreateUser ¶ added in v0.1.0
func GetOrCreateUser(ctx context.Context, tx *sqlx.Tx, id int64, username, firstName, lastName string) (*User, error)
GetOrCreateUser retrieves a user by ID or creates a new one if not found.
func (*User) IsApproved ¶ added in v0.1.0
IsApproved checks if the user is approved.
func (*User) IsRejected ¶ added in v0.1.0
IsRejected checks if the user is rejected.