From e475f6d6fe9e54507d5880009896726451f6d9bb Mon Sep 17 00:00:00 2001 From: Ahad Chowdhury Date: Tue, 24 Sep 2024 17:15:07 +1000 Subject: [PATCH] Add pages for music --- src/Components/Pages/MusicFiles/List.razor | 35 ++++++++++++++++++++ src/Components/Pages/MusicFiles/Upload.razor | 16 +++++++-- 2 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 src/Components/Pages/MusicFiles/List.razor diff --git a/src/Components/Pages/MusicFiles/List.razor b/src/Components/Pages/MusicFiles/List.razor new file mode 100644 index 0000000..7679eeb --- /dev/null +++ b/src/Components/Pages/MusicFiles/List.razor @@ -0,0 +1,35 @@ +@page "/music" +@using Microsoft.AspNetCore.Identity +@using Microsoft.EntityFrameworkCore +@using Blazing.Components.Account +@using Blazing.Data +@inject IdentityUserAccessor UserAccessor +@inject IDbContextFactory DbFactory + +@if (MusicFiles is not null && MusicFiles.Any()) +{ + +} + +Upload + +@code { + public IEnumerable? MusicFiles { get; set; } + + [CascadingParameter] + private HttpContext HttpContext { get; set; } = default!; + + protected async override Task OnInitializedAsync() + { + var user = await UserAccessor.GetRequiredUserAsync(HttpContext); + using var context = DbFactory.CreateDbContext(); + MusicFiles = await context.MusicFiles.Where(ce => ce.User.Id == user.Id).ToListAsync(); + } +} diff --git a/src/Components/Pages/MusicFiles/Upload.razor b/src/Components/Pages/MusicFiles/Upload.razor index 3d6f203..14c1bc7 100644 --- a/src/Components/Pages/MusicFiles/Upload.razor +++ b/src/Components/Pages/MusicFiles/Upload.razor @@ -72,9 +72,21 @@ else if (result.HttpStatusCode == System.Net.HttpStatusCode.OK) { string urlString = GeneratePresignedURL(s3Client, bucketName, FileInput.FileName); - Console.WriteLine(urlString); + using var dbContext = DbFactory.CreateDbContext(); + var user = await UserAccessor.GetRequiredUserAsync(HttpContext); + var currentUser = await dbContext.Users.Where(u => u.Id == user.Id).FirstAsync(); + var newMusic = new MusicFile() + { + Id = Guid.NewGuid(), + User = currentUser, + Title = FileInput.FileName, + S3Url = urlString, + Expires = DateTime.UtcNow.AddHours(12), + }; + dbContext.Entry(currentUser!).State = EntityState.Unchanged; + dbContext.MusicFiles.Add(newMusic); } - NavigationManager.NavigateTo("music"); + NavigationManager.NavigateTo("/music"); } catch (Exception x) {