Files
YandexMusic/PlaylistShared/Components/Pages/CreatePlaylist.razor
2026-04-11 15:41:24 +03:00

80 lines
2.7 KiB
Plaintext

@page "/createplaylist"
@attribute [Authorize]
@using System.Security.Claims
@using Microsoft.AspNetCore.Components.Authorization
@inject AuthenticationStateProvider AuthenticationStateProvider
@inject AppDbContext Db
@inject IYandexMusicService YandexService
@inject NavigationManager Navigation
<h3>Создание общего плейлиста</h3>
<EditForm Model="@model" OnValidSubmit="@HandleSubmit">
<div class="mb-3">
<label for="title">Название плейлиста</label>
<InputText id="title" class="form-control" @bind-Value="model.Title" />
<ValidationMessage For="@(() => model.Title)" />
</div>
<div class="mb-3">
<label for="description">Описание (необязательно)</label>
<InputText id="description" class="form-control" @bind-Value="model.Description" />
</div>
<button type="submit" class="btn btn-primary" disabled="@isLoading">@(isLoading ? "Создание..." : "Создать")</button>
</EditForm>
@code {
private CreateModel model = new();
private bool isLoading;
private string? userId;
public class CreateModel
{
[Required]
public string Title { get; set; } = "";
public string? Description { get; set; }
}
protected override async Task OnInitializedAsync()
{
var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
userId = authState.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
}
private async Task HandleSubmit()
{
if (string.IsNullOrEmpty(userId))
{
// пользователь не авторизован
return;
}
isLoading = true;
try
{
// 1. Создаём плейлист в Яндекс Музыке
var yandexId = await YandexService.CreatePlaylistAsync(userId, model.Title);
// 2. Сохраняем в БД
var playlist = new SharedPlaylist
{
Id = Guid.NewGuid(),
OwnerUserId = userId,
YandexPlaylistId = yandexId,
Title = model.Title,
Description = model.Description,
ShareSlug = Guid.NewGuid().ToString("N"),
CreatedAt = DateTime.UtcNow
};
Db.SharedPlaylists.Add(playlist);
await Db.SaveChangesAsync();
Navigation.NavigateTo("/myplaylists");
}
catch (Exception ex)
{
// показать ошибку (можно добавить переменную errorMessage)
}
finally
{
isLoading = false;
}
}
}