diff --git a/.gitignore b/.gitignore index f052500..b21e782 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ api/api_server/ +api/api_client/ .vscode __debug_bin \ No newline at end of file diff --git a/api/dash_api.yaml b/api/dash_api.yaml index 9d7fe9e..28447bb 100644 --- a/api/dash_api.yaml +++ b/api/dash_api.yaml @@ -7,6 +7,18 @@ servers: - url: http://localhost:8080/api/v1 - url: https://dash.pander.me/api/v1 paths: + /journal/entry/: + post: + operationId: writeJournalEntry + requestBody: + required: true + content: + application/json: + schema: + $ref: '#/components/schemas/JournalEntry' + responses: + '200': + description: Journal entry successfully written. /journal/entry/{date}: get: operationId: getJournalEntryForDate @@ -25,24 +37,6 @@ paths: type: string format: date example: 2022-02-18 - post: - operationId: writeJournalEntryForDate - requestBody: - content: - application/json: - schema: - $ref: '#/components/schemas/JournalEntry' - responses: - '200': - description: Journal entry successfully written. - parameters: - - name: date - in: path - required: true - schema: - type: string - format: date - example: 2022-02-18 delete: operationId: deleteJournalEntryForDate responses: diff --git a/backend/dash_backend.go b/backend/dash_backend.go index 9a4b924..f2ca30b 100644 --- a/backend/dash_backend.go +++ b/backend/dash_backend.go @@ -12,6 +12,8 @@ import ( "log" "net/http" + "github.com/gorilla/handlers" + dashapi "github.com/moustachioed/dash/backend/dashapi" database "github.com/moustachioed/dash/backend/database" mapping "github.com/moustachioed/dash/backend/mapping" @@ -29,8 +31,15 @@ func main() { DefaultApiService := service.NewApiService(db, mapper) DefaultApiController := dashapi.NewDefaultApiController(DefaultApiService) + cors := handlers.CORS( + // handlers.AllowedMethods([]string{"GET", "POST", "DELETE"}), + handlers.AllowedHeaders([]string{"Accept", "Accept-Language", "Content-Type", "Content-Language", "Origin"}), + handlers.AllowedOrigins([]string{"*"}), + ) + router := dashapi.NewRouter(DefaultApiController) + router.Methods("GET", "POST", "DELETE", "OPTIONS") log.Printf("Starting server.") - log.Fatal(http.ListenAndServe(":8080", router)) + log.Fatal(http.ListenAndServe(":8080", cors(router))) } diff --git a/backend/dashapi/api.go b/backend/dashapi/api.go index c197e06..f7f7547 100644 --- a/backend/dashapi/api.go +++ b/backend/dashapi/api.go @@ -22,7 +22,7 @@ import ( type DefaultApiRouter interface { DeleteJournalEntryForDate(http.ResponseWriter, *http.Request) GetJournalEntryForDate(http.ResponseWriter, *http.Request) - WriteJournalEntryForDate(http.ResponseWriter, *http.Request) + WriteJournalEntry(http.ResponseWriter, *http.Request) } @@ -33,5 +33,5 @@ type DefaultApiRouter interface { type DefaultApiServicer interface { DeleteJournalEntryForDate(context.Context, string) (ImplResponse, error) GetJournalEntryForDate(context.Context, string) (ImplResponse, error) - WriteJournalEntryForDate(context.Context, string, JournalEntry) (ImplResponse, error) + WriteJournalEntry(context.Context, JournalEntry) (ImplResponse, error) } diff --git a/backend/dashapi/api_default.go b/backend/dashapi/api_default.go index 8a0d837..a814241 100644 --- a/backend/dashapi/api_default.go +++ b/backend/dashapi/api_default.go @@ -63,10 +63,10 @@ func (c *DefaultApiController) Routes() Routes { c.GetJournalEntryForDate, }, { - "WriteJournalEntryForDate", + "WriteJournalEntry", strings.ToUpper("Post"), - "/api/v1/journal/entry/{date}", - c.WriteJournalEntryForDate, + "/api/v1/journal/entry/", + c.WriteJournalEntry, }, } } @@ -103,11 +103,8 @@ func (c *DefaultApiController) GetJournalEntryForDate(w http.ResponseWriter, r * } -// WriteJournalEntryForDate - -func (c *DefaultApiController) WriteJournalEntryForDate(w http.ResponseWriter, r *http.Request) { - params := mux.Vars(r) - dateParam := params["date"] - +// WriteJournalEntry - +func (c *DefaultApiController) WriteJournalEntry(w http.ResponseWriter, r *http.Request) { journalEntryParam := JournalEntry{} d := json.NewDecoder(r.Body) d.DisallowUnknownFields() @@ -119,7 +116,7 @@ func (c *DefaultApiController) WriteJournalEntryForDate(w http.ResponseWriter, r c.errorHandler(w, r, err, nil) return } - result, err := c.service.WriteJournalEntryForDate(r.Context(), dateParam, journalEntryParam) + 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) diff --git a/backend/dashapi/api_default_service.go b/backend/dashapi/api_default_service.go index e193749..859b899 100644 --- a/backend/dashapi/api_default_service.go +++ b/backend/dashapi/api_default_service.go @@ -48,13 +48,13 @@ func (s *DefaultApiService) GetJournalEntryForDate(ctx context.Context, date str return Response(http.StatusNotImplemented, nil), errors.New("GetJournalEntryForDate method not implemented") } -// WriteJournalEntryForDate - -func (s *DefaultApiService) WriteJournalEntryForDate(ctx context.Context, date string, journalEntry JournalEntry) (ImplResponse, error) { - // TODO - update WriteJournalEntryForDate with the required logic for this service method. +// WriteJournalEntry - +func (s *DefaultApiService) WriteJournalEntry(ctx context.Context, journalEntry JournalEntry) (ImplResponse, error) { + // TODO - update WriteJournalEntry with the required logic for this service method. // Add api_default_service.go to the .openapi-generator-ignore to avoid overwriting this service implementation when updating open api generation. //TODO: Uncomment the next line to return response Response(200, {}) or use other options such as http.Ok ... //return Response(200, nil),nil - return Response(http.StatusNotImplemented, nil), errors.New("WriteJournalEntryForDate method not implemented") + return Response(http.StatusNotImplemented, nil), errors.New("WriteJournalEntry method not implemented") } diff --git a/backend/service/api_service.go b/backend/service/api_service.go index 89d83bf..62bd1ee 100644 --- a/backend/service/api_service.go +++ b/backend/service/api_service.go @@ -42,7 +42,7 @@ func (s *ApiService) GetJournalEntryForDate(ctx context.Context, date string) (d } // WriteJournalEntryForDate - -func (s *ApiService) WriteJournalEntryForDate(ctx context.Context, date string, journalEntry dashapi.JournalEntry) (dashapi.ImplResponse, error) { +func (s *ApiService) WriteJournalEntry(ctx context.Context, journalEntry dashapi.JournalEntry) (dashapi.ImplResponse, error) { journal := s.mapper.JournalApiToDb(journalEntry) s.db.WriteJournalEntry(journal)