From 2112c1de0e96acf5a5b6e573514fc6daa5f23f17 Mon Sep 17 00:00:00 2001 From: Andy Pack Date: Thu, 12 Oct 2023 19:51:29 +0100 Subject: [PATCH] adding tag and user context --- Mixonomer.CLI/Program.cs | 15 ++-- .../Extensions/UserRepoExtensions.cs | 63 ++++++++++++++++ Mixonomer.Fire/Mixonomer.Fire.csproj | 3 - Mixonomer.Fire/Model/Tag.cs | 75 +++++++++++++++++++ Mixonomer.Fire/Model/User.cs | 14 ++++ Mixonomer.Fire/Model/UserContext.cs | 11 +++ Mixonomer.Fire/UserRepo.cs | 13 +++- 7 files changed, 177 insertions(+), 17 deletions(-) create mode 100644 Mixonomer.Fire/Extensions/UserRepoExtensions.cs create mode 100644 Mixonomer.Fire/Model/Tag.cs create mode 100644 Mixonomer.Fire/Model/UserContext.cs diff --git a/Mixonomer.CLI/Program.cs b/Mixonomer.CLI/Program.cs index 2fa3652..66953c2 100644 --- a/Mixonomer.CLI/Program.cs +++ b/Mixonomer.CLI/Program.cs @@ -1,6 +1,8 @@ using System; +using System.Linq; using Mixonomer.Fire; using System.Threading.Tasks; +using Mixonomer.Fire.Extensions; namespace Mixonomer.CLI { @@ -8,18 +10,11 @@ namespace Mixonomer.CLI { static async Task Main(string[] args) { - var repo = new UserRepo(projectId: "sarsooxyz"); + var repo = new UserRepo(projectId: "mixonomer-test"); - var user = await repo.GetUser("andy"); - var playlists = await repo.GetPlaylists("andy"); + var userContext = await repo.GetUserContext("andy"); - await foreach (var playlist in playlists) - { - var dict = playlist.ToDictionary(); - var playlistObj = playlist.ConvertTo(); - } - - Console.WriteLine(user); + Console.WriteLine(userContext.User); } } } diff --git a/Mixonomer.Fire/Extensions/UserRepoExtensions.cs b/Mixonomer.Fire/Extensions/UserRepoExtensions.cs new file mode 100644 index 0000000..3a06383 --- /dev/null +++ b/Mixonomer.Fire/Extensions/UserRepoExtensions.cs @@ -0,0 +1,63 @@ +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Google.Cloud.Firestore; +using Mixonomer.Fire.Model; + +namespace Mixonomer.Fire.Extensions +{ + public static class UserRepoExtensions + { + public static async Task> GetPlaylistDocs(this UserRepo repo, string username) + { + 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; + } + } +} \ No newline at end of file diff --git a/Mixonomer.Fire/Mixonomer.Fire.csproj b/Mixonomer.Fire/Mixonomer.Fire.csproj index 67dc87c..72602be 100644 --- a/Mixonomer.Fire/Mixonomer.Fire.csproj +++ b/Mixonomer.Fire/Mixonomer.Fire.csproj @@ -11,7 +11,4 @@ - - - diff --git a/Mixonomer.Fire/Model/Tag.cs b/Mixonomer.Fire/Model/Tag.cs new file mode 100644 index 0000000..62139ba --- /dev/null +++ b/Mixonomer.Fire/Model/Tag.cs @@ -0,0 +1,75 @@ +using System; +using System.Collections.Generic; +using Google.Cloud.Firestore; + +namespace Mixonomer.Fire +{ + [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 DateTime last_updated { 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 IEnumerable tracks { get; set; } + [FirestoreProperty] + public IEnumerable albums { get; set; } + [FirestoreProperty] + public IEnumerable artists { 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; } + } +} + diff --git a/Mixonomer.Fire/Model/User.cs b/Mixonomer.Fire/Model/User.cs index f9ae964..da9a374 100644 --- a/Mixonomer.Fire/Model/User.cs +++ b/Mixonomer.Fire/Model/User.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using Google.Cloud.Firestore; namespace Mixonomer.Fire @@ -15,6 +16,8 @@ namespace Mixonomer.Fire [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; } @@ -33,6 +36,17 @@ namespace Mixonomer.Fire [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; } + [FirestoreDocumentId] public DocumentReference Reference { get; set; } diff --git a/Mixonomer.Fire/Model/UserContext.cs b/Mixonomer.Fire/Model/UserContext.cs new file mode 100644 index 0000000..b86e697 --- /dev/null +++ b/Mixonomer.Fire/Model/UserContext.cs @@ -0,0 +1,11 @@ +using System.Collections.Generic; + +namespace Mixonomer.Fire.Model +{ + public class UserContext + { + 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 903d393..dab8110 100644 --- a/Mixonomer.Fire/UserRepo.cs +++ b/Mixonomer.Fire/UserRepo.cs @@ -32,13 +32,18 @@ namespace Mixonomer.Fire return querySnapshot.SingleOrDefault()?.ConvertTo(); } - public async Task> GetPlaylists(string username) + public Task> GetPlaylistDocs(User user) { - var user = await GetUser(username); - var playlistCollection = db.Collection($"{USER_COLLECTION}/{user.Reference.Id}/playlists"); - return playlistCollection.StreamAsync(); + 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()); } } }