diff --git a/model/criteria/criteria.go b/model/criteria/criteria.go index c59c392a..bcd72cb7 100644 --- a/model/criteria/criteria.go +++ b/model/criteria/criteria.go @@ -6,9 +6,8 @@ import ( "errors" "strings" - "github.com/navidrome/navidrome/log" - "github.com/Masterminds/squirrel" + "github.com/navidrome/navidrome/log" ) type Expression = squirrel.Sqlizer diff --git a/model/criteria/fields.go b/model/criteria/fields.go index 89187077..c3841ff2 100644 --- a/model/criteria/fields.go +++ b/model/criteria/fields.go @@ -1,9 +1,7 @@ package criteria import ( - "fmt" "strings" - "time" "github.com/navidrome/navidrome/log" ) @@ -62,11 +60,3 @@ func mapFields(expr map[string]interface{}) map[string]interface{} { } return m } - -type Time time.Time - -func (t Time) MarshalJSON() ([]byte, error) { - //do your serializing here - stamp := fmt.Sprintf("\"%s\"", time.Time(t).Format("2006-01-02")) - return []byte(stamp), nil -} diff --git a/model/criteria/json.go b/model/criteria/json.go index ec27fc4d..f1f1e201 100644 --- a/model/criteria/json.go +++ b/model/criteria/json.go @@ -4,6 +4,7 @@ import ( "encoding/json" "fmt" "strings" + "time" ) type unmarshalConjunctionType []Expression @@ -115,3 +116,10 @@ func marshalConjunction(name string, conj []Expression) ([]byte, error) { } return json.Marshal(aux) } + +type date time.Time + +func (t date) MarshalJSON() ([]byte, error) { + stamp := fmt.Sprintf(`"%s"`, time.Time(t).Format("2006-01-02")) + return []byte(stamp), nil +} diff --git a/model/criteria/operators_test.go b/model/criteria/operators_test.go index b61ce1b6..5b7bc042 100644 --- a/model/criteria/operators_test.go +++ b/model/criteria/operators_test.go @@ -10,8 +10,9 @@ import ( ) var _ = Describe("Operators", func() { - rangeStart := Time(time.Date(2021, 10, 01, 0, 0, 0, 0, time.Local)) - rangeEnd := Time(time.Date(2021, 11, 01, 0, 0, 0, 0, time.Local)) + rangeStart := date(time.Date(2021, 10, 01, 0, 0, 0, 0, time.Local)) + rangeEnd := date(time.Date(2021, 11, 01, 0, 0, 0, 0, time.Local)) + DescribeTable("ToSQL", func(op Expression, expectedSql string, expectedArgs ...any) { sql, args, err := op.ToSql() @@ -29,7 +30,7 @@ var _ = Describe("Operators", func() { Entry("startsWith", StartsWith{"title": "Low Rider"}, "media_file.title LIKE ?", "Low Rider%"), Entry("endsWith", EndsWith{"title": "Low Rider"}, "media_file.title LIKE ?", "%Low Rider"), Entry("inTheRange [number]", InTheRange{"year": []int{1980, 1990}}, "(media_file.year >= ? AND media_file.year <= ?)", 1980, 1990), - Entry("inTheRange [date]", InTheRange{"lastPlayed": []Time{rangeStart, rangeEnd}}, "(annotation.play_date >= ? AND annotation.play_date <= ?)", rangeStart, rangeEnd), + Entry("inTheRange [date]", InTheRange{"lastPlayed": []date{rangeStart, rangeEnd}}, "(annotation.play_date >= ? AND annotation.play_date <= ?)", rangeStart, rangeEnd), Entry("before", Before{"lastPlayed": rangeStart}, "annotation.play_date < ?", rangeStart), Entry("after", After{"lastPlayed": rangeStart}, "annotation.play_date > ?", rangeStart), // TODO These may be flaky @@ -37,7 +38,7 @@ var _ = Describe("Operators", func() { Entry("notInTheLast", NotInTheLast{"lastPlayed": 30}, "(annotation.play_date < ? OR annotation.play_date IS NULL)", startOfPeriod(30, time.Now())), ) - DescribeTable("JSON Conversion", + DescribeTable("JSON Marshaling", func(op Expression, jsonString string) { obj := And{op} newJs, err := json.Marshal(obj)