This commit is contained in:
Martin Pander
2023-12-09 19:34:45 +01:00
parent 32346e0aa9
commit 8addda35ea
144 changed files with 7247 additions and 3268 deletions

View File

@ -17,6 +17,13 @@ type Mapper interface {
PlanMonthApiToDs(api interface{}) (db interface{})
PlanMonthDsToApi(db interface{}) (api interface{})
TrackingCategoriesDbToApi(db interface{}) (api interface{})
TrackingEntryDbToApi(db interface{}) (api interface{})
TrackingEntryApiToDb(api interface{}) (db interface{})
InboxApiToDs(api interface{}) (db interface{})
InboxDsToApi(db interface{}) (api interface{})
StringToDate(string) (time.Time, error)
DateToString(time.Time) string
}

View File

@ -206,20 +206,172 @@ func (mapper MapperImpl) PlanDayDsToApi(dm interface{}) interface{} {
}
}
func (mapper MapperImpl) PlanWeekApiToDs(api interface{}) (db interface{}) {
return new(interface{})
func (mapper MapperImpl) PlanWeekApiToDs(am interface{}) (dm interface{}) {
apimodel := am.(api.PlanWeek)
date, err := mapper.StringToDate(apimodel.Date)
if err != nil {
log.Printf("[ERROR] Could not parse date `%s`", apimodel.Date)
}
items, err := json.Marshal(apimodel.Items)
if err != nil {
items = nil
}
return db.PlanWeek{
Date: datatypes.Date(date),
Items: items,
}
}
func (mapper MapperImpl) PlanWeekDsToApi(api interface{}) (db interface{}) {
return new(interface{})
func (mapper MapperImpl) PlanWeekDsToApi(dm interface{}) (am interface{}) {
dbmodel := dm.(db.PlanWeek)
dateValue, err := dbmodel.Date.Value()
var date string
if err != nil {
date = ""
} else {
date = mapper.DateToString(dateValue.(time.Time))
}
var items []api.PlanWeekItem
err = json.Unmarshal(dbmodel.Items, &items)
if err != nil {
items = nil
}
return api.PlanWeek{
Date: date,
Items: items,
}
}
func (mapper MapperImpl) PlanMonthApiToDs(api interface{}) (db interface{}) {
return new(interface{})
// write a function that takes a month and returns a plan month
func (mapper MapperImpl) PlanMonthApiToDs(am interface{}) (dm interface{}) {
apimodel := am.(api.PlanMonth)
date, err := mapper.StringToDate(apimodel.Date)
if err != nil {
log.Printf("[ERROR] Could not parse date `%s`", apimodel.Date)
}
items, err := json.Marshal(apimodel.Items)
if err != nil {
items = nil
}
return db.PlanMonth{
Date: datatypes.Date(date),
Items: items,
}
}
func (mapper MapperImpl) PlanMonthDsToApi(api interface{}) (db interface{}) {
return new(interface{})
// write a function that takes a plan month and returns a month
func (mapper MapperImpl) PlanMonthDsToApi(dm interface{}) (am interface{}) {
dbmodel := dm.(db.PlanMonth)
dateValue, err := dbmodel.Date.Value()
var date string
if err != nil {
date = ""
} else {
date = mapper.DateToString(dateValue.(time.Time))
}
var items []string
err = json.Unmarshal(dbmodel.Items, &items)
if err != nil {
items = nil
}
return api.PlanMonth{
Date: date,
Items: items,
}
}
func (mapper MapperImpl) TrackingCategoriesDbToApi(dm interface{}) (am interface{}) {
dbmodel := dm.([]db.TrackingCategory)
var categories []api.TrackingCategory
for _, category := range dbmodel {
var items []string
err := json.Unmarshal(category.Items, &items)
if err != nil {
items = nil
}
categories = append(categories, api.TrackingCategory{
Name: category.Name,
Type: category.Type,
Items: items,
})
}
return api.TrackingCategories{
Categories: categories,
}
}
func (mapper MapperImpl) TrackingEntryDbToApi(dm interface{}) (am interface{}) {
dbmodel := dm.([]db.TrackingItem)
var items []api.TrackingItem
for _, item := range dbmodel {
items = append(items, api.TrackingItem{
Date: mapper.DateToString(item.Date),
Type: item.Type,
Value: item.Value,
})
}
if len(items) == 0 {
return api.TrackingEntry{}
} else {
return api.TrackingEntry{
Date: mapper.DateToString(dbmodel[0].Date),
Items: items,
}
}
}
func (mapper MapperImpl) TrackingEntryApiToDb(am interface{}) (dm interface{}) {
apimodel := am.(api.TrackingEntry)
date, err := mapper.StringToDate(apimodel.Date)
if err != nil {
log.Printf("[ERROR] Could not parse date `%s`", apimodel.Date)
}
var items []db.TrackingItem
for _, item := range apimodel.Items {
items = append(items, db.TrackingItem{
Date: date,
Type: item.Type,
Value: item.Value,
})
}
return items
}
func (mapper MapperImpl) InboxApiToDs(am interface{}) (dm interface{}) {
apimodel := am.(api.InboxItem)
return db.Inbox{
Id: apimodel.Id,
Item: apimodel.Item,
}
}
func (mapper MapperImpl) InboxDsToApi(dm interface{}) (am interface{}) {
dbmodel := dm.(db.Inbox)
return api.InboxItem{
Id: dbmodel.Id,
Item: dbmodel.Item,
}
}
func (mapper MapperImpl) StringToDate(dateString string) (time.Time, error) {

View File

@ -407,6 +407,150 @@ func TestMapperImpl_PlanDayDsToApi(t *testing.T) {
}
}
func TestMapperImpl_PlanWeekApiToDs(t *testing.T) {
date, _ := time.Parse("2006-01-02", "2022-02-18")
items, _ := json.Marshal([]dashapi.PlanWeekItem{
{
Item: "test1",
NumTodo: 3,
NumDone: 1,
},
{
Item: "test2",
NumTodo: 3,
NumDone: 3,
}})
empty, _ := json.Marshal(nil)
type args struct {
am interface{}
}
tests := []struct {
name string
mapper MapperImpl
args args
wantDm interface{}
}{
{
name: "Full Object",
mapper: MapperImpl{},
args: args{
am: dashapi.PlanWeek{
Date: "2022-02-18",
Items: []dashapi.PlanWeekItem{
{
Item: "test1",
NumTodo: 3,
NumDone: 1,
},
{
Item: "test2",
NumTodo: 3,
NumDone: 3,
},
}}},
wantDm: models.PlanWeek{
Date: datatypes.Date(date),
Items: datatypes.JSON(items),
},
},
{
name: "Empty Object",
mapper: MapperImpl{},
args: args{
am: dashapi.PlanWeek{
Date: "2022-02-18",
Items: nil,
}},
wantDm: models.PlanWeek{
Date: datatypes.Date(date),
Items: datatypes.JSON(empty),
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mapper := MapperImpl{}
if gotDm := mapper.PlanWeekApiToDs(tt.args.am); !reflect.DeepEqual(gotDm, tt.wantDm) {
t.Errorf("MapperImpl.PlanWeekApiToDs() = %v, want %v", gotDm, tt.wantDm)
}
})
}
}
// write a test for PlanMonthApiToDs
func TestMapperImpl_PlanWeekDsToApi(t *testing.T) {
date, _ := time.Parse("2006-01-02", "2022-02-18")
items, _ := json.Marshal([]dashapi.PlanWeekItem{
{
Item: "test1",
NumTodo: 3,
NumDone: 1,
},
{
Item: "test2",
NumTodo: 3,
NumDone: 3,
}})
empty, _ := json.Marshal(nil)
type args struct {
dm interface{}
}
tests := []struct {
name string
mapper MapperImpl
args args
wantAm interface{}
}{
{
name: "Full Object",
mapper: MapperImpl{},
args: args{
dm: models.PlanWeek{
Date: datatypes.Date(date),
Items: datatypes.JSON(items),
}},
wantAm: dashapi.PlanWeek{
Date: "2022-02-18",
Items: []dashapi.PlanWeekItem{
{
Item: "test1",
NumTodo: 3,
NumDone: 1,
},
{
Item: "test2",
NumTodo: 3,
NumDone: 3,
}},
},
},
{
name: "Empty Object",
mapper: MapperImpl{},
args: args{
dm: models.PlanWeek{
Date: datatypes.Date(date),
Items: datatypes.JSON(empty),
}},
wantAm: dashapi.PlanWeek{
Date: "2022-02-18",
Items: nil,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mapper := MapperImpl{}
if gotAm := mapper.PlanWeekDsToApi(tt.args.dm); !reflect.DeepEqual(gotAm, tt.wantAm) {
t.Errorf("MapperImpl.PlanWeekDsToApi() = %v, want %v", gotAm, tt.wantAm)
}
})
}
}
func TestMapperImpl_StringToDate(t *testing.T) {
type args struct {
dateString string
@ -480,3 +624,25 @@ func TestMapperImpl_DateToString(t *testing.T) {
})
}
}
func TestMapperImpl_InboxApiToDs(t *testing.T) {
type args struct {
am interface{}
}
tests := []struct {
name string
mapper MapperImpl
args args
wantDm interface{}
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mapper := MapperImpl{}
if gotDm := mapper.InboxApiToDs(tt.args.am); !reflect.DeepEqual(gotDm, tt.wantDm) {
t.Errorf("MapperImpl.InboxApiToDs() = %v, want %v", gotDm, tt.wantDm)
}
})
}
}