Not tested with leap year birthdays (and probably won't work for obvious reasons). This query is a bit crazy.
98 lines
2.8 KiB
Go
98 lines
2.8 KiB
Go
package models
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/jmoiron/sqlx"
|
|
"github.com/mattn/go-sqlite3"
|
|
"go.destrealm.net/errors"
|
|
)
|
|
|
|
type Birthday struct {
|
|
ID int `db:"id"`
|
|
UserID int `db:"userid"`
|
|
ChatID int64 `db:"chatid"`
|
|
Name string `db:"name"`
|
|
Month int `db:"month"`
|
|
Day int `db:"day"`
|
|
Delta int `db:"leap_delta"`
|
|
|
|
// Birthdate; computed field.
|
|
Birthdate *time.Time `db:"birthdate"`
|
|
}
|
|
|
|
type BirthdayModel struct {
|
|
ctx context.Context
|
|
db *sqlx.DB
|
|
}
|
|
|
|
func NewBirthdayModel(ctx context.Context, db *sqlx.DB) *BirthdayModel {
|
|
return &BirthdayModel{ctx, db}
|
|
}
|
|
|
|
func (bm *BirthdayModel) AddBirthdayByName(chatid int64, name string, month, day, delta int) (*Birthday, error) {
|
|
bday := &Birthday{}
|
|
q := `insert into birthdays (chatid, name, month, day, leap_delta) values (?, ?, ?, ?, ?)`
|
|
err := bm.db.GetContext(bm.ctx, bday, q, chatid, name, month, day, delta)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if err != nil {
|
|
var serr sqlite3.Error
|
|
if errors.As(err, &serr) {
|
|
switch serr.ExtendedCode {
|
|
case sqlite3.ErrConstraintUnique:
|
|
return nil, ErrUniqueConstraint.Do(err)
|
|
}
|
|
}
|
|
return nil, err
|
|
}
|
|
return bday, nil
|
|
}
|
|
|
|
func (bm *BirthdayModel) AddBirthdayByUserID(chatid int64, userid int, month, day int) (*Birthday, error) {
|
|
return nil, errors.NewError("not implemented")
|
|
}
|
|
|
|
func (bm *BirthdayModel) BirthdaysThisWeek(chatid int64) ([]*Birthday, error) {
|
|
var bdays []*Birthday
|
|
q := `select
|
|
id, userid, chatid, name, leap_delta,
|
|
date(concat(strftime('%Y', date('now')), '-',
|
|
format('%02d', month), '-',
|
|
format('%02d', day))) as birthdate
|
|
from birthdays where
|
|
chatid = ? and (
|
|
birthdate >= date('now', 'weekday 0') and
|
|
birthdate <= date('now', 'weekday 0', '+7 days'));`
|
|
if err := bm.db.GetContext(bm.ctx, &bdays, q, chatid); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return bdays, nil
|
|
}
|
|
|
|
func (bm *BirthdayModel) BirthdaysToday(chatid int64) ([]*Birthday, error) {
|
|
var bdays []*Birthday
|
|
q := `select
|
|
id, userid, chatid, name, leap_delta,
|
|
date(concat(strftime('%Y', date('now')), '-',
|
|
format('%02d', month), '-',
|
|
format('%02d', day))) as birthdate
|
|
from birthdays where
|
|
birthdate == date('now');`
|
|
|
|
if err := bm.db.GetContext(bm.ctx, &bdays, q, chatid); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return bdays, nil
|
|
}
|
|
|
|
//month >= cast(strftime('%m', date('now', 'weekday 0')) as integer) and month <= cast(strftime('%m', date(date('now', 'weekday 0'), '+7 days')) as integer)
|
|
|
|
/*
|
|
select * from birthdays where (day >= cast(strftime('%e', date('now', 'weekday 0')) as integer) and day <= max(cast(strftime('%e', date('now', 'weekday 0', '+7 days')) as integer), 31)) or (day >= min(cast(strftime('%e', date('now', 'weekday 0')) as integer), 1) and day <= max(cast(strftime('%e', date('now', 'weekday 0', '+7 days')) as integer), 31));
|
|
|
|
*/
|