Implement Journal read/write

This commit is contained in:
Martin Pander
2022-11-07 19:19:23 +01:00
parent ac6d3dbe8e
commit c3de3dd21f
7 changed files with 378 additions and 44 deletions

View File

@ -1,17 +1,9 @@
/*
* 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 service
import (
"context"
"errors"
"log"
"net/http"
"github.com/moustachioed/dash/backend/dashapi"
@ -19,44 +11,44 @@ import (
"github.com/moustachioed/dash/backend/mapping"
)
// DefaultApiService is a service that implements the logic for the DefaultApiServicer
// This service should implement the business logic for every endpoint for the DefaultApi API.
// ApiService is a service that implements the logic for the DefaultApiServicer
// This service should implement the business logic for every endpoint for the Api API.
// Include any external packages or services that will be required by this service.
type DefaultApiService struct {
type ApiService struct {
db database.Database
mapper mapping.Mapper
}
// NewDefaultApiService creates a default api service
func NewDefaultApiService(db database.Database, mapper mapping.Mapper) dashapi.DefaultApiServicer {
return &DefaultApiService{}
// NewApiService creates a default api service
func NewApiService(db database.Database, mapper mapping.Mapper) dashapi.DefaultApiServicer {
service := ApiService{
db: db,
mapper: mapper,
}
return &service
}
// GetJournalEntryForDate -
func (s *DefaultApiService) GetJournalEntryForDate(ctx context.Context, date string) (dashapi.ImplResponse, error) {
// TODO - update GetJournalEntryForDate 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.
func (s *ApiService) GetJournalEntryForDate(ctx context.Context, date string) (dashapi.ImplResponse, error) {
d, err := s.mapper.StringToDate(date)
if err != nil {
log.Fatal(err)
}
//TODO: Uncomment the next line to return response Response(200, JournalEntry{}) or use other options such as http.Ok ...
//return Response(200, JournalEntry{}), nil
return dashapi.Response(http.StatusNotImplemented, nil), errors.New("GetJournalEntryForDate method not implemented")
dbEntry, _ := s.db.GetJournalEntryForDate(d)
journal := s.mapper.JournalDbToApi(dbEntry)
return dashapi.Response(200, journal), nil
}
// WriteJournalEntryForDate -
func (s *DefaultApiService) WriteJournalEntryForDate(ctx context.Context, date string, journalEntry dashapi.JournalEntry) (dashapi.ImplResponse, error) {
// TODO - update WriteJournalEntryForDate 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
func (s *ApiService) WriteJournalEntryForDate(ctx context.Context, date string, journalEntry dashapi.JournalEntry) (dashapi.ImplResponse, error) {
journal := s.mapper.JournalApiToDb(journalEntry)
s.db.WriteJournalEntry(journal)
// log.Printf(journal.Thankful)
return dashapi.Response(http.StatusNotImplemented, nil), errors.New("WriteJournalEntryForDate method not implemented")
return dashapi.Response(200, nil), nil
}
func (s *DefaultApiService) DeleteJournalEntryForDate(ctx context.Context, date string) (dashapi.ImplResponse, error) {
func (s *ApiService) DeleteJournalEntryForDate(ctx context.Context, date string) (dashapi.ImplResponse, error) {
return dashapi.Response(http.StatusNotImplemented, nil), errors.New("DeleteJournalEntryForDate method not implemented")
}