Make interfaces abstract

This commit is contained in:
Martin Pander
2022-11-08 09:22:14 +01:00
parent c3de3dd21f
commit 8a78711727
6 changed files with 29 additions and 27 deletions

View File

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

View File

@ -40,8 +40,9 @@ func (db *PgDatabase) migrate() {
db.Db.AutoMigrate(&models.Journal{})
}
func (db *PgDatabase) WriteJournalEntry(entry models.Journal) error {
err := db.Db.Clauses(clause.OnConflict{UpdateAll: true}).Create(&entry).Error
func (db *PgDatabase) WriteJournalEntry(entry interface{}) error {
journal := entry.(models.Journal)
err := db.Db.Clauses(clause.OnConflict{UpdateAll: true}).Create(&journal).Error
if err != nil {
log.Print("Error writing journal entry to database.")
return err
@ -50,8 +51,9 @@ func (db *PgDatabase) WriteJournalEntry(entry models.Journal) error {
return nil
}
func (db *PgDatabase) GetJournalEntryForDate(date time.Time) (models.Journal, error) {
func (db *PgDatabase) GetJournalEntryForDate(date time.Time) (interface{}, error) {
entry := models.Journal{Date: datatypes.Date(date)}
db.Db.First(&entry)
return entry, nil
}