Implement rough architecture
This commit is contained in:
131
backend/dashapi/api_default.go
Normal file
131
backend/dashapi/api_default.go
Normal file
@ -0,0 +1,131 @@
|
||||
/*
|
||||
* 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"
|
||||
)
|
||||
|
||||
// DefaultApiController binds http requests to an api service and writes the service results to the http response
|
||||
type DefaultApiController struct {
|
||||
service DefaultApiServicer
|
||||
errorHandler ErrorHandler
|
||||
}
|
||||
|
||||
// DefaultApiOption for how the controller is set up.
|
||||
type DefaultApiOption func(*DefaultApiController)
|
||||
|
||||
// WithDefaultApiErrorHandler inject ErrorHandler into controller
|
||||
func WithDefaultApiErrorHandler(h ErrorHandler) DefaultApiOption {
|
||||
return func(c *DefaultApiController) {
|
||||
c.errorHandler = h
|
||||
}
|
||||
}
|
||||
|
||||
// NewDefaultApiController creates a default api controller
|
||||
func NewDefaultApiController(s DefaultApiServicer, opts ...DefaultApiOption) Router {
|
||||
controller := &DefaultApiController{
|
||||
service: s,
|
||||
errorHandler: DefaultErrorHandler,
|
||||
}
|
||||
|
||||
for _, opt := range opts {
|
||||
opt(controller)
|
||||
}
|
||||
|
||||
return controller
|
||||
}
|
||||
|
||||
// Routes returns all the api routes for the DefaultApiController
|
||||
func (c *DefaultApiController) 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,
|
||||
},
|
||||
{
|
||||
"WriteJournalEntryForDate",
|
||||
strings.ToUpper("Post"),
|
||||
"/api/v1/journal/entry/{date}",
|
||||
c.WriteJournalEntryForDate,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// DeleteJournalEntryForDate -
|
||||
func (c *DefaultApiController) 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 *DefaultApiController) 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)
|
||||
|
||||
}
|
||||
|
||||
// WriteJournalEntryForDate -
|
||||
func (c *DefaultApiController) WriteJournalEntryForDate(w http.ResponseWriter, r *http.Request) {
|
||||
params := mux.Vars(r)
|
||||
dateParam := params["date"]
|
||||
|
||||
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.WriteJournalEntryForDate(r.Context(), dateParam, 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)
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user