Skip to content

Commit

Permalink
Merge pull request #205 from pticostaricags/development
Browse files Browse the repository at this point in the history
Adding localization to UpdateVideoPlan. Converting to code-behind
  • Loading branch information
efonsecab authored Nov 12, 2024
2 parents aa4f41f + 0e931c4 commit af977ca
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 110 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public override string FormatErrorMessage(string name)
}

#region Resource Keys
[ResourceKey(defaultValue: "The {0} must be at least {2} and at max {1} characters long.")]
[ResourceKey(defaultValue: "The {0} must be at least {1} and at max {2} characters long.")]
public const string InvalidLengthTextKey = "InvalidLengthText";
#endregion Resource Keys
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
using FairPlayCombined.Common.GeneratorsAttributes;
using FairPlayCombined.Common.CustomAttributes;
using FairPlayCombined.Common.GeneratorsAttributes;
using System.ComponentModel.DataAnnotations;

namespace FairPlayCombined.Models.FairPlayTube.VideoPlan
{
public class UpdateVideoPlanModel : IUpdateModel
{
[Key]
public long VideoPlanId { get; set; }

[Required]
[CustomRequired]
public string? ApplicationUserId { get; set; }

[Required]
[StringLength(50)]
[CustomRequired]
[CustomStringLength(50)]
public string? VideoName { get; set; }

[Required]
[StringLength(500)]
[CustomRequired]
[CustomStringLength(500)]
public string? VideoDescription { get; set; }

[Required]
[StringLength(1000)]
[CustomRequired]
[CustomStringLength(1000)]
public string? VideoScript { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@page "/Creator/UpdateVideoPlan/{VideoPlanId:long}"
@implements IDisposable
@implements IAsyncDisposable

@attribute [Authorize(Roles = FairPlayCombined.Common.Constants.RoleName.BasicPlanUser)]

Expand All @@ -14,20 +14,12 @@
@using FairPlayTube.SharedUI.Components.Spinners
@using Google.Apis.YouTube.v3.Data


@inject IPromptGeneratorService promptGeneratorService
@inject IOpenAIService openAIService
@inject AzureVideoIndexerServiceConfiguration azureVideoIndexerServiceConfiguration
@inject IAzureVideoIndexerService azureVideoIndexerService
@inject IVideoPlanService videoPlanService
@inject IToastService toastService
@inject IUserProviderService userProviderService
@inject IYouTubeClientService youTubeClientService
@inject ILogger<CreateVideoPlan> logger
@inject NavigationManager navigationManager
<PageTitle>
@Localizer![UpdateVideoPlanTextKey]
</PageTitle>

<FluentLabel Typo="Typography.H3">
Update Video Plan
@Localizer![UpdateVideoPlanTextKey]
</FluentLabel>
<LoadingIndicator ShowSpinners="this.IsBusy"></LoadingIndicator>

Expand All @@ -38,25 +30,25 @@
<FluentValidationSummary></FluentValidationSummary>
</div>
<div>
<FluentLabel Typo="Typography.Body">Name</FluentLabel>
<FluentLabel Typo="Typography.Body">@Localizer![NameTextKey]</FluentLabel>
<FluentTextField @bind-Value="this.updateVideoPlanModel.VideoName" Maxlength="50" style="width:100%;"></FluentTextField>
</div>
<div>
<FluentAccordion>
<FluentAccordionItem Heading="Description" Expanded="true">
<FluentAccordionItem Heading="@Localizer![DescriptionTextKey]" Expanded="true">
<FluentTextArea @bind-Value="this.updateVideoPlanModel.VideoDescription" Rows="10" Maxlength="500" style="width:100%;"></FluentTextArea>
</FluentAccordionItem>
</FluentAccordion>
</div>
<div>
<FluentAccordion>
<FluentAccordionItem Heading="Video Script" Expanded="true">
<FluentAccordionItem Heading="@Localizer![VideoScriptTextKey]" Expanded="true">
<FluentTextArea @bind-Value="this.updateVideoPlanModel.VideoScript" Rows="40" Maxlength="3000" style="width:100%;"></FluentTextArea>
</FluentAccordionItem>
</FluentAccordion>
</div>
<div>
<FluentButton Type="ButtonType.Submit">Save</FluentButton>
<FluentButton Type="ButtonType.Submit">@Localizer![SaveTextKey]</FluentButton>
</div>
</FluentEditForm>

Expand All @@ -72,86 +64,4 @@
</FluentLabel>
</p>
}
</div>

@code {
[Parameter]
public long? VideoPlanId { get; set; }
[SupplyParameterFromForm]
private UpdateVideoPlanModel updateVideoPlanModel { get; set; } = new();
private readonly CancellationTokenSource cancellationTokenSource = new();
private bool IsBusy { get; set; }
private string? GeneratedYouTubeThumbnailUri { get; set; }
private string? RevisedPrompt { get; set; }
private VideoPlanModel? originalVideoPlan { get; set; }

protected override async Task OnInitializedAsync()
{
try
{
this.IsBusy = true;
StateHasChanged();
this.updateVideoPlanModel.ApplicationUserId = this.userProviderService.GetCurrentUserId();
if (this.originalVideoPlan is null)
{
this.originalVideoPlan = await this.videoPlanService.GetVideoPlanByIdAsync(this.VideoPlanId!.Value, this.cancellationTokenSource.Token);
this.updateVideoPlanModel.VideoPlanId = this.VideoPlanId.Value;
this.updateVideoPlanModel.VideoName = this.originalVideoPlan.VideoName;
this.updateVideoPlanModel.VideoDescription = this.originalVideoPlan.VideoDescription;
this.updateVideoPlanModel.VideoScript = this.originalVideoPlan.VideoScript;
}
}
catch (Exception ex)
{
this.toastService.ShowError(ex.Message);
}
finally
{
this.IsBusy = false;
StateHasChanged();
}
}

private async Task OnValidSubmitAsync()
{
try
{
this.IsBusy = true;
StateHasChanged();
await this.videoPlanService.UpdateVideoPlanAsync(
this.updateVideoPlanModel, this.cancellationTokenSource.Token);
this.toastService.ShowSuccess("Your Plan has been updated");
StateHasChanged();
var promptInfo = await this.promptGeneratorService.GetPromptCompleteInfoAsync(promptName:
"YouTubeThumbnail", cancellationToken: this.cancellationTokenSource.Token);
string prompt = $"{promptInfo!.BaseText}. Video Title: {this.updateVideoPlanModel.VideoName}. Video Description: {this.updateVideoPlanModel.VideoDescription}. Video Script: {this.updateVideoPlanModel.VideoScript}";
if (prompt.Length > 4000)
prompt = prompt.Substring(0, 4000);
var result = await this.openAIService.GenerateDallE3ImageAsync(prompt, this.cancellationTokenSource.Token);
if (result != null)
{
this.GeneratedYouTubeThumbnailUri = result!.data![0]!.url!;
this.RevisedPrompt = result!.data[0]!.revised_prompt;
this.toastService.ShowSuccess("Your thumbnail has been created");
}
else
{
this.toastService.ShowError("Your thumbnail could not be created");
}
}
catch (Exception ex)
{
this.toastService.ShowError(ex.Message);
}
finally
{
this.IsBusy = false;
StateHasChanged();
}
}

void IDisposable.Dispose()
{
this.cancellationTokenSource.Dispose();
}
}
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
using FairPlayCombined.Interfaces.Common;
using FairPlayCombined.Interfaces.FairPlayTube;
using FairPlayCombined.Interfaces;
using FairPlayCombined.Models.AzureVideoIndexer;
using FairPlayCombined.Models.FairPlayTube.VideoPlan;
using Microsoft.AspNetCore.Components;
using Microsoft.Extensions.Logging;
using Microsoft.FluentUI.AspNetCore.Components;
using FairPlayCombined.Common.CustomAttributes;
using Microsoft.Extensions.Localization;

namespace FairPlayTube.SharedUI.Components.Pages.Creator
{
public partial class UpdateVideoPlan
{
[Parameter]
public long? VideoPlanId { get; set; }
[SupplyParameterFromForm]
private UpdateVideoPlanModel updateVideoPlanModel { get; set; } = new();
[Inject] IPromptGeneratorService? PromptGeneratorService { get; set; }
[Inject] IOpenAIService? OpenAIService { get; set; }
[Inject] IAzureVideoIndexerService? AzureVideoIndexerService { get; set; }
[Inject] IVideoPlanService? VideoPlanService { get; set; }
[Inject] IToastService? ToastService { get; set; }
[Inject] IUserProviderService? UserProviderService { get; set; }
[Inject] IYouTubeClientService? YouTubeClientService { get; set; }
[Inject] ILogger<CreateVideoPlan>? Logger { get; set; }
[Inject] NavigationManager? NavigationManager { get; set; }
[Inject] AzureVideoIndexerServiceConfiguration? AzureVideoIndexerServiceConfiguration { get; set; }
[Inject] IStringLocalizer<UpdateVideoPlan>? Localizer { get; set; }
private readonly CancellationTokenSource cancellationTokenSource = new();
private bool IsBusy { get; set; }
private string? GeneratedYouTubeThumbnailUri { get; set; }
private string? RevisedPrompt { get; set; }
private VideoPlanModel? originalVideoPlan { get; set; }

protected override async Task OnInitializedAsync()
{
try
{
this.IsBusy = true;
StateHasChanged();
this.updateVideoPlanModel.ApplicationUserId = this.UserProviderService!.GetCurrentUserId();
if (this.originalVideoPlan is null)
{
this.originalVideoPlan = await this.VideoPlanService!.GetVideoPlanByIdAsync(this.VideoPlanId!.Value, this.cancellationTokenSource.Token);
this.updateVideoPlanModel.VideoPlanId = this.VideoPlanId.Value;
this.updateVideoPlanModel.VideoName = this.originalVideoPlan.VideoName;
this.updateVideoPlanModel.VideoDescription = this.originalVideoPlan.VideoDescription;
this.updateVideoPlanModel.VideoScript = this.originalVideoPlan.VideoScript;
}
}
catch (Exception ex)
{
this.ToastService!.ShowError(ex.Message);
}
finally
{
this.IsBusy = false;
StateHasChanged();
}
}

private async Task OnValidSubmitAsync()
{
try
{
this.IsBusy = true;
StateHasChanged();
await this.VideoPlanService!.UpdateVideoPlanAsync(
this.updateVideoPlanModel, this.cancellationTokenSource.Token);
this.ToastService!.ShowSuccess(Localizer![PlanUpdatedTextKey]);
StateHasChanged();
var promptInfo = await this.PromptGeneratorService!.GetPromptCompleteInfoAsync(promptName:
"YouTubeThumbnail", cancellationToken: this.cancellationTokenSource.Token);
string prompt = $"{promptInfo!.BaseText}. Video Title: {this.updateVideoPlanModel.VideoName}. Video Description: {this.updateVideoPlanModel.VideoDescription}. Video Script: {this.updateVideoPlanModel.VideoScript}";
if (prompt.Length > 4000)
prompt = prompt.Substring(0, 4000);
var result = await this.OpenAIService!.GenerateDallE3ImageAsync(prompt, this.cancellationTokenSource.Token);
if (result != null)
{
this.GeneratedYouTubeThumbnailUri = result!.data![0]!.url!;
this.RevisedPrompt = result!.data[0]!.revised_prompt;
this.ToastService!.ShowSuccess(Localizer![ThumbnailCreatedTextKey]);
}
else
{
this.ToastService!.ShowError(Localizer![ThumbnailWasNotCreatedTextKey]);
}
}
catch (Exception ex)
{
this.ToastService!.ShowError(ex.Message);
}
finally
{
this.IsBusy = false;
StateHasChanged();
}
}

public async ValueTask DisposeAsync()
{
await this.cancellationTokenSource.CancelAsync();
this.cancellationTokenSource.Dispose();
}

#region Resource Keys
[ResourceKey(defaultValue: "Update Video Plan")]
public const string UpdateVideoPlanTextKey = "UpdateVideoPlanText";
[ResourceKey(defaultValue: "Name")]
public const string NameTextKey = "NameText";
[ResourceKey(defaultValue: "Description")]
public const string DescriptionTextKey = "DescriptionText";
[ResourceKey(defaultValue: "Video Script")]
public const string VideoScriptTextKey = "VideoScriptText";
[ResourceKey(defaultValue: "Save")]
public const string SaveTextKey = "SaveText";
[ResourceKey(defaultValue: "Your Plan has been updated")]
public const string PlanUpdatedTextKey = "PlanUpdatedText";
[ResourceKey(defaultValue: "Your thumbnail has been created")]
public const string ThumbnailCreatedTextKey = "ThumbnailCreatedTextKey";
[ResourceKey(defaultValue: "Your thumbnail could not be created")]
public const string ThumbnailWasNotCreatedTextKey = "ThumbnailWasNotCreatedText";
#endregion Resource Keys
}
}

0 comments on commit af977ca

Please sign in to comment.