Files
dash/backend/service/api_service.go
2022-11-08 11:01:31 +01:00

55 lines
1.6 KiB
Go

package service
import (
"context"
"errors"
"log"
"net/http"
"github.com/moustachioed/dash/backend/dashapi"
"github.com/moustachioed/dash/backend/database"
"github.com/moustachioed/dash/backend/mapping"
)
// 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 ApiService struct {
db database.Database
mapper mapping.Mapper
}
// 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 *ApiService) GetJournalEntryForDate(ctx context.Context, date string) (dashapi.ImplResponse, error) {
d, err := s.mapper.StringToDate(date)
if err != nil {
log.Fatal(err)
}
dbEntry, _ := s.db.GetJournalEntryForDate(d)
journal := s.mapper.JournalDbToApi(dbEntry)
return dashapi.Response(200, journal), nil
}
// WriteJournalEntryForDate -
func (s *ApiService) WriteJournalEntry(ctx context.Context, journalEntry dashapi.JournalEntry) (dashapi.ImplResponse, error) {
journal := s.mapper.JournalApiToDb(journalEntry)
s.db.WriteJournalEntry(journal)
return dashapi.Response(200, nil), nil
}
func (s *ApiService) DeleteJournalEntryForDate(ctx context.Context, date string) (dashapi.ImplResponse, error) {
return dashapi.Response(http.StatusNotImplemented, nil), errors.New("DeleteJournalEntryForDate method not implemented")
}