change to static Services

pull/1/head
DJh2o2 2023-01-26 17:04:00 +07:00
parent 780aa780f2
commit 4d8cf6facc
6 changed files with 40 additions and 89 deletions

@ -6,7 +6,6 @@
@using System.Diagnostics
@inject GenericController genericController
@inject QuoteHandling QuoteHandling
@inject NavigationManager navigationManager
<h1>Create New Quote</h1>
@ -94,7 +93,8 @@
<h2>Create Quote</h2>
<Button Color="Color.Primary" Clicked="@OnCreateTex">Create Tex from Quote</Button>
<Button Color="Color.Secondary" Clicked="@OnCreatePdf">Create PDF from Tex</Button>
<Button Color="Color.Secondary" Disabled Clicked="@OnOpenPdf">Open PDF</Button>
<Button Color="Color.Secondary" Disabled="@pdfNotReady" Clicked="@OnOpenPdf">Open PDF with App</Button>
<Button Color="Color.Secondary" Disabled="@pdfNotReady" To="@url" Target="Target.Blank">Open PDF in new tab</Button>
<Button Color="Color.Success" Clicked="@OnSave">Save</Button>
<Button Color="Color.Danger" Clicked="@OnCancel">Cancel</Button>
@ -104,7 +104,7 @@
<MemoEdit ReadOnly Size="Size.Small" Autosize @bind-Text="@quote.Tex"/>
}
@if (readyPdf)
@if (!pdfNotReady)
{
<h3>PDF</h3>
<iframe src="@url" style="width:750px;height:750px;" type="application/pdf"></iframe>
@ -118,7 +118,7 @@
private LineItem? selectedLineItem;
private CustomDescription? customDescriptionOfSelectedLineItem;
private readonly CultureInfo cultureInfo = new("de-DE");
private bool readyPdf;
private bool pdfNotReady = true;
private string? url;
protected override Task OnParametersSetAsync()
@ -179,6 +179,7 @@
{
selectedContact = sC;
quote.Recipient = selectedContact;
Debug.WriteLine($"New selected contact: {selectedContact.LastName}");
return Task.CompletedTask;
}
@ -199,10 +200,11 @@
return Task.CompletedTask;
}
private void OnCreatePdf()
private void OnCreatePdf()
{
readyPdf = PdfService.CreatePdf(quote);
url = $"..{Path.DirectorySeparatorChar}Quotes{Path.DirectorySeparatorChar}{quote.Recipient.Account.AccountName}{Path.DirectorySeparatorChar}{DateTime.Today.Year}-{quote.Recipient.LastName}-{quote.Description}{Path.DirectorySeparatorChar}{quote.QuotationNumber}.pdf";
if (PdfService.CreatePdf(quote))
pdfNotReady = false;
url = $"wwwroot{Path.DirectorySeparatorChar}Quotes{Path.DirectorySeparatorChar}{quote.Recipient.Account.AccountName}{Path.DirectorySeparatorChar}{DateTime.Today.Year}-{quote.Recipient.LastName}-{quote.Description}{Path.DirectorySeparatorChar}{quote.QuotationNumber}.pdf";
}
private void OnOpenPdf() => PdfService.OpenPdf(quote);

@ -18,7 +18,6 @@ builder.Services.AddServerSideBlazor();
builder.Services.AddScoped<GremlinDb>();
builder.Services.AddScoped<GenericController>();
builder.Services.AddScoped<QuoteHandling>();
builder.Services.AddBlazorise(options => { options.Immediate = true; }).AddBootstrapProviders().AddFontAwesomeIcons();
builder.Services.AddOptions();

@ -16,9 +16,7 @@ namespace Gremlin_BlazorServer.Services
Assembly assembly = Assembly.GetExecutingAssembly();
// Format: "{Namespace}.{Folder}.{filename}.{Extension}"
string resourcePath = name.StartsWith(nameof(Gremlin_BlazorServer))
? name
: assembly.GetManifestResourceNames().Single(str => str.EndsWith(name));
string resourcePath = name.StartsWith(nameof(Gremlin_BlazorServer)) ? name : assembly.GetManifestResourceNames().Single(str => str.EndsWith(name));
using Stream? stream = assembly.GetManifestResourceStream(resourcePath);
if (stream == null) { return ""; }
@ -44,17 +42,17 @@ namespace Gremlin_BlazorServer.Services
// return _filepath;
// }
internal static void OpenFile(string path, string file, string type)
{
try
{
_ = Process.Start(path == "" ? $"{file}.{type}" : $"{path}{Path.DirectorySeparatorChar}{file}.{type}");
}
catch (Exception ex)
{
Debug.WriteLine(ex);
}
}
// internal static void OpenFile(string path, string file, string type)
// {
// try
// {
// _ = Process.Start(path == "" ? $"{file}.{type}" : $"{path}{Path.DirectorySeparatorChar}{file}.{type}");
// }
// catch (Exception ex)
// {
// Debug.WriteLine(ex);
// }
// }
public static Encoding GetEncoding(string fileName)
{

@ -7,72 +7,18 @@ using System.Text;
namespace Gremlin_BlazorServer.Services
{
public class QuoteHandling
public static class QuoteHandling
{
private static GremlinDb gremlinDb = new();
public QuoteHandling(GremlinDb gC)
public static StringBuilder CreateTex(Quote quote)
{
gremlinDb = gC;
}
private readonly TexService texService = new();
public void ConfigureServices(IServiceCollection services) => services.AddDbContext<GremlinDb>(ServiceLifetime.Scoped);
public async Task<List<Quote>> GetAllQuotesAsync()
{
return await gremlinDb.Quotes.ToListAsync();
}
public async Task<List<LineItem>> GetLineItemsAsync(Quote quote)
{
return await gremlinDb.LineItems.Where(lI => lI.QuoteId == quote.QuoteId).ToListAsync();
}
public async Task<bool> InsertQuoteAsync(Quote quote)
{
try
{
await gremlinDb.Quotes.AddAsync(quote);
await gremlinDb.SaveChangesAsync();
return true;
}
catch (Exception exception)
{
Debug.WriteLine(exception.Message);
return false;
}
}
public async Task<Quote> GetQuoteAsync(uint quoteId)
{
return await gremlinDb.Quotes.FirstOrDefaultAsync(q => q.QuoteId.Equals(quoteId)) ?? new Quote();
}
public async Task<bool> UpdateQuoteAsync(Quote quote)
{
gremlinDb.Quotes.Update(quote);
await gremlinDb.SaveChangesAsync();
return true;
}
public async Task<bool> DeleteQuoteAsync(Quote quote)
{
gremlinDb.Remove(quote);
await gremlinDb.SaveChangesAsync();
return true;
}
public StringBuilder CreateTex(Quote quote)
{
StringBuilder texString = texService.CreateTex(quote);
StringBuilder texString = TexService.CreateTex(quote);
Debug.WriteLine(texString.Length > 0 ? "Creating TexFile succesfully." : "Error during TexFile creation!");
return texString;
}
//FromWindowsGremlin
public Quote ReadLineItems(Quote quote, string clipboard)
public static Quote ReadLineItems(Quote quote, string clipboard)
{
try
{
@ -91,6 +37,8 @@ namespace Gremlin_BlazorServer.Services
foreach (LineItem lineItem in quote.LineItems)
{
if (lineItem.OptionNumber == null) break;
if (lineItem.OptionNumber.StartsWith("8D"))
{
quote.Warranty = int.Parse(lineItem.OptionNumber.Last().ToString()) * 12;

@ -4,19 +4,22 @@ using static Gremlin_BlazorServer.Data.EntityClasses.Enums;
namespace Gremlin_BlazorServer.Services
{
public class TexService
public abstract class TexService
{
private readonly GenericController genericController = new();
private static GenericController genericController = new();
public StringBuilder CreateTex(Quote quote)
public static StringBuilder? CreateTex(Quote quote)
{
StringBuilder texStringBuilder = CreateTexFile(quote);
StringBuilder? texStringBuilder = CreateTexFile(quote);
if (texStringBuilder == null) return null;
string correctedTex = Replace(texStringBuilder.ToString());
return new(correctedTex);
}
private static StringBuilder CreateBriefkopf(Contact recipient, bool tex = false)
private static StringBuilder? CreateBriefkopf(Contact recipient, bool tex = false)
{
if (recipient.Account?.AccountName == null) return null;
StringBuilder briefkopf = new();
_ = recipient.Gender == (byte)Gender.Male
@ -43,8 +46,9 @@ namespace Gremlin_BlazorServer.Services
return briefkopf;
}
private StringBuilder CreateTexFile(Quote quote)
private static StringBuilder? CreateTexFile(Quote quote)
{
if (quote.Recipient == null || quote.LineItems == null) return null;
const string rand = "2"; //RUSettingModel.GetSettingValue(Properties.Settings.Default.userSettingID, "texRand");
StringBuilder texFile = new("\\documentclass[a4paper,ngerman,parskip,10pt]{scrlttr2}\n"
@ -220,7 +224,7 @@ namespace Gremlin_BlazorServer.Services
return texFile;
}
private string CreateRbDisclaimer(Quote quote)
private static string CreateRbDisclaimer(Quote quote)
{
Random r = new();
@ -276,7 +280,7 @@ namespace Gremlin_BlazorServer.Services
return dreipp;
}
private string GetCoverletterRow(LineItem lineItem)
private static string GetCoverletterRow(LineItem lineItem)
{
CustomDescription? customDescription = genericController.Get<CustomDescription>(cD => cD.ProductNumber.Equals(lineItem.ProductNumber) && cD.OptionNumber.Equals(lineItem.OptionNumber));
return customDescription == null
@ -286,7 +290,7 @@ namespace Gremlin_BlazorServer.Services
: $"\\item {customDescription.CoverletterText} (\\#{lineItem.Position})\n";
}
private string CreateCoverletter(Quote quote)
private static string CreateCoverletter(Quote quote)
{
bool subitem = false;
string coverLetter = $"nachfolgend erhalten Sie Ihr gewünschtes Angebot über ein(e) {quote.Description}.\\\\\n" + "Es umfasst im Einzelnen:\n" + "\\begin{itemize}\n";