-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implemented adding a platform to a show
- Loading branch information
1 parent
d6baf52
commit eff76fe
Showing
2 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
@page "/shows/{ShowId}/add-platform" | ||
@using Microsoft.EntityFrameworkCore | ||
@using CineStream.Data | ||
|
||
@inject IDbContextFactory<CineStreamContext> DbFactory | ||
|
||
@inject NavigationManager NavManager | ||
|
||
<PageTitle>Add Platform</PageTitle> | ||
|
||
<h1><i class="bi bi-plus-circle-fill" aria-hidden="true"></i> Add Platform</h1> | ||
|
||
<div class="clr-row"> | ||
<div class="clr-col-lg-6 clr-col-12"> | ||
<Card> | ||
<CardBody> | ||
<CardBlock> | ||
<Form Model="selectedPlatform" OnValidSubmit="HandleValidSubmit" Layout="FormLayout.Vertical"> | ||
<DataAnnotationsValidator /> | ||
|
||
<InputCombobox Label="Select Value" TItem="Platform" Items="@AppPlatforms" | ||
@bind-SelectedItem="selectedPlatform" ItemToText="o => o?.Name" | ||
ValidationFor="() => selectedPlatform" HelpText="A value selection is required."> | ||
<ItemTemplate Context="selectedPlatform"> | ||
@selectedPlatform.Name | ||
</ItemTemplate> | ||
</InputCombobox> | ||
|
||
<Button class="mt-3" Variant="Primary" type="submit">Submit</Button> | ||
</Form> | ||
</CardBlock> | ||
</CardBody> | ||
</Card> | ||
</div> | ||
</div> | ||
|
||
@code { | ||
[Parameter] public string? ShowId { get; set; } | ||
private Platform selectedPlatform = new Platform(); | ||
|
||
protected override async Task OnInitializedAsync() | ||
{ | ||
await LoadDataAsync(); | ||
} | ||
|
||
private ICollection<Platform>? AppPlatforms { get; set; } | ||
|
||
// Get all platforms that are not already associated with the show | ||
private async Task LoadDataAsync() | ||
{ | ||
ICollection<Platform> appShowPlatforms = new List<Platform>(); | ||
|
||
using var context = DbFactory.CreateDbContext(); | ||
|
||
var allPlatforms = await context.Platforms!.ToListAsync(); | ||
var showPlatforms = await context.PlatformShows!.Where(c => c.ShowId.ToString() == ShowId).ToListAsync(); | ||
|
||
foreach (var showPlatform in showPlatforms) | ||
{ | ||
var platform = await context.Platforms!.FirstOrDefaultAsync(c => c.PlatformId == showPlatform.PlatformId); | ||
if (platform != null) appShowPlatforms.Add(platform); | ||
} | ||
|
||
AppPlatforms = allPlatforms.Except(appShowPlatforms).ToList(); | ||
|
||
} | ||
|
||
private async Task HandleValidSubmit() | ||
{ | ||
using var context = await DbFactory.CreateDbContextAsync(); | ||
PlatformShow showPlatform = new PlatformShow(); | ||
showPlatform.PlatformId = selectedPlatform.PlatformId; | ||
showPlatform.ShowId = int.Parse(ShowId); | ||
showPlatform.AddedToPlatformDate = DateTime.Now; | ||
context.PlatformShows?.Add(showPlatform); | ||
await context.SaveChangesAsync(); | ||
NavManager.NavigateTo("/shows/" + ShowId); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters