|
5 | 5 | @using BookStore.Shared.Models |
6 | 6 | @using Microsoft.AspNetCore.Components.Authorization |
7 | 7 | @using BookStore.Web.Components.Catalog |
| 8 | +@using BookStore.Web.Components.Pages.Admin |
8 | 9 | @inject IBooksClient BooksClient |
9 | 10 | @inject IShoppingCartClient CartClient |
10 | 11 | @inject NavigationManager Navigation |
|
53 | 54 | <MudBreadcrumbs Items="_breadcrumbItems" Class="mb-4"/> |
54 | 55 |
|
55 | 56 | <MudPaper Elevation="2" Class="pa-6" Style="border-radius: 12px;"> |
| 57 | + <MudStack Row="true" Justify="Justify.FlexEnd" Class="mb-2"> |
| 58 | + <AuthorizeView Roles="Admin, ADMIN"> |
| 59 | + <Authorized> |
| 60 | + <MudMenu Icon="@Icons.Material.Filled.MoreVert" |
| 61 | + AnchorOrigin="Origin.BottomRight" |
| 62 | + TransformOrigin="Origin.TopRight" |
| 63 | + Dense="true" |
| 64 | + aria-label="Book actions"> |
| 65 | + @if (book!.IsDeleted) |
| 66 | + { |
| 67 | + <MudMenuItem Icon="@Icons.Material.Filled.Restore" OnClick="RestoreBookAsync">Restore</MudMenuItem> |
| 68 | + } |
| 69 | + else |
| 70 | + { |
| 71 | + <MudMenuItem Icon="@Icons.Material.Filled.Edit" OnClick="EditBookAsync">Edit</MudMenuItem> |
| 72 | + <MudMenuItem Icon="@Icons.Material.Filled.Delete" OnClick="DeleteBookAsync">Delete</MudMenuItem> |
| 73 | + } |
| 74 | + </MudMenu> |
| 75 | + </Authorized> |
| 76 | + </AuthorizeView> |
| 77 | + </MudStack> |
| 78 | + |
56 | 79 | <MudGrid> |
57 | 80 | @* Book Cover *@ |
58 | 81 | <MudItem xs="12" md="4"> |
|
287 | 310 | <MudButton Href="/" Variant="Variant.Filled" Color="Color.Primary" StartIcon="@Icons.Material.Filled.ArrowBack"> |
288 | 311 | Back to Catalog |
289 | 312 | </MudButton> |
290 | | - <AuthorizeView Roles="Admin, ADMIN"> |
291 | | - <Authorized> |
292 | | - @if (book.IsDeleted) |
293 | | - { |
294 | | - <MudButton Variant="Variant.Filled" Color="Color.Success" StartIcon="@Icons.Material.Filled.Restore" OnClick="RestoreBookAsync" Class="ml-2"> |
295 | | - Restore Book |
296 | | - </MudButton> |
297 | | - } |
298 | | - else |
299 | | - { |
300 | | - <MudButton Variant="Variant.Filled" Color="Color.Error" StartIcon="@Icons.Material.Filled.Delete" OnClick="DeleteBookAsync" Class="ml-2"> |
301 | | - Delete Book |
302 | | - </MudButton> |
303 | | - } |
304 | | - </Authorized> |
305 | | - </AuthorizeView> |
306 | 313 | </MudStack> |
307 | 314 | } |
308 | 315 | </MudContainer> |
|
563 | 570 | } |
564 | 571 | } |
565 | 572 |
|
| 573 | + private async Task EditBookAsync() |
| 574 | + { |
| 575 | + var currentBook = book; |
| 576 | + if (currentBook == null) |
| 577 | + { |
| 578 | + return; |
| 579 | + } |
| 580 | + |
| 581 | + try |
| 582 | + { |
| 583 | + var adminBook = await BooksClient.GetBookAdminAsync(currentBook.Id, _cts.Token); |
| 584 | + |
| 585 | + var parameters = new DialogParameters<AddBookDialog> |
| 586 | + { |
| 587 | + { |
| 588 | + x => x.Model, new CreateBookRequest |
| 589 | + { |
| 590 | + Title = adminBook.Title, |
| 591 | + Isbn = adminBook.Isbn, |
| 592 | + Language = adminBook.Language, |
| 593 | + Translations = adminBook.Translations.ToDictionary( |
| 594 | + kvp => kvp.Key, |
| 595 | + kvp => new BookTranslationDto(kvp.Value.Description)), |
| 596 | + Prices = adminBook.Prices.ToDictionary(kvp => kvp.Key, kvp => kvp.Value), |
| 597 | + PublicationDate = adminBook.PublicationDate ?? new PartialDate(DateTimeOffset.UtcNow.Year), |
| 598 | + PublisherId = adminBook.Publisher?.Id, |
| 599 | + AuthorIds = adminBook.Authors.Select(a => a.Id).ToList(), |
| 600 | + CategoryIds = adminBook.Categories.Select(c => c.Id).ToList() |
| 601 | + } |
| 602 | + }, |
| 603 | + { x => x.IsEdit, true }, |
| 604 | + { x => x.BookId, adminBook.Id }, |
| 605 | + { x => x.ETag, adminBook.ETag }, |
| 606 | + { x => x.InitialAuthors, adminBook.Authors.ToList() }, |
| 607 | + { x => x.InitialCategories, adminBook.Categories.ToList() }, |
| 608 | + { x => x.InitialPublisher, adminBook.Publisher } |
| 609 | + }; |
| 610 | + |
| 611 | + var options = new DialogOptions { CloseOnEscapeKey = true, MaxWidth = MaxWidth.Medium, FullWidth = true }; |
| 612 | + var dialog = await DialogService.ShowAsync<AddBookDialog>("Edit Book", parameters, options); |
| 613 | + var result = await dialog.Result; |
| 614 | + if (result != null && !result.Canceled && bookQuery != null) |
| 615 | + { |
| 616 | + await bookQuery.LoadAsync(cancellationToken: _cts.Token); |
| 617 | + } |
| 618 | + } |
| 619 | + catch (Exception ex) |
| 620 | + { |
| 621 | + Snackbar.Add($"Failed to load book details for editing: {ex.Message}", Severity.Error); |
| 622 | + } |
| 623 | + } |
| 624 | + |
566 | 625 | // Inject EventsService which wasn't injected before |
567 | 626 | [Inject] private BookStoreEventsService EventsService { get; set; } = default!; |
568 | 627 |
|
|
0 commit comments