Implement rough architecture

This commit is contained in:
Martin Pander
2022-11-07 15:33:08 +01:00
parent 0ff700b945
commit 43c204f5b5
24 changed files with 1402 additions and 0 deletions

View File

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

View File

@ -0,0 +1,14 @@
package models
import (
"gorm.io/datatypes"
)
type Journal struct {
Date datatypes.Date `gorm:"primaryKey"`
Thankful datatypes.JSON
LookingForward datatypes.JSON
BeenGreat datatypes.JSON
DoBetter datatypes.JSON
Journal string
}

View File

@ -0,0 +1,47 @@
package database
import (
"fmt"
"log"
"time"
"gorm.io/driver/postgres"
"gorm.io/gorm"
"github.com/moustachioed/dash/backend/database/models"
)
type PgDatabase struct {
Db *gorm.DB
}
func NewPgDatabase(host string, user string, password string, database string, port uint16) (Database, error) {
db := &PgDatabase{}
dsn := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%d sslmode=disable", host, user, password, database, port)
gdb, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
if err != nil {
log.Print(err.Error())
return nil, err
}
log.Printf("Connected to database.")
db.Db = gdb
db.migrate()
return db, nil
}
func (db *PgDatabase) migrate() {
db.Db.AutoMigrate(&models.Journal{})
}
func (db *PgDatabase) WriteJournalEntry(dbmodel models.Journal) error {
return nil
}
func (db *PgDatabase) GetJournalEntryForDate(date time.Time) (models.Journal, error) {
return models.Journal{}, nil
}