Skip to content

Commit

Permalink
add get info for nodes and edges
Browse files Browse the repository at this point in the history
  • Loading branch information
SwimmingRieux committed Aug 27, 2024
1 parent 640e1ad commit 6fb0189
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 12 deletions.
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/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
4 changes: 3 additions & 1 deletion RelationshipAnalysis/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@
.AddSingleton<IEdgesAdditionService, EdgesAdditionService>()
.AddSingleton<ICsvValidatorService, CsvValidatorService>()
.AddSingleton<IExpansionGraphReceiver, ExpansionGraphReceiver>()
.AddSingleton<IGraphDtoCreator, GraphDtoCreator>();
.AddSingleton<IGraphDtoCreator, GraphDtoCreator>()
.AddKeyedSingleton<IInfoReceiver, NodeInfoReceiver>("node")
.AddKeyedSingleton<IInfoReceiver, EdgeInfoReceiver>("edge");



Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ namespace RelationshipAnalysis.Services.GraphServices.Abstraction;

public interface IInfoReceiver
{
Task<ActionResponse<NodeInfoDto>
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
};
}
}
8 changes: 0 additions & 8 deletions RelationshipAnalysis/Services/GraphServices/InfoReceiver.cs

This file was deleted.

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
};
}
}

0 comments on commit 6fb0189

Please sign in to comment.