2023-10-12 19:51:29 +01:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using Google.Cloud.Firestore;
|
|
|
|
using Mixonomer.Fire.Model;
|
|
|
|
|
2024-01-18 22:30:34 +00:00
|
|
|
namespace Mixonomer.Fire.Extensions;
|
|
|
|
|
|
|
|
public static class UserRepoExtensions
|
2023-10-12 19:51:29 +01:00
|
|
|
{
|
2024-01-18 22:30:34 +00:00
|
|
|
public static async IAsyncEnumerable<User> GetUsers(this UserRepo repo)
|
2023-10-12 19:51:29 +01:00
|
|
|
{
|
2024-01-18 22:30:34 +00:00
|
|
|
var users = repo.GetUserDocs();
|
2023-10-12 19:51:29 +01:00
|
|
|
|
2024-01-18 22:30:34 +00:00
|
|
|
await foreach (var user in users)
|
|
|
|
{
|
|
|
|
yield return user.ConvertTo<User>();
|
2023-10-12 19:51:29 +01:00
|
|
|
}
|
2024-01-18 22:30:34 +00:00
|
|
|
}
|
2023-10-12 19:51:29 +01:00
|
|
|
|
2024-01-18 22:30:34 +00:00
|
|
|
public static async Task<IAsyncEnumerable<DocumentSnapshot>> GetPlaylistDocs(this UserRepo repo, string username)
|
|
|
|
{
|
|
|
|
var user = await repo.GetUser(username).ConfigureAwait(false);
|
2023-10-12 19:51:29 +01:00
|
|
|
|
2024-01-18 22:30:34 +00:00
|
|
|
return repo.GetPlaylistDocs(user);
|
|
|
|
}
|
2023-10-12 19:51:29 +01:00
|
|
|
|
2024-01-18 22:30:34 +00:00
|
|
|
public static async IAsyncEnumerable<Playlist> GetPlaylists(this UserRepo repo, User user)
|
|
|
|
{
|
|
|
|
var playlists = repo.GetPlaylistDocs(user);
|
2023-10-12 19:51:29 +01:00
|
|
|
|
2024-01-18 22:30:34 +00:00
|
|
|
await foreach (var playlist in playlists)
|
|
|
|
{
|
|
|
|
yield return playlist.ConvertTo<Playlist>();
|
2023-10-12 19:51:29 +01:00
|
|
|
}
|
2024-01-18 22:30:34 +00:00
|
|
|
}
|
2023-10-12 19:51:29 +01:00
|
|
|
|
2024-01-18 22:30:34 +00:00
|
|
|
public static async Task<IAsyncEnumerable<DocumentSnapshot>> GetTagDocs(this UserRepo repo, string username)
|
|
|
|
{
|
|
|
|
var user = await repo.GetUser(username).ConfigureAwait(false);
|
2023-10-12 19:51:29 +01:00
|
|
|
|
2024-01-18 22:30:34 +00:00
|
|
|
return repo.GetTagDocs(user);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static async IAsyncEnumerable<Tag> GetTags(this UserRepo repo, User user)
|
|
|
|
{
|
|
|
|
var tags = repo.GetTagDocs(user);
|
|
|
|
|
|
|
|
await foreach (var tag in tags)
|
|
|
|
{
|
|
|
|
yield return tag.ConvertTo<Tag>();
|
2023-10-12 19:51:29 +01:00
|
|
|
}
|
2024-01-18 22:30:34 +00:00
|
|
|
}
|
2023-10-12 19:51:29 +01:00
|
|
|
|
2024-01-18 22:30:34 +00:00
|
|
|
public static async Task<UserContext> GetUserContext(this UserRepo repo, string username)
|
|
|
|
{
|
|
|
|
var user = new UserContext
|
2023-10-12 19:51:29 +01:00
|
|
|
{
|
2024-01-18 22:30:34 +00:00
|
|
|
User = await repo.GetUser(username).ConfigureAwait(false)
|
|
|
|
};
|
2023-10-12 19:51:29 +01:00
|
|
|
|
2024-01-18 22:30:34 +00:00
|
|
|
var playlists = repo.GetPlaylists(user.User).ToListAsync();
|
|
|
|
var tags = repo.GetTags(user.User).ToListAsync();
|
2023-10-12 19:51:29 +01:00
|
|
|
|
2024-01-18 22:30:34 +00:00
|
|
|
await Task.WhenAll(playlists.AsTask(), tags.AsTask()).ConfigureAwait(false);
|
2023-10-12 19:51:29 +01:00
|
|
|
|
2024-01-18 22:30:34 +00:00
|
|
|
user.Playlists = playlists.Result;
|
|
|
|
user.Tags = tags.Result;
|
2023-10-12 19:51:29 +01:00
|
|
|
|
2024-01-18 22:30:34 +00:00
|
|
|
return user;
|
2023-10-12 19:51:29 +01:00
|
|
|
}
|
2024-01-18 22:30:34 +00:00
|
|
|
}
|