diff options
Diffstat (limited to 'internal')
| -rw-r--r-- | internal/api/handlers.go | 16 | ||||
| -rw-r--r-- | internal/api/handlers_more_test.go | 8 | ||||
| -rw-r--r-- | internal/api/handlers_test.go | 4 | ||||
| -rw-r--r-- | internal/repository/media.go | 4 | ||||
| -rw-r--r-- | internal/repository/repository.go | 5 | ||||
| -rw-r--r-- | internal/service/media.go | 2 |
6 files changed, 26 insertions, 13 deletions
diff --git a/internal/api/handlers.go b/internal/api/handlers.go index e964b92..46ab41e 100644 --- a/internal/api/handlers.go +++ b/internal/api/handlers.go @@ -352,10 +352,8 @@ func parseMediaListQuery(q url.Values) repository.MediaFilter { t := model.MediaType(v) filter.Type = &t } - if v := q.Get("favorites"); v != "" { - if uid, err := strconv.ParseInt(v, 10, 64); err == nil { - filter.Favorites = &uid - } + if v := q.Get("favorites"); v == "true" || v == "1" { + filter.Favorites = true } if v := q.Get("tags"); v != "" { filter.Tags = strings.Split(v, ",") @@ -370,6 +368,16 @@ func parseMediaListQuery(q url.Values) repository.MediaFilter { filter.MaxDuration = &f } } + if v := q.Get("filesize_min"); v != "" { + if n, err := strconv.ParseInt(v, 10, 64); err == nil { + filter.MinFileSize = &n + } + } + if v := q.Get("filesize_max"); v != "" { + if n, err := strconv.ParseInt(v, 10, 64); err == nil { + filter.MaxFileSize = &n + } + } if v := q.Get("limit"); v != "" { if n, err := strconv.Atoi(v); err == nil && n > 0 && n <= 1000 { filter.Limit = n diff --git a/internal/api/handlers_more_test.go b/internal/api/handlers_more_test.go index 1939349..13c57cf 100644 --- a/internal/api/handlers_more_test.go +++ b/internal/api/handlers_more_test.go @@ -1705,13 +1705,13 @@ func Test_parseMediaListQuery_defaults(t *testing.T) { if got.Search != want.Search || got.Sort != want.Sort || got.Limit != want.Limit || got.Offset != want.Offset { t.Fatalf("unexpected defaults: %+v", got) } - if got.SetID != nil || got.Type != nil || got.Favorites != nil || got.MinDuration != nil || got.MaxDuration != nil { + if got.SetID != nil || got.Type != nil || got.Favorites != false || got.MinDuration != nil || got.MaxDuration != nil { t.Fatalf("expected nil optional fields, got %+v", got) } } func Test_parseMediaListQuery_allParams(t *testing.T) { - q := mustParseQuery(t, "/api/media?search=foo&sort=name&set_id=7&type=video&favorites=3&tags=bar,baz&min_duration=10&max_duration=100&limit=50&offset=10") + q := mustParseQuery(t, "/api/media?search=foo&sort=name&set_id=7&type=video&favorites=true&tags=bar,baz&min_duration=10&max_duration=100&limit=50&offset=10") got := parseMediaListQuery(q) if got.Search != "foo" { t.Fatalf("unexpected search: %q", got.Search) @@ -1725,7 +1725,7 @@ func Test_parseMediaListQuery_allParams(t *testing.T) { if got.Type == nil || *got.Type != "video" { t.Fatalf("unexpected type: %v", got.Type) } - if got.Favorites == nil || *got.Favorites != 3 { + if got.Favorites != true { t.Fatalf("unexpected favorites: %v", got.Favorites) } if len(got.Tags) != 2 || got.Tags[0] != "bar" || got.Tags[1] != "baz" { @@ -1786,7 +1786,7 @@ func Test_parseMediaListQuery_limitClampingAndNegativeOffset(t *testing.T) { if tt.invalidKey == "set_id" && got.SetID != nil { t.Fatalf("expected set_id nil for bad value, got %v", got.SetID) } - if tt.invalidKey == "favorites" && got.Favorites != nil { + if tt.invalidKey == "favorites" && got.Favorites != false { t.Fatalf("expected favorites nil for bad value, got %v", got.Favorites) } if tt.invalidKey == "min_duration" && got.MinDuration != nil { diff --git a/internal/api/handlers_test.go b/internal/api/handlers_test.go index 89aa177..8c2161c 100644 --- a/internal/api/handlers_test.go +++ b/internal/api/handlers_test.go @@ -577,8 +577,8 @@ func TestServer_MediaList(t *testing.T) { }, { name: "with query params", - query: "?set_id=1&type=video&search=foo&tags=bar,baz&favorites=2&min_duration=10&max_duration=100&sort=name&limit=5&offset=10", - filter: repository.MediaFilter{SetID: intPtr(1), Type: (*model.MediaType)(func() *string { s := "video"; return &s }()), Search: "foo", Tags: []string{"bar", "baz"}, Favorites: intPtr(2), MinDuration: floatPtr(10), MaxDuration: floatPtr(100), Sort: "name", Limit: 5, Offset: 10}, + query: "?set_id=1&type=video&search=foo&tags=bar,baz&favorites=true&min_duration=10&max_duration=100&sort=name&limit=5&offset=10", + filter: repository.MediaFilter{SetID: intPtr(1), Type: (*model.MediaType)(func() *string { s := "video"; return &s }()), Search: "foo", Tags: []string{"bar", "baz"}, Favorites: true, MinDuration: floatPtr(10), MaxDuration: floatPtr(100), Sort: "name", Limit: 5, Offset: 10}, listResult: []model.Media{}, wantCode: http.StatusOK, }, diff --git a/internal/repository/media.go b/internal/repository/media.go index 6c5297b..42b7f03 100644 --- a/internal/repository/media.go +++ b/internal/repository/media.go @@ -134,9 +134,9 @@ func (s *SQLite) ListMedia(ctx context.Context, filter MediaFilter) ([]model.Med like := "%" + term + "%" args = append(args, like, like) } - if filter.Favorites != nil { + if filter.Favorites { joins += ` INNER JOIN favorites f ON f.media_id = media.id AND f.user_id = ?` - args = append(args, *filter.Favorites) + args = append(args, filter.UserID) } if len(filter.Tags) > 0 { joins += ` INNER JOIN media_tags mt ON mt.media_id = media.id INNER JOIN tags t ON t.id = mt.tag_id` diff --git a/internal/repository/repository.go b/internal/repository/repository.go index 227fe79..15da984 100644 --- a/internal/repository/repository.go +++ b/internal/repository/repository.go @@ -97,9 +97,12 @@ type MediaFilter struct { Type *model.MediaType Search string Tags []string - Favorites *int64 // userID if set + Favorites bool // restrict to current user's favorites + UserID int64 MinDuration *float64 MaxDuration *float64 + MinFileSize *int64 + MaxFileSize *int64 Sort string // name, date, duration, play_count, random Limit int Offset int diff --git a/internal/service/media.go b/internal/service/media.go index 7501923..ee6b314 100644 --- a/internal/service/media.go +++ b/internal/service/media.go @@ -158,6 +158,7 @@ func (s *mediaService) ListMedia(ctx context.Context, userID int64, filter repos } if user != nil && user.IsAdmin { + filter.UserID = userID return s.store.ListMedia(ctx, filter) } @@ -171,6 +172,7 @@ func (s *mediaService) ListMedia(ctx context.Context, userID int64, filter repos allowed = append(allowed, p.SetID) } filter.AllowedSetIDs = allowed + filter.UserID = userID return s.store.ListMedia(ctx, filter) } |
