// Code generated by OpenAPI Generator (https://openapi-generator.tech); DO NOT EDIT.

/*
 * External DNS Webhook Server
 *
 * Implements the external DNS webhook endpoints.
 *
 * API version: v0.15.0
 */

package externaldnsapi

import (
	"encoding/json"
	"net/http"
	"strings"
)

// UpdateAPIController binds http requests to an api service and writes the service results to the http response
type UpdateAPIController struct {
	service      UpdateAPIServicer
	errorHandler ErrorHandler
}

// UpdateAPIOption for how the controller is set up.
type UpdateAPIOption func(*UpdateAPIController)

// WithUpdateAPIErrorHandler inject ErrorHandler into controller
func WithUpdateAPIErrorHandler(h ErrorHandler) UpdateAPIOption {
	return func(c *UpdateAPIController) {
		c.errorHandler = h
	}
}

// NewUpdateAPIController creates a default api controller
func NewUpdateAPIController(s UpdateAPIServicer, opts ...UpdateAPIOption) *UpdateAPIController {
	controller := &UpdateAPIController{
		service:      s,
		errorHandler: DefaultErrorHandler,
	}

	for _, opt := range opts {
		opt(controller)
	}

	return controller
}

// Routes returns all the api routes for the UpdateAPIController
func (c *UpdateAPIController) Routes() Routes {
	return Routes{
		"SetRecords": Route{
			"SetRecords",
			strings.ToUpper("Post"),
			"/records",
			c.SetRecords,
		},
		"AdjustRecords": Route{
			"AdjustRecords",
			strings.ToUpper("Post"),
			"/adjustendpoints",
			c.AdjustRecords,
		},
	}
}

// OrderedRoutes returns all the api routes in a deterministic order for the UpdateAPIController
func (c *UpdateAPIController) OrderedRoutes() []Route {
	return []Route{
		Route{
			"SetRecords",
			strings.ToUpper("Post"),
			"/records",
			c.SetRecords,
		},
		Route{
			"AdjustRecords",
			strings.ToUpper("Post"),
			"/adjustendpoints",
			c.AdjustRecords,
		},
	}
}

// SetRecords - Applies the changes.
func (c *UpdateAPIController) SetRecords(w http.ResponseWriter, r *http.Request) {
	var changesParam Changes
	d := json.NewDecoder(r.Body)
	d.DisallowUnknownFields()
	if err := d.Decode(&changesParam); err != nil {
		c.errorHandler(w, r, &ParsingError{Err: err}, nil)
		return
	}
	if err := AssertChangesRequired(changesParam); err != nil {
		c.errorHandler(w, r, err, nil)
		return
	}
	if err := AssertChangesConstraints(changesParam); err != nil {
		c.errorHandler(w, r, err, nil)
		return
	}
	result, err := c.service.SetRecords(r.Context(), changesParam)
	// If an error occurred, encode the error with the status code
	if err != nil {
		c.errorHandler(w, r, err, &result)
		return
	}
	// If no error, encode the body and the result code
	_ = EncodeJSONResponse(result.Body, &result.Code, w)
}

// AdjustRecords - Executes the AdjustEndpoints method.
func (c *UpdateAPIController) AdjustRecords(w http.ResponseWriter, r *http.Request) {
	var endpointParam []Endpoint
	d := json.NewDecoder(r.Body)
	d.DisallowUnknownFields()
	if err := d.Decode(&endpointParam); err != nil {
		c.errorHandler(w, r, &ParsingError{Err: err}, nil)
		return
	}
	for _, el := range endpointParam {
		if err := AssertEndpointRequired(el); err != nil {
			c.errorHandler(w, r, err, nil)
			return
		}
	}
	result, err := c.service.AdjustRecords(r.Context(), endpointParam)
	// If an error occurred, encode the error with the status code
	if err != nil {
		c.errorHandler(w, r, err, &result)
		return
	}
	// If no error, encode the body and the result code
	_ = EncodeJSONResponse(result.Body, &result.Code, w)
}
