From d436a8c34450db781faf19b10a1f1f076c7e1600 Mon Sep 17 00:00:00 2001 From: Matthew Dillon Date: Wed, 3 Jun 2015 16:55:05 -0800 Subject: [PATCH] Species update and create --- handlers.go | 2 ++ species.go | 31 +++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/handlers.go b/handlers.go index 72b0641..ec479a8 100644 --- a/handlers.go +++ b/handlers.go @@ -94,7 +94,9 @@ func Handler() http.Handler { r{handleLister(CharacteristicService{}), "GET", "/characteristics"}, r{handleGetter(CharacteristicService{}), "GET", "/characteristics/{Id:.+}"}, r{handleLister(SpeciesService{}), "GET", "/species"}, + r{handleCreater(SpeciesService{}), "POST", "/species"}, r{handleGetter(SpeciesService{}), "GET", "/species/{Id:.+}"}, + r{handleUpdater(SpeciesService{}), "PUT", "/species/{Id:.+}"}, } for _, route := range routes { diff --git a/species.go b/species.go index f8f02e0..83d0ae8 100644 --- a/species.go +++ b/species.go @@ -121,3 +121,34 @@ func (s SpeciesService) get(id int64, genus string) (entity, error) { } return &species, nil } + +func (s SpeciesService) update(id int64, e *entity, claims Claims) error { + species := (*e).(*Species) + species.UpdatedBy = claims.Sub + species.UpdatedAt = currentTime() + species.Id = id + + count, err := DBH.Update(species.SpeciesBase) + if err != nil { + return err + } + if count != 1 { + return ErrSpeciesNotUpdated + } + return nil +} + +func (s SpeciesService) create(e *entity, claims Claims) error { + species := (*e).(*Species) + ct := currentTime() + species.CreatedBy = claims.Sub + species.CreatedAt = ct + species.UpdatedBy = claims.Sub + species.UpdatedAt = ct + + err := DBH.Insert(species.SpeciesBase) + if err != nil { + return err + } + return nil +}