Add author id to strains
This commit is contained in:
parent
77cb7828cf
commit
1a30cd16bd
2 changed files with 10 additions and 7 deletions
|
@ -10,13 +10,15 @@ CREATE TABLE strains (
|
|||
accession_banks CHARACTER VARYING(100) NULL,
|
||||
genbank_embl_ddb CHARACTER VARYING(100) NULL,
|
||||
isolated_from CHARACTER VARYING(100) NULL,
|
||||
author_id BIGINT NOT NULL,
|
||||
|
||||
created_at TIMESTAMP WITH TIME ZONE NOT NULL,
|
||||
updated_at TIMESTAMP WITH TIME ZONE NOT NULL,
|
||||
deleted_at TIMESTAMP WITH TIME ZONE NULL,
|
||||
|
||||
CONSTRAINT strain_pkey PRIMARY KEY (id),
|
||||
FOREIGN KEY (species_id) REFERENCES species(id)
|
||||
FOREIGN KEY (species_id) REFERENCES species(id),
|
||||
FOREIGN KEY (author_id) REFERENCES users(id)
|
||||
);
|
||||
|
||||
CREATE INDEX species_id_idx ON strains (species_id);
|
||||
|
|
13
strains.go
13
strains.go
|
@ -1,6 +1,7 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
@ -28,6 +29,7 @@ type StrainBase struct {
|
|||
AccessionBanks string `db:"accession_banks" json:"accessionBanks"`
|
||||
GenbankEmblDdb NullString `db:"genbank_embl_ddb" json:"genbankEmblDdb"`
|
||||
IsolatedFrom NullString `db:"isolated_from" json:"isolatedFrom"`
|
||||
AuthorId int64 `db:"author_id" json:"author"`
|
||||
CreatedAt time.Time `db:"created_at" json:"createdAt"`
|
||||
UpdatedAt time.Time `db:"updated_at" json:"updatedAt"`
|
||||
DeletedAt NullTime `db:"deleted_at" json:"deletedAt"`
|
||||
|
@ -139,7 +141,7 @@ func dbGetStrains(opt *StrainListOptions) ([]*Strain, error) {
|
|||
|
||||
func dbGetStrain(id int64, genus string) (*Strain, error) {
|
||||
var strain Strain
|
||||
sql := `SELECT st.*, sp.species_name, array_agg(m.id) AS measurements,
|
||||
q := `SELECT st.*, sp.species_name, array_agg(m.id) AS measurements,
|
||||
COUNT(m) AS total_measurements
|
||||
FROM strains st
|
||||
INNER JOIN species sp ON sp.id=st.species_id
|
||||
|
@ -147,12 +149,11 @@ func dbGetStrain(id int64, genus string) (*Strain, error) {
|
|||
LEFT OUTER JOIN measurements m ON m.strain_id=st.id
|
||||
WHERE st.id=$2
|
||||
GROUP BY st.id, sp.species_name;`
|
||||
err := DBH.SelectOne(&strain, sql, genus, id)
|
||||
if err != nil {
|
||||
if err := DBH.SelectOne(&strain, q, genus, id); err != nil {
|
||||
if err == sql.ErrNoRows {
|
||||
return nil, ErrStrainNotFound
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
if &strain == nil {
|
||||
return nil, ErrStrainNotFound
|
||||
}
|
||||
return &strain, nil
|
||||
}
|
||||
|
|
Reference in a new issue