File size: 1,618 Bytes
b9c7f0e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
using FlowAPI.Application.DTOs.Edge;
using FlowAPI.Application.Interfaces;
using FlowAPI.Domain.Entities;

namespace FlowAPI.Application.Services
{
    public class EdgeService : IEdgeService
    {
        private readonly IEdgeRepository _repo;

        public EdgeService(IEdgeRepository repo)
        {
            _repo = repo;
        }

        public async Task<IEnumerable<EdgeResponseDto>> GetAllByGraphIdAsync(Guid graphId)
        {
            var edges = await _repo.GetAllByGraphIdAsync(graphId);
            return edges.Select(MapToDto);
        }

        public async Task<EdgeResponseDto?> GetByIdAsync(Guid id)
        {
            var edge = await _repo.GetByIdAsync(id);
            return edge is null ? null : MapToDto(edge);
        }

        public async Task<EdgeResponseDto> CreateAsync(CreateEdgeDto dto)
        {
            var edge = new Edge
            {
                Id = Guid.NewGuid(),
                GraphId = dto.GraphId,
                FromNodeId = dto.FromNodeId,
                ToNodeId = dto.ToNodeId,
                Condition = dto.Condition
            };

            var created = await _repo.CreateAsync(edge);
            return MapToDto(created);
        }

        public async Task<bool> DeleteAsync(Guid id)
        {
            return await _repo.DeleteAsync(id);
        }

        private static EdgeResponseDto MapToDto(Edge edge) => new()
        {
            Id = edge.Id,
            GraphId = edge.GraphId,
            FromNodeId = edge.FromNodeId,
            ToNodeId = edge.ToNodeId,
            Condition = edge.Condition
        };
    }
}