henwen/back/store.go

280 lines
6.0 KiB
Go
Raw Normal View History

2020-04-01 06:35:15 +00:00
package back
2020-03-24 14:59:27 +00:00
import (
"context"
2020-03-31 14:47:40 +00:00
"errors"
"log"
2020-03-24 14:59:27 +00:00
"os"
2020-03-31 14:47:40 +00:00
"crawshaw.io/sqlite"
2020-03-24 14:59:27 +00:00
"crawshaw.io/sqlite/sqlitex"
2020-04-21 14:25:15 +00:00
"github.com/rickb777/date"
2020-03-24 14:59:27 +00:00
)
2020-04-01 06:25:01 +00:00
type GenString func(length int) (string, error)
2020-03-24 14:59:27 +00:00
type Store struct {
pool *sqlitex.Pool
genString GenString
}
func NewStore(filename string, genString GenString) (*Store, error) {
var needCreate bool
2020-04-02 02:39:29 +00:00
if _, err := os.Stat(filename); os.IsNotExist(err) {
2020-03-24 14:59:27 +00:00
needCreate = true
}
// If the file exists, then assume it was created properly.
pool, err := sqlitex.Open(filename, 0, 10)
if err != nil {
return nil, err
}
store := &Store{
pool: pool,
genString: genString,
}
if needCreate {
2020-03-31 14:47:40 +00:00
log.Println("creating schema")
2020-03-24 14:59:27 +00:00
err = store.createSchema()
if err != nil {
defer pool.Close()
return nil, err
}
}
return store, nil
}
2020-04-16 20:49:28 +00:00
func NewMemoryStore(genString GenString) (*Store, error) {
pool, err := sqlitex.Open("file::memory:?mode=memory&cache=shared", 0, 10)
if err != nil {
return nil, err
}
store := &Store{
pool: pool,
genString: genString,
}
log.Println("creating schema")
err = store.createSchema()
if err != nil {
defer pool.Close()
return nil, err
}
return store, nil
}
2020-03-24 14:59:27 +00:00
func (s *Store) Close() error {
return s.pool.Close()
}
const schema = `
CREATE TABLE event (
id INTEGER PRIMARY KEY,
alpha_id TEXT NOT NULL,
2020-03-31 14:47:40 +00:00
admin_code TEXT NOT NULL,
2020-03-24 14:59:27 +00:00
name TEXT NOT NULL,
2020-04-01 04:36:41 +00:00
description TEXT NOT NULL,
2020-03-24 14:59:27 +00:00
earliest_date DATE NOT NULL,
latest_date DATE NOT NULL,
UNIQUE (alpha_id)
);
CREATE TABLE response (
id INTEGER PRIMARY KEY,
alpha_id TEXT NOT NULL,
event_id INTEGER NOT NULL,
UNIQUE (alpha_id),
FOREIGN KEY (event_id) REFERENCES event(id)
);
CREATE TABLE response_time (
response_id INTEGER PRIMARY KEY,
2020-04-17 02:36:48 +00:00
date DATE NOT NULL,
2020-03-24 14:59:27 +00:00
time INTEGER NOT NULL,
2020-04-17 02:36:48 +00:00
CHECK (0 <= time < 24),
UNIQUE (response_id, date, time)
);
CREATE TRIGGER enforce_valid_response_dates
BEFORE INSERT ON response_time
BEGIN
SELECT
CASE
WHEN NEW.date BETWEEN event.earliest_date AND event.latest_date
THEN RAISE(ABORT, 'response date is out of range')
END
FROM response
JOIN event ON response.event_id = event.id
WHERE response.id = NEW.response_id;
END;`
2020-03-24 14:59:27 +00:00
func (s *Store) createSchema() error {
conn := s.pool.Get(context.Background())
defer s.pool.Put(conn)
return sqlitex.ExecScript(conn, schema)
}
type CreateEventCommand struct {
2020-04-21 14:25:15 +00:00
Name string
Description string
Earliest, Latest date.Date
2020-03-24 14:59:27 +00:00
}
type CreateEventResult struct {
2020-03-31 14:47:40 +00:00
AlphaID, AdminCode string
2020-03-24 14:59:27 +00:00
}
2020-04-01 06:10:55 +00:00
const dbDateLayout = "2006-01-02"
2020-04-01 04:36:41 +00:00
2020-03-24 14:59:27 +00:00
func (s *Store) CreateEvent(ctx context.Context, cmd CreateEventCommand) (result CreateEventResult, err error) {
2020-04-01 06:25:01 +00:00
const alphaIDLength = 10
const adminCodeLength = 10
2020-03-24 14:59:27 +00:00
conn := s.pool.Get(ctx)
defer s.pool.Put(conn)
2020-04-01 06:25:01 +00:00
alphaID, err := s.genString(alphaIDLength)
2020-03-24 14:59:27 +00:00
if err != nil {
return
}
2020-04-01 06:25:01 +00:00
adminCode, err := s.genString(adminCodeLength)
2020-03-24 14:59:27 +00:00
if err != nil {
return
}
const query = `
2020-04-01 04:40:06 +00:00
INSERT INTO event(alpha_id, admin_code, name, description, earliest_date, latest_date)
VALUES (?, ?, ?, ?, ?, ?);`
2020-03-24 14:59:27 +00:00
err = sqlitex.Exec(conn, query, nil,
alphaID,
2020-03-31 14:47:40 +00:00
adminCode,
2020-03-24 14:59:27 +00:00
cmd.Name,
2020-04-01 04:36:41 +00:00
cmd.Description,
2020-04-21 14:25:15 +00:00
cmd.Earliest.Format(dbDateLayout),
cmd.Latest.Format(dbDateLayout),
2020-03-24 14:59:27 +00:00
)
if err != nil {
return
}
2020-03-31 14:47:40 +00:00
result.AdminCode = adminCode
2020-03-24 14:59:27 +00:00
result.AlphaID = alphaID
return
}
2020-03-31 14:47:40 +00:00
2020-04-16 13:51:24 +00:00
type GetEventMetadataQuery struct {
2020-04-14 05:29:40 +00:00
AlphaID string
2020-03-31 14:47:40 +00:00
}
2020-04-16 13:51:24 +00:00
type GetEventMetadataResult struct {
2020-04-21 14:25:15 +00:00
Name string
Description string
Earliest, Latest date.Date
2020-03-31 14:47:40 +00:00
}
2020-04-16 13:51:24 +00:00
func (s *Store) GetEventMetadata(ctx context.Context, query GetEventMetadataQuery) (GetEventMetadataResult, error) {
2020-03-31 14:47:40 +00:00
conn := s.pool.Get(ctx)
defer s.pool.Put(conn)
const dbQuery = `
2020-04-01 04:40:06 +00:00
SELECT name, description, earliest_date, latest_date
2020-03-31 14:47:40 +00:00
FROM event
2020-04-01 04:36:41 +00:00
WHERE alpha_id = ?;`
2020-04-16 13:51:24 +00:00
var result GetEventMetadataResult
2020-03-31 14:47:40 +00:00
var found bool
err := sqlitex.Exec(conn, dbQuery,
func(stmt *sqlite.Stmt) error {
found = true
2020-04-01 04:36:41 +00:00
result.Name = stmt.ColumnText(0)
result.Description = stmt.ColumnText(1)
earliestDateString := stmt.ColumnText(2)
latestDateString := stmt.ColumnText(3)
var err error
2020-04-21 14:25:15 +00:00
result.Earliest, err = date.Parse(dbDateLayout, earliestDateString)
2020-04-01 04:36:41 +00:00
if err != nil {
return err
}
2020-04-21 14:25:15 +00:00
result.Latest, err = date.Parse(dbDateLayout, latestDateString)
2020-04-01 04:36:41 +00:00
return err
}, query.AlphaID)
2020-03-31 14:47:40 +00:00
if err != nil {
2020-04-16 13:51:24 +00:00
return GetEventMetadataResult{}, err
2020-03-31 14:47:40 +00:00
}
if !found {
2020-04-14 05:29:40 +00:00
// TODO return a constant or a specific error type for Not Found
2020-04-16 13:51:24 +00:00
return GetEventMetadataResult{}, errors.New("not found")
2020-03-31 14:47:40 +00:00
}
2020-04-01 04:36:41 +00:00
return result, nil
2020-03-31 14:47:40 +00:00
}
2020-04-16 13:51:24 +00:00
type GetEventResponsesQuery struct {
AlphaID string
}
type GetEventResponsesResult struct {
TotalResponses int
}
func (s *Store) GetEventResponses(ctx context.Context, query GetEventResponsesQuery) (GetEventResponsesResult, error) {
conn := s.pool.Get(ctx)
defer s.pool.Put(conn)
var result GetEventResponsesResult
const dbQuery = `
SELECT COUNT(*)
FROM response
JOIN event ON response.event_id = event.id
WHERE event.alpha_id = ?;`
err := sqlitex.Exec(conn, dbQuery,
func(stmt *sqlite.Stmt) error {
result.TotalResponses = stmt.ColumnInt(0)
return nil
}, query.AlphaID)
// TODO return an error if the event does not exist?
return result, err
}
2020-04-17 02:36:48 +00:00
type CreateEventResponseCommand struct {
EventAlphaID string
2020-04-21 14:25:15 +00:00
DateHours map[date.Date]int
2020-04-17 02:36:48 +00:00
}
type CreateEventResponseResult struct {
ResponseAlphaID string
}
func (s *Store) CreateEventResponse(ctx context.Context, cmd CreateEventResponseCommand) (result CreateEventResponseResult, err error) {
const responseAlphaIDLength = 10
conn := s.pool.Get(ctx)
defer s.pool.Put(conn)
defer sqlitex.Save(conn)(&err)
responseAlphaID, err := s.genString(responseAlphaIDLength)
if err != nil {
return CreateEventResponseResult{}, err
}
// Create response
const responseQuery = `
INSERT INTO response(event_id, alpha_id)
SELECT event.id AS event_id, ? AS alpha_id
FROM event
WHERE event.alpha_id = ?;`
err = sqlitex.Exec(conn, responseQuery, nil, responseAlphaID, cmd.EventAlphaID)
if err != nil {
return CreateEventResponseResult{}, err
}
// TODO Create response times
return CreateEventResponseResult{
ResponseAlphaID: responseAlphaID,
}, nil
}