Drop characteristic type (for now, might bring back)
This commit is contained in:
parent
2ad00f9aa7
commit
f948fdaadb
3 changed files with 12 additions and 122 deletions
|
@ -1,112 +0,0 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrCharacteristicTypeNotFound = errors.New("Characteristic Type not found")
|
||||
ErrCharacteristicTypeNotFoundJSON = newJSONError(ErrCharacteristicTypeNotFound, http.StatusNotFound)
|
||||
)
|
||||
|
||||
func init() {
|
||||
DB.AddTableWithName(CharacteristicTypeBase{}, "characteristic_types").SetKeys(true, "Id")
|
||||
}
|
||||
|
||||
type CharacteristicTypeService struct{}
|
||||
|
||||
// A CharacteristicType is a lookup type
|
||||
type CharacteristicTypeBase struct {
|
||||
Id int64 `json:"id,omitempty"`
|
||||
CharacteristicTypeName string `db:"characteristic_type_name" json:"characteristicTypeName"`
|
||||
CreatedAt NullTime `db:"created_at" json:"createdAt"`
|
||||
UpdatedAt NullTime `db:"updated_at" json:"updatedAt"`
|
||||
DeletedAt NullTime `db:"deleted_at" json:"deletedAt"`
|
||||
CreatedBy int64 `db:"created_by" json:"createdBy"`
|
||||
UpdatedBy int64 `db:"updated_by" json:"updatedBy"`
|
||||
DeletedBy NullInt64 `db:"deleted_by" json:"deletedBy"`
|
||||
}
|
||||
|
||||
type CharacteristicType struct {
|
||||
*CharacteristicTypeBase
|
||||
Characteristics NullSliceInt64 `db:"characteristics" json:"characteristics"`
|
||||
SortOrder int64 `db:"sort_order" json:"sortOrder"`
|
||||
}
|
||||
|
||||
type CharacteristicTypes []*CharacteristicType
|
||||
|
||||
type CharacteristicTypeJSON struct {
|
||||
CharacteristicType *CharacteristicType `json:"characteristicType"`
|
||||
}
|
||||
|
||||
type CharacteristicTypesJSON struct {
|
||||
CharacteristicTypes *CharacteristicTypes `json:"characteristicTypes"`
|
||||
}
|
||||
|
||||
func (c *CharacteristicType) marshal() ([]byte, error) {
|
||||
return json.Marshal(&CharacteristicTypeJSON{CharacteristicType: c})
|
||||
}
|
||||
|
||||
func (c *CharacteristicTypes) marshal() ([]byte, error) {
|
||||
return json.Marshal(&CharacteristicTypesJSON{CharacteristicTypes: c})
|
||||
}
|
||||
|
||||
func (c CharacteristicTypeService) list(val *url.Values, claims *Claims) (entity, *appError) {
|
||||
if val == nil {
|
||||
return nil, ErrMustProvideOptionsJSON
|
||||
}
|
||||
var opt ListOptions
|
||||
if err := schemaDecoder.Decode(&opt, *val); err != nil {
|
||||
return nil, newJSONError(err, http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
var vals []interface{}
|
||||
sql := `SELECT ct.*, array_agg(c.id) AS characteristics,
|
||||
rank() OVER (ORDER BY ct.characteristic_type_name) AS sort_order
|
||||
FROM characteristic_types ct
|
||||
INNER JOIN characteristics c ON c.characteristic_type_id=ct.id`
|
||||
|
||||
if len(opt.Ids) != 0 {
|
||||
var conds []string
|
||||
|
||||
c := "c.id IN ("
|
||||
for i, id := range opt.Ids {
|
||||
c = c + fmt.Sprintf("$%v,", i+1) // start param index at 1
|
||||
vals = append(vals, id)
|
||||
}
|
||||
c = c[:len(c)-1] + ")"
|
||||
conds = append(conds, c)
|
||||
sql += " WHERE (" + strings.Join(conds, ") AND (") + ")"
|
||||
}
|
||||
|
||||
sql += " GROUP BY ct.id;"
|
||||
|
||||
characteristic_types := make(CharacteristicTypes, 0)
|
||||
err := DBH.Select(&characteristic_types, sql, vals...)
|
||||
if err != nil {
|
||||
return nil, newJSONError(err, http.StatusInternalServerError)
|
||||
}
|
||||
return &characteristic_types, nil
|
||||
}
|
||||
|
||||
func (c CharacteristicTypeService) get(id int64, dummy string, claims *Claims) (entity, *appError) {
|
||||
var characteristic_type CharacteristicType
|
||||
q := `SELECT ct.*, array_agg(c.id) AS characteristics, 0 AS sort_order
|
||||
FROM characteristic_types ct
|
||||
INNER JOIN characteristics c ON c.characteristic_type_id=ct.id
|
||||
WHERE ct.id=$1
|
||||
GROUP BY ct.id;`
|
||||
if err := DBH.SelectOne(&characteristic_type, q, id); err != nil {
|
||||
if err == sql.ErrNoRows {
|
||||
return nil, ErrCharacteristicTypeNotFoundJSON
|
||||
}
|
||||
return nil, newJSONError(err, http.StatusInternalServerError)
|
||||
}
|
||||
return &characteristic_type, nil
|
||||
}
|
|
@ -25,7 +25,7 @@ type CharacteristicService struct{}
|
|||
type CharacteristicBase struct {
|
||||
Id int64 `json:"id,omitempty"`
|
||||
CharacteristicName string `db:"characteristic_name" json:"characteristicName"`
|
||||
CharacteristicTypeId int64 `db:"characteristic_type_id" json:"characteristicType"`
|
||||
CharacteristicTypeId int64 `db:"characteristic_type_id" json:"-"`
|
||||
SortOrder NullInt64 `db:"sort_order" json:"sortOrder"`
|
||||
CreatedAt NullTime `db:"created_at" json:"createdAt"`
|
||||
UpdatedAt NullTime `db:"updated_at" json:"updatedAt"`
|
||||
|
@ -37,8 +37,9 @@ type CharacteristicBase struct {
|
|||
|
||||
type Characteristic struct {
|
||||
*CharacteristicBase
|
||||
Measurements NullSliceInt64 `db:"measurements" json:"measurements"`
|
||||
Strains NullSliceInt64 `db:"strains" json:"strains"`
|
||||
Measurements NullSliceInt64 `db:"measurements" json:"measurements"`
|
||||
Strains NullSliceInt64 `db:"strains" json:"strains"`
|
||||
CharacteristicType string `db:"characteristic_type_name" json:"characteristicTypeName"`
|
||||
}
|
||||
|
||||
type Characteristics []*Characteristic
|
||||
|
@ -69,8 +70,10 @@ func (c CharacteristicService) list(val *url.Values, claims *Claims) (entity, *a
|
|||
}
|
||||
|
||||
var vals []interface{}
|
||||
sql := `SELECT c.*, array_agg(m.id) AS measurements, array_agg(st.id) AS strains
|
||||
sql := `SELECT c.*, array_agg(m.id) AS measurements,
|
||||
array_agg(st.id) AS strains, ct.characteristic_type_name
|
||||
FROM characteristics c
|
||||
INNER JOIN characteristic_types ct ON ct.id=c.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`
|
||||
|
||||
|
@ -87,7 +90,7 @@ func (c CharacteristicService) list(val *url.Values, claims *Claims) (entity, *a
|
|||
sql += " WHERE (" + strings.Join(conds, ") AND (") + ")"
|
||||
}
|
||||
|
||||
sql += " GROUP BY c.id;"
|
||||
sql += " GROUP BY c.id, ct.characteristic_type_name;"
|
||||
|
||||
characteristics := make(Characteristics, 0)
|
||||
err := DBH.Select(&characteristics, sql, vals...)
|
||||
|
@ -99,12 +102,14 @@ func (c CharacteristicService) list(val *url.Values, claims *Claims) (entity, *a
|
|||
|
||||
func (c CharacteristicService) get(id int64, dummy string, claims *Claims) (entity, *appError) {
|
||||
var characteristic Characteristic
|
||||
q := `SELECT c.*, array_agg(m.id) AS measurements, array_agg(st.id) AS strains
|
||||
q := `SELECT c.*, array_agg(m.id) AS measurements,
|
||||
array_agg(st.id) AS strains, ct.characteristic_type_name
|
||||
FROM characteristics c
|
||||
INNER JOIN characteristic_types ct ON ct.id=c.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
|
||||
WHERE c.id=$1
|
||||
GROUP BY c.id;`
|
||||
GROUP BY c.id, ct.characteristic_type_name;`
|
||||
if err := DBH.SelectOne(&characteristic, q, id); err != nil {
|
||||
if err == sql.ErrNoRows {
|
||||
return nil, ErrCharacteristicNotFoundJSON
|
||||
|
|
|
@ -75,7 +75,6 @@ func Handler() http.Handler {
|
|||
strainService := StrainService{}
|
||||
speciesService := SpeciesService{}
|
||||
characteristicService := CharacteristicService{}
|
||||
characteristicTypeService := CharacteristicTypeService{}
|
||||
measurementService := MeasurementService{}
|
||||
|
||||
m.Handle("/authenticate", tokenHandler(j.GenerateToken())).Methods("POST")
|
||||
|
@ -107,8 +106,6 @@ func Handler() http.Handler {
|
|||
r{handleUpdater(strainService), "PUT", "/strains/{Id:.+}"},
|
||||
r{handleLister(characteristicService), "GET", "/characteristics"},
|
||||
r{handleGetter(characteristicService), "GET", "/characteristics/{Id:.+}"},
|
||||
r{handleLister(characteristicTypeService), "GET", "/characteristicTypes"},
|
||||
r{handleGetter(characteristicTypeService), "GET", "/characteristicTypes/{Id:.+}"},
|
||||
r{handleLister(measurementService), "GET", "/measurements"},
|
||||
r{handleGetter(measurementService), "GET", "/measurements/{Id:.+}"},
|
||||
}
|
||||
|
|
Reference in a new issue