Files
StructureHelper/DataAccess/DTOs/Converters/ShapeToDTOConvertStrategy.cs
2025-07-20 21:45:07 +05:00

60 lines
2.3 KiB
C#

using StructureHelperCommon.Infrastructures.Exceptions;
using StructureHelperCommon.Infrastructures.Interfaces;
using StructureHelperCommon.Models;
using StructureHelperCommon.Models.Shapes;
namespace DataAccess.DTOs
{
public class ShapeToDTOConvertStrategy : ConvertStrategy<IShape, IShape>
{
private IConvertStrategy<RectangleShapeDTO, IRectangleShape> rectangleConvertStrategy;
private IConvertStrategy<CircleShapeDTO, ICircleShape> circleConvertStrategy;
public ShapeToDTOConvertStrategy(IBaseConvertStrategy baseConvertStrategy) : base(baseConvertStrategy)
{
}
public override IShape GetNewItem(IShape source)
{
ChildClass = this;
GetNewShape(source);
return NewItem;
}
private void GetNewShape(IShape source)
{
TraceLogger?.AddMessage($"Shape converting Id = {source.Id} has been started", TraceLogStatuses.Debug);
if (source is IRectangleShape rectangle)
{
ProcessRectangle(rectangle);
}
else if (source is ICircleShape circle)
{
ProcessCircle(circle);
}
else
{
string errorString = ErrorStrings.ObjectTypeIsUnknownObj(source) + ": shape type";
throw new StructureHelperException(errorString);
}
TraceLogger?.AddMessage($"Shape converting Id = {NewItem.Id} has been has been finished successfully", TraceLogStatuses.Debug);
}
private void ProcessCircle(ICircleShape circle)
{
TraceLogger?.AddMessage($"Shape is circle", TraceLogStatuses.Debug);
circleConvertStrategy = new DictionaryConvertStrategy<CircleShapeDTO, ICircleShape>
(this, new CircleShapeToDTOConvertStrategy(this));
NewItem = circleConvertStrategy.Convert(circle);
}
private void ProcessRectangle(IRectangleShape rectangle)
{
TraceLogger?.AddMessage($"Shape is rectangle", TraceLogStatuses.Debug);
rectangleConvertStrategy = new DictionaryConvertStrategy<RectangleShapeDTO, IRectangleShape>
(this, new RectangleShapeToDTOConvertStrategy(this));
NewItem = rectangleConvertStrategy.Convert(rectangle);
}
}
}