Skip to content

Commit

Permalink
Added new extensions methods
Browse files Browse the repository at this point in the history
  • Loading branch information
joaofx committed Nov 17, 2023
1 parent 949a459 commit f0aac6f
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 9 deletions.
16 changes: 16 additions & 0 deletions src/Miru.Core/DecimalExtensions.cs
@@ -1,3 +1,4 @@
using System;
using System.Globalization;

namespace Miru.Core;
Expand All @@ -10,4 +11,19 @@ public static class DecimalExtensions
/// </summary>
public static string ToStringInvariant(this decimal value) =>
value.ToString(CultureInfo.InvariantCulture);

public static decimal Negative(this decimal value) =>
value > 0 ? value * -1 : value;

public static int Negative(this int value) =>
value > 0 ? value * -1 : value;

public static decimal Positive(this decimal value) =>
value > 0 ? value : value * -1;

public static decimal PercentOf(this decimal value, decimal total) =>
total != 0 ? value / total * 100 : 0;

public static decimal PercentOf(this int value, decimal total) =>
Convert.ToDecimal(value).PercentOf(total);
}
2 changes: 1 addition & 1 deletion src/Miru.Core/EnumerableExtensions.cs
Expand Up @@ -77,7 +77,7 @@ public static IEnumerable<(T item, int index)> Indexed<T>(this IEnumerable<T> se
{
return self.Select((item, index) => (item, index + startAt));
}

// TODO: uncomment when remove Baseline
// public static string Join(this string[] values, string separator) =>
// string.Join(separator, values);
Expand Down
9 changes: 4 additions & 5 deletions src/Miru/Domain/NotFoundException.cs
@@ -1,9 +1,8 @@
namespace Miru.Domain
namespace Miru.Domain;

public class NotFoundException : DomainException
{
public class NotFoundException : DomainException
public NotFoundException(string message) : base(message)
{
public NotFoundException(string message) : base(message)
{
}
}
}
7 changes: 7 additions & 0 deletions src/Miru/EnumExtensions.cs
Expand Up @@ -44,4 +44,11 @@ public static TAttribute GetAttribute<TAttribute>(this Enum enumValue)
.First()
.GetCustomAttribute<TAttribute>();
}

public static IEnumerable<T> GetFlags<T>(this T flags) where T : Enum
{
foreach (Enum value in Enum.GetValues(flags.GetType()))
if (flags.HasFlag(value))
yield return (T) value;
}
}
13 changes: 13 additions & 0 deletions src/Miru/EnumerableExtensions.cs
@@ -0,0 +1,13 @@
using System.Collections.Generic;
using System.Linq;
using Miru.Domain;

namespace Miru;

public static class EnumerableExtensions
{
public static T SingleOrFail<T>(this IEnumerable<T> enumerable, string exceptionMessage)
{
return enumerable.SingleOrDefault() ?? throw new NotFoundException(exceptionMessage);
}
}
16 changes: 16 additions & 0 deletions src/Miru/FeatureInfo.cs
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Miru.Mailing;

namespace Miru;
Expand Down Expand Up @@ -112,4 +113,19 @@ public string GetName()

return Type.Name;
}

/// <summary>
/// It looks into inspected class and also the parent for the TAttribute
/// </summary>
public static TAttribute GetFeatureAttribute<TAttribute>(object request) where TAttribute : Attribute
{
var attribute = request.GetType().GetAttribute<TAttribute>();

if (attribute != null)
{
return attribute;
}

return request.GetType().ReflectedType?.GetCustomAttribute<TAttribute>();
}
}
2 changes: 1 addition & 1 deletion src/Miru/Mailing/MailingOptions.cs
Expand Up @@ -9,7 +9,7 @@ public class MailingOptions
public string AppUrl { get; set; }
public SmtpOptions Smtp { get; set; } = new();
public string TemplatePath { get; set; }
public string QueueName { get; set; }
public string QueueName { get; set; } = "default";

public void EmailDefaults(Action<Email> defaultMail)
{
Expand Down
2 changes: 0 additions & 2 deletions src/Miru/Pipeline/PipelineBuilder.cs
@@ -1,6 +1,4 @@
using System;
using MediatR;
using Microsoft.Extensions.DependencyInjection;

namespace Miru.Pipeline;

Expand Down
15 changes: 15 additions & 0 deletions src/Miru/Queuing/JobsExtensions.cs
@@ -0,0 +1,15 @@
using MediatR;

namespace Miru.Queuing;

public static class JobsExtensions
{
public static void EnqueueIn<TJob>(
this Jobs jobs,
TimeSpan startIn,
TJob job,
string queue = "default") where TJob : IBaseRequest
{
jobs.Enqueue(job, startIn: startIn, queue);
}
}

0 comments on commit f0aac6f

Please sign in to comment.