@ -22,7 +22,7 @@ func (s *Store) AddAbsence(ctx context.Context, session time.Time, userId string
return fmt . Errorf ( "failed to prepare absence query: %w" , err )
return fmt . Errorf ( "failed to prepare absence query: %w" , err )
}
}
_ , err = stmt . ExecContext ( ctx , session . Format ( "2006-01-02" ) , userId )
_ , err = stmt . ExecContext ( ctx , session . Format ( time . DateOnly ) , userId )
if err != nil {
if err != nil {
return fmt . Errorf ( "failed to insert absence: %w" , err )
return fmt . Errorf ( "failed to insert absence: %w" , err )
}
}
@ -42,7 +42,7 @@ func (s *Store) GetAbsentees(ctx context.Context, session time.Time) ([]string,
return nil , fmt . Errorf ( "failed to prepare query: %w" , err )
return nil , fmt . Errorf ( "failed to prepare query: %w" , err )
}
}
rows , err := stmt . QueryContext ( ctx , session . Format ( "2006-01-02" ) )
rows , err := stmt . QueryContext ( ctx , session . Format ( time . DateOnly ) )
if err != nil {
if err != nil {
return nil , fmt . Errorf ( "failed to execute query: %w" , err )
return nil , fmt . Errorf ( "failed to execute query: %w" , err )
}
}
@ -61,12 +61,13 @@ func (s *Store) GetAbsentees(ctx context.Context, session time.Time) ([]string,
return absentees , nil
return absentees , nil
}
}
type Foo struct {
// map session_date -> list of absentees
session_date string
type Schedule map [ string ] [ ] string
userid string
}
func ( s * Store ) GetSchedule ( ctx context . Context , from , to time . Time ) ( Schedule , error ) {
schedule := make ( Schedule )
initSchedule ( schedule , from , to )
func ( s * Store ) GetSchedule ( ctx context . Context , from , to time . Time ) ( [ ] Foo , error ) {
tx , err := s . db . Begin ( )
tx , err := s . db . Begin ( )
if err != nil {
if err != nil {
return nil , fmt . Errorf ( "failed to begin transaction: %w" , err )
return nil , fmt . Errorf ( "failed to begin transaction: %w" , err )
@ -78,21 +79,32 @@ func (s *Store) GetSchedule(ctx context.Context, from, to time.Time) ([]Foo, err
return nil , fmt . Errorf ( "failed to prepare query: %w" , err )
return nil , fmt . Errorf ( "failed to prepare query: %w" , err )
}
}
rows , err := stmt . QueryContext ( ctx , from . Format ( "2006-01-02" ) , to . Format ( "2006-01-02" ) )
rows , err := stmt . QueryContext ( ctx , from . Format ( time . DateOnly ) , to . Format ( time . DateOnly ) )
if err != nil {
if err != nil {
return nil , fmt . Errorf ( "failed to execute query: %w" , err )
return nil , fmt . Errorf ( "failed to execute query: %w" , err )
}
}
schedule := make ( [ ] Foo , 0 )
for rows . Next ( ) {
for rows . Next ( ) {
var foo Foo
var date string
err = rows . Scan ( & foo . session_date , & foo . userid )
var user string
err = rows . Scan ( & date , & user )
if err != nil {
if err != nil {
return nil , fmt . Errorf ( "failed to scan row: %w" , err )
return nil , fmt . Errorf ( "failed to scan row: %w" , err )
}
}
schedule = append ( schedule , foo )
if _ , ok := schedule [ date ] ; ok {
schedule [ date ] = append ( schedule [ date ] , user )
} else {
schedule [ date ] = [ ] string { user }
}
}
}
return schedule , nil
return schedule , nil
}
}
func initSchedule ( schedule Schedule , from , to time . Time ) {
for from . Before ( to ) || from . Equal ( to ) {
schedule [ from . Format ( time . DateOnly ) ] = [ ] string { }
from = from . AddDate ( 0 , 0 , 7 )
}
}