130 lines
3.2 KiB
Go
130 lines
3.2 KiB
Go
/*
|
|
* Dash API
|
|
*
|
|
* No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
|
|
*
|
|
* API version: 0.1
|
|
* Generated by: OpenAPI Generator (https://openapi-generator.tech)
|
|
*/
|
|
|
|
package dashapi
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"strings"
|
|
|
|
"github.com/gorilla/mux"
|
|
)
|
|
|
|
// InboxApiController binds http requests to an api service and writes the service results to the http response
|
|
type InboxApiController struct {
|
|
service InboxApiServicer
|
|
errorHandler ErrorHandler
|
|
}
|
|
|
|
// InboxApiOption for how the controller is set up.
|
|
type InboxApiOption func(*InboxApiController)
|
|
|
|
// WithInboxApiErrorHandler inject ErrorHandler into controller
|
|
func WithInboxApiErrorHandler(h ErrorHandler) InboxApiOption {
|
|
return func(c *InboxApiController) {
|
|
c.errorHandler = h
|
|
}
|
|
}
|
|
|
|
// NewInboxApiController creates a default api controller
|
|
func NewInboxApiController(s InboxApiServicer, opts ...InboxApiOption) Router {
|
|
controller := &InboxApiController{
|
|
service: s,
|
|
errorHandler: DefaultErrorHandler,
|
|
}
|
|
|
|
for _, opt := range opts {
|
|
opt(controller)
|
|
}
|
|
|
|
return controller
|
|
}
|
|
|
|
// Routes returns all the api routes for the InboxApiController
|
|
func (c *InboxApiController) Routes() Routes {
|
|
return Routes{
|
|
{
|
|
"AddInboxItem",
|
|
strings.ToUpper("Post"),
|
|
"/api/v1/inbox/",
|
|
c.AddInboxItem,
|
|
},
|
|
{
|
|
"DeleteInboxItem",
|
|
strings.ToUpper("Delete"),
|
|
"/api/v1/inbox/{item}",
|
|
c.DeleteInboxItem,
|
|
},
|
|
{
|
|
"GetInboxItems",
|
|
strings.ToUpper("Get"),
|
|
"/api/v1/inbox/",
|
|
c.GetInboxItems,
|
|
},
|
|
}
|
|
}
|
|
|
|
// AddInboxItem -
|
|
func (c *InboxApiController) AddInboxItem(w http.ResponseWriter, r *http.Request) {
|
|
inboxItemParam := InboxItem{}
|
|
d := json.NewDecoder(r.Body)
|
|
d.DisallowUnknownFields()
|
|
if err := d.Decode(&inboxItemParam); err != nil {
|
|
c.errorHandler(w, r, &ParsingError{Err: err}, nil)
|
|
return
|
|
}
|
|
if err := AssertInboxItemRequired(inboxItemParam); err != nil {
|
|
c.errorHandler(w, r, err, nil)
|
|
return
|
|
}
|
|
result, err := c.service.AddInboxItem(r.Context(), inboxItemParam)
|
|
// 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)
|
|
|
|
}
|
|
|
|
// DeleteInboxItem -
|
|
func (c *InboxApiController) DeleteInboxItem(w http.ResponseWriter, r *http.Request) {
|
|
params := mux.Vars(r)
|
|
itemParam, err := parseInt32Parameter(params["item"], true)
|
|
if err != nil {
|
|
c.errorHandler(w, r, &ParsingError{Err: err}, nil)
|
|
return
|
|
}
|
|
|
|
result, err := c.service.DeleteInboxItem(r.Context(), itemParam)
|
|
// 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)
|
|
|
|
}
|
|
|
|
// GetInboxItems -
|
|
func (c *InboxApiController) GetInboxItems(w http.ResponseWriter, r *http.Request) {
|
|
result, err := c.service.GetInboxItems(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)
|
|
|
|
}
|