adding tag and user context

This commit is contained in:
Andy Pack 2023-10-12 19:51:29 +01:00
parent 97538659e3
commit 2112c1de0e
Signed by: sarsoo
GPG Key ID: A55BA3536A5E0ED7
7 changed files with 177 additions and 17 deletions

View File

@ -1,6 +1,8 @@
using System; using System;
using System.Linq;
using Mixonomer.Fire; using Mixonomer.Fire;
using System.Threading.Tasks; using System.Threading.Tasks;
using Mixonomer.Fire.Extensions;
namespace Mixonomer.CLI namespace Mixonomer.CLI
{ {
@ -8,18 +10,11 @@ namespace Mixonomer.CLI
{ {
static async Task Main(string[] args) 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 userContext = await repo.GetUserContext("andy");
var playlists = await repo.GetPlaylists("andy");
await foreach (var playlist in playlists) Console.WriteLine(userContext.User);
{
var dict = playlist.ToDictionary();
var playlistObj = playlist.ConvertTo<Playlist>();
}
Console.WriteLine(user);
} }
} }
} }

View File

@ -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<IAsyncEnumerable<DocumentSnapshot>> 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<Playlist> GetPlaylists(this UserRepo repo, User user)
{
var playlists = await repo.GetPlaylistDocs(user).ConfigureAwait(false);
await foreach (var playlist in playlists)
{
yield return playlist.ConvertTo<Playlist>();
}
}
public static async Task<IAsyncEnumerable<DocumentSnapshot>> 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<Tag> GetTags(this UserRepo repo, User user)
{
var tags = await repo.GetTagDocs(user).ConfigureAwait(false);
await foreach (var tag in tags)
{
yield return tag.ConvertTo<Tag>();
}
}
public static async Task<UserContext> 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;
}
}
}

View File

@ -11,7 +11,4 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="Google.Cloud.Firestore" Version="3.3.0" /> <PackageReference Include="Google.Cloud.Firestore" Version="3.3.0" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<Folder Include="Model\" />
</ItemGroup>
</Project> </Project>

View File

@ -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<TagItem> tracks { get; set; }
[FirestoreProperty]
public IEnumerable<TagItem> albums { get; set; }
[FirestoreProperty]
public IEnumerable<TagItem> 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; }
}
}

View File

@ -1,4 +1,5 @@
using System; using System;
using System.Collections.Generic;
using Google.Cloud.Firestore; using Google.Cloud.Firestore;
namespace Mixonomer.Fire namespace Mixonomer.Fire
@ -15,6 +16,8 @@ namespace Mixonomer.Fire
[FirestoreProperty] [FirestoreProperty]
public DateTime last_refreshed { get; set; } public DateTime last_refreshed { get; set; }
[FirestoreProperty] [FirestoreProperty]
public DateTime last_keygen { get; set; }
[FirestoreProperty]
public string lastfm_username { get; set; } public string lastfm_username { get; set; }
[FirestoreProperty] [FirestoreProperty]
public bool locked { get; set; } public bool locked { get; set; }
@ -33,6 +36,17 @@ namespace Mixonomer.Fire
[FirestoreProperty] [FirestoreProperty]
public bool validated { get; set; } public bool validated { get; set; }
[FirestoreProperty]
public IEnumerable<string> 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] [FirestoreDocumentId]
public DocumentReference Reference { get; set; } public DocumentReference Reference { get; set; }

View File

@ -0,0 +1,11 @@
using System.Collections.Generic;
namespace Mixonomer.Fire.Model
{
public class UserContext
{
public User User { get; set; }
public IEnumerable<Playlist> Playlists { get; set; }
public IEnumerable<Tag> Tags { get; set; }
}
}

View File

@ -32,13 +32,18 @@ namespace Mixonomer.Fire
return querySnapshot.SingleOrDefault()?.ConvertTo<User>(); return querySnapshot.SingleOrDefault()?.ConvertTo<User>();
} }
public async Task<IAsyncEnumerable<DocumentSnapshot>> GetPlaylists(string username) public Task<IAsyncEnumerable<DocumentSnapshot>> GetPlaylistDocs(User user)
{ {
var user = await GetUser(username);
var playlistCollection = db.Collection($"{USER_COLLECTION}/{user.Reference.Id}/playlists"); var playlistCollection = db.Collection($"{USER_COLLECTION}/{user.Reference.Id}/playlists");
return playlistCollection.StreamAsync(); return Task.FromResult(playlistCollection.StreamAsync());
}
public Task<IAsyncEnumerable<DocumentSnapshot>> GetTagDocs(User user)
{
var playlistCollection = db.Collection($"{USER_COLLECTION}/{user.Reference.Id}/tags");
return Task.FromResult(playlistCollection.StreamAsync());
} }
} }
} }