2018-08-24 20:51:37 -07:00
|
|
|
// Copyright 2018 Team 254. All Rights Reserved.
|
|
|
|
|
// Author: pat@patfairbank.com (Patrick Fairbank)
|
|
|
|
|
//
|
|
|
|
|
// Model and datastore CRUD methods for a schedule block at an event.
|
|
|
|
|
|
|
|
|
|
package model
|
|
|
|
|
|
|
|
|
|
import (
|
2021-05-12 18:20:01 -07:00
|
|
|
"sort"
|
2018-08-24 20:51:37 -07:00
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type ScheduleBlock struct {
|
2021-05-12 18:20:01 -07:00
|
|
|
Id int `db:"id"`
|
2018-08-24 20:51:37 -07:00
|
|
|
MatchType string
|
|
|
|
|
StartTime time.Time
|
|
|
|
|
NumMatches int
|
|
|
|
|
MatchSpacingSec int
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (database *Database) CreateScheduleBlock(block *ScheduleBlock) error {
|
2021-05-12 18:20:01 -07:00
|
|
|
return database.scheduleBlockTable.create(block)
|
2018-08-24 20:51:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (database *Database) GetScheduleBlocksByMatchType(matchType string) ([]ScheduleBlock, error) {
|
2022-04-04 20:39:19 -07:00
|
|
|
scheduleBlocks, err := database.scheduleBlockTable.getAll()
|
|
|
|
|
if err != nil {
|
2021-05-12 18:20:01 -07:00
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var matchingScheduleBlocks []ScheduleBlock
|
|
|
|
|
for _, scheduleBlock := range scheduleBlocks {
|
|
|
|
|
if scheduleBlock.MatchType == matchType {
|
|
|
|
|
matchingScheduleBlocks = append(matchingScheduleBlocks, scheduleBlock)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sort.Slice(matchingScheduleBlocks, func(i, j int) bool {
|
|
|
|
|
return matchingScheduleBlocks[i].StartTime.Before(matchingScheduleBlocks[j].StartTime)
|
|
|
|
|
})
|
|
|
|
|
return matchingScheduleBlocks, nil
|
2018-08-24 20:51:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (database *Database) DeleteScheduleBlocksByMatchType(matchType string) error {
|
2021-05-12 18:20:01 -07:00
|
|
|
scheduleBlocks, err := database.GetScheduleBlocksByMatchType(matchType)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, scheduleBlock := range scheduleBlocks {
|
|
|
|
|
if err = database.scheduleBlockTable.delete(scheduleBlock.Id); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
2018-08-24 20:51:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (database *Database) TruncateScheduleBlocks() error {
|
2021-05-12 18:20:01 -07:00
|
|
|
return database.scheduleBlockTable.truncate()
|
2018-08-24 20:51:37 -07:00
|
|
|
}
|