structure app by rest api standards

add getusers
tidy up BusinessException
This commit is contained in:
Alex Bezdieniezhnykh
2024-11-14 22:45:36 +02:00
parent ace57eaf27
commit 4bc76bbbac
12 changed files with 182 additions and 37 deletions
+39 -10
View File
@@ -1,20 +1,49 @@
namespace Azaion.Common;
using System.ComponentModel;
using Azaion.Common.Extensions;
public class BusinessException(ExceptionEnum exEnum, string message) : Exception(message)
namespace Azaion.Common;
public class BusinessException(ExceptionEnum exEnum) : Exception(GetMessage(exEnum))
{
private static readonly Dictionary<ExceptionEnum, string> ExceptionDescriptions;
static BusinessException()
{
ExceptionDescriptions = EnumExtensions.GetDescriptions<ExceptionEnum>();
}
private ExceptionEnum ExceptionEnum { get; set; } = exEnum;
public static string GetMessage(ExceptionEnum exEnum) => ExceptionDescriptions.GetValueOrDefault(exEnum) ?? exEnum.ToString();
}
public enum ExceptionEnum
{
NoUserFound = 10,
NoUser = 15,
UserExists = 20,
PasswordIncorrect = 30,
UserLengthIncorrect = 33,
WrongEmail = 35,
PasswordLengthIncorrect = 37,
[Description("No such email found.")]
NoEmailFound = 10,
[Description("Email already exists.")]
EmailExists = 20,
[Description("Passwords do not match.")]
WrongPassword = 30,
[Description("Password should be at least 8 characters.")]
PasswordLengthIncorrect = 32,
EmailLengthIncorrect = 35,
WrongEmail = 37,
[Description("Hardware mismatch! You are not authorized to access this resource from this hardware.")]
HardwareIdMismatch = 40,
[Description("Hardware Id should be at least 8 characters.")]
HardwareIdLength = 45,
[Description("Wrong resource type.")]
WrongResourceType = 50,
NoFile = 60
[Description("No file provided.")]
NoFileProvided = 60,
}
@@ -1,4 +1,5 @@
using Azaion.Common.Entities;
using Azaion.Common.Extensions;
using LinqToDB;
using LinqToDB.Mapping;
@@ -0,0 +1,54 @@
using System.ComponentModel;
using System.Reflection;
namespace Azaion.Common.Extensions;
/// <summary>Enum extensions</summary>
public static class EnumExtensions
{
/// <summary>Get all enums with descriptions </summary>
public static Dictionary<T, string> GetDescriptions<T>() where T : Enum =>
Enum.GetValues(typeof(T)).Cast<T>()
.ToDictionary(x => x, x => x.GetEnumAttrib<T, DescriptionAttribute>()?.Description ?? x.ToString());
/// <summary>
/// Get the Description from the DescriptionAttribute.
/// </summary>
/// <param name="enumValue"></param>
/// <returns></returns>
public static string GetDescription(this Enum enumValue)
{
return enumValue.GetType()
.GetMember(enumValue.ToString())
.First()
.GetCustomAttribute<DescriptionAttribute>()?
.Description ?? enumValue.ToString();
}
/// <summary> Get attribute for enum's member, usually is used for getting Description attribute </summary>
public static TAttrib GetEnumAttrib<T, TAttrib>(this T value) where T: Enum
{
var field = value.GetType().GetField(value.ToString());
if (field == null)
return default;
return field.GetCustomAttributes(typeof(TAttrib), false)
.Cast<TAttrib>()
.FirstOrDefault();
}
/// <summary>
/// Get default value for enum
/// </summary>
/// <typeparam name="TEnum"></typeparam>
/// <returns></returns>
public static TEnum GetDefaultValue<TEnum>() where TEnum : struct
{
var t = typeof(TEnum);
var attributes = (DefaultValueAttribute[])t.GetCustomAttributes(typeof(DefaultValueAttribute), false);
if (attributes is { Length: > 0 })
return (TEnum)attributes[0].Value!;
return default;
}
}
@@ -0,0 +1,27 @@
using System.Linq.Expressions;
namespace Azaion.Common.Extensions;
public static class QueryableExtensions
{
/// <summary>
/// Adds Where true predicate only if result of condition is true.
/// If false predicate provided, uses it in case of false result
/// Useful for filters, when filters should be applied only when it was set (not NULL)
/// </summary>
public static IQueryable<TSource> WhereIf<TSource>(this IQueryable<TSource> query, bool? condition,
Expression<Func<TSource, bool>> truePredicate,
Expression<Func<TSource, bool>>? falsePredicate = null)
{
if (!condition.HasValue)
return query;
if (condition.Value)
return query.Where(truePredicate);
return falsePredicate != null
? query.Where(falsePredicate)
: query;
}
}
@@ -1,6 +1,6 @@
using System.Text;
namespace Azaion.Common;
namespace Azaion.Common.Extensions;
public static class StringExtensions
{
+11 -2
View File
@@ -15,9 +15,18 @@ public class GetResourceRequestValidator : AbstractValidator<GetResourceRequest>
public GetResourceRequestValidator()
{
RuleFor(r => r.Password)
.MinimumLength(8).WithErrorCode(ExceptionEnum.PasswordLengthIncorrect.ToString()).WithMessage("Password should be at least 8 characters.");
.MinimumLength(8)
.WithErrorCode(ExceptionEnum.PasswordLengthIncorrect.ToString())
.WithMessage(_ => BusinessException.GetMessage(ExceptionEnum.PasswordLengthIncorrect));
RuleFor(r => r.HardwareId)
.NotEmpty().WithErrorCode(ExceptionEnum.HardwareIdMismatch.ToString()).WithMessage("Hardware Id should be not empty.");
.MinimumLength(8)
.WithErrorCode(ExceptionEnum.HardwareIdLength.ToString())
.WithMessage(_ => BusinessException.GetMessage(ExceptionEnum.HardwareIdLength));
RuleFor(r => r.ResourceEnum)
.NotEqual(ResourceEnum.None)
.WithErrorCode(ExceptionEnum.WrongResourceType.ToString())
.WithMessage(_ => BusinessException.GetMessage(ExceptionEnum.WrongResourceType));
}
}
@@ -15,7 +15,7 @@ public class RegisterUserValidator : AbstractValidator<RegisterUserRequest>
public RegisterUserValidator()
{
RuleFor(r => r.Email)
.MinimumLength(8).WithErrorCode(ExceptionEnum.UserLengthIncorrect.ToString()).WithMessage("Email address should be at least 8 characters.")
.MinimumLength(8).WithErrorCode(ExceptionEnum.EmailLengthIncorrect.ToString()).WithMessage("Email address should be at least 8 characters.")
.EmailAddress().WithErrorCode(ExceptionEnum.WrongEmail.ToString()).WithMessage("Email address is not valid.");
RuleFor(r => r.Password)