Add Plan page with day planner

This commit is contained in:
Martin Pander
2022-11-26 18:41:39 +01:00
parent d9b92ab4e9
commit 32346e0aa9
48 changed files with 2400 additions and 148 deletions

View File

@ -28,8 +28,11 @@ func main() {
mapper := mapping.NewMapperImpl()
DefaultApiService := service.NewApiService(db, mapper)
DefaultApiController := dashapi.NewDefaultApiController(DefaultApiService)
JournalApiService := service.NewJournalApiService(db, mapper)
JournalApiController := dashapi.NewJournalApiController(JournalApiService)
PlanApiService := service.NewPlanApiService(db, mapper)
PlanApiController := dashapi.NewPlanApiController(PlanApiService)
cors := handlers.CORS(
// handlers.AllowedMethods([]string{"GET", "POST", "DELETE"}),
@ -37,7 +40,7 @@ func main() {
handlers.AllowedOrigins([]string{"*"}),
)
router := dashapi.NewRouter(DefaultApiController)
router := dashapi.NewRouter(JournalApiController, PlanApiController)
router.Methods("GET", "POST", "DELETE", "OPTIONS")
log.Printf("Starting server.")

View File

@ -16,22 +16,47 @@ import (
// DefaultApiRouter defines the required methods for binding the api requests to a responses for the DefaultApi
// The DefaultApiRouter implementation should parse necessary information from the http request,
// pass the data to a DefaultApiServicer to perform the required actions, then write the service results to the http response.
type DefaultApiRouter interface {
// JournalApiRouter defines the required methods for binding the api requests to a responses for the JournalApi
// The JournalApiRouter implementation should parse necessary information from the http request,
// pass the data to a JournalApiServicer to perform the required actions, then write the service results to the http response.
type JournalApiRouter interface {
DeleteJournalEntryForDate(http.ResponseWriter, *http.Request)
GetJournalEntryForDate(http.ResponseWriter, *http.Request)
WriteJournalEntry(http.ResponseWriter, *http.Request)
}
// PlanApiRouter defines the required methods for binding the api requests to a responses for the PlanApi
// The PlanApiRouter implementation should parse necessary information from the http request,
// pass the data to a PlanApiServicer to perform the required actions, then write the service results to the http response.
type PlanApiRouter interface {
GetPlanDayForDate(http.ResponseWriter, *http.Request)
GetPlanMonthForDate(http.ResponseWriter, *http.Request)
GetPlanWeekForDate(http.ResponseWriter, *http.Request)
SavePlanForDay(http.ResponseWriter, *http.Request)
SavePlanForMonth(http.ResponseWriter, *http.Request)
SavePlanForWeek(http.ResponseWriter, *http.Request)
}
// DefaultApiServicer defines the api actions for the DefaultApi service
// JournalApiServicer defines the api actions for the JournalApi service
// This interface intended to stay up to date with the openapi yaml used to generate it,
// while the service implementation can be ignored with the .openapi-generator-ignore file
// and updated with the logic required for the API.
type DefaultApiServicer interface {
type JournalApiServicer interface {
DeleteJournalEntryForDate(context.Context, string) (ImplResponse, error)
GetJournalEntryForDate(context.Context, string) (ImplResponse, error)
WriteJournalEntry(context.Context, JournalEntry) (ImplResponse, error)
}
// PlanApiServicer defines the api actions for the PlanApi service
// This interface intended to stay up to date with the openapi yaml used to generate it,
// while the service implementation can be ignored with the .openapi-generator-ignore file
// and updated with the logic required for the API.
type PlanApiServicer interface {
GetPlanDayForDate(context.Context, string) (ImplResponse, error)
GetPlanMonthForDate(context.Context, string) (ImplResponse, error)
GetPlanWeekForDate(context.Context, string) (ImplResponse, error)
SavePlanForDay(context.Context, PlanDay) (ImplResponse, error)
SavePlanForMonth(context.Context, PlanMonth) (ImplResponse, error)
SavePlanForWeek(context.Context, PlanWeek) (ImplResponse, error)
}

View File

@ -17,25 +17,25 @@ import (
"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
// JournalApiController binds http requests to an api service and writes the service results to the http response
type JournalApiController struct {
service JournalApiServicer
errorHandler ErrorHandler
}
// DefaultApiOption for how the controller is set up.
type DefaultApiOption func(*DefaultApiController)
// JournalApiOption for how the controller is set up.
type JournalApiOption func(*JournalApiController)
// WithDefaultApiErrorHandler inject ErrorHandler into controller
func WithDefaultApiErrorHandler(h ErrorHandler) DefaultApiOption {
return func(c *DefaultApiController) {
// WithJournalApiErrorHandler inject ErrorHandler into controller
func WithJournalApiErrorHandler(h ErrorHandler) JournalApiOption {
return func(c *JournalApiController) {
c.errorHandler = h
}
}
// NewDefaultApiController creates a default api controller
func NewDefaultApiController(s DefaultApiServicer, opts ...DefaultApiOption) Router {
controller := &DefaultApiController{
// NewJournalApiController creates a default api controller
func NewJournalApiController(s JournalApiServicer, opts ...JournalApiOption) Router {
controller := &JournalApiController{
service: s,
errorHandler: DefaultErrorHandler,
}
@ -47,8 +47,8 @@ func NewDefaultApiController(s DefaultApiServicer, opts ...DefaultApiOption) Rou
return controller
}
// Routes returns all the api routes for the DefaultApiController
func (c *DefaultApiController) Routes() Routes {
// Routes returns all the api routes for the JournalApiController
func (c *JournalApiController) Routes() Routes {
return Routes{
{
"DeleteJournalEntryForDate",
@ -72,7 +72,7 @@ func (c *DefaultApiController) Routes() Routes {
}
// DeleteJournalEntryForDate -
func (c *DefaultApiController) DeleteJournalEntryForDate(w http.ResponseWriter, r *http.Request) {
func (c *JournalApiController) DeleteJournalEntryForDate(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
dateParam := params["date"]
@ -88,7 +88,7 @@ func (c *DefaultApiController) DeleteJournalEntryForDate(w http.ResponseWriter,
}
// GetJournalEntryForDate -
func (c *DefaultApiController) GetJournalEntryForDate(w http.ResponseWriter, r *http.Request) {
func (c *JournalApiController) GetJournalEntryForDate(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
dateParam := params["date"]
@ -104,7 +104,7 @@ func (c *DefaultApiController) GetJournalEntryForDate(w http.ResponseWriter, r *
}
// WriteJournalEntry -
func (c *DefaultApiController) WriteJournalEntry(w http.ResponseWriter, r *http.Request) {
func (c *JournalApiController) WriteJournalEntry(w http.ResponseWriter, r *http.Request) {
journalEntryParam := JournalEntry{}
d := json.NewDecoder(r.Body)
d.DisallowUnknownFields()

View File

@ -15,21 +15,21 @@ import (
"errors"
)
// 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.
// JournalApiService is a service that implements the logic for the JournalApiServicer
// This service should implement the business logic for every endpoint for the JournalApi API.
// Include any external packages or services that will be required by this service.
type DefaultApiService struct {
type JournalApiService struct {
}
// NewDefaultApiService creates a default api service
func NewDefaultApiService() DefaultApiServicer {
return &DefaultApiService{}
// NewJournalApiService creates a default api service
func NewJournalApiService() JournalApiServicer {
return &JournalApiService{}
}
// DeleteJournalEntryForDate -
func (s *DefaultApiService) DeleteJournalEntryForDate(ctx context.Context, date string) (ImplResponse, error) {
func (s *JournalApiService) DeleteJournalEntryForDate(ctx context.Context, date string) (ImplResponse, error) {
// TODO - update DeleteJournalEntryForDate 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.
// Add api_journal_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
@ -38,9 +38,9 @@ func (s *DefaultApiService) DeleteJournalEntryForDate(ctx context.Context, date
}
// GetJournalEntryForDate -
func (s *DefaultApiService) GetJournalEntryForDate(ctx context.Context, date string) (ImplResponse, error) {
func (s *JournalApiService) GetJournalEntryForDate(ctx context.Context, date string) (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.
// Add api_journal_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, JournalEntry{}) or use other options such as http.Ok ...
//return Response(200, JournalEntry{}), nil
@ -49,9 +49,9 @@ func (s *DefaultApiService) GetJournalEntryForDate(ctx context.Context, date str
}
// WriteJournalEntry -
func (s *DefaultApiService) WriteJournalEntry(ctx context.Context, journalEntry JournalEntry) (ImplResponse, error) {
func (s *JournalApiService) 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.
// Add api_journal_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

210
backend/dashapi/api_plan.go Normal file
View File

@ -0,0 +1,210 @@
/*
* 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)
}

View File

@ -0,0 +1,93 @@
/*
* 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 (
"context"
"net/http"
"errors"
)
// PlanApiService is a service that implements the logic for the PlanApiServicer
// This service should implement the business logic for every endpoint for the PlanApi API.
// Include any external packages or services that will be required by this service.
type PlanApiService struct {
}
// NewPlanApiService creates a default api service
func NewPlanApiService() PlanApiServicer {
return &PlanApiService{}
}
// GetPlanDayForDate -
func (s *PlanApiService) GetPlanDayForDate(ctx context.Context, date string) (ImplResponse, error) {
// TODO - update GetPlanDayForDate with the required logic for this service method.
// Add api_plan_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, PlanDay{}) or use other options such as http.Ok ...
//return Response(200, PlanDay{}), nil
return Response(http.StatusNotImplemented, nil), errors.New("GetPlanDayForDate method not implemented")
}
// GetPlanMonthForDate -
func (s *PlanApiService) GetPlanMonthForDate(ctx context.Context, date string) (ImplResponse, error) {
// TODO - update GetPlanMonthForDate with the required logic for this service method.
// Add api_plan_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, PlanMonth{}) or use other options such as http.Ok ...
//return Response(200, PlanMonth{}), nil
return Response(http.StatusNotImplemented, nil), errors.New("GetPlanMonthForDate method not implemented")
}
// GetPlanWeekForDate -
func (s *PlanApiService) GetPlanWeekForDate(ctx context.Context, date string) (ImplResponse, error) {
// TODO - update GetPlanWeekForDate with the required logic for this service method.
// Add api_plan_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, PlanWeek{}) or use other options such as http.Ok ...
//return Response(200, PlanWeek{}), nil
return Response(http.StatusNotImplemented, nil), errors.New("GetPlanWeekForDate method not implemented")
}
// SavePlanForDay -
func (s *PlanApiService) SavePlanForDay(ctx context.Context, planDay PlanDay) (ImplResponse, error) {
// TODO - update SavePlanForDay with the required logic for this service method.
// Add api_plan_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("SavePlanForDay method not implemented")
}
// SavePlanForMonth -
func (s *PlanApiService) SavePlanForMonth(ctx context.Context, planMonth PlanMonth) (ImplResponse, error) {
// TODO - update SavePlanForMonth with the required logic for this service method.
// Add api_plan_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("SavePlanForMonth method not implemented")
}
// SavePlanForWeek -
func (s *PlanApiService) SavePlanForWeek(ctx context.Context, planWeek PlanWeek) (ImplResponse, error) {
// TODO - update SavePlanForWeek with the required logic for this service method.
// Add api_plan_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("SavePlanForWeek method not implemented")
}

View File

@ -0,0 +1,53 @@
/*
* 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
type PlanDay struct {
Date string `json:"date"`
Morning []string `json:"morning,omitempty"`
Midday []string `json:"midday,omitempty"`
Afternoon []string `json:"afternoon,omitempty"`
Evening []string `json:"evening,omitempty"`
Pleasant []string `json:"pleasant,omitempty"`
Reminders []string `json:"reminders,omitempty"`
}
// AssertPlanDayRequired checks if the required fields are not zero-ed
func AssertPlanDayRequired(obj PlanDay) error {
elements := map[string]interface{}{
"date": obj.Date,
}
for name, el := range elements {
if isZero := IsZeroValue(el); isZero {
return &RequiredError{Field: name}
}
}
return nil
}
// AssertRecursePlanDayRequired recursively checks if required fields are not zero-ed in a nested slice.
// Accepts only nested slice of PlanDay (e.g. [][]PlanDay), otherwise ErrTypeAssertionError is thrown.
func AssertRecursePlanDayRequired(objSlice interface{}) error {
return AssertRecurseInterfaceRequired(objSlice, func(obj interface{}) error {
aPlanDay, ok := obj.(PlanDay)
if !ok {
return ErrTypeAssertionError
}
return AssertPlanDayRequired(aPlanDay)
})
}

View File

@ -0,0 +1,43 @@
/*
* 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
type PlanMonth struct {
Date string `json:"date"`
Items []string `json:"items,omitempty"`
}
// AssertPlanMonthRequired checks if the required fields are not zero-ed
func AssertPlanMonthRequired(obj PlanMonth) error {
elements := map[string]interface{}{
"date": obj.Date,
}
for name, el := range elements {
if isZero := IsZeroValue(el); isZero {
return &RequiredError{Field: name}
}
}
return nil
}
// AssertRecursePlanMonthRequired recursively checks if required fields are not zero-ed in a nested slice.
// Accepts only nested slice of PlanMonth (e.g. [][]PlanMonth), otherwise ErrTypeAssertionError is thrown.
func AssertRecursePlanMonthRequired(objSlice interface{}) error {
return AssertRecurseInterfaceRequired(objSlice, func(obj interface{}) error {
aPlanMonth, ok := obj.(PlanMonth)
if !ok {
return ErrTypeAssertionError
}
return AssertPlanMonthRequired(aPlanMonth)
})
}

View File

@ -0,0 +1,48 @@
/*
* 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
type PlanWeek struct {
Date string `json:"date"`
Items []PlanWeekItem `json:"items,omitempty"`
}
// AssertPlanWeekRequired checks if the required fields are not zero-ed
func AssertPlanWeekRequired(obj PlanWeek) error {
elements := map[string]interface{}{
"date": obj.Date,
}
for name, el := range elements {
if isZero := IsZeroValue(el); isZero {
return &RequiredError{Field: name}
}
}
for _, el := range obj.Items {
if err := AssertPlanWeekItemRequired(el); err != nil {
return err
}
}
return nil
}
// AssertRecursePlanWeekRequired recursively checks if required fields are not zero-ed in a nested slice.
// Accepts only nested slice of PlanWeek (e.g. [][]PlanWeek), otherwise ErrTypeAssertionError is thrown.
func AssertRecursePlanWeekRequired(objSlice interface{}) error {
return AssertRecurseInterfaceRequired(objSlice, func(obj interface{}) error {
aPlanWeek, ok := obj.(PlanWeek)
if !ok {
return ErrTypeAssertionError
}
return AssertPlanWeekRequired(aPlanWeek)
})
}

View File

@ -0,0 +1,45 @@
/*
* 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
type PlanWeekItem struct {
Item string `json:"item"`
NumTodos int32 `json:"numTodos,omitempty"`
NumDone int32 `json:"numDone,omitempty"`
}
// AssertPlanWeekItemRequired checks if the required fields are not zero-ed
func AssertPlanWeekItemRequired(obj PlanWeekItem) error {
elements := map[string]interface{}{
"item": obj.Item,
}
for name, el := range elements {
if isZero := IsZeroValue(el); isZero {
return &RequiredError{Field: name}
}
}
return nil
}
// AssertRecursePlanWeekItemRequired recursively checks if required fields are not zero-ed in a nested slice.
// Accepts only nested slice of PlanWeekItem (e.g. [][]PlanWeekItem), otherwise ErrTypeAssertionError is thrown.
func AssertRecursePlanWeekItemRequired(objSlice interface{}) error {
return AssertRecurseInterfaceRequired(objSlice, func(obj interface{}) error {
aPlanWeekItem, ok := obj.(PlanWeekItem)
if !ok {
return ErrTypeAssertionError
}
return AssertPlanWeekItemRequired(aPlanWeekItem)
})
}

View File

@ -1,11 +0,0 @@
package database
import (
"time"
// "github.com/moustachioed/dash/backend/database/models"
)
type Database interface {
WriteJournalEntry(interface{}) error
GetJournalEntryForDate(time.Time) (interface{}, error)
}

View File

@ -0,0 +1,25 @@
package models
import (
"gorm.io/datatypes"
)
type PlanDay struct {
Date datatypes.Date `gorm:"primaryKey"`
Morning datatypes.JSON
Midday datatypes.JSON
Afternoon datatypes.JSON
Evening datatypes.JSON
Pleasant datatypes.JSON
Reminders datatypes.JSON
}
type PlanWeek struct {
Date datatypes.Date `gorm:"primaryKey"`
Items datatypes.JSON
}
type PlanMonth struct {
Date datatypes.Date `gorm:"primaryKey"`
Items datatypes.JSON
}

View File

@ -11,13 +11,14 @@ import (
"gorm.io/gorm/clause"
"github.com/moustachioed/dash/backend/database/models"
"github.com/moustachioed/dash/backend/service"
)
type PgDatabase struct {
Db *gorm.DB
}
func NewPgDatabase(host string, user string, password string, database string, port uint16) (Database, error) {
func NewPgDatabase(host string, user string, password string, database string, port uint16) (service.DataStore, error) {
db := &PgDatabase{}
dsn := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%d sslmode=disable", host, user, password, database, port)
@ -38,6 +39,9 @@ func NewPgDatabase(host string, user string, password string, database string, p
func (db *PgDatabase) migrate() {
db.Db.AutoMigrate(&models.Journal{})
db.Db.AutoMigrate(&models.PlanDay{})
db.Db.AutoMigrate(&models.PlanWeek{})
db.Db.AutoMigrate(&models.PlanMonth{})
}
func (db *PgDatabase) WriteJournalEntry(entry interface{}) error {
@ -57,3 +61,57 @@ func (db *PgDatabase) GetJournalEntryForDate(date time.Time) (interface{}, error
return entry, nil
}
func (db *PgDatabase) WritePlanDay(entry interface{}) error {
planDay := entry.(models.PlanDay)
err := db.Db.Clauses(clause.OnConflict{UpdateAll: true}).Create(&planDay).Error
if err != nil {
log.Print("Error writing plan day to database.")
return err
}
return nil
}
func (db *PgDatabase) GetPlanDayForDate(date time.Time) (interface{}, error) {
entry := models.PlanDay{Date: datatypes.Date(date)}
db.Db.First(&entry)
return entry, nil
}
func (db *PgDatabase) WritePlanWeek(entry interface{}) error {
planWeek := entry.(models.PlanWeek)
err := db.Db.Clauses(clause.OnConflict{UpdateAll: true}).Create(&planWeek).Error
if err != nil {
log.Print("Error writing plan week to database.")
return err
}
return nil
}
func (db *PgDatabase) GetPlanWeekForDate(date time.Time) (interface{}, error) {
entry := models.PlanWeek{Date: datatypes.Date(date)}
db.Db.First(&entry)
return entry, nil
}
func (db *PgDatabase) WritePlanMonth(entry interface{}) error {
planMonth := entry.(models.PlanMonth)
err := db.Db.Clauses(clause.OnConflict{UpdateAll: true}).Create(&planMonth).Error
if err != nil {
log.Print("Error writing plan month to database.")
return err
}
return nil
}
func (db *PgDatabase) GetPlanMonthForDate(date time.Time) (interface{}, error) {
entry := models.PlanMonth{Date: datatypes.Date(date)}
db.Db.First(&entry)
return entry, nil
}

View File

@ -2,13 +2,20 @@ package mapping
import (
"time"
// api "github.com/moustachioed/dash/backend/dashapi"
// db "github.com/moustachioed/dash/backend/database/models"
)
type Mapper interface {
JournalApiToDb(api interface{}) (db interface{})
JournalDbToApi(db interface{}) (api interface{})
JournalApiToDs(api interface{}) (db interface{})
JournalDsToApi(db interface{}) (api interface{})
PlanDayApiToDs(api interface{}) (db interface{})
PlanDayDsToApi(db interface{}) (api interface{})
PlanWeekApiToDs(api interface{}) (db interface{})
PlanWeekDsToApi(db interface{}) (api interface{})
PlanMonthApiToDs(api interface{}) (db interface{})
PlanMonthDsToApi(db interface{}) (api interface{})
StringToDate(string) (time.Time, error)
DateToString(time.Time) string

View File

@ -17,9 +17,9 @@ func NewMapperImpl() Mapper {
return MapperImpl{}
}
func (mapper MapperImpl) JournalApiToDb(am interface{}) interface{} {
func (mapper MapperImpl) JournalApiToDs(am interface{}) interface{} {
apimodel := am.(api.JournalEntry)
date, err := time.Parse("2006-01-02", apimodel.Date)
date, err := mapper.StringToDate(apimodel.Date)
if err != nil {
log.Printf("[ERROR] Could not parse date `%s`", apimodel.Date)
}
@ -54,7 +54,7 @@ func (mapper MapperImpl) JournalApiToDb(am interface{}) interface{} {
}
}
func (mapper MapperImpl) JournalDbToApi(dm interface{}) interface{} {
func (mapper MapperImpl) JournalDsToApi(dm interface{}) interface{} {
dbmodel := dm.(db.Journal)
dateValue, err := dbmodel.Date.Value()
@ -62,7 +62,8 @@ func (mapper MapperImpl) JournalDbToApi(dm interface{}) interface{} {
if err != nil {
date = ""
} else {
date = dateValue.(time.Time).Format("2006-01-02")
// date = dateValue.(time.Time).Format("2006-01-02")
date = mapper.DateToString(dateValue.(time.Time))
}
var thankful []string
@ -74,19 +75,19 @@ func (mapper MapperImpl) JournalDbToApi(dm interface{}) interface{} {
var lookingForward []string
err = json.Unmarshal(dbmodel.LookingForward, &lookingForward)
if err != nil {
thankful = nil
lookingForward = nil
}
var beenGreat []string
err = json.Unmarshal(dbmodel.BeenGreat, &beenGreat)
if err != nil {
thankful = nil
beenGreat = nil
}
var doBetter []string
err = json.Unmarshal(dbmodel.DoBetter, &doBetter)
if err != nil {
thankful = nil
doBetter = nil
}
return api.JournalEntry{
@ -99,11 +100,133 @@ func (mapper MapperImpl) JournalDbToApi(dm interface{}) interface{} {
}
}
func (mapper MapperImpl) PlanDayApiToDs(am interface{}) interface{} {
apimodel := am.(api.PlanDay)
date, err := mapper.StringToDate(apimodel.Date)
if err != nil {
log.Printf("[ERROR] Could not parse date `%s`", apimodel.Date)
}
morning, err := json.Marshal(apimodel.Morning)
if err != nil {
morning = nil
}
midday, err := json.Marshal(apimodel.Midday)
if err != nil {
midday = nil
}
afternoon, err := json.Marshal(apimodel.Afternoon)
if err != nil {
afternoon = nil
}
evening, err := json.Marshal(apimodel.Evening)
if err != nil {
evening = nil
}
pleasant, err := json.Marshal(apimodel.Pleasant)
if err != nil {
pleasant = nil
}
reminders, err := json.Marshal(apimodel.Reminders)
if err != nil {
reminders = nil
}
return db.PlanDay{
Date: datatypes.Date(date),
Morning: datatypes.JSON(morning),
Midday: datatypes.JSON(midday),
Afternoon: datatypes.JSON(afternoon),
Evening: datatypes.JSON(evening),
Pleasant: datatypes.JSON(pleasant),
Reminders: datatypes.JSON(reminders),
}
}
func (mapper MapperImpl) PlanDayDsToApi(dm interface{}) interface{} {
dbmodel := dm.(db.PlanDay)
dateValue, err := dbmodel.Date.Value()
var date string
if err != nil {
date = ""
} else {
date = mapper.DateToString(dateValue.(time.Time))
}
var morning []string
err = json.Unmarshal(dbmodel.Morning, &morning)
if err != nil {
morning = nil
}
var midday []string
err = json.Unmarshal(dbmodel.Midday, &midday)
if err != nil {
midday = nil
}
var afternoon []string
err = json.Unmarshal(dbmodel.Afternoon, &afternoon)
if err != nil {
afternoon = nil
}
var evening []string
err = json.Unmarshal(dbmodel.Evening, &evening)
if err != nil {
evening = nil
}
var pleasant []string
err = json.Unmarshal(dbmodel.Pleasant, &pleasant)
if err != nil {
pleasant = nil
}
var reminders []string
err = json.Unmarshal(dbmodel.Reminders, &reminders)
if err != nil {
reminders = nil
}
return api.PlanDay{
Date: date,
Morning: morning,
Midday: midday,
Afternoon: afternoon,
Evening: evening,
Pleasant: pleasant,
Reminders: reminders,
}
}
func (mapper MapperImpl) PlanWeekApiToDs(api interface{}) (db interface{}) {
return new(interface{})
}
func (mapper MapperImpl) PlanWeekDsToApi(api interface{}) (db interface{}) {
return new(interface{})
}
func (mapper MapperImpl) PlanMonthApiToDs(api interface{}) (db interface{}) {
return new(interface{})
}
func (mapper MapperImpl) PlanMonthDsToApi(api interface{}) (db interface{}) {
return new(interface{})
}
func (mapper MapperImpl) StringToDate(dateString string) (time.Time, error) {
date, err := time.Parse("2006-01-02", dateString)
if err != nil {
log.Printf("[ERROR] Could not parse date string %s", dateString)
return time.Now(), err
return time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC), err
}
return date, nil
}

View File

@ -2,6 +2,7 @@ package mapping
import (
"encoding/json"
"reflect"
"testing"
"time"
@ -22,7 +23,7 @@ func TestJournalApiToDbFullObject(t *testing.T) {
Journal: "jtest",
}
db := mapper.JournalApiToDb(api).(models.Journal)
db := mapper.JournalApiToDs(api).(models.Journal)
gotDate, _ := db.Date.Value()
got := gotDate.(time.Time).Format("2006-01-02")
@ -80,7 +81,7 @@ func TestJournalApiToDbPartialObject(t *testing.T) {
Journal: "",
}
db := mapper.JournalApiToDb(api).(models.Journal)
db := mapper.JournalApiToDs(api).(models.Journal)
gotDate, _ := db.Date.Value()
got := gotDate.(time.Time).Format("2006-01-02")
@ -145,7 +146,7 @@ func TestJournalDbToApiFullObject(t *testing.T) {
Journal: journal,
}
api := mapper.JournalDbToApi(db).(dashapi.JournalEntry)
api := mapper.JournalDsToApi(db).(dashapi.JournalEntry)
got, _ := json.Marshal(api.Date)
wantDate := "\"2022-02-18\""
@ -209,7 +210,7 @@ func TestJournalDbToApiPartialObject(t *testing.T) {
Journal: journal,
}
api := mapper.JournalDbToApi(db).(dashapi.JournalEntry)
api := mapper.JournalDsToApi(db).(dashapi.JournalEntry)
got, _ := json.Marshal(api.Date)
wantDate := "\"2022-02-18\""
@ -253,3 +254,229 @@ func TestJournalDbToApiPartialObject(t *testing.T) {
t.Errorf("Mapped string %s not equal want string %s", gotJournal, wantJournal)
}
}
func TestMapperImpl_PlanDayApiToDs(t *testing.T) {
date, _ := time.Parse("2006-01-02", "2022-02-18")
morning, _ := json.Marshal([]string{"motest1", "motest2", "motest3"})
midday, _ := json.Marshal([]string{"mitest1", "mitest2", "mitest3"})
afternoon, _ := json.Marshal([]string{"antest1", "antest2", "antest3"})
evening, _ := json.Marshal([]string{"etest1", "etest2", "etest3"})
pleasant, _ := json.Marshal([]string{"ptest1", "ptest2", "ptest3"})
reminders, _ := json.Marshal([]string{"rtest1", "rtest2", "rtest3"})
empty, _ := json.Marshal(nil)
type args struct {
am interface{}
}
tests := []struct {
name string
mapper MapperImpl
args args
want interface{}
}{
{
name: "Full Object",
mapper: MapperImpl{},
args: args{
am: dashapi.PlanDay{
Date: "2022-02-18",
Morning: []string{"motest1", "motest2", "motest3"},
Midday: []string{"mitest1", "mitest2", "mitest3"},
Afternoon: []string{"antest1", "antest2", "antest3"},
Evening: []string{"etest1", "etest2", "etest3"},
Pleasant: []string{"ptest1", "ptest2", "ptest3"},
Reminders: []string{"rtest1", "rtest2", "rtest3"},
}},
want: models.PlanDay{
Date: datatypes.Date(date),
Morning: datatypes.JSON(morning),
Midday: datatypes.JSON(midday),
Afternoon: datatypes.JSON(afternoon),
Evening: datatypes.JSON(evening),
Pleasant: datatypes.JSON(pleasant),
Reminders: datatypes.JSON(reminders),
},
},
{
name: "Partial Object",
mapper: MapperImpl{},
args: args{
am: dashapi.PlanDay{
Date: "2022-02-18",
Morning: []string{"motest1", "motest2", "motest3"},
Midday: nil,
Afternoon: nil,
Evening: nil,
Pleasant: []string{"ptest1", "ptest2", "ptest3"},
Reminders: nil,
}},
want: models.PlanDay{
Date: datatypes.Date(date),
Morning: datatypes.JSON(morning),
Midday: datatypes.JSON(empty),
Afternoon: datatypes.JSON(empty),
Evening: datatypes.JSON(empty),
Pleasant: datatypes.JSON(pleasant),
Reminders: datatypes.JSON(empty),
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mapper := MapperImpl{}
if got := mapper.PlanDayApiToDs(tt.args.am); !reflect.DeepEqual(got, tt.want) {
t.Errorf("MapperImpl.PlanDayApiToDs() = %v, want %v", got, tt.want)
}
})
}
}
func TestMapperImpl_PlanDayDsToApi(t *testing.T) {
date, _ := time.Parse("2006-01-02", "2022-02-18")
morning, _ := json.Marshal([]string{"motest1", "motest2", "motest3"})
midday, _ := json.Marshal([]string{"mitest1", "mitest2", "mitest3"})
afternoon, _ := json.Marshal([]string{"antest1", "antest2", "antest3"})
evening, _ := json.Marshal([]string{"etest1", "etest2", "etest3"})
pleasant, _ := json.Marshal([]string{"ptest1", "ptest2", "ptest3"})
reminders, _ := json.Marshal([]string{"rtest1", "rtest2", "rtest3"})
empty, _ := json.Marshal(nil)
type args struct {
dm interface{}
}
tests := []struct {
name string
mapper MapperImpl
args args
want interface{}
}{
{
name: "Full Object",
mapper: MapperImpl{},
args: args{
dm: models.PlanDay{
Date: datatypes.Date(date),
Morning: datatypes.JSON(morning),
Midday: datatypes.JSON(midday),
Afternoon: datatypes.JSON(afternoon),
Evening: datatypes.JSON(evening),
Pleasant: datatypes.JSON(pleasant),
Reminders: datatypes.JSON(reminders),
}},
want: dashapi.PlanDay{
Date: "2022-02-18",
Morning: []string{"motest1", "motest2", "motest3"},
Midday: []string{"mitest1", "mitest2", "mitest3"},
Afternoon: []string{"antest1", "antest2", "antest3"},
Evening: []string{"etest1", "etest2", "etest3"},
Pleasant: []string{"ptest1", "ptest2", "ptest3"},
Reminders: []string{"rtest1", "rtest2", "rtest3"},
},
},
{
name: "Partial Object",
mapper: MapperImpl{},
args: args{
dm: models.PlanDay{
Date: datatypes.Date(date),
Morning: datatypes.JSON(morning),
Midday: datatypes.JSON(empty),
Afternoon: datatypes.JSON(afternoon),
Evening: datatypes.JSON(empty),
Pleasant: datatypes.JSON(pleasant),
Reminders: datatypes.JSON(empty),
}},
want: dashapi.PlanDay{
Date: "2022-02-18",
Morning: []string{"motest1", "motest2", "motest3"},
Midday: nil,
Afternoon: []string{"antest1", "antest2", "antest3"},
Evening: nil,
Pleasant: []string{"ptest1", "ptest2", "ptest3"},
Reminders: nil,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mapper := MapperImpl{}
if got := mapper.PlanDayDsToApi(tt.args.dm); !reflect.DeepEqual(got, tt.want) {
t.Errorf("MapperImpl.PlanDayDsToApi() = %v, want %v", got, tt.want)
}
})
}
}
func TestMapperImpl_StringToDate(t *testing.T) {
type args struct {
dateString string
}
tests := []struct {
name string
mapper MapperImpl
args args
want time.Time
wantErr bool
}{
{
name: "Well-formed date",
mapper: MapperImpl{},
args: args{
dateString: "1987-02-18",
},
want: time.Date(1987, 2, 18, 0, 0, 0, 0, time.UTC),
wantErr: false,
},
{
name: "False string",
mapper: MapperImpl{},
args: args{
dateString: "1987-02-18'T'13:02:18",
},
want: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC),
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mapper := MapperImpl{}
got, err := mapper.StringToDate(tt.args.dateString)
if (err != nil) != tt.wantErr {
t.Errorf("MapperImpl.StringToDate() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("MapperImpl.StringToDate() = %v, want %v", got, tt.want)
}
})
}
}
func TestMapperImpl_DateToString(t *testing.T) {
type args struct {
date time.Time
}
tests := []struct {
name string
mapper MapperImpl
args args
want string
}{
{
name: "Well-formed date",
mapper: MapperImpl{},
args: args{
date: time.Date(1987, 2, 18, 0, 0, 0, 0, time.UTC),
},
want: "1987-02-18",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mapper := MapperImpl{}
if got := mapper.DateToString(tt.args.date); got != tt.want {
t.Errorf("MapperImpl.DateToString() = %v, want %v", got, tt.want)
}
})
}
}

View File

@ -0,0 +1,67 @@
/*
* 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"
"github.com/moustachioed/dash/backend/mapping"
)
// JournalApiService is a service that implements the logic for the JournalApiServicer
type JournalApiService struct {
ds DataStore
mapper mapping.Mapper
}
// NewJournalApiService creates a default api service
func NewJournalApiService(ds DataStore, mapper mapping.Mapper) dashapi.JournalApiServicer {
service := JournalApiService{
ds: ds,
mapper: mapper,
}
return &service
}
// DeleteJournalEntryForDate -
func (s *JournalApiService) DeleteJournalEntryForDate(ctx context.Context, date string) (dashapi.ImplResponse, error) {
// TODO - update DeleteJournalEntryForDate with the required logic for this service method.
// Add api_journal_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 dashapi.Response(http.StatusNotImplemented, nil), errors.New("DeleteJournalEntryForDate method not implemented")
}
// GetJournalEntryForDate -
func (s *JournalApiService) GetJournalEntryForDate(ctx context.Context, date string) (dashapi.ImplResponse, error) {
d, err := s.mapper.StringToDate(date)
if err != nil {
log.Fatal(err)
}
dbEntry, _ := s.ds.GetJournalEntryForDate(d)
journal := s.mapper.JournalDsToApi(dbEntry)
return dashapi.Response(200, journal), nil
}
// WriteJournalEntry -
func (s *JournalApiService) WriteJournalEntry(ctx context.Context, journalEntry dashapi.JournalEntry) (dashapi.ImplResponse, error) {
journal := s.mapper.JournalApiToDs(journalEntry)
s.ds.WriteJournalEntry(journal)
return dashapi.Response(200, nil), nil
}

View File

@ -0,0 +1,102 @@
/*
* 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"
"github.com/moustachioed/dash/backend/mapping"
)
// PlanApiService is a service that implements the logic for the PlanApiServicer
// This service should implement the business logic for every endpoint for the PlanApi API.
// Include any external packages or services that will be required by this service.
type PlanApiService struct {
ds DataStore
mapper mapping.Mapper
}
// NewPlanApiService creates a default api service
func NewPlanApiService(ds DataStore, mapper mapping.Mapper) dashapi.PlanApiServicer {
service := PlanApiService{
ds: ds,
mapper: mapper,
}
return &service
}
// GetPlanDayForDate -
func (s *PlanApiService) GetPlanDayForDate(ctx context.Context, date string) (dashapi.ImplResponse, error) {
d, err := s.mapper.StringToDate(date)
if err != nil {
log.Fatal(err)
}
dbEntry, _ := s.ds.GetPlanDayForDate(d)
planDay := s.mapper.PlanDayDsToApi(dbEntry)
return dashapi.Response(200, planDay), nil
}
// GetPlanMonthForDate -
func (s *PlanApiService) GetPlanMonthForDate(ctx context.Context, date string) (dashapi.ImplResponse, error) {
// TODO - update GetPlanMonthForDate with the required logic for this service method.
// Add api_plan_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, PlanMonth{}) or use other options such as http.Ok ...
//return Response(200, PlanMonth{}), nil
return dashapi.Response(http.StatusNotImplemented, nil), errors.New("GetPlanMonthForDate method not implemented")
}
// GetPlanWeekForDate -
func (s *PlanApiService) GetPlanWeekForDate(ctx context.Context, date string) (dashapi.ImplResponse, error) {
// TODO - update GetPlanWeekForDate with the required logic for this service method.
// Add api_plan_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, PlanWeek{}) or use other options such as http.Ok ...
//return Response(200, PlanWeek{}), nil
return dashapi.Response(http.StatusNotImplemented, nil), errors.New("GetPlanWeekForDate method not implemented")
}
// SavePlanForDay -
func (s *PlanApiService) SavePlanForDay(ctx context.Context, planDay dashapi.PlanDay) (dashapi.ImplResponse, error) {
plan := s.mapper.PlanDayApiToDs(planDay)
s.ds.WritePlanDay(plan)
return dashapi.Response(200, nil), nil
}
// SavePlanForMonth -
func (s *PlanApiService) SavePlanForMonth(ctx context.Context, planMonth dashapi.PlanMonth) (dashapi.ImplResponse, error) {
// TODO - update SavePlanForMonth with the required logic for this service method.
// Add api_plan_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 dashapi.Response(http.StatusNotImplemented, nil), errors.New("SavePlanForMonth method not implemented")
}
// SavePlanForWeek -
func (s *PlanApiService) SavePlanForWeek(ctx context.Context, planWeek dashapi.PlanWeek) (dashapi.ImplResponse, error) {
// TODO - update SavePlanForWeek with the required logic for this service method.
// Add api_plan_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 dashapi.Response(http.StatusNotImplemented, nil), errors.New("SavePlanForWeek method not implemented")
}

View File

@ -1,54 +0,0 @@
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")
}

View File

@ -0,0 +1,19 @@
package service
import (
"time"
)
type DataStore interface {
WriteJournalEntry(interface{}) error
GetJournalEntryForDate(time.Time) (interface{}, error)
WritePlanDay(interface{}) error
GetPlanDayForDate(time.Time) (interface{}, error)
WritePlanWeek(interface{}) error
GetPlanWeekForDate(time.Time) (interface{}, error)
WritePlanMonth(interface{}) error
GetPlanMonthForDate(time.Time) (interface{}, error)
}