@@ -0,0 +1,47 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// CreatePlantLocation assigns a plant to a location.
|
||||
func (c *Client) CreatePlantLocation(ctx context.Context, gardenID, plantID int, input PlantLocationInput) (PlantLocation, *Response, error) {
|
||||
return c.writePlantLocation(ctx, http.MethodPost, plantLocationCollectionPath(gardenID, plantID), input)
|
||||
}
|
||||
|
||||
// PlantLocations lists a plant's location assignments.
|
||||
func (c *Client) PlantLocations(ctx context.Context, gardenID, plantID int) ([]PlantLocation, *Response, error) {
|
||||
var envelope struct {
|
||||
PlantLocations []PlantLocation `json:"plant_locations"`
|
||||
}
|
||||
response, err := c.do(ctx, http.MethodGet, plantLocationCollectionPath(gardenID, plantID), nil, &envelope)
|
||||
return envelope.PlantLocations, response, err
|
||||
}
|
||||
|
||||
// UpdatePlantLocation updates one location assignment.
|
||||
func (c *Client) UpdatePlantLocation(ctx context.Context, gardenID, plantID, assignmentID int, input PlantLocationInput) (PlantLocation, *Response, error) {
|
||||
return c.writePlantLocation(ctx, http.MethodPatch, plantLocationPath(gardenID, plantID, assignmentID), input)
|
||||
}
|
||||
|
||||
// DeletePlantLocation deletes one location assignment.
|
||||
func (c *Client) DeletePlantLocation(ctx context.Context, gardenID, plantID, assignmentID int) (*Response, error) {
|
||||
return c.do(ctx, http.MethodDelete, plantLocationPath(gardenID, plantID, assignmentID), nil, nil)
|
||||
}
|
||||
|
||||
func (c *Client) writePlantLocation(ctx context.Context, method, path string, input PlantLocationInput) (PlantLocation, *Response, error) {
|
||||
var envelope struct {
|
||||
PlantLocation PlantLocation `json:"plant_location"`
|
||||
}
|
||||
response, err := c.do(ctx, method, path, input, &envelope)
|
||||
return envelope.PlantLocation, response, err
|
||||
}
|
||||
|
||||
func plantLocationCollectionPath(gardenID, plantID int) string {
|
||||
return plantPath(gardenID, plantID) + "/locations"
|
||||
}
|
||||
|
||||
func plantLocationPath(gardenID, plantID, assignmentID int) string {
|
||||
return plantLocationCollectionPath(gardenID, plantID) + "/" + strconv.Itoa(assignmentID)
|
||||
}
|
||||
Reference in New Issue
Block a user