From ac8ea2723ae993dac3e3ccf707e13990afe245bc Mon Sep 17 00:00:00 2001 From: Andy Pack Date: Thu, 18 Jan 2024 22:30:34 +0000 Subject: [PATCH] completing part walker, adding month string getters --- Mixonomer.CLI/Mixonomer.CLI.csproj | 2 + Mixonomer.CLI/Program.cs | 20 +-- .../Extensions/UserRepoExtensions.cs | 113 ++++++++------- Mixonomer.Fire/IRepository.cs | 8 +- Mixonomer.Fire/Mixonomer.Fire.csproj | 1 + Mixonomer.Fire/Model/Playlist.cs | 129 +++++++++--------- Mixonomer.Fire/Model/Tag.cs | 123 +++++++++-------- Mixonomer.Fire/Model/User.cs | 102 +++++++------- Mixonomer.Fire/Model/UserContext.cs | 13 +- Mixonomer.Fire/UserRepo.cs | 64 +++++---- Mixonomer.Func/Mixonomer.Func.csproj | 3 + Mixonomer.Func/RunUserPlaylist.cs | 40 +++--- Mixonomer.NET.sln | 6 + Mixonomer.Playlist/Mixonomer.Playlist.csproj | 14 ++ Mixonomer.Playlist/Months.cs | 13 ++ Mixonomer.Playlist/PartTreeWalker.cs | 61 +++++++++ Mixonomer.Tests/Mixonomer.Tests.csproj | 2 + Mixonomer.Tests/UnitTest1.cs | 18 ++- 18 files changed, 416 insertions(+), 316 deletions(-) create mode 100644 Mixonomer.Playlist/Mixonomer.Playlist.csproj create mode 100644 Mixonomer.Playlist/Months.cs create mode 100644 Mixonomer.Playlist/PartTreeWalker.cs diff --git a/Mixonomer.CLI/Mixonomer.CLI.csproj b/Mixonomer.CLI/Mixonomer.CLI.csproj index 71435b4..3e0f274 100644 --- a/Mixonomer.CLI/Mixonomer.CLI.csproj +++ b/Mixonomer.CLI/Mixonomer.CLI.csproj @@ -3,9 +3,11 @@ Exe net6.0 + default + diff --git a/Mixonomer.CLI/Program.cs b/Mixonomer.CLI/Program.cs index 66953c2..1e57858 100644 --- a/Mixonomer.CLI/Program.cs +++ b/Mixonomer.CLI/Program.cs @@ -3,19 +3,21 @@ using System.Linq; using Mixonomer.Fire; using System.Threading.Tasks; using Mixonomer.Fire.Extensions; +using Mixonomer.Playlist; -namespace Mixonomer.CLI +namespace Mixonomer.CLI; + +class Program { - class Program + static async Task Main(string[] args) { - static async Task Main(string[] args) - { - var repo = new UserRepo(projectId: "mixonomer-test"); + var repo = new UserRepo(projectId: System.Environment.GetEnvironmentVariable("GOOGLE_CLOUD_PROJECT")); - var userContext = await repo.GetUserContext("andy"); + var userContext = await repo.GetUserContext("andy"); - Console.WriteLine(userContext.User); - } + Console.WriteLine(userContext.User); + + var walker = new PartTreeWalker(repo); + var partPlaylists = await walker.GetPlaylistParts("andy", "RAP"); } } - diff --git a/Mixonomer.Fire/Extensions/UserRepoExtensions.cs b/Mixonomer.Fire/Extensions/UserRepoExtensions.cs index 3a06383..1665c4e 100644 --- a/Mixonomer.Fire/Extensions/UserRepoExtensions.cs +++ b/Mixonomer.Fire/Extensions/UserRepoExtensions.cs @@ -4,60 +4,69 @@ using System.Threading.Tasks; using Google.Cloud.Firestore; using Mixonomer.Fire.Model; -namespace Mixonomer.Fire.Extensions +namespace Mixonomer.Fire.Extensions; + +public static class UserRepoExtensions { - public static class UserRepoExtensions + public static async IAsyncEnumerable GetUsers(this UserRepo repo) { - public static async Task> GetPlaylistDocs(this UserRepo repo, string username) + var users = repo.GetUserDocs(); + + await foreach (var user in users) { - var user = await repo.GetUser(username).ConfigureAwait(false); - - return await repo.GetPlaylistDocs(user).ConfigureAwait(false); - } - - public static async IAsyncEnumerable GetPlaylists(this UserRepo repo, User user) - { - var playlists = await repo.GetPlaylistDocs(user).ConfigureAwait(false); - - await foreach (var playlist in playlists) - { - yield return playlist.ConvertTo(); - } - } - - public static async Task> GetTagDocs(this UserRepo repo, string username) - { - var user = await repo.GetUser(username).ConfigureAwait(false); - - return await repo.GetTagDocs(user).ConfigureAwait(false); - } - - public static async IAsyncEnumerable GetTags(this UserRepo repo, User user) - { - var tags = await repo.GetTagDocs(user).ConfigureAwait(false); - - await foreach (var tag in tags) - { - yield return tag.ConvertTo(); - } - } - - public static async Task GetUserContext(this UserRepo repo, string username) - { - var user = new UserContext - { - User = await repo.GetUser(username).ConfigureAwait(false) - }; - - var playlists = repo.GetPlaylists(user.User).ToListAsync(); - var tags = repo.GetTags(user.User).ToListAsync(); - - await Task.WhenAll(playlists.AsTask(), tags.AsTask()).ConfigureAwait(false); - - user.Playlists = playlists.Result; - user.Tags = tags.Result; - - return user; + yield return user.ConvertTo(); } } -} \ No newline at end of file + + public static async Task> GetPlaylistDocs(this UserRepo repo, string username) + { + var user = await repo.GetUser(username).ConfigureAwait(false); + + return repo.GetPlaylistDocs(user); + } + + public static async IAsyncEnumerable GetPlaylists(this UserRepo repo, User user) + { + var playlists = repo.GetPlaylistDocs(user); + + await foreach (var playlist in playlists) + { + yield return playlist.ConvertTo(); + } + } + + public static async Task> GetTagDocs(this UserRepo repo, string username) + { + var user = await repo.GetUser(username).ConfigureAwait(false); + + return repo.GetTagDocs(user); + } + + public static async IAsyncEnumerable GetTags(this UserRepo repo, User user) + { + var tags = repo.GetTagDocs(user); + + await foreach (var tag in tags) + { + yield return tag.ConvertTo(); + } + } + + public static async Task GetUserContext(this UserRepo repo, string username) + { + var user = new UserContext + { + User = await repo.GetUser(username).ConfigureAwait(false) + }; + + var playlists = repo.GetPlaylists(user.User).ToListAsync(); + var tags = repo.GetTags(user.User).ToListAsync(); + + await Task.WhenAll(playlists.AsTask(), tags.AsTask()).ConfigureAwait(false); + + user.Playlists = playlists.Result; + user.Tags = tags.Result; + + return user; + } +} diff --git a/Mixonomer.Fire/IRepository.cs b/Mixonomer.Fire/IRepository.cs index 2cdc53b..923cdc0 100644 --- a/Mixonomer.Fire/IRepository.cs +++ b/Mixonomer.Fire/IRepository.cs @@ -1,9 +1,7 @@ using System; -namespace Mixonomer.Fire +namespace Mixonomer.Fire; + +public interface IRepository { - public interface IRepository - { - } } - diff --git a/Mixonomer.Fire/Mixonomer.Fire.csproj b/Mixonomer.Fire/Mixonomer.Fire.csproj index 72602be..9ff9b17 100644 --- a/Mixonomer.Fire/Mixonomer.Fire.csproj +++ b/Mixonomer.Fire/Mixonomer.Fire.csproj @@ -2,6 +2,7 @@ netstandard2.1 + default diff --git a/Mixonomer.Fire/Model/Playlist.cs b/Mixonomer.Fire/Model/Playlist.cs index 3a86905..18bf838 100644 --- a/Mixonomer.Fire/Model/Playlist.cs +++ b/Mixonomer.Fire/Model/Playlist.cs @@ -2,84 +2,83 @@ using System.Collections.Generic; using Google.Cloud.Firestore; -namespace Mixonomer.Fire +namespace Mixonomer.Fire; + +[FirestoreData] +public class Playlist { - [FirestoreData] - public class Playlist - { - [FirestoreProperty] - public string uri { get; set; } - [FirestoreProperty] - public string name { get; set; } - [FirestoreProperty] - public string type { get; set; } + [FirestoreProperty] + public string uri { get; set; } + [FirestoreProperty] + public string name { get; set; } + [FirestoreProperty] + public string type { get; set; } - [FirestoreProperty] - public bool include_recommendations { get; set; } - [FirestoreProperty] - public int recommendation_sample { get; set; } - [FirestoreProperty] - public bool include_library_tracks { get; set; } + [FirestoreProperty] + public bool include_recommendations { get; set; } + [FirestoreProperty] + public int recommendation_sample { get; set; } + [FirestoreProperty] + public bool include_library_tracks { get; set; } - [FirestoreProperty] - public IEnumerable parts { get; set; } - [FirestoreProperty] - public IEnumerable playlist_references { get; set; } - [FirestoreProperty] - public bool shuffle { get; set; } + [FirestoreProperty] + public IEnumerable parts { get; set; } + [FirestoreProperty] + public IEnumerable playlist_references { get; set; } + [FirestoreProperty] + public bool shuffle { get; set; } - [FirestoreProperty] - public string sort { get; set; } - [FirestoreProperty] - public string description_overwrite { get; set; } - [FirestoreProperty] - public string description_suffix { get; set; } + [FirestoreProperty] + public string sort { get; set; } + [FirestoreProperty] + public string description_overwrite { get; set; } + [FirestoreProperty] + public string description_suffix { get; set; } - [FirestoreProperty] - public DateTime last_updated { get; set; } + [FirestoreProperty] + public DateTime last_updated { get; set; } - [FirestoreProperty] - public int lastfm_stat_count { get; set; } - [FirestoreProperty] - public int lastfm_stat_album_count { get; set; } - [FirestoreProperty] - public int lastfm_stat_artist_count { get; set; } + [FirestoreProperty] + public int lastfm_stat_count { get; set; } + [FirestoreProperty] + public int lastfm_stat_album_count { get; set; } + [FirestoreProperty] + public int lastfm_stat_artist_count { get; set; } - [FirestoreProperty] - public double lastfm_stat_percent { get; set; } - [FirestoreProperty] - public double lastfm_stat_album_percent { get; set; } - [FirestoreProperty] - public double lastfm_stat_artist_percent { get; set; } + [FirestoreProperty] + public double lastfm_stat_percent { get; set; } + [FirestoreProperty] + public double lastfm_stat_album_percent { get; set; } + [FirestoreProperty] + public double lastfm_stat_artist_percent { get; set; } - [FirestoreProperty] - public DateTime lastfm_stat_last_refresh { get; set; } + [FirestoreProperty] + public DateTime lastfm_stat_last_refresh { get; set; } - [FirestoreProperty] - public bool add_last_month { get; set; } - [FirestoreProperty] - public bool add_this_month { get; set; } + [FirestoreProperty] + public bool add_last_month { get; set; } + [FirestoreProperty] + public bool add_this_month { get; set; } - [FirestoreProperty] - public int day_boundary { get; set; } + [FirestoreProperty] + public int day_boundary { get; set; } - [FirestoreProperty] - public bool include_spotify_owned { get; set; } + [FirestoreProperty] + public bool include_spotify_owned { get; set; } - [FirestoreProperty] - public string chart_range { get; set; } - [FirestoreProperty] - public int chart_limit { get; set; } + [FirestoreProperty] + public string chart_range { get; set; } + [FirestoreProperty] + public int chart_limit { get; set; } - [FirestoreDocumentId] - public DocumentReference Reference { get; set; } + [FirestoreDocumentId] + public DocumentReference Reference { get; set; } - [FirestoreDocumentCreateTimestamp] - public Timestamp CreateTime { get; set; } - [FirestoreDocumentUpdateTimestamp] - public Timestamp UpdateTime { get; set; } - [FirestoreDocumentReadTimestamp] - public Timestamp ReadTime { get; set; } - } + [FirestoreDocumentCreateTimestamp] + public Timestamp CreateTime { get; set; } + [FirestoreDocumentUpdateTimestamp] + public Timestamp UpdateTime { get; set; } + [FirestoreDocumentReadTimestamp] + public Timestamp ReadTime { get; set; } } diff --git a/Mixonomer.Fire/Model/Tag.cs b/Mixonomer.Fire/Model/Tag.cs index 62139ba..34b28b4 100644 --- a/Mixonomer.Fire/Model/Tag.cs +++ b/Mixonomer.Fire/Model/Tag.cs @@ -2,74 +2,73 @@ using System.Collections.Generic; using Google.Cloud.Firestore; -namespace Mixonomer.Fire +namespace Mixonomer.Fire; + +[FirestoreData] +public class Tag { - [FirestoreData] - public class Tag - { - [FirestoreProperty] - public string name { get; set; } - [FirestoreProperty] - public string tag_id { get; set; } - [FirestoreProperty] - public string username { get; set; } + [FirestoreProperty] + public string name { get; set; } + [FirestoreProperty] + public string tag_id { get; set; } + [FirestoreProperty] + public string username { get; set; } - [FirestoreProperty] - public DateTime last_updated { get; set; } + [FirestoreProperty] + public DateTime last_updated { get; set; } - [FirestoreProperty] - public int count { get; set; } - [FirestoreProperty] - public double proportion { get; set; } + [FirestoreProperty] + public int count { get; set; } + [FirestoreProperty] + public double proportion { get; set; } - [FirestoreProperty] - public bool time_objects { get; set; } - [FirestoreProperty] - public string total_time { get; set; } - [FirestoreProperty] - public int total_time_ms { get; set; } - [FirestoreProperty] - public int total_user_scrobbles { get; set; } + [FirestoreProperty] + public bool time_objects { get; set; } + [FirestoreProperty] + public string total_time { get; set; } + [FirestoreProperty] + public int total_time_ms { get; set; } + [FirestoreProperty] + public int total_user_scrobbles { get; set; } - [FirestoreProperty] - public IEnumerable tracks { get; set; } - [FirestoreProperty] - public IEnumerable albums { get; set; } - [FirestoreProperty] - public IEnumerable artists { get; set; } + [FirestoreProperty] + public IEnumerable tracks { get; set; } + [FirestoreProperty] + public IEnumerable albums { get; set; } + [FirestoreProperty] + public IEnumerable artists { get; set; } - [FirestoreDocumentId] - public DocumentReference Reference { get; set; } + [FirestoreDocumentId] + public DocumentReference Reference { get; set; } - [FirestoreDocumentCreateTimestamp] - public Timestamp CreateTime { get; set; } - [FirestoreDocumentUpdateTimestamp] - public Timestamp UpdateTime { get; set; } - [FirestoreDocumentReadTimestamp] - public Timestamp ReadTime { get; set; } - } - - [FirestoreData] - public class TagItem - { - [FirestoreProperty] - public string name { get; set; } - [FirestoreProperty] - public string time { get; set; } - [FirestoreProperty] - public int time_ms { get; set; } - [FirestoreProperty] - public int count { get; set; } - - [FirestoreDocumentId] - public DocumentReference Reference { get; set; } - - [FirestoreDocumentCreateTimestamp] - public Timestamp CreateTime { get; set; } - [FirestoreDocumentUpdateTimestamp] - public Timestamp UpdateTime { get; set; } - [FirestoreDocumentReadTimestamp] - public Timestamp ReadTime { get; set; } - } + [FirestoreDocumentCreateTimestamp] + public Timestamp CreateTime { get; set; } + [FirestoreDocumentUpdateTimestamp] + public Timestamp UpdateTime { get; set; } + [FirestoreDocumentReadTimestamp] + public Timestamp ReadTime { get; set; } +} + +[FirestoreData] +public class TagItem +{ + [FirestoreProperty] + public string name { get; set; } + [FirestoreProperty] + public string time { get; set; } + [FirestoreProperty] + public int time_ms { get; set; } + [FirestoreProperty] + public int count { get; set; } + + [FirestoreDocumentId] + public DocumentReference Reference { get; set; } + + [FirestoreDocumentCreateTimestamp] + public Timestamp CreateTime { get; set; } + [FirestoreDocumentUpdateTimestamp] + public Timestamp UpdateTime { get; set; } + [FirestoreDocumentReadTimestamp] + public Timestamp ReadTime { get; set; } } diff --git a/Mixonomer.Fire/Model/User.cs b/Mixonomer.Fire/Model/User.cs index da9a374..57d2ee3 100644 --- a/Mixonomer.Fire/Model/User.cs +++ b/Mixonomer.Fire/Model/User.cs @@ -2,60 +2,58 @@ using System.Collections.Generic; using Google.Cloud.Firestore; -namespace Mixonomer.Fire +namespace Mixonomer.Fire; + +[FirestoreData] +public class User { - [FirestoreData] - public class User - { - [FirestoreProperty] - public string access_token { get; set; } - [FirestoreProperty] - public string email { get; set; } - [FirestoreProperty] - public DateTime last_login { get; set; } - [FirestoreProperty] - public DateTime last_refreshed { get; set; } - [FirestoreProperty] - public DateTime last_keygen { get; set; } - [FirestoreProperty] - public string lastfm_username { get; set; } - [FirestoreProperty] - public bool locked { get; set; } - [FirestoreProperty] - public string password { get; set; } - [FirestoreProperty] - public string refresh_token { get; set; } - [FirestoreProperty] - public bool spotify_linked { get; set; } - [FirestoreProperty] - public int token_expiry { get; set; } - [FirestoreProperty] - public string type { get; set; } - [FirestoreProperty] - public string username { get; set; } - [FirestoreProperty] - public bool validated { get; set; } + [FirestoreProperty] + public string access_token { get; set; } + [FirestoreProperty] + public string email { get; set; } + [FirestoreProperty] + public DateTime last_login { get; set; } + [FirestoreProperty] + public DateTime last_refreshed { get; set; } + [FirestoreProperty] + public DateTime last_keygen { get; set; } + [FirestoreProperty] + public string lastfm_username { get; set; } + [FirestoreProperty] + public bool locked { get; set; } + [FirestoreProperty] + public string password { get; set; } + [FirestoreProperty] + public string refresh_token { get; set; } + [FirestoreProperty] + public bool spotify_linked { get; set; } + [FirestoreProperty] + public int token_expiry { get; set; } + [FirestoreProperty] + public string type { get; set; } + [FirestoreProperty] + public string username { get; set; } + [FirestoreProperty] + public bool validated { get; set; } - [FirestoreProperty] - public IEnumerable apns_tokens { get; set; } - [FirestoreProperty] - public bool notify { get; set; } - [FirestoreProperty] - public bool notify_admins { get; set; } - [FirestoreProperty] - public bool notify_playlist_updates { get; set; } - [FirestoreProperty] - public bool notify_tag_updates { get; set; } + [FirestoreProperty] + public IEnumerable apns_tokens { get; set; } + [FirestoreProperty] + public bool notify { get; set; } + [FirestoreProperty] + public bool notify_admins { get; set; } + [FirestoreProperty] + public bool notify_playlist_updates { get; set; } + [FirestoreProperty] + public bool notify_tag_updates { get; set; } - [FirestoreDocumentId] - public DocumentReference Reference { get; set; } + [FirestoreDocumentId] + public DocumentReference Reference { get; set; } - [FirestoreDocumentCreateTimestamp] - public Timestamp CreateTime { get; set; } - [FirestoreDocumentUpdateTimestamp] - public Timestamp UpdateTime { get; set; } - [FirestoreDocumentReadTimestamp] - public Timestamp ReadTime { get; set; } - } + [FirestoreDocumentCreateTimestamp] + public Timestamp CreateTime { get; set; } + [FirestoreDocumentUpdateTimestamp] + public Timestamp UpdateTime { get; set; } + [FirestoreDocumentReadTimestamp] + public Timestamp ReadTime { get; set; } } - diff --git a/Mixonomer.Fire/Model/UserContext.cs b/Mixonomer.Fire/Model/UserContext.cs index b86e697..cf010b0 100644 --- a/Mixonomer.Fire/Model/UserContext.cs +++ b/Mixonomer.Fire/Model/UserContext.cs @@ -1,11 +1,10 @@ using System.Collections.Generic; -namespace Mixonomer.Fire.Model +namespace Mixonomer.Fire.Model; + +public class UserContext { - public class UserContext - { - public User User { get; set; } - public IEnumerable Playlists { get; set; } - public IEnumerable Tags { get; set; } - } + public User User { get; set; } + public IEnumerable Playlists { get; set; } + public IEnumerable Tags { get; set; } } \ No newline at end of file diff --git a/Mixonomer.Fire/UserRepo.cs b/Mixonomer.Fire/UserRepo.cs index dab8110..655d03e 100644 --- a/Mixonomer.Fire/UserRepo.cs +++ b/Mixonomer.Fire/UserRepo.cs @@ -4,47 +4,45 @@ using System.Linq; using System.Threading.Tasks; using Google.Cloud.Firestore; -namespace Mixonomer.Fire +namespace Mixonomer.Fire; + +public class UserRepo { - public class UserRepo + private static readonly string USER_COLLECTION = "spotify_users"; + + private readonly FirestoreDb db; + private readonly CollectionReference userCollection; + + public UserRepo(FirestoreDb db = null, string projectId = null) { - private static readonly string USER_COLLECTION = "spotify_users"; + this.db = db ?? FirestoreDb.Create(projectId); + userCollection = this.db.Collection(USER_COLLECTION); + } - private readonly FirestoreDb db; - private readonly CollectionReference userCollection; + public IAsyncEnumerable GetUserDocs() + { + return userCollection.StreamAsync(); + } - public UserRepo(FirestoreDb db = null, string projectId = null) - { - this.db = db ?? FirestoreDb.Create(projectId); - userCollection = this.db.Collection(USER_COLLECTION); - } + public async Task GetUser(string username) + { + var query = userCollection.WhereEqualTo("username", username.ToLower()); + var querySnapshot = await query.GetSnapshotAsync().ConfigureAwait(false); - public IAsyncEnumerable GetUsers() - { - return userCollection.StreamAsync(); - } + return querySnapshot.SingleOrDefault()?.ConvertTo(); + } - public async Task GetUser(string username) - { - var query = userCollection.WhereEqualTo("username", username.ToLower()); - var querySnapshot = await query.GetSnapshotAsync().ConfigureAwait(false); + public IAsyncEnumerable GetPlaylistDocs(User user) + { + var playlistCollection = db.Collection($"{USER_COLLECTION}/{user.Reference.Id}/playlists"); - return querySnapshot.SingleOrDefault()?.ConvertTo(); - } + return playlistCollection.StreamAsync(); + } - public Task> GetPlaylistDocs(User user) - { - var playlistCollection = db.Collection($"{USER_COLLECTION}/{user.Reference.Id}/playlists"); + public IAsyncEnumerable GetTagDocs(User user) + { + var playlistCollection = db.Collection($"{USER_COLLECTION}/{user.Reference.Id}/tags"); - return Task.FromResult(playlistCollection.StreamAsync()); - } - - public Task> GetTagDocs(User user) - { - var playlistCollection = db.Collection($"{USER_COLLECTION}/{user.Reference.Id}/tags"); - - return Task.FromResult(playlistCollection.StreamAsync()); - } + return playlistCollection.StreamAsync(); } } - diff --git a/Mixonomer.Func/Mixonomer.Func.csproj b/Mixonomer.Func/Mixonomer.Func.csproj index 3460e4b..e2a9a99 100644 --- a/Mixonomer.Func/Mixonomer.Func.csproj +++ b/Mixonomer.Func/Mixonomer.Func.csproj @@ -3,16 +3,19 @@ Exe net6.0 + default + + diff --git a/Mixonomer.Func/RunUserPlaylist.cs b/Mixonomer.Func/RunUserPlaylist.cs index 0f47034..16bfd4f 100644 --- a/Mixonomer.Func/RunUserPlaylist.cs +++ b/Mixonomer.Func/RunUserPlaylist.cs @@ -8,28 +8,26 @@ using Google.Events.Protobuf.Cloud.PubSub.V1; using Microsoft.Extensions.Logging; using Mixonomer.Fire; -namespace Mixonomer.Func +namespace Mixonomer.Func; + +public class RunUserPlaylist : ICloudEventFunction { - public class RunUserPlaylist : ICloudEventFunction + private readonly ILogger _logger; + + public RunUserPlaylist(ILogger logger) { - private readonly ILogger _logger; - - public RunUserPlaylist(ILogger logger) - { - _logger = logger; - } - - - public async Task HandleAsync(CloudEvent cloudEvent, MessagePublishedData data, CancellationToken cancellationToken) - { - _logger.LogInformation($"Received message in C# {data.Message}, {cloudEvent.GetPopulatedAttributes()}"); - - var userRepo = new UserRepo(projectId: System.Environment.GetEnvironmentVariable("GOOGLE_CLOUD_PROJECT")); - - var user = await userRepo.GetUser(data.Message.Attributes["username"]); - - _logger.LogInformation($"{user.username} was last refreshed at {user.last_refreshed}"); - } + _logger = logger; } -} + + public async Task HandleAsync(CloudEvent cloudEvent, MessagePublishedData data, CancellationToken cancellationToken) + { + _logger.LogInformation($"Received message in C# {data.Message}, {cloudEvent.GetPopulatedAttributes()}"); + + var userRepo = new UserRepo(projectId: System.Environment.GetEnvironmentVariable("GOOGLE_CLOUD_PROJECT")); + + var user = await userRepo.GetUser(data.Message.Attributes["username"]); + + _logger.LogInformation($"{user.username} was last refreshed at {user.last_refreshed}"); + } +} \ No newline at end of file diff --git a/Mixonomer.NET.sln b/Mixonomer.NET.sln index a657ffe..3c0ec00 100644 --- a/Mixonomer.NET.sln +++ b/Mixonomer.NET.sln @@ -11,6 +11,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Mixonomer.Tests", "Mixonome EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Mixonomer.CLI", "Mixonomer.CLI\Mixonomer.CLI.csproj", "{7469F571-DBF2-4D60-85FE-4041C445A490}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Mixonomer.Playlist", "Mixonomer.Playlist\Mixonomer.Playlist.csproj", "{274560D0-2EBB-4C6D-BA45-2270DB92F12C}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -33,6 +35,10 @@ Global {7469F571-DBF2-4D60-85FE-4041C445A490}.Debug|Any CPU.Build.0 = Debug|Any CPU {7469F571-DBF2-4D60-85FE-4041C445A490}.Release|Any CPU.ActiveCfg = Release|Any CPU {7469F571-DBF2-4D60-85FE-4041C445A490}.Release|Any CPU.Build.0 = Release|Any CPU + {274560D0-2EBB-4C6D-BA45-2270DB92F12C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {274560D0-2EBB-4C6D-BA45-2270DB92F12C}.Debug|Any CPU.Build.0 = Debug|Any CPU + {274560D0-2EBB-4C6D-BA45-2270DB92F12C}.Release|Any CPU.ActiveCfg = Release|Any CPU + {274560D0-2EBB-4C6D-BA45-2270DB92F12C}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Mixonomer.Playlist/Mixonomer.Playlist.csproj b/Mixonomer.Playlist/Mixonomer.Playlist.csproj new file mode 100644 index 0000000..e613689 --- /dev/null +++ b/Mixonomer.Playlist/Mixonomer.Playlist.csproj @@ -0,0 +1,14 @@ + + + + net6.0 + enable + enable + default + + + + + + + diff --git a/Mixonomer.Playlist/Months.cs b/Mixonomer.Playlist/Months.cs new file mode 100644 index 0000000..cc6e3d0 --- /dev/null +++ b/Mixonomer.Playlist/Months.cs @@ -0,0 +1,13 @@ +namespace Mixonomer.Playlist; + +public static class Months +{ + public static string ThisMonth() => DateTime.Now.ToString("MMMM yy").ToLowerInvariant(); + + public static string LastMonth() + { + var now = DateTime.Now; + var lastMonth = now.AddDays(-now.Day - 1); + return lastMonth.ToString("MMMM yy").ToLowerInvariant(); + } +} \ No newline at end of file diff --git a/Mixonomer.Playlist/PartTreeWalker.cs b/Mixonomer.Playlist/PartTreeWalker.cs new file mode 100644 index 0000000..edb1e6f --- /dev/null +++ b/Mixonomer.Playlist/PartTreeWalker.cs @@ -0,0 +1,61 @@ +using Google.Cloud.Firestore; +using Mixonomer.Fire; +using Mixonomer.Fire.Extensions; + +namespace Mixonomer.Playlist; + +public class PartTreeWalker +{ + private readonly UserRepo _userRepo; + + private readonly HashSet _processedPlaylists = new(); + public HashSet? SpotifyPlaylistNames { get; private set; } + + public PartTreeWalker(UserRepo userRepo) + { + _userRepo = userRepo; + } + + public async Task> GetPlaylistParts(string username, string playlistName) + { + var user = await _userRepo.GetUser(username); + + return await GetPlaylistParts(user, playlistName); + } + + public async Task> GetPlaylistParts(User user, string playlistName) + { + var playlist = await _userRepo.GetPlaylists(user).Where(x => x.name == playlistName).FirstOrDefaultAsync(); + + if (playlist is not null) + { + SpotifyPlaylistNames = new HashSet(playlist.parts); + + foreach (var part in playlist.playlist_references) + { + await ProcessPlaylist(part); + } + } + + return SpotifyPlaylistNames; + } + + private async Task ProcessPlaylist(DocumentReference documentReference) + { + if (!_processedPlaylists.Contains(documentReference.Id)) + { + var playlist = (await documentReference.GetSnapshotAsync()).ConvertTo(); + + _processedPlaylists.Add(documentReference.Id); + foreach (var p in playlist.parts) + { + SpotifyPlaylistNames?.Add(p); + } + + foreach (var p in playlist.playlist_references) + { + await ProcessPlaylist(p); + } + } + } +} \ No newline at end of file diff --git a/Mixonomer.Tests/Mixonomer.Tests.csproj b/Mixonomer.Tests/Mixonomer.Tests.csproj index 07a65b1..d79255d 100644 --- a/Mixonomer.Tests/Mixonomer.Tests.csproj +++ b/Mixonomer.Tests/Mixonomer.Tests.csproj @@ -4,6 +4,8 @@ net6.0 false + + default diff --git a/Mixonomer.Tests/UnitTest1.cs b/Mixonomer.Tests/UnitTest1.cs index 6335500..f9e88f0 100644 --- a/Mixonomer.Tests/UnitTest1.cs +++ b/Mixonomer.Tests/UnitTest1.cs @@ -2,17 +2,15 @@ using Mixonomer.Fire; using Xunit; -namespace Mixonomer.Tests +namespace Mixonomer.Tests; + +public class UnitTest1 { - public class UnitTest1 + [Fact] + public async void Test1() { - [Fact] - public async void Test1() - { - // var repo = new UserRepo(); + // var repo = new UserRepo(); - // var user = await repo.GetUser("andy"); - } + // var user = await repo.GetUser("andy"); } -} - +} \ No newline at end of file