Add genus to update/PUT
This commit is contained in:
parent
49fdb7d41e
commit
a14f4f0418
5 changed files with 29 additions and 11 deletions
|
@ -15,7 +15,7 @@ type lister interface {
|
||||||
}
|
}
|
||||||
|
|
||||||
type updater interface {
|
type updater interface {
|
||||||
update(int64, *entity, Claims) *appError
|
update(int64, *entity, string, Claims) *appError
|
||||||
unmarshal([]byte) (entity, error)
|
unmarshal([]byte) (entity, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -182,7 +182,7 @@ func handleUpdater(u updater) errorHandler {
|
||||||
|
|
||||||
claims := getClaims(r)
|
claims := getClaims(r)
|
||||||
|
|
||||||
appErr := u.update(id, &e, claims)
|
appErr := u.update(id, &e, mux.Vars(r)["genus"], claims)
|
||||||
if appErr != nil {
|
if appErr != nil {
|
||||||
return appErr
|
return appErr
|
||||||
}
|
}
|
||||||
|
|
10
species.go
10
species.go
|
@ -145,13 +145,13 @@ func (s SpeciesService) get(id int64, genus string, claims Claims) (entity, *app
|
||||||
return &payload, nil
|
return &payload, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s SpeciesService) update(id int64, e *entity, claims Claims) *appError {
|
func (s SpeciesService) update(id int64, e *entity, genus string, claims Claims) *appError {
|
||||||
payload := (*e).(*SpeciesPayload)
|
payload := (*e).(*SpeciesPayload)
|
||||||
payload.Species.UpdatedBy = claims.Sub
|
payload.Species.UpdatedBy = claims.Sub
|
||||||
payload.Species.UpdatedAt = currentTime()
|
payload.Species.UpdatedAt = currentTime()
|
||||||
payload.Species.Id = id
|
payload.Species.Id = id
|
||||||
|
|
||||||
genus_id, err := genusIdFromName(payload.Species.GenusName)
|
genus_id, err := genusIdFromName(genus)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return newJSONError(err, http.StatusInternalServerError)
|
return newJSONError(err, http.StatusInternalServerError)
|
||||||
}
|
}
|
||||||
|
@ -165,12 +165,12 @@ func (s SpeciesService) update(id int64, e *entity, claims Claims) *appError {
|
||||||
return ErrSpeciesNotUpdatedJSON
|
return ErrSpeciesNotUpdatedJSON
|
||||||
}
|
}
|
||||||
|
|
||||||
species, err := getSpecies(id, payload.Species.GenusName)
|
species, err := getSpecies(id, genus)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return newJSONError(err, http.StatusInternalServerError)
|
return newJSONError(err, http.StatusInternalServerError)
|
||||||
}
|
}
|
||||||
|
|
||||||
strains, err := strainsFromSpeciesId(id, payload.Species.GenusName)
|
strains, err := strainsFromSpeciesId(id, genus)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return newJSONError(err, http.StatusInternalServerError)
|
return newJSONError(err, http.StatusInternalServerError)
|
||||||
}
|
}
|
||||||
|
@ -179,7 +179,7 @@ func (s SpeciesService) update(id int64, e *entity, claims Claims) *appError {
|
||||||
payload.Strains = strains
|
payload.Strains = strains
|
||||||
payload.Meta = &SpeciesMeta{
|
payload.Meta = &SpeciesMeta{
|
||||||
CanAdd: canAdd(claims),
|
CanAdd: canAdd(claims),
|
||||||
CanEdit: canEdit(claims, map[int64]int64{payload.Species.Id: payload.Species.CreatedBy}),
|
CanEdit: canEdit(claims, map[int64]int64{species.Id: species.CreatedBy}),
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|
22
strains.go
22
strains.go
|
@ -149,7 +149,7 @@ func (s StrainService) get(id int64, genus string, claims Claims) (entity, *appE
|
||||||
return &payload, nil
|
return &payload, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s StrainService) update(id int64, e *entity, claims Claims) *appError {
|
func (s StrainService) update(id int64, e *entity, genus string, claims Claims) *appError {
|
||||||
payload := (*e).(*StrainPayload)
|
payload := (*e).(*StrainPayload)
|
||||||
payload.Strain.UpdatedBy = claims.Sub
|
payload.Strain.UpdatedBy = claims.Sub
|
||||||
payload.Strain.UpdatedAt = currentTime()
|
payload.Strain.UpdatedAt = currentTime()
|
||||||
|
@ -163,7 +163,25 @@ func (s StrainService) update(id int64, e *entity, claims Claims) *appError {
|
||||||
return ErrStrainNotUpdatedJSON
|
return ErrStrainNotUpdatedJSON
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: add species and meta to payload
|
strain, err := getStrain(id, genus)
|
||||||
|
if err != nil {
|
||||||
|
return newJSONError(err, http.StatusInternalServerError)
|
||||||
|
}
|
||||||
|
|
||||||
|
species, err := getSpecies(strain.SpeciesId, genus)
|
||||||
|
if err != nil {
|
||||||
|
return newJSONError(err, http.StatusInternalServerError)
|
||||||
|
}
|
||||||
|
|
||||||
|
var many_species ManySpecies = []*Species{species}
|
||||||
|
|
||||||
|
payload.Strain = strain
|
||||||
|
|
||||||
|
payload.Species = &many_species
|
||||||
|
payload.Meta = &StrainMeta{
|
||||||
|
CanAdd: canAdd(claims),
|
||||||
|
CanEdit: canEdit(claims, map[int64]int64{strain.Id: strain.CreatedBy}),
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
4
users.go
4
users.go
|
@ -145,7 +145,7 @@ func (u UserService) list(val *url.Values, claims Claims) (entity, *appError) {
|
||||||
return &users, nil
|
return &users, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u UserService) get(id int64, genus string, claims Claims) (entity, *appError) {
|
func (u UserService) get(id int64, dummy string, claims Claims) (entity, *appError) {
|
||||||
var user User
|
var user User
|
||||||
q := `SELECT id, email, 'password' AS password, name, role,
|
q := `SELECT id, email, 'password' AS password, name, role,
|
||||||
created_at, updated_at, deleted_at
|
created_at, updated_at, deleted_at
|
||||||
|
@ -162,7 +162,7 @@ func (u UserService) get(id int64, genus string, claims Claims) (entity, *appErr
|
||||||
return &user, nil
|
return &user, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u UserService) update(id int64, e *entity, claims Claims) *appError {
|
func (u UserService) update(id int64, e *entity, dummy string, claims Claims) *appError {
|
||||||
user := (*e).(*User)
|
user := (*e).(*User)
|
||||||
user.UpdatedAt = currentTime()
|
user.UpdatedAt = currentTime()
|
||||||
user.Id = id
|
user.Id = id
|
||||||
|
|
Reference in a new issue