Skip to content

Commit

Permalink
feat(fennec): scaffold layer types
Browse files Browse the repository at this point in the history
Add rudimentary layer types such as:
 - Filter
 - Aggregation
 - Naming
 - Positioning

NETANOL-189
  • Loading branch information
Kemitoxs committed Dec 5, 2023
1 parent 8d5d9e9 commit cd86787
Show file tree
Hide file tree
Showing 8 changed files with 168 additions and 4 deletions.
115 changes: 115 additions & 0 deletions Packrat/Fennec/Controllers/LayerController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
using Fennec.Database;
using Fennec.Database.Domain.Layers;
using Microsoft.AspNetCore.Mvc;
using Swashbuckle.AspNetCore.Annotations;

namespace Fennec.Controllers;

/// <summary>
/// Represents a simplified layout with its data replaced by a short description.
/// </summary>
/// <param name="Type"></param>
/// <param name="Name"></param>
/// <param name="Description"></param>
public record ShortLayerDto(LayerType Type, string Name, bool Enabled, string Description);

/// <summary>
/// Fully fledged layer with an arbitrary data object.
/// </summary>
/// <param name="Type"></param>
/// <param name="Name"></param>
/// <param name="Enabled"></param>
/// <param name="Data"></param>
public record FullLayerDto(LayerType Type, string Name, bool Enabled, object Data);

/// <summary>
/// Provides direct manipulation capabilities to change the layers of a layout.
/// </summary>
[SwaggerTag("Manage Layers within Layouts")]
[Route("layout/{layoutName}/layers")]
public class LayerController : ControllerBase
{
private readonly ILayoutRepository _layouts;

public LayerController(ILayoutRepository layouts)
{
_layouts = layouts;
}

/// <summary>
/// Insert a new layer into the given layout at the given index.
/// If no index is given, the layer will be appended to the end.
/// </summary>
/// <param name="layoutName">The name of the layout</param>
/// <param name="index"></param>
/// <param name="creationRequest"></param>
/// <returns></returns>
[HttpPost]
[SwaggerResponse(StatusCodes.Status200OK, "The layer was successfully inserted.")]
[SwaggerResponse(StatusCodes.Status400BadRequest,
"Invalid data was provided, reference the message for more information.")]
public async Task<IActionResult> InsertLayer(string layoutName, [FromQuery] int? index,
[FromBody] FullLayerDto creationRequest)
{
var layout = await _layouts.GetLayout(layoutName);
if (layout == null)
return NotFound("The given layout could not be found.");

ILayoutLayer? layer = creationRequest.Type switch
{
LayerType.Filter => new FilterLayer(),
_ => null
};
if (layer == null)
return BadRequest("The given layer type is not supported.");

if (index == null)
layout.Layers.Add(layer);
else if (index < 0 || index > layout.Layers.Count)
return BadRequest("The given index is out of bounds.");
else
layout.Layers.Insert(index.Value, layer);

return Ok(layout);
}

[HttpPut("{oldIndex}/{newIndex}")]
public async Task<IActionResult> MoveLayer(string layoutName, int oldIndex, int newIndex)
{
var layout = await _layouts.GetLayout(layoutName);
if (layout == null)
return NotFound("The given layout could not be found.");

return Ok();
}

[HttpGet("{index}")]
public async Task<IActionResult> DeleteLayer(string layoutName, int index)
{
var layout = await _layouts.GetLayout(layoutName);
if (layout == null)
return NotFound("The given layout could not be found.");

return Ok();
}

[HttpPut("{index}")]
public async Task<IActionResult> UpdateLayer(string layoutName, int index)
{
var layout = await _layouts.GetLayout(layoutName);
if (layout == null)
return NotFound("The given layout could not be found.");

return Ok();
}

[HttpDelete("{index}")]
public async Task<IActionResult> GetLayer(string layoutName, int index)
{
var layout = await _layouts.GetLayout(layoutName);
if (layout == null)
return NotFound("The given layout could not be found.");

return Ok();
}
}
1 change: 1 addition & 0 deletions Packrat/Fennec/Database/Domain/Layers/AggregationLayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
/// </summary>
public class AggregationLayer : ILayoutLayer
{
public LayerType Type { get; set; } = LayerType.Aggregation;
public string? Name { get; set; }
public bool Enabled { get; set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
/// <summary>
/// Includes or excludes certain nodes based on their IP address.
/// </summary>
public class SelectionLayer : ILayoutLayer
public class FilterLayer : ILayoutLayer
{
public LayerType Type { get; set; } = LayerType.Filter;
public string? Name { get; set; }
public bool Enabled { get; set; }

Expand Down
36 changes: 35 additions & 1 deletion Packrat/Fennec/Database/Domain/Layers/ILayoutLayer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
namespace Fennec.Database.Domain.Layers;
using MongoDB.Bson.Serialization;
using MongoDB.Bson.Serialization.Serializers;

namespace Fennec.Database.Domain.Layers;

public enum LayerType
{
Filter,
Aggregation,
Naming,
Positioning
}

/// <summary>
///
Expand All @@ -10,6 +21,11 @@ public interface ILayoutLayer
/// </summary>
public string? Name { get; set; }

/// <summary>
/// The type of the layer.
/// </summary>
public LayerType Type { get; set; }

/// <summary>
/// Sets whether this layer should be executed or not.
/// </summary>
Expand All @@ -20,4 +36,22 @@ public interface ILayoutLayer
public object GetPreview();

public object GetFullView();
}

public class LayoutLayerSerializer : SerializerBase<ILayoutLayer>
{
public override ILayoutLayer Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
{
var document = BsonDocumentSerializer.Instance.Deserialize(context);
var type = document["Type"].AsInt32;

return (LayerType)type switch
{
LayerType.Filter => BsonSerializer.Deserialize<FilterLayer>(document),
LayerType.Aggregation => BsonSerializer.Deserialize<AggregationLayer>(document),
LayerType.Naming => BsonSerializer.Deserialize<NamingLayer>(document),
LayerType.Positioning => BsonSerializer.Deserialize<PositioningLayer>(document),
_ => throw new NotSupportedException($"Unknown layer type: {type}")
};
}
}
1 change: 1 addition & 0 deletions Packrat/Fennec/Database/Domain/Layers/NamingLayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
/// </summary>
public class NamingLayer : ILayoutLayer
{
public LayerType Type { get; set; } = LayerType.Naming;
public string? Name { get; set; }
public bool Enabled { get; set; }

Expand Down
1 change: 1 addition & 0 deletions Packrat/Fennec/Database/Domain/Layers/PositioningLayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
/// </summary>
public class PositioningLayer : ILayoutLayer
{
public LayerType Type { get; set; } = LayerType.Positioning;
public string? Name { get; set; }
public bool Enabled { get; set; }

Expand Down
14 changes: 12 additions & 2 deletions Packrat/Fennec/Database/Domain/Layout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class Layout
{
[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
public string Id { get; set; }
public string? Id { get; set; }

/// <summary>
/// The name of the layout as can be selected by the user.
Expand All @@ -23,5 +23,15 @@ public class Layout
/// The layers
/// </summary>
[BsonElement("layers")]
public ILayoutLayer[] Layers { get; set; }
public IList<ILayoutLayer> Layers { get; set; }

public Layout(string name)
{
Name = name;
Layers = new List<ILayoutLayer>();
}

#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
protected Layout() { }
#pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
}
1 change: 1 addition & 0 deletions Packrat/Packrat.sln.DotSettings
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:Boolean x:Key="/Default/UserDictionary/Words/=dtos/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Netanol/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>

0 comments on commit cd86787

Please sign in to comment.