Make interfaces abstract
This commit is contained in:
@ -2,11 +2,10 @@ package database
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"time"
|
"time"
|
||||||
|
// "github.com/moustachioed/dash/backend/database/models"
|
||||||
"github.com/moustachioed/dash/backend/database/models"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Database interface {
|
type Database interface {
|
||||||
WriteJournalEntry(models.Journal) error
|
WriteJournalEntry(interface{}) error
|
||||||
GetJournalEntryForDate(time.Time) (models.Journal, error)
|
GetJournalEntryForDate(time.Time) (interface{}, error)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -40,8 +40,9 @@ func (db *PgDatabase) migrate() {
|
|||||||
db.Db.AutoMigrate(&models.Journal{})
|
db.Db.AutoMigrate(&models.Journal{})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db *PgDatabase) WriteJournalEntry(entry models.Journal) error {
|
func (db *PgDatabase) WriteJournalEntry(entry interface{}) error {
|
||||||
err := db.Db.Clauses(clause.OnConflict{UpdateAll: true}).Create(&entry).Error
|
journal := entry.(models.Journal)
|
||||||
|
err := db.Db.Clauses(clause.OnConflict{UpdateAll: true}).Create(&journal).Error
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Print("Error writing journal entry to database.")
|
log.Print("Error writing journal entry to database.")
|
||||||
return err
|
return err
|
||||||
@ -50,8 +51,9 @@ func (db *PgDatabase) WriteJournalEntry(entry models.Journal) error {
|
|||||||
return nil
|
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)}
|
entry := models.Journal{Date: datatypes.Date(date)}
|
||||||
db.Db.First(&entry)
|
db.Db.First(&entry)
|
||||||
|
|
||||||
return entry, nil
|
return entry, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,14 +2,13 @@ package mapping
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"time"
|
"time"
|
||||||
|
// api "github.com/moustachioed/dash/backend/dashapi"
|
||||||
api "github.com/moustachioed/dash/backend/dashapi"
|
// db "github.com/moustachioed/dash/backend/database/models"
|
||||||
db "github.com/moustachioed/dash/backend/database/models"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Mapper interface {
|
type Mapper interface {
|
||||||
JournalApiToDb(api.JournalEntry) db.Journal
|
JournalApiToDb(api interface{}) (db interface{})
|
||||||
JournalDbToApi(db.Journal) api.JournalEntry
|
JournalDbToApi(db interface{}) (api interface{})
|
||||||
|
|
||||||
StringToDate(string) (time.Time, error)
|
StringToDate(string) (time.Time, error)
|
||||||
DateToString(time.Time) string
|
DateToString(time.Time) string
|
||||||
|
|||||||
@ -17,7 +17,8 @@ func NewMapperImpl() Mapper {
|
|||||||
return MapperImpl{}
|
return MapperImpl{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (mapper MapperImpl) JournalApiToDb(apimodel api.JournalEntry) db.Journal {
|
func (mapper MapperImpl) JournalApiToDb(am interface{}) interface{} {
|
||||||
|
apimodel := am.(api.JournalEntry)
|
||||||
date, err := time.Parse("2006-01-02", apimodel.Date)
|
date, err := time.Parse("2006-01-02", apimodel.Date)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("[ERROR] Could not parse date `%s`", apimodel.Date)
|
log.Printf("[ERROR] Could not parse date `%s`", apimodel.Date)
|
||||||
@ -53,7 +54,9 @@ func (mapper MapperImpl) JournalApiToDb(apimodel api.JournalEntry) db.Journal {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (mapper MapperImpl) JournalDbToApi(dbmodel db.Journal) api.JournalEntry {
|
func (mapper MapperImpl) JournalDbToApi(dm interface{}) interface{} {
|
||||||
|
dbmodel := dm.(db.Journal)
|
||||||
|
|
||||||
dateValue, err := dbmodel.Date.Value()
|
dateValue, err := dbmodel.Date.Value()
|
||||||
var date string
|
var date string
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@ -22,7 +22,7 @@ func TestJournalApiToDbFullObject(t *testing.T) {
|
|||||||
Journal: "jtest",
|
Journal: "jtest",
|
||||||
}
|
}
|
||||||
|
|
||||||
db := mapper.JournalApiToDb(api)
|
db := mapper.JournalApiToDb(api).(models.Journal)
|
||||||
|
|
||||||
gotDate, _ := db.Date.Value()
|
gotDate, _ := db.Date.Value()
|
||||||
got := gotDate.(time.Time).Format("2006-01-02")
|
got := gotDate.(time.Time).Format("2006-01-02")
|
||||||
@ -80,7 +80,7 @@ func TestJournalApiToDbPartialObject(t *testing.T) {
|
|||||||
Journal: "",
|
Journal: "",
|
||||||
}
|
}
|
||||||
|
|
||||||
db := mapper.JournalApiToDb(api)
|
db := mapper.JournalApiToDb(api).(models.Journal)
|
||||||
|
|
||||||
gotDate, _ := db.Date.Value()
|
gotDate, _ := db.Date.Value()
|
||||||
got := gotDate.(time.Time).Format("2006-01-02")
|
got := gotDate.(time.Time).Format("2006-01-02")
|
||||||
@ -145,7 +145,7 @@ func TestJournalDbToApiFullObject(t *testing.T) {
|
|||||||
Journal: journal,
|
Journal: journal,
|
||||||
}
|
}
|
||||||
|
|
||||||
api := mapper.JournalDbToApi(db)
|
api := mapper.JournalDbToApi(db).(dashapi.JournalEntry)
|
||||||
|
|
||||||
got, _ := json.Marshal(api.Date)
|
got, _ := json.Marshal(api.Date)
|
||||||
wantDate := "\"2022-02-18\""
|
wantDate := "\"2022-02-18\""
|
||||||
@ -209,7 +209,7 @@ func TestJournalDbToApiPartialObject(t *testing.T) {
|
|||||||
Journal: journal,
|
Journal: journal,
|
||||||
}
|
}
|
||||||
|
|
||||||
api := mapper.JournalDbToApi(db)
|
api := mapper.JournalDbToApi(db).(dashapi.JournalEntry)
|
||||||
|
|
||||||
got, _ := json.Marshal(api.Date)
|
got, _ := json.Marshal(api.Date)
|
||||||
wantDate := "\"2022-02-18\""
|
wantDate := "\"2022-02-18\""
|
||||||
|
|||||||
@ -1,11 +1,12 @@
|
|||||||
@startuml
|
@startuml "Backend Components"
|
||||||
|
|
||||||
package "Service" {
|
package "Service" {
|
||||||
component "API Service" as apiservice
|
component "API Service" as apiservice
|
||||||
}
|
}
|
||||||
|
|
||||||
package "Mapping" {
|
package "Mapping" {
|
||||||
interface "Mapper Interface" as mapperint
|
interface "Mapper Interface" as mapperint
|
||||||
component "Mapper" as mapper
|
component "Mapper Implementation" as mapperimp
|
||||||
}
|
}
|
||||||
|
|
||||||
package "DashAPI" {
|
package "DashAPI" {
|
||||||
@ -15,7 +16,7 @@ package "DashAPI" {
|
|||||||
package "Database" {
|
package "Database" {
|
||||||
interface "Database Interface" as dbint
|
interface "Database Interface" as dbint
|
||||||
component "Database Implementation" as dbimp
|
component "Database Implementation" as dbimp
|
||||||
component "Database Model" as dbmodel
|
component "Database Models" as dbmodels
|
||||||
}
|
}
|
||||||
|
|
||||||
apiservice --> apimodels
|
apiservice --> apimodels
|
||||||
@ -23,13 +24,11 @@ apiservice --> dbint
|
|||||||
apiservice --> mapperint
|
apiservice --> mapperint
|
||||||
|
|
||||||
dbimp .up.> dbint
|
dbimp .up.> dbint
|
||||||
dbimp --> dbmodel
|
dbimp --> dbmodels
|
||||||
|
|
||||||
|
|
||||||
mapper .up.> mapperint
|
|
||||||
mapper --> apimodels
|
|
||||||
mapper --> dbmodel
|
|
||||||
|
|
||||||
|
|
||||||
|
mapperimp .up.> mapperint
|
||||||
|
mapperimp --> apimodels
|
||||||
|
mapperimp --> dbmodels
|
||||||
|
|
||||||
@enduml
|
@enduml
|
||||||
Reference in New Issue
Block a user