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,14 +2,13 @@ package mapping
import (
"time"
api "github.com/moustachioed/dash/backend/dashapi"
db "github.com/moustachioed/dash/backend/database/models"
// api "github.com/moustachioed/dash/backend/dashapi"
// db "github.com/moustachioed/dash/backend/database/models"
)
type Mapper interface {
JournalApiToDb(api.JournalEntry) db.Journal
JournalDbToApi(db.Journal) api.JournalEntry
JournalApiToDb(api interface{}) (db interface{})
JournalDbToApi(db interface{}) (api interface{})
StringToDate(string) (time.Time, error)
DateToString(time.Time) string

View File

@ -17,7 +17,8 @@ func NewMapperImpl() Mapper {
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)
if err != nil {
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()
var date string
if err != nil {

View File

@ -22,7 +22,7 @@ func TestJournalApiToDbFullObject(t *testing.T) {
Journal: "jtest",
}
db := mapper.JournalApiToDb(api)
db := mapper.JournalApiToDb(api).(models.Journal)
gotDate, _ := db.Date.Value()
got := gotDate.(time.Time).Format("2006-01-02")
@ -80,7 +80,7 @@ func TestJournalApiToDbPartialObject(t *testing.T) {
Journal: "",
}
db := mapper.JournalApiToDb(api)
db := mapper.JournalApiToDb(api).(models.Journal)
gotDate, _ := db.Date.Value()
got := gotDate.(time.Time).Format("2006-01-02")
@ -145,7 +145,7 @@ func TestJournalDbToApiFullObject(t *testing.T) {
Journal: journal,
}
api := mapper.JournalDbToApi(db)
api := mapper.JournalDbToApi(db).(dashapi.JournalEntry)
got, _ := json.Marshal(api.Date)
wantDate := "\"2022-02-18\""
@ -209,7 +209,7 @@ func TestJournalDbToApiPartialObject(t *testing.T) {
Journal: journal,
}
api := mapper.JournalDbToApi(db)
api := mapper.JournalDbToApi(db).(dashapi.JournalEntry)
got, _ := json.Marshal(api.Date)
wantDate := "\"2022-02-18\""