Initial birthday model.
This commit is contained in:
parent
4690ad0cd8
commit
e26f39a823
1 changed files with 55 additions and 0 deletions
55
internal/models/birthdays.go
Normal file
55
internal/models/birthdays.go
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
package models
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"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"`
|
||||
}
|
||||
|
||||
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) ListBirthdays
|
||||
//month >= cast(strftime('%m', date('now', 'weekday 0')) as integer) and month <= cast(strftime('%m', date(date('now', 'weekday 0'), '+7 days')) as integer)
|
||||
Loading…
Reference in a new issue