@@ -0,0 +1,56 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// CreateSpecies creates garden-specific plant master data.
|
||||
func (c *Client) CreateSpecies(ctx context.Context, gardenID int, input SpeciesInput) (Species, *Response, error) {
|
||||
return c.writeSpecies(ctx, http.MethodPost, speciesCollectionPath(gardenID), input)
|
||||
}
|
||||
|
||||
// SpeciesForGarden lists global and garden-specific species available to a garden.
|
||||
func (c *Client) SpeciesForGarden(ctx context.Context, gardenID int) ([]Species, *Response, error) {
|
||||
var envelope struct {
|
||||
Species []Species `json:"species"`
|
||||
}
|
||||
response, err := c.do(ctx, http.MethodGet, speciesCollectionPath(gardenID), nil, &envelope)
|
||||
return envelope.Species, response, err
|
||||
}
|
||||
|
||||
// Species returns species data available to a garden.
|
||||
func (c *Client) Species(ctx context.Context, gardenID, speciesID int) (Species, *Response, error) {
|
||||
var envelope struct {
|
||||
Species Species `json:"species"`
|
||||
}
|
||||
response, err := c.do(ctx, http.MethodGet, speciesPath(gardenID, speciesID), nil, &envelope)
|
||||
return envelope.Species, response, err
|
||||
}
|
||||
|
||||
// UpdateSpecies partially updates garden-specific species data.
|
||||
func (c *Client) UpdateSpecies(ctx context.Context, gardenID, speciesID int, input SpeciesInput) (Species, *Response, error) {
|
||||
return c.writeSpecies(ctx, http.MethodPatch, speciesPath(gardenID, speciesID), input)
|
||||
}
|
||||
|
||||
// DeleteSpecies deletes garden-specific species data.
|
||||
func (c *Client) DeleteSpecies(ctx context.Context, gardenID, speciesID int) (*Response, error) {
|
||||
return c.do(ctx, http.MethodDelete, speciesPath(gardenID, speciesID), nil, nil)
|
||||
}
|
||||
|
||||
func (c *Client) writeSpecies(ctx context.Context, method, path string, input SpeciesInput) (Species, *Response, error) {
|
||||
var envelope struct {
|
||||
Species Species `json:"species"`
|
||||
}
|
||||
response, err := c.do(ctx, method, path, input, &envelope)
|
||||
return envelope.Species, response, err
|
||||
}
|
||||
|
||||
func speciesCollectionPath(gardenID int) string {
|
||||
return "v1/gardens/" + strconv.Itoa(gardenID) + "/species"
|
||||
}
|
||||
|
||||
func speciesPath(gardenID, speciesID int) string {
|
||||
return speciesCollectionPath(gardenID) + "/" + strconv.Itoa(speciesID)
|
||||
}
|
||||
Reference in New Issue
Block a user