Golint
This commit is contained in:
parent
a880fdea82
commit
efb0cc13fa
41 changed files with 569 additions and 386 deletions
|
@ -12,56 +12,59 @@ import (
|
|||
"github.com/thermokarst/bactdb/types"
|
||||
)
|
||||
|
||||
// CharacteristicService provides for CRUD operations
|
||||
type CharacteristicService struct{}
|
||||
|
||||
// Unmarshal satisfies interface Updater and interface Creater
|
||||
func (c CharacteristicService) Unmarshal(b []byte) (types.Entity, error) {
|
||||
var cj payloads.Characteristic
|
||||
err := json.Unmarshal(b, &cj)
|
||||
return &cj, err
|
||||
}
|
||||
|
||||
// List lists all characteristics
|
||||
func (c CharacteristicService) List(val *url.Values, claims *types.Claims) (types.Entity, *types.AppError) {
|
||||
if val == nil {
|
||||
return nil, NewJSONError(errors.MustProvideOptions, http.StatusInternalServerError)
|
||||
return nil, newJSONError(errors.ErrMustProvideOptions, http.StatusInternalServerError)
|
||||
}
|
||||
var opt helpers.ListOptions
|
||||
if err := helpers.SchemaDecoder.Decode(&opt, *val); err != nil {
|
||||
return nil, NewJSONError(err, http.StatusInternalServerError)
|
||||
return nil, newJSONError(err, http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
characteristics, err := models.ListCharacteristics(opt, claims)
|
||||
if err != nil {
|
||||
return nil, NewJSONError(err, http.StatusInternalServerError)
|
||||
return nil, newJSONError(err, http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
strains_opt, err := models.StrainOptsFromCharacteristics(opt)
|
||||
strainsOpt, err := models.StrainOptsFromCharacteristics(opt)
|
||||
if err != nil {
|
||||
return nil, NewJSONError(err, http.StatusInternalServerError)
|
||||
return nil, newJSONError(err, http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
strains, err := models.ListStrains(*strains_opt, claims)
|
||||
strains, err := models.ListStrains(*strainsOpt, claims)
|
||||
if err != nil {
|
||||
return nil, NewJSONError(err, http.StatusInternalServerError)
|
||||
return nil, newJSONError(err, http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
species_opt, err := models.SpeciesOptsFromStrains(*strains_opt)
|
||||
speciesOpt, err := models.SpeciesOptsFromStrains(*strainsOpt)
|
||||
if err != nil {
|
||||
return nil, NewJSONError(err, http.StatusInternalServerError)
|
||||
return nil, newJSONError(err, http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
species, err := models.ListSpecies(*species_opt, claims)
|
||||
species, err := models.ListSpecies(*speciesOpt, claims)
|
||||
if err != nil {
|
||||
return nil, NewJSONError(err, http.StatusInternalServerError)
|
||||
return nil, newJSONError(err, http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
measurements_opt, err := models.MeasurementOptsFromCharacteristics(opt)
|
||||
measurementsOpt, err := models.MeasurementOptsFromCharacteristics(opt)
|
||||
if err != nil {
|
||||
return nil, NewJSONError(err, http.StatusInternalServerError)
|
||||
return nil, newJSONError(err, http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
measurements, err := models.ListMeasurements(*measurements_opt, claims)
|
||||
measurements, err := models.ListMeasurements(*measurementsOpt, claims)
|
||||
if err != nil {
|
||||
return nil, NewJSONError(err, http.StatusInternalServerError)
|
||||
return nil, newJSONError(err, http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
payload := payloads.Characteristics{
|
||||
|
@ -77,30 +80,31 @@ func (c CharacteristicService) List(val *url.Values, claims *types.Claims) (type
|
|||
return &payload, nil
|
||||
}
|
||||
|
||||
// Get retrieves a single characteristic
|
||||
func (c CharacteristicService) Get(id int64, genus string, claims *types.Claims) (types.Entity, *types.AppError) {
|
||||
characteristic, err := models.GetCharacteristic(id, genus, claims)
|
||||
if err != nil {
|
||||
return nil, NewJSONError(err, http.StatusInternalServerError)
|
||||
return nil, newJSONError(err, http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
strains, strain_opts, err := models.StrainsFromCharacteristicId(id, genus, claims)
|
||||
strains, strainOpts, err := models.StrainsFromCharacteristicID(id, genus, claims)
|
||||
if err != nil {
|
||||
return nil, NewJSONError(err, http.StatusInternalServerError)
|
||||
return nil, newJSONError(err, http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
species_opt, err := models.SpeciesOptsFromStrains(*strain_opts)
|
||||
speciesOpt, err := models.SpeciesOptsFromStrains(*strainOpts)
|
||||
if err != nil {
|
||||
return nil, NewJSONError(err, http.StatusInternalServerError)
|
||||
return nil, newJSONError(err, http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
species, err := models.ListSpecies(*species_opt, claims)
|
||||
species, err := models.ListSpecies(*speciesOpt, claims)
|
||||
if err != nil {
|
||||
return nil, NewJSONError(err, http.StatusInternalServerError)
|
||||
return nil, newJSONError(err, http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
measurements, _, err := models.MeasurementsFromCharacteristicId(id, genus, claims)
|
||||
measurements, _, err := models.MeasurementsFromCharacteristicID(id, genus, claims)
|
||||
if err != nil {
|
||||
return nil, NewJSONError(err, http.StatusInternalServerError)
|
||||
return nil, newJSONError(err, http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
payload := payloads.Characteristic{
|
||||
|
@ -113,43 +117,44 @@ func (c CharacteristicService) Get(id int64, genus string, claims *types.Claims)
|
|||
return &payload, nil
|
||||
}
|
||||
|
||||
// Update modifies an existing characteristic
|
||||
func (c CharacteristicService) Update(id int64, e *types.Entity, genus string, claims *types.Claims) *types.AppError {
|
||||
payload := (*e).(*payloads.Characteristic)
|
||||
payload.Characteristic.UpdatedBy = claims.Sub
|
||||
payload.Characteristic.Id = id
|
||||
payload.Characteristic.ID = id
|
||||
|
||||
// First, handle Characteristic Type
|
||||
id, err := models.InsertOrGetCharacteristicType(payload.Characteristic.CharacteristicType, claims)
|
||||
if err != nil {
|
||||
return NewJSONError(err, http.StatusInternalServerError)
|
||||
return newJSONError(err, http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
payload.Characteristic.CanEdit = helpers.CanEdit(claims, payload.Characteristic.CreatedBy)
|
||||
|
||||
payload.Characteristic.CharacteristicTypeId = id
|
||||
payload.Characteristic.CharacteristicTypeID = id
|
||||
// TODO: fix this
|
||||
count, err := models.DBH.Update(payload.Characteristic.CharacteristicBase)
|
||||
if err != nil {
|
||||
return NewJSONError(err, http.StatusInternalServerError)
|
||||
return newJSONError(err, http.StatusInternalServerError)
|
||||
}
|
||||
if count != 1 {
|
||||
// TODO: fix this
|
||||
return NewJSONError(errors.CharacteristicNotUpdated, http.StatusBadRequest)
|
||||
return newJSONError(errors.ErrCharacteristicNotUpdated, http.StatusBadRequest)
|
||||
}
|
||||
|
||||
strains, strain_opts, err := models.StrainsFromCharacteristicId(id, genus, claims)
|
||||
strains, strainOpts, err := models.StrainsFromCharacteristicID(id, genus, claims)
|
||||
if err != nil {
|
||||
return NewJSONError(err, http.StatusInternalServerError)
|
||||
return newJSONError(err, http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
species_opt, err := models.SpeciesOptsFromStrains(*strain_opts)
|
||||
speciesOpt, err := models.SpeciesOptsFromStrains(*strainOpts)
|
||||
if err != nil {
|
||||
return NewJSONError(err, http.StatusInternalServerError)
|
||||
return newJSONError(err, http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
species, err := models.ListSpecies(*species_opt, claims)
|
||||
species, err := models.ListSpecies(*speciesOpt, claims)
|
||||
if err != nil {
|
||||
return NewJSONError(err, http.StatusInternalServerError)
|
||||
return newJSONError(err, http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
payload.Strains = strains
|
||||
|
@ -160,6 +165,7 @@ func (c CharacteristicService) Update(id int64, e *types.Entity, genus string, c
|
|||
return nil
|
||||
}
|
||||
|
||||
// Create initializes a new characteristic
|
||||
func (c CharacteristicService) Create(e *types.Entity, genus string, claims *types.Claims) *types.AppError {
|
||||
payload := (*e).(*payloads.Characteristic)
|
||||
payload.Characteristic.CreatedBy = claims.Sub
|
||||
|
@ -167,19 +173,19 @@ func (c CharacteristicService) Create(e *types.Entity, genus string, claims *typ
|
|||
|
||||
id, err := models.InsertOrGetCharacteristicType(payload.Characteristic.CharacteristicType, claims)
|
||||
if err != nil {
|
||||
return NewJSONError(err, http.StatusInternalServerError)
|
||||
return newJSONError(err, http.StatusInternalServerError)
|
||||
}
|
||||
payload.Characteristic.CharacteristicTypeId = id
|
||||
payload.Characteristic.CharacteristicTypeID = id
|
||||
|
||||
// TODO: fix this
|
||||
err = models.DBH.Insert(payload.Characteristic.CharacteristicBase)
|
||||
if err != nil {
|
||||
return NewJSONError(err, http.StatusInternalServerError)
|
||||
return newJSONError(err, http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
characteristic, err := models.GetCharacteristic(payload.Characteristic.Id, genus, claims)
|
||||
characteristic, err := models.GetCharacteristic(payload.Characteristic.ID, genus, claims)
|
||||
if err != nil {
|
||||
return NewJSONError(err, http.StatusInternalServerError)
|
||||
return newJSONError(err, http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
payload.Characteristic = characteristic
|
||||
|
|
|
@ -16,6 +16,7 @@ import (
|
|||
"github.com/thermokarst/bactdb/types"
|
||||
)
|
||||
|
||||
// HandleCompare is a HTTP handler for comparision.
|
||||
func HandleCompare(w http.ResponseWriter, r *http.Request) *types.AppError {
|
||||
// types
|
||||
type Comparisions map[string]map[string]string
|
||||
|
@ -43,23 +44,23 @@ func HandleCompare(w http.ResponseWriter, r *http.Request) *types.AppError {
|
|||
measurementsPayload := (measurementsEntity).(*payloads.Measurements)
|
||||
|
||||
// Assemble matrix
|
||||
characteristic_ids := strings.Split(opt.Get("characteristic_ids"), ",")
|
||||
strain_ids := strings.Split(opt.Get("strain_ids"), ",")
|
||||
characteristicIDs := strings.Split(opt.Get("characteristic_ids"), ",")
|
||||
strainIDs := strings.Split(opt.Get("strain_ids"), ",")
|
||||
|
||||
comparisions := make(Comparisions)
|
||||
for _, characteristic_id := range characteristic_ids {
|
||||
characteristic_id_int, _ := strconv.ParseInt(characteristic_id, 10, 0)
|
||||
for _, characteristicID := range characteristicIDs {
|
||||
characteristicIDInt, _ := strconv.ParseInt(characteristicID, 10, 0)
|
||||
values := make(map[string]string)
|
||||
for _, strain_id := range strain_ids {
|
||||
strain_id_int, _ := strconv.ParseInt(strain_id, 10, 0)
|
||||
for _, strainID := range strainIDs {
|
||||
strainIDInt, _ := strconv.ParseInt(strainID, 10, 0)
|
||||
for _, m := range *measurementsPayload.Measurements {
|
||||
if (m.CharacteristicId == characteristic_id_int) && (m.StrainId == strain_id_int) {
|
||||
values[strain_id] = m.Value()
|
||||
if (m.CharacteristicID == characteristicIDInt) && (m.StrainID == strainIDInt) {
|
||||
values[strainID] = m.Value()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
comparisions[characteristic_id] = values
|
||||
comparisions[characteristicID] = values
|
||||
}
|
||||
|
||||
// Return, based on mimetype
|
||||
|
@ -68,10 +69,10 @@ func HandleCompare(w http.ResponseWriter, r *http.Request) *types.AppError {
|
|||
header = "application/json"
|
||||
|
||||
comparisionsJSON := make(ComparisionsJSON, 0)
|
||||
for _, characteristic_id := range characteristic_ids {
|
||||
row := []string{characteristic_id}
|
||||
for _, strain_id := range strain_ids {
|
||||
row = append(row, comparisions[characteristic_id][strain_id])
|
||||
for _, characteristicID := range characteristicIDs {
|
||||
row := []string{characteristicID}
|
||||
for _, strainID := range strainIDs {
|
||||
row = append(row, comparisions[characteristicID][strainID])
|
||||
}
|
||||
comparisionsJSON = append(comparisionsJSON, row)
|
||||
}
|
||||
|
@ -83,11 +84,11 @@ func HandleCompare(w http.ResponseWriter, r *http.Request) *types.AppError {
|
|||
// maps to translate ids
|
||||
strains := make(map[string]string)
|
||||
for _, strain := range *measurementsPayload.Strains {
|
||||
strains[fmt.Sprintf("%d", strain.Id)] = fmt.Sprintf("%s (%s)", strain.SpeciesName(), strain.StrainName)
|
||||
strains[fmt.Sprintf("%d", strain.ID)] = fmt.Sprintf("%s (%s)", strain.SpeciesName(), strain.StrainName)
|
||||
}
|
||||
characteristics := make(map[string]string)
|
||||
for _, characteristic := range *measurementsPayload.Characteristics {
|
||||
characteristics[fmt.Sprintf("%d", characteristic.Id)] = characteristic.CharacteristicName
|
||||
characteristics[fmt.Sprintf("%d", characteristic.ID)] = characteristic.CharacteristicName
|
||||
}
|
||||
|
||||
b := &bytes.Buffer{}
|
||||
|
@ -95,8 +96,8 @@ func HandleCompare(w http.ResponseWriter, r *http.Request) *types.AppError {
|
|||
|
||||
// Write header row
|
||||
r := []string{"Characteristic"}
|
||||
for _, strain_id := range strain_ids {
|
||||
r = append(r, strains[strain_id])
|
||||
for _, strainID := range strainIDs {
|
||||
r = append(r, strains[strainID])
|
||||
}
|
||||
wr.Write(r)
|
||||
|
||||
|
|
|
@ -6,23 +6,29 @@ import (
|
|||
"github.com/thermokarst/bactdb/types"
|
||||
)
|
||||
|
||||
// Getter gets a single entity.
|
||||
type Getter interface {
|
||||
Get(int64, string, *types.Claims) (types.Entity, *types.AppError)
|
||||
}
|
||||
|
||||
// Lister lists entities.
|
||||
type Lister interface {
|
||||
List(*url.Values, *types.Claims) (types.Entity, *types.AppError)
|
||||
}
|
||||
|
||||
// Updater updates entities.
|
||||
type Updater interface {
|
||||
Update(int64, *types.Entity, string, *types.Claims) *types.AppError
|
||||
Unmarshal([]byte) (types.Entity, error)
|
||||
}
|
||||
|
||||
// Creater creates entities.
|
||||
type Creater interface {
|
||||
Create(*types.Entity, string, *types.Claims) *types.AppError
|
||||
Unmarshal([]byte) (types.Entity, error)
|
||||
}
|
||||
|
||||
// Deleter deletes entities.
|
||||
type Deleter interface {
|
||||
Delete(int64, string, *types.Claims) *types.AppError
|
||||
}
|
||||
|
|
|
@ -2,7 +2,7 @@ package api
|
|||
|
||||
import "github.com/thermokarst/bactdb/types"
|
||||
|
||||
func NewJSONError(err error, status int) *types.AppError {
|
||||
func newJSONError(err error, status int) *types.AppError {
|
||||
return &types.AppError{
|
||||
Error: types.ErrorJSON{Err: err},
|
||||
Status: status,
|
||||
|
|
|
@ -12,46 +12,49 @@ import (
|
|||
"github.com/thermokarst/bactdb/types"
|
||||
)
|
||||
|
||||
// MeasurementService provides for CRUD operations.
|
||||
type MeasurementService struct{}
|
||||
|
||||
func (s MeasurementService) Unmarshal(b []byte) (types.Entity, error) {
|
||||
// Unmarshal satisfies interface Updater and interface Creater.
|
||||
func (m MeasurementService) Unmarshal(b []byte) (types.Entity, error) {
|
||||
var mj payloads.Measurement
|
||||
err := json.Unmarshal(b, &mj)
|
||||
return &mj, err
|
||||
}
|
||||
|
||||
// List lists all measurements.
|
||||
func (m MeasurementService) List(val *url.Values, claims *types.Claims) (types.Entity, *types.AppError) {
|
||||
if val == nil {
|
||||
return nil, NewJSONError(errors.MustProvideOptions, http.StatusInternalServerError)
|
||||
return nil, newJSONError(errors.ErrMustProvideOptions, http.StatusInternalServerError)
|
||||
}
|
||||
var opt helpers.MeasurementListOptions
|
||||
if err := helpers.SchemaDecoder.Decode(&opt, *val); err != nil {
|
||||
return nil, NewJSONError(err, http.StatusInternalServerError)
|
||||
return nil, newJSONError(err, http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
measurements, err := models.ListMeasurements(opt, claims)
|
||||
if err != nil {
|
||||
return nil, NewJSONError(err, http.StatusInternalServerError)
|
||||
return nil, newJSONError(err, http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
char_opts, err := models.CharacteristicOptsFromMeasurements(opt)
|
||||
charOpts, err := models.CharacteristicOptsFromMeasurements(opt)
|
||||
if err != nil {
|
||||
return nil, NewJSONError(err, http.StatusInternalServerError)
|
||||
return nil, newJSONError(err, http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
characteristics, err := models.ListCharacteristics(*char_opts, claims)
|
||||
characteristics, err := models.ListCharacteristics(*charOpts, claims)
|
||||
if err != nil {
|
||||
return nil, NewJSONError(err, http.StatusInternalServerError)
|
||||
return nil, newJSONError(err, http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
strain_opts, err := models.StrainOptsFromMeasurements(opt)
|
||||
strainOpts, err := models.StrainOptsFromMeasurements(opt)
|
||||
if err != nil {
|
||||
return nil, NewJSONError(err, http.StatusInternalServerError)
|
||||
return nil, newJSONError(err, http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
strains, err := models.ListStrains(*strain_opts, claims)
|
||||
strains, err := models.ListStrains(*strainOpts, claims)
|
||||
if err != nil {
|
||||
return nil, NewJSONError(err, http.StatusInternalServerError)
|
||||
return nil, newJSONError(err, http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
payload := payloads.Measurements{
|
||||
|
@ -63,10 +66,11 @@ func (m MeasurementService) List(val *url.Values, claims *types.Claims) (types.E
|
|||
return &payload, nil
|
||||
}
|
||||
|
||||
// Get retrieves a single measurement.
|
||||
func (m MeasurementService) Get(id int64, genus string, claims *types.Claims) (types.Entity, *types.AppError) {
|
||||
measurement, err := models.GetMeasurement(id, genus, claims)
|
||||
if err != nil {
|
||||
return nil, NewJSONError(err, http.StatusInternalServerError)
|
||||
return nil, newJSONError(err, http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
payload := payloads.Measurement{
|
||||
|
@ -76,33 +80,34 @@ func (m MeasurementService) Get(id int64, genus string, claims *types.Claims) (t
|
|||
return &payload, nil
|
||||
}
|
||||
|
||||
func (s MeasurementService) Update(id int64, e *types.Entity, genus string, claims *types.Claims) *types.AppError {
|
||||
// Update modifies a single measurement.
|
||||
func (m MeasurementService) Update(id int64, e *types.Entity, genus string, claims *types.Claims) *types.AppError {
|
||||
payload := (*e).(*payloads.Measurement)
|
||||
payload.Measurement.UpdatedBy = claims.Sub
|
||||
payload.Measurement.Id = id
|
||||
payload.Measurement.ID = id
|
||||
|
||||
if payload.Measurement.TextMeasurementType.Valid {
|
||||
id, err := models.GetTextMeasurementTypeId(payload.Measurement.TextMeasurementType.String)
|
||||
id, err := models.GetTextMeasurementTypeID(payload.Measurement.TextMeasurementType.String)
|
||||
if err != nil {
|
||||
return NewJSONError(err, http.StatusInternalServerError)
|
||||
return newJSONError(err, http.StatusInternalServerError)
|
||||
}
|
||||
payload.Measurement.TextMeasurementTypeId.Int64 = id
|
||||
payload.Measurement.TextMeasurementTypeId.Valid = true
|
||||
payload.Measurement.TextMeasurementTypeID.Int64 = id
|
||||
payload.Measurement.TextMeasurementTypeID.Valid = true
|
||||
}
|
||||
|
||||
// TODO: fix this
|
||||
count, err := models.DBH.Update(payload.Measurement.MeasurementBase)
|
||||
if err != nil {
|
||||
return NewJSONError(err, http.StatusInternalServerError)
|
||||
return newJSONError(err, http.StatusInternalServerError)
|
||||
}
|
||||
if count != 1 {
|
||||
// TODO: fix this
|
||||
return NewJSONError(errors.StrainNotUpdated, http.StatusBadRequest)
|
||||
return newJSONError(errors.ErrStrainNotUpdated, http.StatusBadRequest)
|
||||
}
|
||||
|
||||
measurement, err := models.GetMeasurement(id, genus, claims)
|
||||
if err != nil {
|
||||
return NewJSONError(err, http.StatusInternalServerError)
|
||||
return newJSONError(err, http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
payload.Measurement = measurement
|
||||
|
@ -110,16 +115,18 @@ func (s MeasurementService) Update(id int64, e *types.Entity, genus string, clai
|
|||
return nil
|
||||
}
|
||||
|
||||
// Delete deletes a single measurement.
|
||||
func (m MeasurementService) Delete(id int64, genus string, claims *types.Claims) *types.AppError {
|
||||
q := `DELETE FROM measurements WHERE id=$1;`
|
||||
// TODO: fix this
|
||||
_, err := models.DBH.Exec(q, id)
|
||||
if err != nil {
|
||||
return NewJSONError(err, http.StatusInternalServerError)
|
||||
return newJSONError(err, http.StatusInternalServerError)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Create initializes a new measurement.
|
||||
func (m MeasurementService) Create(e *types.Entity, genus string, claims *types.Claims) *types.AppError {
|
||||
payload := (*e).(*payloads.Measurement)
|
||||
payload.Measurement.CreatedBy = claims.Sub
|
||||
|
@ -127,7 +134,7 @@ func (m MeasurementService) Create(e *types.Entity, genus string, claims *types.
|
|||
|
||||
// TODO: fix this
|
||||
if err := models.DBH.Insert(payload.Measurement.MeasurementBase); err != nil {
|
||||
return NewJSONError(err, http.StatusInternalServerError)
|
||||
return newJSONError(err, http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
|
|
@ -12,36 +12,39 @@ import (
|
|||
"github.com/thermokarst/bactdb/types"
|
||||
)
|
||||
|
||||
// SpeciesService provides for CRUD operations
|
||||
type SpeciesService struct{}
|
||||
|
||||
// Unmarshal satisfies interface Updater and interface Creater
|
||||
func (s SpeciesService) Unmarshal(b []byte) (types.Entity, error) {
|
||||
var sj payloads.Species
|
||||
err := json.Unmarshal(b, &sj)
|
||||
return &sj, err
|
||||
}
|
||||
|
||||
// List lists species
|
||||
func (s SpeciesService) List(val *url.Values, claims *types.Claims) (types.Entity, *types.AppError) {
|
||||
if val == nil {
|
||||
return nil, NewJSONError(errors.MustProvideOptions, http.StatusInternalServerError)
|
||||
return nil, newJSONError(errors.ErrMustProvideOptions, http.StatusInternalServerError)
|
||||
}
|
||||
var opt helpers.ListOptions
|
||||
if err := helpers.SchemaDecoder.Decode(&opt, *val); err != nil {
|
||||
return nil, NewJSONError(err, http.StatusInternalServerError)
|
||||
return nil, newJSONError(err, http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
species, err := models.ListSpecies(opt, claims)
|
||||
if err != nil {
|
||||
return nil, NewJSONError(err, http.StatusInternalServerError)
|
||||
return nil, newJSONError(err, http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
strains_opt, err := models.StrainOptsFromSpecies(opt)
|
||||
strainsOpt, err := models.StrainOptsFromSpecies(opt)
|
||||
if err != nil {
|
||||
return nil, NewJSONError(err, http.StatusInternalServerError)
|
||||
return nil, newJSONError(err, http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
strains, err := models.ListStrains(*strains_opt, claims)
|
||||
strains, err := models.ListStrains(*strainsOpt, claims)
|
||||
if err != nil {
|
||||
return nil, NewJSONError(err, http.StatusInternalServerError)
|
||||
return nil, newJSONError(err, http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
payload := payloads.ManySpecies{
|
||||
|
@ -55,15 +58,16 @@ func (s SpeciesService) List(val *url.Values, claims *types.Claims) (types.Entit
|
|||
return &payload, nil
|
||||
}
|
||||
|
||||
// Get retrieves a single species
|
||||
func (s SpeciesService) Get(id int64, genus string, claims *types.Claims) (types.Entity, *types.AppError) {
|
||||
species, err := models.GetSpecies(id, genus, claims)
|
||||
if err != nil {
|
||||
return nil, NewJSONError(err, http.StatusInternalServerError)
|
||||
return nil, newJSONError(err, http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
strains, err := models.StrainsFromSpeciesId(id, genus, claims)
|
||||
strains, err := models.StrainsFromSpeciesID(id, genus, claims)
|
||||
if err != nil {
|
||||
return nil, NewJSONError(err, http.StatusInternalServerError)
|
||||
return nil, newJSONError(err, http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
payload := payloads.Species{
|
||||
|
@ -77,36 +81,37 @@ func (s SpeciesService) Get(id int64, genus string, claims *types.Claims) (types
|
|||
return &payload, nil
|
||||
}
|
||||
|
||||
// Update modifies an existing species
|
||||
func (s SpeciesService) Update(id int64, e *types.Entity, genus string, claims *types.Claims) *types.AppError {
|
||||
payload := (*e).(*payloads.Species)
|
||||
payload.Species.UpdatedBy = claims.Sub
|
||||
payload.Species.Id = id
|
||||
payload.Species.ID = id
|
||||
|
||||
genus_id, err := models.GenusIdFromName(genus)
|
||||
genusID, err := models.GenusIDFromName(genus)
|
||||
if err != nil {
|
||||
return NewJSONError(err, http.StatusInternalServerError)
|
||||
return newJSONError(err, http.StatusInternalServerError)
|
||||
}
|
||||
payload.Species.SpeciesBase.GenusID = genus_id
|
||||
payload.Species.SpeciesBase.GenusID = genusID
|
||||
|
||||
// TODO: fix this
|
||||
count, err := models.DBH.Update(payload.Species.SpeciesBase)
|
||||
if err != nil {
|
||||
return NewJSONError(err, http.StatusInternalServerError)
|
||||
return newJSONError(err, http.StatusInternalServerError)
|
||||
}
|
||||
if count != 1 {
|
||||
// TODO: fix this
|
||||
return NewJSONError(errors.SpeciesNotUpdated, http.StatusBadRequest)
|
||||
return newJSONError(errors.ErrSpeciesNotUpdated, http.StatusBadRequest)
|
||||
}
|
||||
|
||||
// Reload to send back down the wire
|
||||
species, err := models.GetSpecies(id, genus, claims)
|
||||
if err != nil {
|
||||
return NewJSONError(err, http.StatusInternalServerError)
|
||||
return newJSONError(err, http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
strains, err := models.StrainsFromSpeciesId(id, genus, claims)
|
||||
strains, err := models.StrainsFromSpeciesID(id, genus, claims)
|
||||
if err != nil {
|
||||
return NewJSONError(err, http.StatusInternalServerError)
|
||||
return newJSONError(err, http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
payload.Species = species
|
||||
|
@ -118,27 +123,28 @@ func (s SpeciesService) Update(id int64, e *types.Entity, genus string, claims *
|
|||
return nil
|
||||
}
|
||||
|
||||
// Create initializes a new species
|
||||
func (s SpeciesService) Create(e *types.Entity, genus string, claims *types.Claims) *types.AppError {
|
||||
payload := (*e).(*payloads.Species)
|
||||
payload.Species.CreatedBy = claims.Sub
|
||||
payload.Species.UpdatedBy = claims.Sub
|
||||
|
||||
genus_id, err := models.GenusIdFromName(genus)
|
||||
genusID, err := models.GenusIDFromName(genus)
|
||||
if err != nil {
|
||||
return NewJSONError(err, http.StatusInternalServerError)
|
||||
return newJSONError(err, http.StatusInternalServerError)
|
||||
}
|
||||
payload.Species.SpeciesBase.GenusID = genus_id
|
||||
payload.Species.SpeciesBase.GenusID = genusID
|
||||
|
||||
// TODO: fix this
|
||||
err = models.DBH.Insert(payload.Species.SpeciesBase)
|
||||
if err != nil {
|
||||
return NewJSONError(err, http.StatusInternalServerError)
|
||||
return newJSONError(err, http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
// Reload to send back down the wire
|
||||
species, err := models.GetSpecies(payload.Species.Id, genus, claims)
|
||||
species, err := models.GetSpecies(payload.Species.ID, genus, claims)
|
||||
if err != nil {
|
||||
return NewJSONError(err, http.StatusInternalServerError)
|
||||
return newJSONError(err, http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
// Note, no strains when new species
|
||||
|
|
108
api/strains.go
108
api/strains.go
|
@ -12,69 +12,72 @@ import (
|
|||
"github.com/thermokarst/bactdb/types"
|
||||
)
|
||||
|
||||
// StrainService provides for CRUD operations
|
||||
type StrainService struct{}
|
||||
|
||||
// Unmarshal satisfies interface Updater and interface Creater
|
||||
func (s StrainService) Unmarshal(b []byte) (types.Entity, error) {
|
||||
var sj payloads.Strain
|
||||
err := json.Unmarshal(b, &sj)
|
||||
return &sj, err
|
||||
}
|
||||
|
||||
// List lists all strains
|
||||
func (s StrainService) List(val *url.Values, claims *types.Claims) (types.Entity, *types.AppError) {
|
||||
if val == nil {
|
||||
return nil, NewJSONError(errors.MustProvideOptions, http.StatusInternalServerError)
|
||||
return nil, newJSONError(errors.ErrMustProvideOptions, http.StatusInternalServerError)
|
||||
}
|
||||
var opt helpers.ListOptions
|
||||
if err := helpers.SchemaDecoder.Decode(&opt, *val); err != nil {
|
||||
return nil, NewJSONError(err, http.StatusInternalServerError)
|
||||
return nil, newJSONError(err, http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
strains, err := models.ListStrains(opt, claims)
|
||||
if err != nil {
|
||||
return nil, NewJSONError(err, http.StatusInternalServerError)
|
||||
return nil, newJSONError(err, http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
species_opt, err := models.SpeciesOptsFromStrains(opt)
|
||||
speciesOpt, err := models.SpeciesOptsFromStrains(opt)
|
||||
if err != nil {
|
||||
return nil, NewJSONError(err, http.StatusInternalServerError)
|
||||
return nil, newJSONError(err, http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
species, err := models.ListSpecies(*species_opt, claims)
|
||||
species, err := models.ListSpecies(*speciesOpt, claims)
|
||||
if err != nil {
|
||||
return nil, NewJSONError(err, http.StatusInternalServerError)
|
||||
return nil, newJSONError(err, http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
characteristics_opt, err := models.CharacteristicsOptsFromStrains(opt)
|
||||
characteristicsOpt, err := models.CharacteristicsOptsFromStrains(opt)
|
||||
if err != nil {
|
||||
return nil, NewJSONError(err, http.StatusInternalServerError)
|
||||
return nil, newJSONError(err, http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
characteristics, err := models.ListCharacteristics(*characteristics_opt, claims)
|
||||
characteristics, err := models.ListCharacteristics(*characteristicsOpt, claims)
|
||||
if err != nil {
|
||||
return nil, NewJSONError(err, http.StatusInternalServerError)
|
||||
return nil, newJSONError(err, http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
characteristic_ids := []int64{}
|
||||
characteristicIDs := []int64{}
|
||||
for _, c := range *characteristics {
|
||||
characteristic_ids = append(characteristic_ids, c.Id)
|
||||
characteristicIDs = append(characteristicIDs, c.ID)
|
||||
}
|
||||
|
||||
strain_ids := []int64{}
|
||||
strainIDs := []int64{}
|
||||
for _, s := range *strains {
|
||||
strain_ids = append(strain_ids, s.Id)
|
||||
strainIDs = append(strainIDs, s.ID)
|
||||
}
|
||||
|
||||
measurement_opt := helpers.MeasurementListOptions{
|
||||
measurementOpt := helpers.MeasurementListOptions{
|
||||
ListOptions: helpers.ListOptions{
|
||||
Genus: opt.Genus,
|
||||
},
|
||||
Strains: strain_ids,
|
||||
Characteristics: characteristic_ids,
|
||||
Strains: strainIDs,
|
||||
Characteristics: characteristicIDs,
|
||||
}
|
||||
|
||||
measurements, err := models.ListMeasurements(measurement_opt, claims)
|
||||
measurements, err := models.ListMeasurements(measurementOpt, claims)
|
||||
if err != nil {
|
||||
return nil, NewJSONError(err, http.StatusInternalServerError)
|
||||
return nil, newJSONError(err, http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
payload := payloads.Strains{
|
||||
|
@ -90,51 +93,52 @@ func (s StrainService) List(val *url.Values, claims *types.Claims) (types.Entity
|
|||
return &payload, nil
|
||||
}
|
||||
|
||||
// Get retrieves a single strain
|
||||
func (s StrainService) Get(id int64, genus string, claims *types.Claims) (types.Entity, *types.AppError) {
|
||||
strain, err := models.GetStrain(id, genus, claims)
|
||||
if err != nil {
|
||||
return nil, NewJSONError(err, http.StatusInternalServerError)
|
||||
return nil, newJSONError(err, http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
species, err := models.GetSpecies(strain.SpeciesId, genus, claims)
|
||||
species, err := models.GetSpecies(strain.SpeciesID, genus, claims)
|
||||
if err != nil {
|
||||
return nil, NewJSONError(err, http.StatusInternalServerError)
|
||||
return nil, newJSONError(err, http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
opt := helpers.ListOptions{Genus: genus, Ids: []int64{id}}
|
||||
characteristics_opt, err := models.CharacteristicsOptsFromStrains(opt)
|
||||
opt := helpers.ListOptions{Genus: genus, IDs: []int64{id}}
|
||||
characteristicsOpt, err := models.CharacteristicsOptsFromStrains(opt)
|
||||
if err != nil {
|
||||
return nil, NewJSONError(err, http.StatusInternalServerError)
|
||||
return nil, newJSONError(err, http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
characteristics, err := models.ListCharacteristics(*characteristics_opt, claims)
|
||||
characteristics, err := models.ListCharacteristics(*characteristicsOpt, claims)
|
||||
if err != nil {
|
||||
return nil, NewJSONError(err, http.StatusInternalServerError)
|
||||
return nil, newJSONError(err, http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
characteristic_ids := []int64{}
|
||||
characteristicIDs := []int64{}
|
||||
for _, c := range *characteristics {
|
||||
characteristic_ids = append(characteristic_ids, c.Id)
|
||||
characteristicIDs = append(characteristicIDs, c.ID)
|
||||
}
|
||||
|
||||
measurement_opt := helpers.MeasurementListOptions{
|
||||
measurementOpt := helpers.MeasurementListOptions{
|
||||
ListOptions: helpers.ListOptions{
|
||||
Genus: genus,
|
||||
},
|
||||
Strains: []int64{id},
|
||||
Characteristics: characteristic_ids,
|
||||
Characteristics: characteristicIDs,
|
||||
}
|
||||
|
||||
measurements, err := models.ListMeasurements(measurement_opt, claims)
|
||||
measurements, err := models.ListMeasurements(measurementOpt, claims)
|
||||
if err != nil {
|
||||
return nil, NewJSONError(err, http.StatusInternalServerError)
|
||||
return nil, newJSONError(err, http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
var many_species models.ManySpecies = []*models.Species{species}
|
||||
var manySpecies models.ManySpecies = []*models.Species{species}
|
||||
|
||||
payload := payloads.Strain{
|
||||
Strain: strain,
|
||||
Species: &many_species,
|
||||
Species: &manySpecies,
|
||||
Characteristics: characteristics,
|
||||
Measurements: measurements,
|
||||
Meta: &models.StrainMeta{
|
||||
|
@ -145,35 +149,36 @@ func (s StrainService) Get(id int64, genus string, claims *types.Claims) (types.
|
|||
return &payload, nil
|
||||
}
|
||||
|
||||
// Update modifies an existing strain
|
||||
func (s StrainService) Update(id int64, e *types.Entity, genus string, claims *types.Claims) *types.AppError {
|
||||
payload := (*e).(*payloads.Strain)
|
||||
payload.Strain.UpdatedBy = claims.Sub
|
||||
payload.Strain.Id = id
|
||||
payload.Strain.ID = id
|
||||
|
||||
// TODO: fix this
|
||||
count, err := models.DBH.Update(payload.Strain.StrainBase)
|
||||
if err != nil {
|
||||
return NewJSONError(err, http.StatusInternalServerError)
|
||||
return newJSONError(err, http.StatusInternalServerError)
|
||||
}
|
||||
if count != 1 {
|
||||
// TODO: fix this
|
||||
return NewJSONError(errors.StrainNotUpdated, http.StatusBadRequest)
|
||||
return newJSONError(errors.ErrStrainNotUpdated, http.StatusBadRequest)
|
||||
}
|
||||
|
||||
strain, err := models.GetStrain(id, genus, claims)
|
||||
if err != nil {
|
||||
return NewJSONError(err, http.StatusInternalServerError)
|
||||
return newJSONError(err, http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
species, err := models.GetSpecies(strain.SpeciesId, genus, claims)
|
||||
species, err := models.GetSpecies(strain.SpeciesID, genus, claims)
|
||||
if err != nil {
|
||||
return NewJSONError(err, http.StatusInternalServerError)
|
||||
return newJSONError(err, http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
var many_species models.ManySpecies = []*models.Species{species}
|
||||
var manySpecies models.ManySpecies = []*models.Species{species}
|
||||
|
||||
payload.Strain = strain
|
||||
payload.Species = &many_species
|
||||
payload.Species = &manySpecies
|
||||
payload.Meta = &models.StrainMeta{
|
||||
CanAdd: helpers.CanAdd(claims),
|
||||
}
|
||||
|
@ -181,6 +186,7 @@ func (s StrainService) Update(id int64, e *types.Entity, genus string, claims *t
|
|||
return nil
|
||||
}
|
||||
|
||||
// Create initializes a new strain
|
||||
func (s StrainService) Create(e *types.Entity, genus string, claims *types.Claims) *types.AppError {
|
||||
payload := (*e).(*payloads.Strain)
|
||||
payload.Strain.CreatedBy = claims.Sub
|
||||
|
@ -188,23 +194,23 @@ func (s StrainService) Create(e *types.Entity, genus string, claims *types.Claim
|
|||
|
||||
// TODO: fix this
|
||||
if err := models.DBH.Insert(payload.Strain.StrainBase); err != nil {
|
||||
return NewJSONError(err, http.StatusInternalServerError)
|
||||
return newJSONError(err, http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
strain, err := models.GetStrain(payload.Strain.Id, genus, claims)
|
||||
strain, err := models.GetStrain(payload.Strain.ID, genus, claims)
|
||||
if err != nil {
|
||||
return NewJSONError(err, http.StatusInternalServerError)
|
||||
return newJSONError(err, http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
species, err := models.GetSpecies(strain.SpeciesId, genus, claims)
|
||||
species, err := models.GetSpecies(strain.SpeciesID, genus, claims)
|
||||
if err != nil {
|
||||
return NewJSONError(err, http.StatusInternalServerError)
|
||||
return newJSONError(err, http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
var many_species models.ManySpecies = []*models.Species{species}
|
||||
var manySpecies models.ManySpecies = []*models.Species{species}
|
||||
|
||||
payload.Strain = strain
|
||||
payload.Species = &many_species
|
||||
payload.Species = &manySpecies
|
||||
payload.Meta = &models.StrainMeta{
|
||||
CanAdd: helpers.CanAdd(claims),
|
||||
}
|
||||
|
|
93
api/users.go
93
api/users.go
|
@ -20,24 +20,28 @@ import (
|
|||
)
|
||||
|
||||
var (
|
||||
// MgAccts is a map of Mailgun accounts.
|
||||
MgAccts = make(map[string]mailgun.Mailgun)
|
||||
)
|
||||
|
||||
// UserService provides for CRUD operations.
|
||||
type UserService struct{}
|
||||
|
||||
// Unmarshal satisfies interface Updater and interface Creater.
|
||||
func (u UserService) Unmarshal(b []byte) (types.Entity, error) {
|
||||
var uj payloads.User
|
||||
err := json.Unmarshal(b, &uj)
|
||||
return &uj, err
|
||||
}
|
||||
|
||||
// List lists all users.
|
||||
func (u UserService) List(val *url.Values, claims *types.Claims) (types.Entity, *types.AppError) {
|
||||
if val == nil {
|
||||
return nil, NewJSONError(errors.MustProvideOptions, http.StatusInternalServerError)
|
||||
return nil, newJSONError(errors.ErrMustProvideOptions, http.StatusInternalServerError)
|
||||
}
|
||||
var opt helpers.ListOptions
|
||||
if err := helpers.SchemaDecoder.Decode(&opt, *val); err != nil {
|
||||
return nil, NewJSONError(err, http.StatusInternalServerError)
|
||||
return nil, newJSONError(err, http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
// TODO: fix this
|
||||
|
@ -48,16 +52,23 @@ func (u UserService) List(val *url.Values, claims *types.Claims) (types.Entity,
|
|||
WHERE verified IS TRUE
|
||||
AND deleted_at IS NULL;`
|
||||
if err := models.DBH.Select(&users, sql); err != nil {
|
||||
return nil, NewJSONError(err, http.StatusInternalServerError)
|
||||
return nil, newJSONError(err, http.StatusInternalServerError)
|
||||
}
|
||||
return &users, nil
|
||||
payload := payloads.Users{
|
||||
Users: &users,
|
||||
Meta: &models.UserMeta{
|
||||
CanAdd: claims.Role == "A",
|
||||
},
|
||||
}
|
||||
return &payload, nil
|
||||
}
|
||||
|
||||
// Get retrieves a single user.
|
||||
func (u UserService) Get(id int64, dummy string, claims *types.Claims) (types.Entity, *types.AppError) {
|
||||
user, err := models.DbGetUserById(id)
|
||||
user, err := models.DbGetUserByID(id)
|
||||
user.Password = ""
|
||||
if err != nil {
|
||||
return nil, NewJSONError(err, http.StatusInternalServerError)
|
||||
return nil, newJSONError(err, http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
user.CanEdit = claims.Role == "A" || id == claims.Sub
|
||||
|
@ -71,17 +82,18 @@ func (u UserService) Get(id int64, dummy string, claims *types.Claims) (types.En
|
|||
return &payload, nil
|
||||
}
|
||||
|
||||
// Update modifies an existing user.
|
||||
func (u UserService) Update(id int64, e *types.Entity, dummy string, claims *types.Claims) *types.AppError {
|
||||
user := (*e).(*payloads.User).User
|
||||
|
||||
original_user, err := models.DbGetUserById(id)
|
||||
originalUser, err := models.DbGetUserByID(id)
|
||||
if err != nil {
|
||||
return NewJSONError(err, http.StatusInternalServerError)
|
||||
return newJSONError(err, http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
user.Id = id
|
||||
user.Password = original_user.Password
|
||||
user.Verified = original_user.Verified
|
||||
user.ID = id
|
||||
user.Password = originalUser.Password
|
||||
user.Verified = originalUser.Verified
|
||||
user.UpdatedAt = helpers.CurrentTime()
|
||||
|
||||
if err := user.Validate(); err != nil {
|
||||
|
@ -92,15 +104,16 @@ func (u UserService) Update(id int64, e *types.Entity, dummy string, claims *typ
|
|||
count, err := models.DBH.Update(user)
|
||||
user.Password = ""
|
||||
if err != nil {
|
||||
return NewJSONError(err, http.StatusInternalServerError)
|
||||
return newJSONError(err, http.StatusInternalServerError)
|
||||
}
|
||||
if count != 1 {
|
||||
return NewJSONError(errors.UserNotUpdated, http.StatusInternalServerError)
|
||||
return newJSONError(errors.ErrUserNotUpdated, http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Create initializes a new user.
|
||||
func (u UserService) Create(e *types.Entity, dummy string, claims *types.Claims) *types.AppError {
|
||||
user := (*e).(*payloads.User).User
|
||||
if err := user.Validate(); err != nil {
|
||||
|
@ -111,20 +124,20 @@ func (u UserService) Create(e *types.Entity, dummy string, claims *types.Claims)
|
|||
user.UpdatedAt = ct
|
||||
hash, err := bcrypt.GenerateFromPassword([]byte(user.Password), 12)
|
||||
if err != nil {
|
||||
return NewJSONError(err, http.StatusInternalServerError)
|
||||
return newJSONError(err, http.StatusInternalServerError)
|
||||
}
|
||||
user.Password = string(hash)
|
||||
user.Role = "R"
|
||||
user.Verified = false
|
||||
|
||||
// TODO: fix this
|
||||
if err := models.DBH.Insert(user); err != nil {
|
||||
if err := models.DBH.Insert(user.UserBase); err != nil {
|
||||
if err, ok := err.(*pq.Error); ok {
|
||||
if err.Code == "23505" {
|
||||
return NewJSONError(errors.EmailAddressTaken, http.StatusInternalServerError)
|
||||
return newJSONError(errors.ErrEmailAddressTaken, http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
return NewJSONError(err, http.StatusInternalServerError)
|
||||
return newJSONError(err, http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
user.Password = "password" // don't want to send the hashed PW back to the client
|
||||
|
@ -133,12 +146,12 @@ func (u UserService) Create(e *types.Entity, dummy string, claims *types.Claims)
|
|||
// TODO: move helpers.GenerateNonce
|
||||
nonce, err := helpers.GenerateNonce()
|
||||
if err != nil {
|
||||
return NewJSONError(err, http.StatusInternalServerError)
|
||||
return newJSONError(err, http.StatusInternalServerError)
|
||||
}
|
||||
// TODO: fix this
|
||||
_, err = models.DBH.Exec(q, user.Id, nonce, claims.Ref, ct)
|
||||
_, err = models.DBH.Exec(q, user.ID, nonce, claims.Ref, ct)
|
||||
if err != nil {
|
||||
return NewJSONError(err, http.StatusInternalServerError)
|
||||
return newJSONError(err, http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
// Send out confirmation email
|
||||
|
@ -157,34 +170,35 @@ func (u UserService) Create(e *types.Entity, dummy string, claims *types.Claims)
|
|||
_, _, err := mg.Send(m)
|
||||
if err != nil {
|
||||
log.Printf("%+v\n", err)
|
||||
return NewJSONError(err, http.StatusInternalServerError)
|
||||
return newJSONError(err, http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// HandleUserVerify is a HTTP handler for verifiying a user.
|
||||
func HandleUserVerify(w http.ResponseWriter, r *http.Request) *types.AppError {
|
||||
// TODO: clean this up
|
||||
nonce := mux.Vars(r)["Nonce"]
|
||||
q := `SELECT user_id, referer FROM verification WHERE nonce=$1;`
|
||||
|
||||
var ver struct {
|
||||
User_id int64
|
||||
UserID int64
|
||||
Referer string
|
||||
}
|
||||
if err := models.DBH.SelectOne(&ver, q, nonce); err != nil {
|
||||
log.Print(err)
|
||||
return NewJSONError(err, http.StatusInternalServerError)
|
||||
return newJSONError(err, http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
if ver.User_id == 0 {
|
||||
return NewJSONError(errors.UserNotFound, http.StatusInternalServerError)
|
||||
if ver.UserID == 0 {
|
||||
return newJSONError(errors.ErrUserNotFound, http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
var user models.User
|
||||
if err := models.DBH.Get(&user, ver.User_id); err != nil {
|
||||
return NewJSONError(err, http.StatusInternalServerError)
|
||||
if err := models.DBH.Get(&user, ver.UserID); err != nil {
|
||||
return newJSONError(err, http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
user.UpdatedAt = helpers.CurrentTime()
|
||||
|
@ -192,39 +206,40 @@ func HandleUserVerify(w http.ResponseWriter, r *http.Request) *types.AppError {
|
|||
|
||||
count, err := models.DBH.Update(&user)
|
||||
if err != nil {
|
||||
return NewJSONError(err, http.StatusInternalServerError)
|
||||
return newJSONError(err, http.StatusInternalServerError)
|
||||
}
|
||||
if count != 1 {
|
||||
return NewJSONError(errors.UserNotUpdated, http.StatusInternalServerError)
|
||||
return newJSONError(errors.ErrUserNotUpdated, http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
q = `DELETE FROM verification WHERE user_id=$1;`
|
||||
_, err = models.DBH.Exec(q, user.Id)
|
||||
_, err = models.DBH.Exec(q, user.ID)
|
||||
if err != nil {
|
||||
return NewJSONError(err, http.StatusInternalServerError)
|
||||
return newJSONError(err, http.StatusInternalServerError)
|
||||
}
|
||||
fmt.Fprintln(w, `{"msg":"All set! Please log in."}`)
|
||||
return nil
|
||||
}
|
||||
|
||||
// HandleUserLockout is a HTTP handler for unlocking a user's account.
|
||||
func HandleUserLockout(w http.ResponseWriter, r *http.Request) *types.AppError {
|
||||
email := r.FormValue("email")
|
||||
if email == "" {
|
||||
return NewJSONError(errors.UserMissingEmail, http.StatusInternalServerError)
|
||||
return newJSONError(errors.ErrUserMissingEmail, http.StatusInternalServerError)
|
||||
}
|
||||
token, err := auth.Middleware.CreateToken(email)
|
||||
if err != nil {
|
||||
return NewJSONError(err, http.StatusInternalServerError)
|
||||
return newJSONError(err, http.StatusInternalServerError)
|
||||
}
|
||||
origin := r.Header.Get("Origin")
|
||||
hostUrl, err := url.Parse(origin)
|
||||
hostURL, err := url.Parse(origin)
|
||||
if err != nil {
|
||||
return NewJSONError(err, http.StatusInternalServerError)
|
||||
return newJSONError(err, http.StatusInternalServerError)
|
||||
}
|
||||
hostUrl.Path += "/users/lockoutauthenticate"
|
||||
hostURL.Path += "/users/lockoutauthenticate"
|
||||
params := url.Values{}
|
||||
params.Add("token", token)
|
||||
hostUrl.RawQuery = params.Encode()
|
||||
hostURL.RawQuery = params.Encode()
|
||||
|
||||
// Send out email
|
||||
// TODO: clean this up
|
||||
|
@ -237,12 +252,12 @@ func HandleUserLockout(w http.ResponseWriter, r *http.Request) *types.AppError {
|
|||
"address was used in an account lockout request at %s. Please visit "+
|
||||
"this URL to complete the process: %s. If you did not request help "+
|
||||
"with a lockout, please disregard this message.",
|
||||
mg.Domain(), hostUrl.String())
|
||||
mg.Domain(), hostURL.String())
|
||||
m := mailgun.NewMessage(sender, subject, message, recipient)
|
||||
_, _, err := mg.Send(m)
|
||||
if err != nil {
|
||||
log.Printf("%+v\n", err)
|
||||
return NewJSONError(err, http.StatusInternalServerError)
|
||||
return newJSONError(err, http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Reference in a new issue