Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expansion+Get info #52

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion RelationshipAnalysis/Controllers/EdgeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ namespace RelationshipAnalysis.Controllers;
public class EdgeController(
ICreateEdgeCategoryService createEdgeCategoryService,
IEdgeCategoryReceiver edgeCategoryReceiver,
IEdgesAdditionService edgesAdditionService)
IEdgesAdditionService edgesAdditionService,
[FromKeyedServices("edge")] IInfoReceiver infoReceiver)
: ControllerBase
{

Expand All @@ -25,6 +26,14 @@ public async Task<IActionResult> GetAllEdgeCategories()
return Ok(result);
}


[HttpGet]
public async Task<IActionResult> GetInfo(int edgeId)
{
var result = await infoReceiver.GetInfo(edgeId);
return StatusCode((int)result.StatusCode, result.Data);
}

[HttpPost("categories")]
public async Task<IActionResult> CreateEdgeCategory([FromBody] CreateEdgeCategoryDto createEdgeCategoryDto)
{
Expand Down
9 changes: 8 additions & 1 deletion RelationshipAnalysis/Controllers/GraphController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,18 @@ namespace RelationshipAnalysis.Controllers;
[ApiController]
[Authorize]
[Route("api/[controller]")]
public class GraphController(IGraphReceiver graphReceiver) : ControllerBase
public class GraphController(IGraphReceiver graphReceiver,
IExpansionGraphReceiver expansionGraphReceiver) : ControllerBase
{
[HttpGet]
public async Task<IActionResult> GetGraph()
{
return Ok(await graphReceiver.GetGraph());
}
[HttpGet("expansion")]
public async Task<IActionResult> GetExpansionGraph(int nodeId, string sourceCategoryName, string targetCategoryName, string edgeCategoryName)
{
var result = await expansionGraphReceiver.GetExpansionGraph(nodeId, sourceCategoryName, targetCategoryName, edgeCategoryName);
return Ok(result);
}
}
9 changes: 8 additions & 1 deletion RelationshipAnalysis/Controllers/NodeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ namespace RelationshipAnalysis.Controllers;
public class NodeController(
ICreateNodeCategoryService createNodeCategoryService,
INodeCategoryReceiver nodeCategoryReceiver,
INodesAdditionService nodesAdditionService)
INodesAdditionService nodesAdditionService,
[FromKeyedServices("node")] IInfoReceiver infoReceiver)
: ControllerBase
{
[HttpGet("categories")]
Expand All @@ -24,6 +25,12 @@ public async Task<IActionResult> GetAllNodeCategories()
return Ok(result);
}

[HttpGet]
public async Task<IActionResult> GetInfo(int nodeId)
{
var result = await infoReceiver.GetInfo(nodeId);
return StatusCode((int)result.StatusCode, result.Data);
}

[HttpPost("categories")]
public async Task<IActionResult> CreateNodeCategory([FromBody] CreateNodeCategoryDto createNodeCategoryDto)
Expand Down
15 changes: 15 additions & 0 deletions RelationshipAnalysis/Dto/Graph/ExpansionDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.ComponentModel.DataAnnotations;

namespace RelationshipAnalysis.Dto.Graph;

public class ExpansionDto
{
[Required]
public int NodeId { get; set; }
[Required]
public int SourceCategoryId { get; set; }
[Required]
public int TargetCategoryId { get; set; }
[Required]
public int EdgeCategoryId { get; set; }
}
6 changes: 5 additions & 1 deletion RelationshipAnalysis/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,11 @@
.AddSingleton<ICsvProcessorService, CsvProcessorService>()
.AddSingleton<ISingleEdgeAdditionService, SingleEdgeAdditionService>()
.AddSingleton<IEdgesAdditionService, EdgesAdditionService>()
.AddSingleton<ICsvValidatorService, CsvValidatorService>();
.AddSingleton<ICsvValidatorService, CsvValidatorService>()
.AddSingleton<IExpansionGraphReceiver, ExpansionGraphReceiver>()
.AddSingleton<IGraphDtoCreator, GraphDtoCreator>()
.AddKeyedSingleton<IInfoReceiver, NodeInfoReceiver>("node")
.AddKeyedSingleton<IInfoReceiver, EdgeInfoReceiver>("edge");



Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using RelationshipAnalysis.Dto;
using RelationshipAnalysis.Dto.Graph;

namespace RelationshipAnalysis.Services.GraphServices.Abstraction;

public interface IExpansionGraphReceiver
{
Task<GraphDto> GetExpansionGraph(int nodeId, string sourceCategoryName, string targetCategoryName,
string edgeCategoryName);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using RelationshipAnalysis.Dto.Graph;
using RelationshipAnalysis.Models.Graph;
using Node = AngleSharp.Dom.Node;

namespace RelationshipAnalysis.Services.GraphServices.Abstraction;

public interface IGraphDtoCreator
{
GraphDto CreateResultGraphDto(List<Models.Graph.Node> contextNodes, List<Models.Graph.Edge> contextEdges);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using RelationshipAnalysis.Dto;

namespace RelationshipAnalysis.Services.GraphServices.Abstraction;

public interface IInfoReceiver
{
Task<ActionResponse<IDictionary<string, string>>> GetInfo(int id);
}
41 changes: 41 additions & 0 deletions RelationshipAnalysis/Services/GraphServices/EdgeInfoReceiver.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using RelationshipAnalysis.Context;
using RelationshipAnalysis.Dto;
using RelationshipAnalysis.Enums;
using RelationshipAnalysis.Services.GraphServices.Abstraction;

namespace RelationshipAnalysis.Services.GraphServices;

public class EdgeInfoReceiver(IServiceProvider serviceProvider) : IInfoReceiver
{
public Task<ActionResponse<IDictionary<string, string>>> GetInfo(int edgeId)
{
using var scope = serviceProvider.CreateScope();
var context = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();

var result = new Dictionary<string, string>();
var selectedEdge = context.Edges.SingleOrDefault(e => e.EdgeId == edgeId);
if (selectedEdge == null)
{
return NotFoundResult();
}
selectedEdge.EdgeValues.ToList().ForEach(v => result.Add(v.EdgeAttribute.EdgeAttributeName, v.ValueData));
return SuccessResult(result);
}

private async Task<ActionResponse<IDictionary<string, string>>> NotFoundResult()
{
return new ActionResponse<IDictionary<string, string>>()
{
StatusCode = StatusCodeType.NotFound
};
}

private async Task<ActionResponse<IDictionary<string, string>>> SuccessResult(Dictionary<string, string> result)
{
return new ActionResponse<IDictionary<string, string>>()
{
StatusCode = StatusCodeType.Success,
Data = result
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using Microsoft.EntityFrameworkCore;
using RelationshipAnalysis.Context;
using RelationshipAnalysis.Dto.Graph;
using RelationshipAnalysis.Models.Graph;
using RelationshipAnalysis.Services.GraphServices.Abstraction;

namespace RelationshipAnalysis.Services.GraphServices;

public class ExpansionGraphReceiver(IServiceProvider serviceProvider, IGraphDtoCreator graphDtoCreator) : IExpansionGraphReceiver
{
public async Task<GraphDto> GetExpansionGraph(int nodeId, string sourceCategoryName, string targetCategoryName, string edgeCategoryName)
{
using var scope = serviceProvider.CreateScope();
var context = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();

var inputNodes = await GetInputNodes(sourceCategoryName, context);
var outputNodes = await GetOutputNodes(targetCategoryName, context);
var validEdges = await GetValidEdges(edgeCategoryName, context, inputNodes, outputNodes);

return graphDtoCreator.CreateResultGraphDto(inputNodes.Union(outputNodes).ToList(), validEdges);
}

private async Task<List<Edge>> GetValidEdges(string edgeCategoryName, ApplicationDbContext context, List<Node> inputNodes,
List<Node> outputNodes)
{
var validEdges = await context.Edges.Where(e =>
e.EdgeCategory.EdgeCategoryName == edgeCategoryName &&
inputNodes.Contains(e.NodeSource) &&
outputNodes.Contains(e.NodeDestination)).ToListAsync();
return validEdges;
}

private async Task<List<Node>> GetOutputNodes(string targetCategoryName, ApplicationDbContext context)
{
var outputNodes =
await context.Nodes.Where(n => n.NodeCategory.NodeCategoryName == targetCategoryName).ToListAsync();
return outputNodes;
}

private async Task<List<Node>> GetInputNodes(string sourceCategoryName, ApplicationDbContext context)
{
var inputNodes =
await context.Nodes.Where(n => n.NodeCategory.NodeCategoryName == sourceCategoryName).ToListAsync();
return inputNodes;
}
}
28 changes: 28 additions & 0 deletions RelationshipAnalysis/Services/GraphServices/GraphDtoCreator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using RelationshipAnalysis.Dto.Graph;
using RelationshipAnalysis.Models.Graph;
using RelationshipAnalysis.Services.GraphServices.Abstraction;
using Node = AngleSharp.Dom.Node;

namespace RelationshipAnalysis.Services.GraphServices;

public class GraphDtoCreator : IGraphDtoCreator
{

public GraphDto CreateResultGraphDto(List<Models.Graph.Node> contextNodes, List<Models.Graph.Edge> contextEdges)
{
if (contextEdges == null || contextNodes == null) throw new ArgumentNullException();
var resultGraphDto = new GraphDto();
contextNodes.ForEach(n => resultGraphDto.nodes.Add(new NodeDto
{
id = n.NodeId.ToString(),
label = $"{n.NodeCategory.NodeCategoryName}/{n.NodeUniqueString}"
}));
contextEdges.ForEach(e => resultGraphDto.edges.Add(new EdgeDto
{
id = e.EdgeId.ToString(),
source = e.EdgeSourceNodeId.ToString(),
target = e.EdgeDestinationNodeId.ToString()
}));
return resultGraphDto;
}
}
42 changes: 42 additions & 0 deletions RelationshipAnalysis/Services/GraphServices/NodeInfoReceiver.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using Microsoft.AspNetCore.Mvc;
using RelationshipAnalysis.Context;
using RelationshipAnalysis.Dto;
using RelationshipAnalysis.Enums;
using RelationshipAnalysis.Services.GraphServices.Abstraction;

namespace RelationshipAnalysis.Services.GraphServices;

public class NodeInfoReceiver(IServiceProvider serviceProvider) : IInfoReceiver
{
public Task<ActionResponse<IDictionary<string, string>>> GetInfo(int nodeId)
{
using var scope = serviceProvider.CreateScope();
var context = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();

var result = new Dictionary<string, string>();
var selectedNode = context.Nodes.SingleOrDefault(n => n.NodeId == nodeId);
if (selectedNode == null)
{
return NotFoundResult();
}
selectedNode.Values.ToList().ForEach(v => result.Add(v.NodeAttribute.NodeAttributeName, v.ValueData));
return SuccessResult(result);
}

private async Task<ActionResponse<IDictionary<string, string>>> NotFoundResult()
{
return new ActionResponse<IDictionary<string, string>>()
{
StatusCode = StatusCodeType.NotFound
};
}

private async Task<ActionResponse<IDictionary<string, string>>> SuccessResult(Dictionary<string, string> result)
{
return new ActionResponse<IDictionary<string, string>>()
{
StatusCode = StatusCodeType.Success,
Data = result
};
}
}