WIP birthdays.
This commit is contained in:
parent
82cfcc7ce2
commit
3086da9284
3 changed files with 139 additions and 18 deletions
|
|
@ -2,7 +2,6 @@ package models
|
|||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/jmoiron/sqlx"
|
||||
"github.com/mattn/go-sqlite3"
|
||||
|
|
@ -11,7 +10,7 @@ import (
|
|||
|
||||
type Birthday struct {
|
||||
ID int `db:"id"`
|
||||
UserID int `db:"userid"`
|
||||
UserID *int `db:"userid"`
|
||||
ChatID int64 `db:"chatid"`
|
||||
Name string `db:"name"`
|
||||
Month int `db:"month"`
|
||||
|
|
@ -19,7 +18,7 @@ type Birthday struct {
|
|||
Delta int `db:"leap_delta"`
|
||||
|
||||
// Birthdate; computed field.
|
||||
Birthdate *time.Time `db:"birthdate"`
|
||||
Birthdate string `db:"birthdate"`
|
||||
}
|
||||
|
||||
type BirthdayModel struct {
|
||||
|
|
@ -56,17 +55,25 @@ func (bm *BirthdayModel) AddBirthdayByUserID(chatid int64, userid int, month, da
|
|||
}
|
||||
|
||||
func (bm *BirthdayModel) BirthdaysThisWeek(chatid int64) ([]*Birthday, error) {
|
||||
var bdays []*Birthday
|
||||
bdays := []*Birthday{}
|
||||
q := `select
|
||||
id, userid, chatid, name, leap_delta,
|
||||
date(concat(strftime('%Y', date('now')), '-',
|
||||
date(concat(strftime('%Y', date('now', 'localtime')), '-',
|
||||
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 {
|
||||
birthdate >= date('now', 'localtime', 'weekday 0') and
|
||||
birthdate <= date('now', 'localtime', 'weekday 0', '+7 days')) order by birthdate asc;`
|
||||
err := bm.db.SelectContext(bm.ctx, &bdays, q, chatid)
|
||||
if err != nil {
|
||||
var serr sqlite3.Error
|
||||
if errors.As(err, &serr) {
|
||||
switch serr.Code {
|
||||
case sqlite3.ErrMismatch:
|
||||
return nil, ErrNoRows.Do(err)
|
||||
}
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
|
@ -77,22 +84,26 @@ 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')), '-',
|
||||
date(concat(strftime('%Y',
|
||||
date('now', 'localtime')), '-',
|
||||
format('%02d', month), '-',
|
||||
format('%02d', day))) as birthdate
|
||||
from birthdays where
|
||||
birthdate == date('now');`
|
||||
chatid = ? and
|
||||
birthdate == date('now', 'localtime')
|
||||
order by name;`
|
||||
|
||||
if err := bm.db.GetContext(bm.ctx, &bdays, q, chatid); err != nil {
|
||||
err := bm.db.SelectContext(bm.ctx, &bdays, q, chatid)
|
||||
if err != nil {
|
||||
var serr sqlite3.Error
|
||||
if errors.As(err, &serr) {
|
||||
switch serr.Code {
|
||||
case sqlite3.ErrMismatch:
|
||||
return nil, ErrNoRows.Do(err)
|
||||
}
|
||||
}
|
||||
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