Skip to content

Commit

Permalink
refactoring: use .NET8 primary constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
phmatray committed Nov 24, 2023
1 parent 7f86947 commit 2cc0775
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,17 @@

namespace Geometrix.Application.UseCases.GenerateImage;

public sealed class GenerateImageValidationUseCase : IGenerateImageUseCase
public sealed class GenerateImageValidationUseCase(
IGenerateImageUseCase useCase,
Notification notification)
: IGenerateImageUseCase
{
private readonly IGenerateImageUseCase _useCase;
private readonly Notification _notification;
private IOutputPort _outputPort;

public GenerateImageValidationUseCase(
IGenerateImageUseCase useCase,
Notification notification)
{
_useCase = useCase;
_notification = notification;
_outputPort = new GenerateImagePresenter();
}
private IOutputPort _outputPort = new GenerateImagePresenter();

public void SetOutputPort(IOutputPort outputPort)
{
_outputPort = outputPort;
_useCase.SetOutputPort(outputPort);
useCase.SetOutputPort(outputPort);
}

public async Task Execute(
Expand All @@ -36,31 +28,31 @@ public async Task Execute(
{
if (mirrorPowerHorizontal is < 1 or > 4)
{
_notification
notification
.Add(nameof(mirrorPowerHorizontal), "MirrorPowerHorizontal is required.");
}

if (mirrorPowerVertical is < 1 or > 4)
{
_notification
notification
.Add(nameof(mirrorPowerVertical), "MirrorPowerVertical is required.");
}

if (cellGroupLength is < 2 or > 8)
{
_notification
notification
.Add(nameof(cellGroupLength), "CellGroupLength is required.");
}

if (cellWidthPixel is < 32 or > 256)
{
_notification
notification
.Add(nameof(cellWidthPixel), "CellWidthPixel is required.");
}

if (seed is < 0 or > 100000)
{
_notification
notification
.Add(nameof(seed), "Seed is required.");
}

Expand All @@ -74,7 +66,7 @@ public async Task Execute(
backgroundColor != ThemeColor.Purple.Value &&
backgroundColor != ThemeColor.Pink.Value)
{
_notification
notification
.Add(nameof(backgroundColor), "BackgroundColor is required.");
}

Expand All @@ -88,17 +80,17 @@ public async Task Execute(
foregroundColor != ThemeColor.Purple.Value &&
foregroundColor != ThemeColor.Pink.Value)
{
_notification
notification
.Add(nameof(backgroundColor), "ForegroundColor is required.");
}

if (_notification.IsInvalid)
if (notification.IsInvalid)
{
_outputPort.Invalid();
return;
}

await _useCase
await useCase
.Execute(
mirrorPowerHorizontal, mirrorPowerVertical, cellGroupLength,
cellWidthPixel, includeEmptyAndFill, seed,
Expand Down
10 changes: 3 additions & 7 deletions geometrix-api/Geometrix.Domain/ValueObjects/TriangleDirection.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
namespace Geometrix.Domain.ValueObjects;

public readonly struct TriangleDirection : IEquatable<TriangleDirection>
public readonly struct TriangleDirection(TriangleDirection.Direction value)
: IEquatable<TriangleDirection>
{
public Direction Value { get; }

public TriangleDirection(Direction value)
{
Value = value;
}
public Direction Value { get; } = value;

public override bool Equals(object? obj)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,18 @@
namespace Geometrix.Infrastructure.FileStorage;

[Obsolete("Use FileStorageServiceV2 instead")]
public class FileStorageService : IFileStorageService
public class FileStorageService(
IHostEnvironment env,
ILogger<FileStorageService> logger)
: IFileStorageService
{
private readonly IHostEnvironment _env;
private readonly ILogger<FileStorageService> _logger;

public FileStorageService(
IHostEnvironment env,
ILogger<FileStorageService> logger)
{
_env = env;
_logger = logger;
}

public async Task<string?> SaveFileAsync(
byte[] dataArray,
string nameWithoutExtension,
string extension = "")
{
var fileName = $"{nameWithoutExtension}.png";
var path = Path.Combine(_env.ContentRootPath, "wwwroot", "images");
var path = Path.Combine(env.ContentRootPath, "wwwroot", "images");

if (!Directory.Exists(path))
{
Expand All @@ -50,11 +42,11 @@ public FileStorageService(
if (dataArray[i] == fileStream.ReadByte())
continue;

_logger.LogError("Error writing data");
logger.LogError("Error writing data");
return null;
}

_logger.LogInformation("The data was written to {Name} and verified", fileStream.Name);
logger.LogInformation("The data was written to {Name} and verified", fileStream.Name);

return fileName;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,21 @@

namespace Geometrix.Infrastructure.FileStorage;

public class FileStorageServiceV2 : IFileStorageService
public class FileStorageServiceV2(
IHostEnvironment env,
ILogger<FileStorageServiceV2> logger)
: IFileStorageService
{
private readonly IHostEnvironment _env;
private readonly ILogger<FileStorageServiceV2> _logger;
private const string DefaultImageExtension = ".png";
private const string DefaultImageFolder = "wwwroot/images";

public FileStorageServiceV2(
IHostEnvironment env,
ILogger<FileStorageServiceV2> logger)
{
_env = env;
_logger = logger;
}

public async Task<string?> SaveFileAsync(
byte[] dataArray,
string nameWithoutExtension,
string extension = DefaultImageExtension)
{
var fileName = $"{nameWithoutExtension}{extension}";
var path = Path.Combine(_env.ContentRootPath, DefaultImageFolder);
var path = Path.Combine(env.ContentRootPath, DefaultImageFolder);

if (!Directory.Exists(path))
{
Expand All @@ -37,12 +30,12 @@ public FileStorageServiceV2(
try
{
await File.WriteAllBytesAsync(fullFileLocation, dataArray);
_logger.LogInformation("The data was written to {Name}", fullFileLocation);
logger.LogInformation("The data was written to {Name}", fullFileLocation);
return Path.GetFileName(fullFileLocation);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error writing data to {Name}", fullFileLocation);
logger.LogError(ex, "Error writing data to {Name}", fullFileLocation);
return null;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static IServiceCollection AddInvalidRequestLogging(this IServiceCollectio
.ToList();

var jsonModelState = JsonSerializer.Serialize(errors);
logger.LogWarning("Invalid request {jsonModelState}", jsonModelState);
logger.LogWarning("Invalid request {JsonModelState}", jsonModelState);

var problemDetails = new ValidationProblemDetails(actionContext.ModelState);
return new BadRequestObjectResult(problemDetails);
Expand Down
2 changes: 1 addition & 1 deletion geometrix-api/Geometrix.WebApi/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ public static void Main(string[] args)

private static IHostBuilder CreateHostBuilder(string[] args)
=> Host.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((hostContext, configApp) => { configApp.AddCommandLine(args); })
.ConfigureAppConfiguration((_, configApp) => { configApp.AddCommandLine(args); })
.ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); });
}

0 comments on commit 2cc0775

Please sign in to comment.