added squid - refactor get state function

This commit is contained in:
FH 2022-03-24 18:40:28 +01:00
parent 0e1f64ae88
commit e61318864f
5 changed files with 201 additions and 5 deletions

View file

@ -20,7 +20,8 @@
<table class="table table-hover table-striped justify-content-center">
<thead>
<tr>
<th scope="col" style="width: 50%;">Navn</th>
<th scope="col" style="width: 40%;">Navn</th>
<th scope="col" style="width: 10%;">Fork</th>
<th scope="col" style="width: 30%;" class="text-nowrap">Varenr</th>
<th scope="col" style="width: 20%">Stk / Pris</th>
</tr>
@ -32,6 +33,7 @@
<td>
@salesItem.Name
</td>
<td>@salesItem.ShortName</td>
<td>
@salesItem.Sku
</td>

View file

@ -0,0 +1,193 @@
// ***********************************************************************
// Assembly : FCS.Lib.Utility
// Author : FH
// Created : 2020-07-01
//
// Last Modified By : FH
// Last Modified On : 02-24-2022
// ***********************************************************************
// <copyright file="Squid.cs" company="FCS">
// Copyright (C) 2022 FCS Frede's Computer Services.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the Affero GNU General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// Affero GNU General Public License for more details.
//
// You should have received a copy of the Affero GNU General Public License
// along with this program. If not, see [https://www.gnu.org/licenses/agpl-3.0.en.html]
// </copyright>
// <summary>Derived from https:github.com/csharpvitamins/CSharpVitamins.ShortGuid</summary>
// ***********************************************************************
using System.Diagnostics;
namespace Wonky.Client.Helpers
{
[DebuggerDisplay("{" + nameof(Value) + "}")]
public readonly struct Squid : IEquatable<Squid>
{
public static readonly Squid Empty = new(Guid.Empty);
public Squid(string value)
{
Value = value;
Guid = DecodeSquid(value);
}
public Squid(Guid obj)
{
Value = EncodeGuid(obj);
Guid = obj;
}
#pragma warning disable CA1720 // Identifier contains type name
public Guid Guid { get; }
#pragma warning restore CA1720 // Identifier contains type name
public string Value { get; }
public override string ToString()
{
return Value;
}
public override bool Equals(object obj)
{
return obj is Squid other && Equals(other);
}
public bool Equals(Squid obj)
{
return Guid.Equals(obj.Guid) && Value == obj.Value;
}
public override int GetHashCode()
{
unchecked
{
return (Guid.GetHashCode() * 397) ^ (Value != null ? Value.GetHashCode() : 0);
}
}
public static Squid NewGuid()
{
return new(Guid.NewGuid());
}
public static string EncodeString(string value)
{
var guid = new Guid(value);
return EncodeGuid(guid);
}
public static string EncodeGuid(Guid obj)
{
var encoded = Convert.ToBase64String(obj.ToByteArray());
encoded = encoded
.Replace("/", "_")
.Replace("+", "-");
return encoded.Substring(0, 22);
}
public static Guid DecodeSquid(string value)
{
if (value == null) return Empty;
value = value
.Replace("_", "/")
.Replace("-", "+");
var blob = Convert.FromBase64String(value + "==");
return new Guid(blob);
}
public static Guid FromSquid(Squid obj)
{
return obj.Guid;
}
public static Squid FromString(string value)
{
if (string.IsNullOrEmpty(value))
return Empty;
return TryParse(value, out Squid obj) ? obj : Empty;
}
public static bool TryDecode(string value, out Guid obj)
{
try
{
// Decode as Squid
obj = DecodeSquid(value);
return true;
}
#pragma warning disable CA1031 // Do not catch general exception types
catch (Exception)
#pragma warning restore CA1031 // Do not catch general exception types
{
// Return empty Guid
obj = Guid.Empty;
return false;
}
}
public static bool TryParse(string value, out Squid obj)
{
// Parse as Squid string.
if (TryDecode(value, out var oGuid))
{
obj = oGuid;
return true;
}
// Parse as Guid string.
if (Guid.TryParse(value, out oGuid))
{
obj = oGuid;
return true;
}
obj = Empty;
return false;
}
public static bool TryParse(string value, out Guid obj)
{
// Try a Squid string.
if (TryDecode(value, out obj))
return true;
// Try a Guid string.
if (Guid.TryParse(value, out obj))
return true;
obj = Guid.Empty;
return false;
}
public static bool operator ==(Squid x, Squid y)
{
return x.Guid == y.Guid;
}
public static bool operator ==(Squid x, Guid y)
{
return x.Guid == y;
}
public static bool operator ==(Guid x, Squid y)
{
return y == x; // NB: order of arguments
}
public static bool operator !=(Squid x, Squid y)
{
return !(x == y);
}
public static bool operator !=(Squid x, Guid y)
{
return !(x == y);
}
public static bool operator !=(Guid x, Squid y)
{
return !(x == y);
}
public static implicit operator string(Squid oSquid)
{
return oSquid.Value;
}
public static implicit operator Guid(Squid oSquid)
{
return oSquid.Guid;
}
public static implicit operator Squid(string value)
{
if (string.IsNullOrEmpty(value))
return Empty;
return TryParse(value, out Squid oSquid) ? oSquid : Empty;
}
public static implicit operator Squid(Guid oGuid)
{
return oGuid == Guid.Empty ? Empty : new Squid(oGuid);
}
}
}

View file

@ -26,12 +26,9 @@ public static class Utils
public static string GetVisitState(string dtNextVisit)
{
if (dtNextVisit == "1970-01-01")
if (dtNextVisit == "1970-01-01" || !DateTime.TryParse(dtNextVisit, out var dtNext))
return "the-draw";
var dtNow = DateTime.Now;
var dtValid = DateTime.TryParse(dtNextVisit, out var dtNext);
if (!dtValid)
return "the-draw";
if (dtNow >= dtNext)
return "the-ugly";
return (dtNow > dtNext.AddDays(-14)) ? "the-bad" : "the-good";

View file

@ -18,6 +18,7 @@ using Wonky.Client.HttpInterceptors;
using Wonky.Client.HttpRepository;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Forms;
using Wonky.Client.Helpers;
using Wonky.Client.Models;
using Wonky.Client.Services;
using Wonky.Entity.DTO;
@ -53,6 +54,7 @@ public partial class CompanyUpdate : IDisposable
Interceptor.RegisterBeforeSendEvent();
if(!string.IsNullOrWhiteSpace(_companyDto.VatNumber))
await GetInfoFromVat(_companyDto.VatNumber);
_companyDto.CompanyId = Squid.DecodeSquid(_companyDto.CompanyId).ToString();
}
private async Task Update()

View file

@ -18,6 +18,7 @@ using System.Threading.Tasks;
using Wonky.Client.HttpInterceptors;
using Wonky.Client.HttpRepository;
using Microsoft.AspNetCore.Components;
using Wonky.Client.Helpers;
using Wonky.Client.Services;
using Wonky.Entity.DTO;
using Wonky.Entity.Models;
@ -51,6 +52,7 @@ public partial class CompanyView : IDisposable
CompanyDto = await CompanyRepo.GetCompanyById(CompanyId);
}
}
var theUgly = DateTime.Parse(CompanyDto.NextVisit);
var theBad = theUgly.AddDays(-14);
if (DateTime.Now <= theUgly)