Skip to content

Commit

Permalink
Merge pull request #51 from phmatray/net8
Browse files Browse the repository at this point in the history
Next version
  • Loading branch information
phmatray authored Dec 11, 2023
2 parents 6d1f5b9 + acf1ad4 commit fad25cb
Show file tree
Hide file tree
Showing 12 changed files with 81 additions and 106 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
namespace Geometrix.Application.UseCases.GenerateImage;

/// <summary>
/// Represents the use case for generating an image.
/// </summary>
public interface IGenerateImageUseCase
{
/// <summary>
/// Executes the Use Case.
/// Executes the Use Case.
/// </summary>
/// <returns>Task.</returns>
Task Execute(
Expand All @@ -17,7 +20,7 @@ Task Execute(
string foregroundColor);

/// <summary>
/// Sets the Output Port.
/// Sets the Output Port.
/// </summary>
/// <param name="outputPort">Output Port</param>
void SetOutputPort(IOutputPort outputPort);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@

namespace Geometrix.Application.UseCases.GenerateImage;

/// <summary>
/// Represents an output port for image processing.
/// </summary>
public interface IOutputPort
{
/// <summary>
/// Image created.
/// Image created.
/// </summary>
void Ok(ImageDescription image, byte[] bytes, string fileLocation);

/// <summary>
/// Invalid input.
/// Invalid input.
/// </summary>
void Invalid();
}
30 changes: 8 additions & 22 deletions geometrix-api/Geometrix.Domain/ValueObjects/ThemeColor.cs
Original file line number Diff line number Diff line change
@@ -1,38 +1,24 @@
namespace Geometrix.Domain.ValueObjects;

public readonly struct ThemeColor : IEquatable<ThemeColor>
public readonly struct ThemeColor(string value)
: IEquatable<ThemeColor>
{
public string Value { get; }
public string Value { get; } = value;

public ThemeColor(string value)
{
Value = value;
}

public override bool Equals(object? obj)
{
return obj is ThemeColor o && Equals(o);
}
=> obj is ThemeColor o && Equals(o);

public bool Equals(ThemeColor other)
{
return Value == other.Value;
}
=> Value == other.Value;

public override int GetHashCode()
{
return HashCode.Combine(Value);
}
=> HashCode.Combine(Value);

public static bool operator ==(ThemeColor left, ThemeColor right)
{
return left.Equals(right);
}
=> left.Equals(right);

public static bool operator !=(ThemeColor left, ThemeColor right)
{
return !(left == right);
}
=> !(left == right);

/// <summary>
/// Light.
Expand Down
32 changes: 9 additions & 23 deletions geometrix-api/Geometrix.Domain/ValueObjects/TriangleDirection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,19 @@ public readonly struct TriangleDirection(TriangleDirection.Direction value)
public Direction Value { get; } = value;

public override bool Equals(object? obj)
{
return obj is TriangleDirection other && Equals(other);
}
=> obj is TriangleDirection other && Equals(other);

public bool Equals(TriangleDirection other)
{
return Value == other.Value;
}
=> Value == other.Value;

public override int GetHashCode()
{
return (int) Value;
}
=> (int) Value;

public static bool operator ==(TriangleDirection left, TriangleDirection right)
{
return left.Equals(right);
}
=> left.Equals(right);

public static bool operator !=(TriangleDirection left, TriangleDirection right)
{
return !(left == right);
}
=> !(left == right);

public static readonly TriangleDirection None = new(Direction.None);
public static readonly TriangleDirection TopLeft = new(Direction.TopLeft);
Expand All @@ -38,32 +28,28 @@ public override int GetHashCode()
public static readonly TriangleDirection Filled = new(Direction.Filled);

public static TriangleDirection MirrorRight(TriangleDirection direction)
{
return direction.Value switch
=> direction.Value switch
{
Direction.None => None,
Direction.TopLeft => TopRight,
Direction.TopRight => TopLeft,
Direction.BottomLeft => BottomRight,
Direction.BottomRight => BottomLeft,
Direction.Filled => Filled,
_ => throw new ArgumentOutOfRangeException()
_ => throw new ArgumentOutOfRangeException(nameof(direction))
};
}

public static TriangleDirection MirrorDown(TriangleDirection direction)
{
return direction.Value switch
=> direction.Value switch
{
Direction.None => None,
Direction.TopLeft => BottomLeft,
Direction.TopRight => BottomRight,
Direction.BottomLeft => TopLeft,
Direction.BottomRight => TopRight,
Direction.Filled => Filled,
_ => throw new ArgumentOutOfRangeException()
_ => throw new ArgumentOutOfRangeException(nameof(direction))
};
}

public static TriangleDirection CreateRandom(Random random, bool includeEmptyAndFill)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ namespace Geometrix.Infrastructure.DataAccess;

public sealed class EntityDescriptionFactory : IImageDescriptionFactory
{
public ImageDescription NewImage(Pattern pattern, Settings settings) =>
new(pattern, settings);
public ImageDescription NewImage(Pattern pattern, Settings settings)
=> new(pattern, settings);

public Pattern NewPattern(
int mirrorPowerHorizontal,
int mirrorPowerVertical,
int cellGroupLength,
bool includeEmptyAndFill,
int seed) =>
new(mirrorPowerHorizontal, mirrorPowerVertical, cellGroupLength, includeEmptyAndFill, seed);
int seed)
=> new(mirrorPowerHorizontal, mirrorPowerVertical, cellGroupLength, includeEmptyAndFill, seed);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.0" />
<PackageReference Include="SixLabors.ImageSharp" Version="3.0.2" />
<PackageReference Include="SixLabors.ImageSharp.Drawing" Version="2.0.1" />
<PackageReference Include="SixLabors.ImageSharp" Version="3.1.0" />
<PackageReference Include="SixLabors.ImageSharp.Drawing" Version="2.1.0" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using Geometrix.Application.Services;
using Geometrix.Domain;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;

namespace Geometrix.Infrastructure.ImageCreation;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
using Geometrix.Domain.Patterns;
using Geometrix.Domain.ValueObjects;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Drawing;
using SixLabors.ImageSharp.Drawing.Processing;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;

namespace Geometrix.Infrastructure.ImageCreation;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
using Geometrix.Domain.ValueObjects;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Drawing;

namespace Geometrix.Infrastructure.ImageCreation;

public sealed class TriangleService
{
private readonly TriangleFactory _triangleFactory = new();

/// <summary>
/// Get a triangle from a direction and a position.
/// Get a triangle from a direction and a position.
/// </summary>
/// <param name="direction">The direction of the triangle.</param>
/// <param name="x">The x position of the triangle.</param>
Expand All @@ -16,76 +19,58 @@ public sealed class TriangleService
/// <exception cref="ArgumentOutOfRangeException">direction</exception>
public Polygon? GetTriangle(TriangleDirection direction, int x, int y, int cellWidthPixel)
{
return direction.Value switch
if (!Enum.IsDefined(typeof(TriangleDirection.Direction), direction.Value))
{
TriangleDirection.Direction.None => null,
TriangleDirection.Direction.TopLeft => CreateTopLeftTriangle(x, y, cellWidthPixel),
TriangleDirection.Direction.TopRight => CreateTopRightTriangle(x, y, cellWidthPixel),
TriangleDirection.Direction.BottomLeft => CreateBottomLeftTriangle(x, y, cellWidthPixel),
TriangleDirection.Direction.BottomRight => CreateBottomRightTriangle(x, y, cellWidthPixel),
TriangleDirection.Direction.Filled => CreateFilled(x, y, cellWidthPixel),
_ => throw new ArgumentOutOfRangeException(nameof(direction), direction, null)
};
}
throw new ArgumentOutOfRangeException(nameof(direction), $"Invalid triangle direction: {direction}");
}

private static Polygon CreateTopLeftTriangle(int x, int y, int cellWidthPixel)
{
return new Polygon(new List<ILineSegment>
{
new LinearLineSegment(
new PointF(x, y),
new PointF(x + cellWidthPixel, y),
new PointF(x, y + cellWidthPixel)
)
});
return direction.Value == TriangleDirection.Direction.None
? null
: _triangleFactory.CreateTriangle(direction, x, y, cellWidthPixel);
}
}

private static Polygon CreateTopRightTriangle(int x, int y, int cellWidthPixel)
public class TriangleFactory
{
public Polygon CreateTriangle(TriangleDirection direction, int x, int y, int cellWidthPixel)
{
return new Polygon(new List<ILineSegment>
return direction.Value switch
{
new LinearLineSegment(
new PointF(x + cellWidthPixel, y),
new PointF(x + cellWidthPixel, y + cellWidthPixel),
new PointF(x, y)
)
});
TriangleDirection.Direction.TopLeft => CreateTriangle(new[] { (x, y), (x + cellWidthPixel, y), (x, y + cellWidthPixel) }),
TriangleDirection.Direction.TopRight => CreateTriangle(new[] { (x + cellWidthPixel, y), (x + cellWidthPixel, y + cellWidthPixel), (x, y) }),
TriangleDirection.Direction.BottomLeft => CreateTriangle(new[] { (x, y + cellWidthPixel), (x, y), (x + cellWidthPixel, y + cellWidthPixel) }),
TriangleDirection.Direction.BottomRight => CreateTriangle(new[] { (x + cellWidthPixel, y + cellWidthPixel), (x, y + cellWidthPixel), (x + cellWidthPixel, y) }),
TriangleDirection.Direction.Filled => CreateRectangle(x, y, cellWidthPixel),
_ => throw new ArgumentException("Invalid triangle direction", nameof(direction))
};
}

private static Polygon CreateBottomLeftTriangle(int x, int y, int cellWidthPixel)
private Polygon CreateTriangle((int x, int y)[] points)
{
return new Polygon(new List<ILineSegment>
var lineSegments = new List<ILineSegment>
{
new LinearLineSegment(
new PointF(x, y + cellWidthPixel),
new PointF(x, y),
new PointF(x + cellWidthPixel, y + cellWidthPixel)
new PointF(points[0].x, points[0].y),
new PointF(points[1].x, points[1].y),
new PointF(points[2].x, points[2].y)
)
});
}
};

private static Polygon CreateBottomRightTriangle(int x, int y, int cellWidthPixel)
{
return new Polygon(new List<ILineSegment>
{
new LinearLineSegment(
new PointF(x + cellWidthPixel, y + cellWidthPixel),
new PointF(x, y + cellWidthPixel),
new PointF(x + cellWidthPixel, y)
)
});
return new Polygon(lineSegments);
}

private static Polygon CreateFilled(int x, int y, int cellWidthPixel)
private Polygon CreateRectangle(int x, int y, int cellWidthPixel)
{
return new Polygon(new List<ILineSegment>
var lineSegments = new List<ILineSegment>
{
new LinearLineSegment(
new PointF(x, y),
new PointF(x + cellWidthPixel, y),
new PointF(x + cellWidthPixel, y + cellWidthPixel),
new PointF(x, y + cellWidthPixel)
)
});
};

return new Polygon(lineSegments);
}
}
}
2 changes: 1 addition & 1 deletion geometrix-api/Geometrix.WebApi/Geometrix.WebApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

<ItemGroup>
<!-- Third party -->
<PackageReference Include="prometheus-net.AspNetCore" Version="8.1.0" />
<PackageReference Include="prometheus-net.AspNetCore" Version="8.2.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
<PackageReference Include="EPPlus.Core" Version="1.5.4" />
<!-- Decorator injection comparison https://greatrexpectations.com/2018/10/25/decorators-in-net-core-with-dependency-injection -->
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions geometrix-api/global.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"sdk": {
"version": "8.0.0",
"rollForward": "latestMajor",
"allowPrerelease": true
}
}

0 comments on commit fad25cb

Please sign in to comment.