211 lines
5.4 KiB
Go
211 lines
5.4 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"
|
|
)
|
|
|
|
// PlanApiController binds http requests to an api service and writes the service results to the http response
|
|
type PlanApiController struct {
|
|
service PlanApiServicer
|
|
errorHandler ErrorHandler
|
|
}
|
|
|
|
// PlanApiOption for how the controller is set up.
|
|
type PlanApiOption func(*PlanApiController)
|
|
|
|
// WithPlanApiErrorHandler inject ErrorHandler into controller
|
|
func WithPlanApiErrorHandler(h ErrorHandler) PlanApiOption {
|
|
return func(c *PlanApiController) {
|
|
c.errorHandler = h
|
|
}
|
|
}
|
|
|
|
// NewPlanApiController creates a default api controller
|
|
func NewPlanApiController(s PlanApiServicer, opts ...PlanApiOption) Router {
|
|
controller := &PlanApiController{
|
|
service: s,
|
|
errorHandler: DefaultErrorHandler,
|
|
}
|
|
|
|
for _, opt := range opts {
|
|
opt(controller)
|
|
}
|
|
|
|
return controller
|
|
}
|
|
|
|
// Routes returns all the api routes for the PlanApiController
|
|
func (c *PlanApiController) Routes() Routes {
|
|
return Routes{
|
|
{
|
|
"GetPlanDayForDate",
|
|
strings.ToUpper("Get"),
|
|
"/api/v1/plan/day/entry/{date}",
|
|
c.GetPlanDayForDate,
|
|
},
|
|
{
|
|
"GetPlanMonthForDate",
|
|
strings.ToUpper("Get"),
|
|
"/api/v1/plan/month/entry/{date}",
|
|
c.GetPlanMonthForDate,
|
|
},
|
|
{
|
|
"GetPlanWeekForDate",
|
|
strings.ToUpper("Get"),
|
|
"/api/v1/plan/week/entry/{date}",
|
|
c.GetPlanWeekForDate,
|
|
},
|
|
{
|
|
"SavePlanForDay",
|
|
strings.ToUpper("Post"),
|
|
"/api/v1/plan/day/entry/",
|
|
c.SavePlanForDay,
|
|
},
|
|
{
|
|
"SavePlanForMonth",
|
|
strings.ToUpper("Post"),
|
|
"/api/v1/plan/month/entry/",
|
|
c.SavePlanForMonth,
|
|
},
|
|
{
|
|
"SavePlanForWeek",
|
|
strings.ToUpper("Post"),
|
|
"/api/v1/plan/week/entry/",
|
|
c.SavePlanForWeek,
|
|
},
|
|
}
|
|
}
|
|
|
|
// GetPlanDayForDate -
|
|
func (c *PlanApiController) GetPlanDayForDate(w http.ResponseWriter, r *http.Request) {
|
|
params := mux.Vars(r)
|
|
dateParam := params["date"]
|
|
|
|
result, err := c.service.GetPlanDayForDate(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)
|
|
|
|
}
|
|
|
|
// GetPlanMonthForDate -
|
|
func (c *PlanApiController) GetPlanMonthForDate(w http.ResponseWriter, r *http.Request) {
|
|
params := mux.Vars(r)
|
|
dateParam := params["date"]
|
|
|
|
result, err := c.service.GetPlanMonthForDate(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)
|
|
|
|
}
|
|
|
|
// GetPlanWeekForDate -
|
|
func (c *PlanApiController) GetPlanWeekForDate(w http.ResponseWriter, r *http.Request) {
|
|
params := mux.Vars(r)
|
|
dateParam := params["date"]
|
|
|
|
result, err := c.service.GetPlanWeekForDate(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)
|
|
|
|
}
|
|
|
|
// SavePlanForDay -
|
|
func (c *PlanApiController) SavePlanForDay(w http.ResponseWriter, r *http.Request) {
|
|
planDayParam := PlanDay{}
|
|
d := json.NewDecoder(r.Body)
|
|
d.DisallowUnknownFields()
|
|
if err := d.Decode(&planDayParam); err != nil {
|
|
c.errorHandler(w, r, &ParsingError{Err: err}, nil)
|
|
return
|
|
}
|
|
if err := AssertPlanDayRequired(planDayParam); err != nil {
|
|
c.errorHandler(w, r, err, nil)
|
|
return
|
|
}
|
|
result, err := c.service.SavePlanForDay(r.Context(), planDayParam)
|
|
// 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)
|
|
|
|
}
|
|
|
|
// SavePlanForMonth -
|
|
func (c *PlanApiController) SavePlanForMonth(w http.ResponseWriter, r *http.Request) {
|
|
planMonthParam := PlanMonth{}
|
|
d := json.NewDecoder(r.Body)
|
|
d.DisallowUnknownFields()
|
|
if err := d.Decode(&planMonthParam); err != nil {
|
|
c.errorHandler(w, r, &ParsingError{Err: err}, nil)
|
|
return
|
|
}
|
|
if err := AssertPlanMonthRequired(planMonthParam); err != nil {
|
|
c.errorHandler(w, r, err, nil)
|
|
return
|
|
}
|
|
result, err := c.service.SavePlanForMonth(r.Context(), planMonthParam)
|
|
// 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)
|
|
|
|
}
|
|
|
|
// SavePlanForWeek -
|
|
func (c *PlanApiController) SavePlanForWeek(w http.ResponseWriter, r *http.Request) {
|
|
planWeekParam := PlanWeek{}
|
|
d := json.NewDecoder(r.Body)
|
|
d.DisallowUnknownFields()
|
|
if err := d.Decode(&planWeekParam); err != nil {
|
|
c.errorHandler(w, r, &ParsingError{Err: err}, nil)
|
|
return
|
|
}
|
|
if err := AssertPlanWeekRequired(planWeekParam); err != nil {
|
|
c.errorHandler(w, r, err, nil)
|
|
return
|
|
}
|
|
result, err := c.service.SavePlanForWeek(r.Context(), planWeekParam)
|
|
// 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)
|
|
|
|
}
|