Get a single characteristic
This commit is contained in:
parent
31a91d64d7
commit
b774da0bfb
2 changed files with 41 additions and 2 deletions
|
@ -4,8 +4,8 @@ import (
|
|||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
|
@ -54,7 +54,6 @@ func serveCharacteristicsList(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
|
||||
opt.Genus = mux.Vars(r)["genus"]
|
||||
log.Printf("%v", opt)
|
||||
|
||||
characteristics, err := dbGetCharacteristics(&opt)
|
||||
if err != nil {
|
||||
|
@ -73,6 +72,27 @@ func serveCharacteristicsList(w http.ResponseWriter, r *http.Request) {
|
|||
w.Write(data)
|
||||
}
|
||||
|
||||
func serveCharacteristic(w http.ResponseWriter, r *http.Request) {
|
||||
id, err := strconv.ParseInt(mux.Vars(r)["Id"], 10, 0)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
characteristic, err := dbGetCharacteristic(id, mux.Vars(r)["genus"])
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
data, err := json.Marshal(CharacteristicJSON{Characteristic: characteristic})
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
||||
w.Write(data)
|
||||
}
|
||||
|
||||
func dbGetCharacteristics(opt *CharacteristicListOptions) ([]*Characteristic, error) {
|
||||
if opt == nil {
|
||||
return nil, errors.New("must provide options")
|
||||
|
@ -110,3 +130,21 @@ func dbGetCharacteristics(opt *CharacteristicListOptions) ([]*Characteristic, er
|
|||
}
|
||||
return characteristics, nil
|
||||
}
|
||||
|
||||
func dbGetCharacteristic(id int64, genus string) (*Characteristic, error) {
|
||||
var characteristic Characteristic
|
||||
sql := `SELECT c.*, ct.characteristic_type_name,
|
||||
array_agg(m.id) AS measurements, array_agg(st.id) AS strains
|
||||
FROM characteristics c
|
||||
INNER JOIN characteristic_types ct ON ct.id=characteristic_type_id
|
||||
LEFT OUTER JOIN measurements m ON m.characteristic_id=c.id
|
||||
LEFT OUTER JOIN strains st ON st.id=m.strain_id
|
||||
INNER JOIN species sp ON sp.id=st.species_id
|
||||
INNER JOIN genera g ON g.id=sp.genus_id AND LOWER(g.genus_name)=$1
|
||||
WHERE c.id=$2
|
||||
GROUP BY c.id, ct.characteristic_type_name;`
|
||||
if err := DBH.SelectOne(&characteristic, sql, genus, id); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &characteristic, nil
|
||||
}
|
||||
|
|
|
@ -28,6 +28,7 @@ func Handler() http.Handler {
|
|||
|
||||
// Characteristics
|
||||
s.Handle("/characteristics", authHandler(serveCharacteristicsList)).Methods("GET")
|
||||
s.Handle("/characteristics/{Id:.+}", authHandler(serveCharacteristic)).Methods("GET")
|
||||
|
||||
return corsHandler(m)
|
||||
}
|
||||
|
|
Reference in a new issue