|
|
|
package themis
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (s *Store) AddAbsence(ctx context.Context, session time.Time, userId string) error {
|
|
|
|
if session.Weekday() != time.Monday {
|
|
|
|
return fmt.Errorf("not a monday")
|
|
|
|
}
|
|
|
|
|
|
|
|
tx, err := s.db.Begin()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to begin transaction: %w", err)
|
|
|
|
}
|
|
|
|
defer tx.Commit() //nolint:errcheck
|
|
|
|
|
|
|
|
stmt, err := s.db.PrepareContext(ctx, "INSERT INTO absences (session_date, userid) VALUES (?, ?)")
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to prepare absence query: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = stmt.ExecContext(ctx, session.Format("2006-01-02"), userId)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to insert absence: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Store) GetAbsentees(ctx context.Context, session time.Time) ([]string, error) {
|
|
|
|
tx, err := s.db.Begin()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to begin transaction: %w", err)
|
|
|
|
}
|
|
|
|
defer tx.Commit() //nolint:errcheck
|
|
|
|
|
|
|
|
stmt, err := s.db.PrepareContext(ctx, `SELECT userid FROM absences WHERE session_date = ?`)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to prepare query: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
rows, err := stmt.QueryContext(ctx, session.Format("2006-01-02"))
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to execute query: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
absentees := make([]string, 0)
|
|
|
|
for rows.Next() {
|
|
|
|
var abs string
|
|
|
|
err = rows.Scan(&abs)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to scan row: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
absentees = append(absentees, abs)
|
|
|
|
}
|
|
|
|
|
|
|
|
return absentees, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type Foo struct {
|
|
|
|
session_date string
|
|
|
|
userid string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Store) GetSchedule(ctx context.Context, from, to time.Time) ([]Foo, error) {
|
|
|
|
tx, err := s.db.Begin()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to begin transaction: %w", err)
|
|
|
|
}
|
|
|
|
defer tx.Commit() //nolint:errcheck
|
|
|
|
|
|
|
|
stmt, err := s.db.PrepareContext(ctx, `SELECT session_date, userid FROM absences WHERE session_date BETWEEN ? AND ? ORDER BY session_date ASC`)
|
|
|
|
if err != nil {
|
|
|
|
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"))
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to execute query: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
schedule := make([]Foo, 0)
|
|
|
|
for rows.Next() {
|
|
|
|
var foo Foo
|
|
|
|
err = rows.Scan(&foo.session_date, &foo.userid)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to scan row: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
schedule = append(schedule, foo)
|
|
|
|
}
|
|
|
|
|
|
|
|
return schedule, nil
|
|
|
|
}
|