Create new characteristic
This commit is contained in:
parent
e30089ecb0
commit
86e6c45a2f
3 changed files with 53 additions and 24 deletions
|
@ -66,6 +66,7 @@ type CharacteristicPayload struct {
|
||||||
Measurements *Measurements `json:"measurements"`
|
Measurements *Measurements `json:"measurements"`
|
||||||
Strains *Strains `json:"strains"`
|
Strains *Strains `json:"strains"`
|
||||||
Species *ManySpecies `json:"species"`
|
Species *ManySpecies `json:"species"`
|
||||||
|
Meta *CharacteristicMeta `json:"meta"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type CharacteristicsPayload struct {
|
type CharacteristicsPayload struct {
|
||||||
|
@ -228,18 +229,45 @@ func (c CharacteristicService) update(id int64, e *entity, genus string, claims
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c CharacteristicService) create(e *entity, genus string, claims *Claims) *appError {
|
||||||
|
payload := (*e).(*CharacteristicPayload)
|
||||||
|
payload.Characteristic.CreatedBy = claims.Sub
|
||||||
|
payload.Characteristic.UpdatedBy = claims.Sub
|
||||||
|
|
||||||
|
id, err := insertOrGetCharacteristicType(payload.Characteristic.CharacteristicType, claims)
|
||||||
|
if err != nil {
|
||||||
|
return newJSONError(err, http.StatusInternalServerError)
|
||||||
|
}
|
||||||
|
payload.Characteristic.CharacteristicTypeId = id
|
||||||
|
|
||||||
|
err = DBH.Insert(payload.Characteristic.CharacteristicBase)
|
||||||
|
if err != nil {
|
||||||
|
return newJSONError(err, http.StatusInternalServerError)
|
||||||
|
}
|
||||||
|
|
||||||
|
characteristic, err := getCharacteristic(payload.Characteristic.Id, genus, claims)
|
||||||
|
if err != nil {
|
||||||
|
return newJSONError(err, http.StatusInternalServerError)
|
||||||
|
}
|
||||||
|
|
||||||
|
payload.Characteristic = characteristic
|
||||||
|
payload.Meta = &CharacteristicMeta{
|
||||||
|
CanAdd: canAdd(claims),
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func listCharacteristics(opt ListOptions, claims *Claims) (*Characteristics, error) {
|
func listCharacteristics(opt ListOptions, claims *Claims) (*Characteristics, error) {
|
||||||
var vals []interface{}
|
var vals []interface{}
|
||||||
|
|
||||||
q := `SELECT c.*, array_agg(m.id) AS measurements,
|
q := `SELECT c.*, ct.characteristic_type_name,
|
||||||
array_agg(st.id) AS strains,
|
array_agg(DISTINCT st.id) AS strains, array_agg(DISTINCT m.id) AS measurements
|
||||||
ct.characteristic_type_name
|
FROM strains st
|
||||||
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
|
|
||||||
INNER JOIN species sp ON sp.id=st.species_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)=LOWER($1)`
|
INNER JOIN genera g ON g.id=sp.genus_id AND LOWER(g.genus_name)=LOWER($1)
|
||||||
|
INNER JOIN measurements m ON m.strain_id=st.id
|
||||||
|
RIGHT OUTER JOIN characteristics c ON c.id=m.characteristic_id
|
||||||
|
INNER JOIN characteristic_types ct ON ct.id=c.characteristic_type_id`
|
||||||
vals = append(vals, opt.Genus)
|
vals = append(vals, opt.Genus)
|
||||||
|
|
||||||
if len(opt.Ids) != 0 {
|
if len(opt.Ids) != 0 {
|
||||||
|
@ -357,14 +385,14 @@ func measurementsFromCharacteristicId(id int64, genus string, claims *Claims) (*
|
||||||
|
|
||||||
func getCharacteristic(id int64, genus string, claims *Claims) (*Characteristic, error) {
|
func getCharacteristic(id int64, genus string, claims *Claims) (*Characteristic, error) {
|
||||||
var characteristic Characteristic
|
var characteristic Characteristic
|
||||||
q := `SELECT c.*, array_agg(m.id) AS measurements,
|
q := `SELECT c.*, ct.characteristic_type_name,
|
||||||
array_agg(st.id) AS strains, ct.characteristic_type_name
|
array_agg(DISTINCT st.id) AS strains, array_agg(DISTINCT m.id) AS measurements
|
||||||
FROM characteristics c
|
FROM strains st
|
||||||
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
|
|
||||||
INNER JOIN species sp ON sp.id=st.species_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)=LOWER($1)
|
INNER JOIN genera g ON g.id=sp.genus_id AND LOWER(g.genus_name)=LOWER($1)
|
||||||
|
INNER JOIN measurements m ON m.strain_id=st.id
|
||||||
|
RIGHT OUTER JOIN characteristics c ON c.id=m.characteristic_id
|
||||||
|
INNER JOIN characteristic_types ct ON ct.id=c.characteristic_type_id
|
||||||
WHERE c.id=$2
|
WHERE c.id=$2
|
||||||
GROUP BY c.id, ct.characteristic_type_name;`
|
GROUP BY c.id, ct.characteristic_type_name;`
|
||||||
if err := DBH.SelectOne(&characteristic, q, genus, id); err != nil {
|
if err := DBH.SelectOne(&characteristic, q, genus, id); err != nil {
|
||||||
|
|
|
@ -114,6 +114,7 @@ func Handler() http.Handler {
|
||||||
r{handleGetter(strainService), "GET", "/strains/{Id:.+}"},
|
r{handleGetter(strainService), "GET", "/strains/{Id:.+}"},
|
||||||
r{handleUpdater(strainService), "PUT", "/strains/{Id:.+}"},
|
r{handleUpdater(strainService), "PUT", "/strains/{Id:.+}"},
|
||||||
r{handleLister(characteristicService), "GET", "/characteristics"},
|
r{handleLister(characteristicService), "GET", "/characteristics"},
|
||||||
|
r{handleCreater(characteristicService), "POST", "/characteristics"},
|
||||||
r{handleGetter(characteristicService), "GET", "/characteristics/{Id:.+}"},
|
r{handleGetter(characteristicService), "GET", "/characteristics/{Id:.+}"},
|
||||||
r{handleUpdater(characteristicService), "PUT", "/characteristics/{Id:.+}"},
|
r{handleUpdater(characteristicService), "PUT", "/characteristics/{Id:.+}"},
|
||||||
r{handleLister(measurementService), "GET", "/measurements"},
|
r{handleLister(measurementService), "GET", "/measurements"},
|
||||||
|
|
2
types.go
2
types.go
|
@ -176,8 +176,8 @@ func (i *NullSliceInt64) Scan(src interface{}) error {
|
||||||
func strToIntSlice(s string) []int64 {
|
func strToIntSlice(s string) []int64 {
|
||||||
r := strings.Trim(s, "{}")
|
r := strings.Trim(s, "{}")
|
||||||
a := []int64(nil)
|
a := []int64(nil)
|
||||||
if r != "NULL" {
|
|
||||||
for _, t := range strings.Split(r, ",") {
|
for _, t := range strings.Split(r, ",") {
|
||||||
|
if t != "NULL" {
|
||||||
i, _ := strconv.ParseInt(t, 10, 64)
|
i, _ := strconv.ParseInt(t, 10, 64)
|
||||||
a = append(a, i)
|
a = append(a, i)
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue