This repository has been archived on 2025-03-30. You can view files and clone it, but cannot push or open issues or pull requests.
bactdb/datastore/observations.go
2014-11-20 10:36:21 -09:00

36 lines
840 B
Go

package datastore
import (
"time"
"github.com/thermokarst/bactdb/models"
)
func init() {
DB.AddTableWithName(models.Observation{}, "observations").SetKeys(true, "Id")
}
type observationsStore struct {
*Datastore
}
func (s *observationsStore) Get(id int64) (*models.Observation, error) {
var observation []*models.Observation
if err := s.dbh.Select(&observation, `SELECT * FROM observations WHERE id=$1;`, id); err != nil {
return nil, err
}
if len(observation) == 0 {
return nil, models.ErrObservationNotFound
}
return observation[0], nil
}
func (s *observationsStore) Create(observation *models.Observation) (bool, error) {
currentTime := time.Now()
observation.CreatedAt = currentTime
observation.UpdatedAt = currentTime
if err := s.dbh.Insert(observation); err != nil {
return false, err
}
return true, nil
}