/* * 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" ) // JournalApiController binds http requests to an api service and writes the service results to the http response type JournalApiController struct { service JournalApiServicer errorHandler ErrorHandler } // JournalApiOption for how the controller is set up. type JournalApiOption func(*JournalApiController) // WithJournalApiErrorHandler inject ErrorHandler into controller func WithJournalApiErrorHandler(h ErrorHandler) JournalApiOption { return func(c *JournalApiController) { c.errorHandler = h } } // NewJournalApiController creates a default api controller func NewJournalApiController(s JournalApiServicer, opts ...JournalApiOption) Router { controller := &JournalApiController{ service: s, errorHandler: DefaultErrorHandler, } for _, opt := range opts { opt(controller) } return controller } // Routes returns all the api routes for the JournalApiController func (c *JournalApiController) Routes() Routes { return Routes{ { "DeleteJournalEntryForDate", strings.ToUpper("Delete"), "/api/v1/journal/entry/{date}", c.DeleteJournalEntryForDate, }, { "GetJournalEntryForDate", strings.ToUpper("Get"), "/api/v1/journal/entry/{date}", c.GetJournalEntryForDate, }, { "WriteJournalEntry", strings.ToUpper("Post"), "/api/v1/journal/entry/", c.WriteJournalEntry, }, } } // DeleteJournalEntryForDate - func (c *JournalApiController) DeleteJournalEntryForDate(w http.ResponseWriter, r *http.Request) { params := mux.Vars(r) dateParam := params["date"] result, err := c.service.DeleteJournalEntryForDate(r.Context(), dateParam) // 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) } // GetJournalEntryForDate - func (c *JournalApiController) GetJournalEntryForDate(w http.ResponseWriter, r *http.Request) { params := mux.Vars(r) dateParam := params["date"] result, err := c.service.GetJournalEntryForDate(r.Context(), dateParam) // 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) } // WriteJournalEntry - func (c *JournalApiController) WriteJournalEntry(w http.ResponseWriter, r *http.Request) { journalEntryParam := JournalEntry{} d := json.NewDecoder(r.Body) d.DisallowUnknownFields() if err := d.Decode(&journalEntryParam); err != nil { c.errorHandler(w, r, &ParsingError{Err: err}, nil) return } if err := AssertJournalEntryRequired(journalEntryParam); err != nil { c.errorHandler(w, r, err, nil) return } result, err := c.service.WriteJournalEntry(r.Context(), journalEntryParam) // 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) }