// 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 (
	"net/http"
	"strings"
)

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

// ListingAPIOption for how the controller is set up.
type ListingAPIOption func(*ListingAPIController)

// WithListingAPIErrorHandler inject ErrorHandler into controller
func WithListingAPIErrorHandler(h ErrorHandler) ListingAPIOption {
	return func(c *ListingAPIController) {
		c.errorHandler = h
	}
}

// NewListingAPIController creates a default api controller
func NewListingAPIController(s ListingAPIServicer, opts ...ListingAPIOption) *ListingAPIController {
	controller := &ListingAPIController{
		service:      s,
		errorHandler: DefaultErrorHandler,
	}

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

	return controller
}

// Routes returns all the api routes for the ListingAPIController
func (c *ListingAPIController) Routes() Routes {
	return Routes{
		"GetRecords": Route{
			"GetRecords",
			strings.ToUpper("Get"),
			"/records",
			c.GetRecords,
		},
	}
}

// OrderedRoutes returns all the api routes in a deterministic order for the ListingAPIController
func (c *ListingAPIController) OrderedRoutes() []Route {
	return []Route{
		Route{
			"GetRecords",
			strings.ToUpper("Get"),
			"/records",
			c.GetRecords,
		},
	}
}

// GetRecords - Returns the current records.
func (c *ListingAPIController) GetRecords(w http.ResponseWriter, r *http.Request) {
	result, err := c.service.GetRecords(r.Context())
	// 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)
}
