Skip to content

Commit

Permalink
Add pages for music
Browse files Browse the repository at this point in the history
  • Loading branch information
maacpiash committed Sep 24, 2024
1 parent 9d8e6de commit e475f6d
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
35 changes: 35 additions & 0 deletions src/Components/Pages/MusicFiles/List.razor
Original file line number Diff line number Diff line change
@@ -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<Blazing.Data.ApplicationDbContext> DbFactory

@if (MusicFiles is not null && MusicFiles.Any())
{
<ul class="list-group">
@foreach (var musicFile in MusicFiles)
{
<li class="card" style="margin-bottom: 10px;">
<a href="@musicFile.S3Url">@musicFile.Title</a>
</li>
}
</ul>
}

<a href="/music/upload" role="button" class="btn btn-outline-primary">Upload</a>

@code {
public IEnumerable<MusicFile>? 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();
}
}
16 changes: 14 additions & 2 deletions src/Components/Pages/MusicFiles/Upload.razor
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down

0 comments on commit e475f6d

Please sign in to comment.