Spaces:
Running
Running
| 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 | |
| }; | |
| } | |
| } | |