Add contact feature
This commit is contained in:
7
BeauFindlay/src/BeauFindlay.Shared/Abstractions/Error.cs
Normal file
7
BeauFindlay/src/BeauFindlay.Shared/Abstractions/Error.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
namespace BeauFindlay.Shared.Abstractions;
|
||||
|
||||
public record Error(string Code, string Message)
|
||||
{
|
||||
public static readonly Error None = new(string.Empty, string.Empty);
|
||||
public static readonly Error NullValue = new("Error.NullValue", "Null value was provided.");
|
||||
}
|
||||
66
BeauFindlay/src/BeauFindlay.Shared/Abstractions/Result.cs
Normal file
66
BeauFindlay/src/BeauFindlay.Shared/Abstractions/Result.cs
Normal file
@@ -0,0 +1,66 @@
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
|
||||
namespace BeauFindlay.Shared.Abstractions;
|
||||
|
||||
public class Result
|
||||
{
|
||||
protected Result(bool isSuccess, Error error)
|
||||
{
|
||||
switch (isSuccess)
|
||||
{
|
||||
case true when error != Error.None:
|
||||
throw new InvalidOperationException("Successful result cannot contain an error.");
|
||||
case false when error == Error.None:
|
||||
throw new InvalidOperationException("Failed result must contain an error.");
|
||||
default:
|
||||
IsSuccess = isSuccess;
|
||||
Error = error;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public bool IsSuccess { get; }
|
||||
|
||||
public bool IsFailure => !IsSuccess;
|
||||
|
||||
public Error Error { get; }
|
||||
|
||||
public static Result Success()
|
||||
{
|
||||
return new Result(true, Error.None);
|
||||
}
|
||||
|
||||
public static Result Failure(Error error)
|
||||
{
|
||||
return new Result(false, error);
|
||||
}
|
||||
|
||||
public static Result<TValue> Success<TValue>(TValue value)
|
||||
{
|
||||
return new Result<TValue>(value, true, Error.None);
|
||||
}
|
||||
|
||||
public static Result<TValue> Failure<TValue>(Error error)
|
||||
{
|
||||
return new Result<TValue>(default, false, error);
|
||||
}
|
||||
|
||||
protected static Result<TValue> Create<TValue>(TValue? value)
|
||||
{
|
||||
return value is not null ? Success(value) : Failure<TValue>(Error.NullValue);
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class Result<TValue>(TValue? value, bool isSuccess, Error error)
|
||||
: Result(isSuccess, error)
|
||||
{
|
||||
[NotNull]
|
||||
public TValue Value => IsSuccess
|
||||
? value!
|
||||
: throw new InvalidOperationException("The value of a failure result can not be accessed.");
|
||||
|
||||
public static implicit operator Result<TValue>(TValue? value)
|
||||
{
|
||||
return Create(value);
|
||||
}
|
||||
}
|
||||
13
BeauFindlay/src/BeauFindlay.Shared/BeauFindlay.Shared.csproj
Normal file
13
BeauFindlay/src/BeauFindlay.Shared/BeauFindlay.Shared.csproj
Normal file
@@ -0,0 +1,13 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,19 @@
|
||||
using System.Net;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace BeauFindlay.Shared.Contracts;
|
||||
|
||||
public sealed class ErrorResponse
|
||||
{
|
||||
[JsonProperty("code")]
|
||||
public int Code { get; set; }
|
||||
|
||||
[JsonProperty("message")]
|
||||
public string Message { get; set; } = string.Empty;
|
||||
|
||||
public static ErrorResponse Generic => new ErrorResponse
|
||||
{
|
||||
Code = (int)HttpStatusCode.BadRequest,
|
||||
Message = "Opps... something went wrong."
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
namespace BeauFindlay.Shared.Contracts;
|
||||
|
||||
public record SendContactEmailRequest(string Name, string FromEmail, string Message, string RecaptchaResponse);
|
||||
Reference in New Issue
Block a user