Added subscription ui for user

This commit is contained in:
Marcel Baumgartner
2023-04-04 01:23:07 +02:00
parent 2298bab71e
commit bb7be3b820
8 changed files with 329 additions and 42 deletions

View File

@@ -3,10 +3,15 @@
@using Moonlight.App.Database.Entities
@using Moonlight.App.Repositories
@using BlazorTable
@using Moonlight.App.Services.Interop
@inject SmartTranslateService SmartTranslateService
@inject SubscriptionRepository SubscriptionRepository
@inject SubscriptionAdminService SubscriptionAdminService
@inject AlertService AlertService
@inject ClipboardService ClipboardService
<OnlyAdmin>
<div class="card">
<LazyLoader @ref="LazyLoader" Load="Load">
@@ -36,9 +41,16 @@
</a>
</Template>
</Column>
<Column TableItem="Subscription" Title="@(SmartTranslateService.Translate("Manage"))" Field="@(x => x.Id)" Sortable="false" Filterable="false">
<Column TableItem="Subscription" Title="" Field="@(x => x.Id)" Sortable="false" Filterable="false">
<Template>
<DeleteButton Confirm="true" OnClick="() => Delete(context)" />
<div class="float-end">
<WButton Text="@(SmartTranslateService.Translate("Create code"))"
WorkingText="@(SmartTranslateService.Translate("Working"))"
CssClasses="btn-primary"
OnClick="() => GenerateCode(context)">
</WButton>
<DeleteButton Confirm="true" OnClick="() => Delete(context)"/>
</div>
</Template>
</Column>
<Pager ShowPageNumber="true" ShowTotalCount="true"/>
@@ -66,7 +78,24 @@
private async Task Delete(Subscription subscription)
{
SubscriptionRepository.Delete(subscription);
await LazyLoader.Reload();
}
private async Task GenerateCode(Subscription subscription)
{
var durationText = await AlertService.Text(
SmartTranslateService.Translate("Duration"),
SmartTranslateService.Translate("Enter duration of subscription"),
"30"
);
if (int.TryParse(durationText, out int duration))
{
var code = await SubscriptionAdminService.GenerateCode(subscription, duration);
await ClipboardService.Copy(code);
await AlertService.Success(SmartTranslateService.Translate("Copied code to clipboard"));
}
}
}

View File

@@ -0,0 +1,112 @@
@page "/profile/subscriptions"
@using Moonlight.Shared.Components.Navigations
@using Moonlight.App.Services
@using Moonlight.App.Database.Entities
@using Moonlight.App.Helpers
@using Moonlight.App.Services.Interop
@inject ConfigService ConfigService
@inject AlertService AlertService
@inject SubscriptionService SubscriptionService
@inject SmartTranslateService SmartTranslateService
<ProfileNavigation Index="2"/>
<div class="card mb-3">
<div class="row g-0">
<div class="col-md-4 p-10">
<img src="/assets/media/svg/subscription.svg" class="img-fluid rounded-start" alt="Subscription">
</div>
<div class="col-md-8">
<div class="card-body">
<LazyLoader @ref="LazyLoader" Load="Load">
@if (Subscription == null)
{
var config = ConfigService
.GetSection("Moonlight")
.GetSection("Subscriptions")
.GetSection("Sellpass");
var enableSellpass = config.GetValue<bool>("Enable");
var url = config.GetValue<string>("Url");
<h3 class="mb-2">
<div class="input-group mb-3">
<input @bind="Code" type="text" class="form-control" placeholder="@(SmartTranslateService.Translate("Enter code"))">
<WButton Text="@(SmartTranslateService.Translate("Submit"))"
WorkingText="@(SmartTranslateService.Translate("Working"))"
CssClasses="btn btn-primary"
OnClick="OnSubmit">
</WButton>
</div>
</h3>
if (enableSellpass)
{
<div class="d-flex justify-content-end pb-0 px-0">
<a href="@(url)" class="btn btn-light">Buy subscription</a>
</div>
}
}
else
{
var d = User.SubscriptionSince.AddDays(User.SubscriptionDuration).ToUniversalTime();
<h3 class="mb-2">
<TL>Active until</TL> @(Formatter.FormatDateOnly(d))
</h3>
<p class="fs-5 text-gray-600 fw-semibold">
<TL>Current subscription</TL>: @(Subscription.Name)
</p>
<p class="fs-6 text-gray-600 fw-semibold">
@(Subscription.Description)
</p>
<p class="fs-7 text-gray-600 fw-semibold">
<TL>We will send you a notification upon subscription expiration</TL>
</p>
<div class="d-flex justify-content-end pb-0 px-0">
<WButton Text="@(SmartTranslateService.Translate("Cancel"))"
WorkingText="@(SmartTranslateService.Translate("Working"))"
CssClasses="btn btn-light"
OnClick="Cancel">
</WButton>
</div>
}
</LazyLoader>
</div>
</div>
</div>
</div>
@code
{
[CascadingParameter]
public User User { get; set; }
private Subscription? Subscription;
private LazyLoader LazyLoader;
private string Code = "";
private async Task Load(LazyLoader arg)
{
Subscription = await SubscriptionService.GetCurrent();
}
private async Task Cancel()
{
if (await AlertService.ConfirmMath())
{
await SubscriptionService.Cancel();
await LazyLoader.Reload();
}
}
private async Task OnSubmit()
{
await SubscriptionService.ApplyCode(Code);
Code = "";
await LazyLoader.Reload();
}
}

View File

@@ -1,14 +1,20 @@
@page "/servers/new"
@page "/servers/create"
@using Moonlight.App.Services
@using Moonlight.App.Database.Entities
@using Moonlight.App.Models.Forms
@using Moonlight.App.Models.Misc
@using Moonlight.App.Repositories
@using Moonlight.App.Repositories.Servers
@using Microsoft.EntityFrameworkCore
@using Moonlight.App.Exceptions
@inject SubscriptionService SubscriptionService
@inject ImageRepository ImageRepository
@inject SmartTranslateService SmartTranslateService
@inject SmartDeployService SmartDeployService
@inject ServerRepository ServerRepository
@inject NavigationManager NavigationManager
@inject ServerService ServerService
<LazyLoader Load="Load">
@if (DeployNode == null)
@@ -133,6 +139,9 @@
@code
{
[CascadingParameter]
public User User { get; set; }
private Node? DeployNode;
private Subscription? Subscription;
@@ -163,12 +172,55 @@
if (limit.Amount > 0)
{
Images.Add(image, limit);
var serversCount = ServerRepository
.Get()
.Include(x => x.Owner)
.Include(x => x.Image)
.Where(x => x.Owner.Id == User.Id)
.Count(x => x.Image.Id == image.Id);
if(serversCount < limit.Amount)
Images.Add(image, limit);
}
}
}
private async Task OnValidSubmit()
{
var limit = await SubscriptionService.GetLimit("image." + Model.Image.Id);
if (limit.Amount > 0)
{
var serversCount = ServerRepository
.Get()
.Include(x => x.Owner)
.Include(x => x.Image)
.Where(x => x.Owner.Id == User.Id)
.Count(x => x.Image.Id == Model.Image.Id);
if (serversCount < limit.Amount)
{
if(int.TryParse(limit.ReadValue("cpu"), out int cpu) &&
int.TryParse(limit.ReadValue("memory"), out int memory) &&
int.TryParse(limit.ReadValue("disk"), out int disk))
{
var server = await ServerService.Create(
Model.Name,
cpu,
memory,
disk,
User,
Model.Image,
DeployNode
);
NavigationManager.NavigateTo($"/server/{server.Uuid}");
}
else
{
throw new DisplayException("Limits cannot be parsed");
}
}
}
}
}