File size: 2,685 Bytes
b9c7f0e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1b321bd
 
 
 
 
 
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
using FlowAPI.Application.DTOs.TaskNode;
using FlowAPI.Application.Interfaces;
using FlowAPI.Domain.Entities;

namespace FlowAPI.Application.Services
{
    public class TaskNodeService : ITaskNodeService
    {
        private readonly ITaskNodeRepository _repo;

        public TaskNodeService(ITaskNodeRepository repo)
        {
            _repo = repo;
        }

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

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

        public async Task<TaskNodeResponseDto> CreateAsync(CreateTaskNodeDto dto)
        {
            if (dto.Label.Length > 30)
                throw new ArgumentException("Label cannot exceed 30 characters.");

            var node = new TaskNode
            {
                Id = Guid.NewGuid(),
                GraphId = dto.GraphId,
                Label = dto.Label,
                PosX = dto.PosX,
                PosY = dto.PosY,
                State = dto.State
            };

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

        public async Task<TaskNodeResponseDto?> UpdateAsync(Guid id, UpdateTaskNodeDto dto)
        {
            var node = await _repo.GetByIdAsync(id);
            if (node is null) return null;

            if (dto.Label is not null)
            {
                if (dto.Label.Length > 30) throw new ArgumentException("Label cannot exceed 30 characters.");
                node.Label = dto.Label;
            }
            if (dto.PosX.HasValue) node.PosX = dto.PosX.Value;
            if (dto.PosY.HasValue) node.PosY = dto.PosY.Value;
            if (dto.State.HasValue) node.State = dto.State.Value;

            var updated = await _repo.UpdateAsync(node);
            return MapToDto(updated);
        }

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

        public async Task<string?> GetNodeDataAsync(Guid id)
        {
            var node = await _repo.GetByIdAsync(id);
            return node?.Data;
        }

        private static TaskNodeResponseDto MapToDto(TaskNode node) => new()
        {
            Id = node.Id,
            GraphId = node.GraphId,
            Label = node.Label,
            PosX = node.PosX,
            PosY = node.PosY,
            State = node.State
        };
    }
}