Added birthdays for the week/day derived from month/day only.
Not tested with leap year birthdays (and probably won't work for obvious reasons). This query is a bit crazy.
This commit is contained in:
parent
e26f39a823
commit
82cfcc7ce2
1 changed files with 44 additions and 1 deletions
|
|
@ -2,6 +2,7 @@ package models
|
|||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/jmoiron/sqlx"
|
||||
"github.com/mattn/go-sqlite3"
|
||||
|
|
@ -16,6 +17,9 @@ type Birthday struct {
|
|||
Month int `db:"month"`
|
||||
Day int `db:"day"`
|
||||
Delta int `db:"leap_delta"`
|
||||
|
||||
// Birthdate; computed field.
|
||||
Birthdate *time.Time `db:"birthdate"`
|
||||
}
|
||||
|
||||
type BirthdayModel struct {
|
||||
|
|
@ -51,5 +55,44 @@ func (bm *BirthdayModel) AddBirthdayByUserID(chatid int64, userid int, month, da
|
|||
return nil, errors.NewError("not implemented")
|
||||
}
|
||||
|
||||
//func (bm *BirthdayModel) ListBirthdays
|
||||
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));
|
||||
|
||||
*/
|
||||
|
|
|
|||
Loading…
Reference in a new issue