Merge pull request #54 from Moonlight-Panel/WebsiteOrder

Implemented website order
This commit is contained in:
Marcel Baumgartner 2023-04-11 00:12:43 +02:00 committed by GitHub
commit 89892e8c37
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 180 additions and 10 deletions

View file

@ -4,7 +4,7 @@ namespace Moonlight.App.Models.Forms;
public class WebsiteDataModel
{
[Required(ErrorMessage = "You need a domain")]
[Required(ErrorMessage = "You need to enter a domain")]
[RegularExpression(@"([a-z0-9|-]+\.)*[a-z0-9|-]+\.[a-z]+", ErrorMessage = "You need to enter a valid domain")]
public string BaseDomain { get; set; } = "";
}

View file

@ -0,0 +1,10 @@
using System.ComponentModel.DataAnnotations;
namespace Moonlight.App.Models.Forms;
public class WebsiteOrderDataModel
{
[Required(ErrorMessage = "You need to enter a domain")]
[RegularExpression(@"([a-z0-9|-]+\.)*[a-z0-9|-]+\.[a-z]+", ErrorMessage = "You need to enter a valid domain")]
public string BaseDomain { get; set; } = "";
}

View file

@ -6,12 +6,18 @@ namespace Moonlight.App.Services;
public class SmartDeployService
{
private readonly NodeRepository NodeRepository;
private readonly PleskServerRepository PleskServerRepository;
private readonly WebsiteService WebsiteService;
private readonly NodeService NodeService;
public SmartDeployService(NodeRepository nodeRepository, NodeService nodeService)
public SmartDeployService(
NodeRepository nodeRepository,
NodeService nodeService, PleskServerRepository pleskServerRepository, WebsiteService websiteService)
{
NodeRepository = nodeRepository;
NodeService = nodeService;
PleskServerRepository = pleskServerRepository;
WebsiteService = websiteService;
}
public async Task<Node?> GetNode()
@ -32,6 +38,21 @@ public class SmartDeployService
return data.MaxBy(x => x.Value).Key;
}
public async Task<PleskServer?> GetPleskServer()
{
var result = new List<PleskServer>();
foreach (var pleskServer in PleskServerRepository.Get().ToArray())
{
if (await WebsiteService.IsHostUp(pleskServer))
{
result.Add(pleskServer);
}
}
return result.FirstOrDefault();
}
private async Task<double> GetUsageScore(Node node)
{
var score = 0;

View file

@ -27,7 +27,7 @@
<TL>No node found</TL>
</h4>
<p class="card-text">
<TL>No node found to deploy to found</TL>
<TL>No node found to deploy to</TL>
</p>
</div>
</div>
@ -36,8 +36,8 @@
else
{
<div class="d-flex flex-column flex-lg-row">
<div class="w-100 flex-lg-row-auto w-lg-300px mb-7 me-7 me-lg-10" data-select2-id="select2-data-131-dr2d">
<div class="card card-flush py-4" data-select2-id="select2-data-130-ru5y">
<div class="w-100 flex-lg-row-auto w-lg-300px mb-7 me-7 me-lg-10">
<div class="card card-flush py-4">
<div class="card-header">
<div class="card-title">
<h2>
@ -48,7 +48,7 @@
<div class="card-body pt-0">
<div class="d-flex flex-column gap-10">
<div class="fv-row">
<label class="form-label">Node</label>
<label class="form-label"><TL>Node</TL></label>
<div class="fw-bold fs-3">@(DeployNode.Name)</div>
</div>
@if (Model.Image != null)
@ -159,11 +159,9 @@
Subscription = await SubscriptionService.GetCurrent();
await lazyLoader.SetText(SmartTranslateService.Translate("Searching for deploy node"));
DeployNode = await SmartDeployService.GetNode();
await lazyLoader.SetText(SmartTranslateService.Translate("Searching for available images"));
var images = ImageRepository.Get().ToArray();
foreach (var image in images)
@ -179,7 +177,7 @@
.Where(x => x.Owner.Id == User.Id)
.Count(x => x.Image.Id == image.Id);
if(serversCount <= limit.Amount) //TODO: FIX COUNTING
if(serversCount < limit.Amount)
Images.Add(image, limit);
}
}

View file

@ -0,0 +1,138 @@
@page "/websites/create"
@using Moonlight.App.Services
@using Moonlight.App.Database.Entities
@using Moonlight.App.Models.Forms
@using Moonlight.App.Repositories
@inject SubscriptionService SubscriptionService
@inject WebsiteService WebsiteService
@inject WebsiteRepository WebsiteRepository
@inject SmartDeployService SmartDeployService
@inject SmartTranslateService SmartTranslateService
@inject NavigationManager NavigationManager
<LazyLoader Load="Load">
@if (PleskServer == null)
{
<div class="d-flex justify-content-center flex-center">
<div class="card">
<img src="/assets/media/svg/nodata.svg" class="card-img-top w-25 mx-auto pt-5" alt="Not found image"/>
<div class="card-body text-center">
<h4 class="card-title">
<TL>No plesk server found</TL>
</h4>
<p class="card-text">
<TL>No plesk server found to deploy to</TL>
</p>
</div>
</div>
</div>
}
else
{
<div class="d-flex flex-column flex-lg-row">
<div class="w-100 flex-lg-row-auto w-lg-300px mb-7 me-7 me-lg-10">
<div class="card card-flush py-4">
<div class="card-header">
<div class="card-title">
<h2>
<TL>Website details</TL>
</h2>
</div>
</div>
<div class="card-body pt-0">
<div class="d-flex flex-column gap-10">
<div class="fv-row">
<label class="form-label">
<TL>Plesk server</TL>
</label>
<div class="fw-bold fs-3">@(PleskServer.Name)</div>
</div>
@if (AllowOrder)
{
<div class="fv-row">
<label class="form-label">
<TL>Domain</TL>
</label>
<div class="fw-bold fs-3">@(Model.BaseDomain)</div>
</div>
}
</div>
</div>
</div>
</div>
<div class="d-flex flex-column flex-lg-row-fluid gap-7 gap-lg-10">
<div class="card card-flush py-4">
<div class="card-header">
<div class="card-title">
<h2>
<TL>Configure your website</TL>
</h2>
</div>
</div>
<div class="card-body pt-0">
<SmartForm Model="Model" OnValidSubmit="OnValidSubmit">
@if (AllowOrder)
{
<label class="form-label">
<TL>Domain</TL>
</label>
<div class="input-group mb-5">
<InputText @bind-Value="Model.BaseDomain" class="form-control"></InputText>
</div>
<button type="submit" class="mt-5 float-end btn btn-primary">
<TL>Create</TL>
</button>
}
else
{
<div class="alert alert-warning d-flex align-items-center p-5 mb-10">
<span>
<TL>You reached the maximum amount of websites in your subscription</TL>: @(Subscription == null ? SmartTranslateService.Translate("Default") : Subscription.Name)
</span>
</div>
}
</SmartForm>
</div>
</div>
</div>
</div>
}
</LazyLoader>
@code
{
[CascadingParameter]
public User User { get; set; }
private Subscription? Subscription;
private PleskServer? PleskServer;
private bool AllowOrder = false;
private WebsiteOrderDataModel Model = new();
private async Task Load(LazyLoader lazyLoader)
{
// Reset state
Model = new();
await lazyLoader.SetText(SmartTranslateService.Translate("Loading your subscription"));
Subscription = await SubscriptionService.GetCurrent();
await lazyLoader.SetText(SmartTranslateService.Translate("Searching for deploy plesk server"));
PleskServer = await SmartDeployService.GetPleskServer();
AllowOrder = WebsiteRepository.Get().Count() < (await SubscriptionService.GetLimit("websites")).Amount;
}
private async Task OnValidSubmit()
{
if (WebsiteRepository.Get().Count() < (await SubscriptionService.GetLimit("websites")).Amount)
{
var website = await WebsiteService.Create(Model.BaseDomain, User, PleskServer);
NavigationManager.NavigateTo($"/website/{website.Id}");
}
}
}

View file

@ -1 +0,0 @@
@page "/websites/new"

View file

@ -531,3 +531,7 @@ Month;Month
Year;Year
All time;All time
This function is not implemented;This function is not implemented
Searching for deploy plesk server;Searching for deploy plesk server
Website details;Website details
Configure your website;Configure your website
You reached the maximum amount of websites in your subscription;You reached the maximum amount of websites in your subscription