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

@ -7,6 +7,6 @@ import (
)
type Database interface {
WriteJournalEntry(dbmodel models.Journal) error
GetJournalEntryForDate(date time.Time) (models.Journal, error)
WriteJournalEntry(models.Journal) error
GetJournalEntryForDate(time.Time) (models.Journal, error)
}

View File

@ -5,8 +5,10 @@ import (
"log"
"time"
"gorm.io/datatypes"
"gorm.io/driver/postgres"
"gorm.io/gorm"
"gorm.io/gorm/clause"
"github.com/moustachioed/dash/backend/database/models"
)
@ -38,10 +40,18 @@ func (db *PgDatabase) migrate() {
db.Db.AutoMigrate(&models.Journal{})
}
func (db *PgDatabase) WriteJournalEntry(dbmodel models.Journal) error {
func (db *PgDatabase) WriteJournalEntry(entry models.Journal) error {
err := db.Db.Clauses(clause.OnConflict{UpdateAll: true}).Create(&entry).Error
if err != nil {
log.Print("Error writing journal entry to database.")
return err
}
return nil
}
func (db *PgDatabase) GetJournalEntryForDate(date time.Time) (models.Journal, error) {
return models.Journal{}, nil
entry := models.Journal{Date: datatypes.Date(date)}
db.Db.First(&entry)
return entry, nil
}