Spaces:
Sleeping
Sleeping
Commit ·
b9c7f0e
1
Parent(s): 9c5dea4
init
Browse filesThis view is limited to 50 files because it contains too many changes. See raw diff
- .gitignore +21 -0
- Dockerfile +63 -0
- FlowAPI.Application/DTOs/AI/AIDtos.cs +36 -0
- FlowAPI.Application/DTOs/AchievementDto.cs +17 -0
- FlowAPI.Application/DTOs/Edge/EdgeDtos.cs +26 -0
- FlowAPI.Application/DTOs/Graph/GraphDtos.cs +69 -0
- FlowAPI.Application/DTOs/Habit/HabitDtos.cs +17 -0
- FlowAPI.Application/DTOs/Sync/SyncDtos.cs +27 -0
- FlowAPI.Application/DTOs/TaskNode/TaskNodeDtos.cs +32 -0
- FlowAPI.Application/DTOs/User/UserDtos.cs +43 -0
- FlowAPI.Application/DTOs/UserAchievementDto.cs +14 -0
- FlowAPI.Application/FlowAPI.Application.csproj +20 -0
- FlowAPI.Application/Interfaces/IAIService.cs +12 -0
- FlowAPI.Application/Interfaces/IAchievementRepository.cs +13 -0
- FlowAPI.Application/Interfaces/IAchievementService.cs +14 -0
- FlowAPI.Application/Interfaces/IEdgeRepository.cs +9 -0
- FlowAPI.Application/Interfaces/IEdgeService.cs +12 -0
- FlowAPI.Application/Interfaces/IGeminiBridgeManager.cs +9 -0
- FlowAPI.Application/Interfaces/IGenericRepository.cs +11 -0
- FlowAPI.Application/Interfaces/IGraphRepository.cs +12 -0
- FlowAPI.Application/Interfaces/IGraphService.cs +19 -0
- FlowAPI.Application/Interfaces/IHabitRecordRepository.cs +10 -0
- FlowAPI.Application/Interfaces/IHabitService.cs +11 -0
- FlowAPI.Application/Interfaces/IPaymentService.cs +8 -0
- FlowAPI.Application/Interfaces/ISharedGraphRepository.cs +14 -0
- FlowAPI.Application/Interfaces/ISyncService.cs +9 -0
- FlowAPI.Application/Interfaces/ITaskNodeRepository.cs +9 -0
- FlowAPI.Application/Interfaces/ITaskNodeService.cs +13 -0
- FlowAPI.Application/Interfaces/ITokenService.cs +9 -0
- FlowAPI.Application/Interfaces/IUserAchievementRepository.cs +13 -0
- FlowAPI.Application/Interfaces/IUserRepository.cs +9 -0
- FlowAPI.Application/Interfaces/IUserService.cs +15 -0
- FlowAPI.Application/Services/AIService.cs +740 -0
- FlowAPI.Application/Services/AchievementService.cs +161 -0
- FlowAPI.Application/Services/EdgeService.cs +57 -0
- FlowAPI.Application/Services/GeminiBridgeManager.cs +203 -0
- FlowAPI.Application/Services/GraphService.cs +320 -0
- FlowAPI.Application/Services/HabitService.cs +85 -0
- FlowAPI.Application/Services/PaymentService.cs +61 -0
- FlowAPI.Application/Services/SyncService.cs +95 -0
- FlowAPI.Application/Services/TaskNodeService.cs +80 -0
- FlowAPI.Application/Services/TokenService.cs +49 -0
- FlowAPI.Application/Services/UserService.cs +146 -0
- FlowAPI.Domain/Entities/Achievement.cs +23 -0
- FlowAPI.Domain/Entities/Edge.cs +18 -0
- FlowAPI.Domain/Entities/Graph.cs +18 -0
- FlowAPI.Domain/Entities/HabitRecord.cs +14 -0
- FlowAPI.Domain/Entities/SharedGraph.cs +17 -0
- FlowAPI.Domain/Entities/TaskNode.cs +22 -0
- FlowAPI.Domain/Entities/User.cs +19 -0
.gitignore
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
.vs/
|
| 2 |
+
bin/
|
| 3 |
+
obj/
|
| 4 |
+
*.user
|
| 5 |
+
*.lock.json
|
| 6 |
+
*.suo
|
| 7 |
+
*.userosscache
|
| 8 |
+
*.sln.docstates
|
| 9 |
+
out/
|
| 10 |
+
[Dd]ebug/
|
| 11 |
+
[Rr]elease/
|
| 12 |
+
appsettings.Development.json
|
| 13 |
+
*.db-shm
|
| 14 |
+
*.db-wal
|
| 15 |
+
bridge_log.txt
|
| 16 |
+
gemini_profile_bridge/
|
| 17 |
+
*.sqlite-shm
|
| 18 |
+
*.sqlite-wal
|
| 19 |
+
# Ignore SQLite database files to avoid checking them in
|
| 20 |
+
taskflow.db
|
| 21 |
+
/app/data/
|
Dockerfile
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Stage 1: Build .NET 10 API
|
| 2 |
+
FROM mcr.microsoft.com/dotnet/sdk:10.0 AS build
|
| 3 |
+
WORKDIR /src
|
| 4 |
+
|
| 5 |
+
# Copy csproj files and restore dependencies
|
| 6 |
+
COPY ["FlowAPI/FlowAPI.csproj", "FlowAPI/"]
|
| 7 |
+
COPY ["FlowAPI.Application/FlowAPI.Application.csproj", "FlowAPI.Application/"]
|
| 8 |
+
COPY ["FlowAPI.Domain/FlowAPI.Domain.csproj", "FlowAPI.Domain/"]
|
| 9 |
+
COPY ["FlowAPI.Infrastructure/FlowAPI.Infrastructure.csproj", "FlowAPI.Infrastructure/"]
|
| 10 |
+
RUN dotnet restore "FlowAPI/FlowAPI.csproj"
|
| 11 |
+
|
| 12 |
+
# Copy everything else and build
|
| 13 |
+
COPY . .
|
| 14 |
+
WORKDIR "/src/FlowAPI"
|
| 15 |
+
RUN dotnet publish "FlowAPI.csproj" -c Release -o /app/publish /p:UseAppHost=false
|
| 16 |
+
|
| 17 |
+
# Stage 2: Runtime
|
| 18 |
+
FROM mcr.microsoft.com/dotnet/aspnet:10.0 AS final
|
| 19 |
+
WORKDIR /app
|
| 20 |
+
EXPOSE 8080
|
| 21 |
+
EXPOSE 8081
|
| 22 |
+
|
| 23 |
+
# Install Python, pip, and system dependencies for Camoufox (Playwright/Firefox)
|
| 24 |
+
RUN apt-get update && apt-get install -y \
|
| 25 |
+
python3 \
|
| 26 |
+
python3-pip \
|
| 27 |
+
python3-venv \
|
| 28 |
+
curl \
|
| 29 |
+
libgtk-3-0 \
|
| 30 |
+
libasound2 \
|
| 31 |
+
libdbus-1-3 \
|
| 32 |
+
libx11-xcb1 \
|
| 33 |
+
libxcomposite1 \
|
| 34 |
+
libxdamage1 \
|
| 35 |
+
libxrandr2 \
|
| 36 |
+
libxtst6 \
|
| 37 |
+
libnss3 \
|
| 38 |
+
libatk1.0-0 \
|
| 39 |
+
libatk-bridge2.0-0 \
|
| 40 |
+
libdrm2 \
|
| 41 |
+
libgbm1 \
|
| 42 |
+
libxkbcommon0 \
|
| 43 |
+
libpango-1.0-0 \
|
| 44 |
+
libcairo2 \
|
| 45 |
+
&& rm -rf /var/lib/apt/lists/*
|
| 46 |
+
|
| 47 |
+
# Set up python virtual environment to install packages cleanly
|
| 48 |
+
RUN python3 -m venv /opt/venv
|
| 49 |
+
ENV PATH="/opt/venv/bin:$PATH"
|
| 50 |
+
|
| 51 |
+
# Install camoufox python library and its dependencies
|
| 52 |
+
RUN pip3 install --no-cache-dir camoufox
|
| 53 |
+
# Pre-download Camoufox/Playwright browser engines so they are ready
|
| 54 |
+
RUN python3 -m camoufox fetch
|
| 55 |
+
|
| 56 |
+
# Copy published .NET app
|
| 57 |
+
COPY --from=build /app/publish .
|
| 58 |
+
|
| 59 |
+
# Explicitly copy the python bridge script to ensure it is in the run directory
|
| 60 |
+
COPY --from=build /src/FlowAPI/gemini_bridge.py .
|
| 61 |
+
|
| 62 |
+
# Set entrypoint
|
| 63 |
+
ENTRYPOINT ["dotnet", "FlowAPI.dll"]
|
FlowAPI.Application/DTOs/AI/AIDtos.cs
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
using FlowAPI.Application.DTOs.TaskNode;
|
| 2 |
+
using FlowAPI.Application.DTOs.Edge;
|
| 3 |
+
using FlowAPI.Application.DTOs.Graph;
|
| 4 |
+
|
| 5 |
+
namespace FlowAPI.Application.DTOs.AI
|
| 6 |
+
{
|
| 7 |
+
public class GenerateAlternativeRequestDto
|
| 8 |
+
{
|
| 9 |
+
public Guid GraphId { get; set; }
|
| 10 |
+
public Guid FailedNodeId { get; set; }
|
| 11 |
+
}
|
| 12 |
+
|
| 13 |
+
public class AIPathResponseDto
|
| 14 |
+
{
|
| 15 |
+
public List<TaskNodeResponseDto> SuggestedNodes { get; set; } = new();
|
| 16 |
+
public List<EdgeResponseDto> SuggestedEdges { get; set; } = new();
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
+
public class GenerateGoalGraphRequestDto
|
| 20 |
+
{
|
| 21 |
+
public Guid UserId { get; set; }
|
| 22 |
+
public string Goal { get; set; } = string.Empty;
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
public class AICanvasAssistRequestDto
|
| 26 |
+
{
|
| 27 |
+
public Guid GraphId { get; set; }
|
| 28 |
+
public string UserPrompt { get; set; } = string.Empty;
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
public class AICanvasAssistResponseDto
|
| 32 |
+
{
|
| 33 |
+
public List<NodeDto> NewNodes { get; set; } = new();
|
| 34 |
+
public List<EdgeDto> NewEdges { get; set; } = new();
|
| 35 |
+
}
|
| 36 |
+
}
|
FlowAPI.Application/DTOs/AchievementDto.cs
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
using FlowAPI.Domain.Enums;
|
| 2 |
+
using System;
|
| 3 |
+
|
| 4 |
+
namespace FlowAPI.Application.DTOs
|
| 5 |
+
{
|
| 6 |
+
public class AchievementDto
|
| 7 |
+
{
|
| 8 |
+
public Guid Id { get; set; }
|
| 9 |
+
public string Title { get; set; } = string.Empty;
|
| 10 |
+
public string Description { get; set; } = string.Empty;
|
| 11 |
+
public string IconUrl { get; set; } = string.Empty;
|
| 12 |
+
public string Category { get; set; } = string.Empty;
|
| 13 |
+
public AchievementType Type { get; set; }
|
| 14 |
+
public int TargetValue { get; set; }
|
| 15 |
+
public int RewardPoints { get; set; }
|
| 16 |
+
}
|
| 17 |
+
}
|
FlowAPI.Application/DTOs/Edge/EdgeDtos.cs
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
using FlowAPI.Domain.Enums;
|
| 2 |
+
|
| 3 |
+
namespace FlowAPI.Application.DTOs.Edge
|
| 4 |
+
{
|
| 5 |
+
public class CreateEdgeDto
|
| 6 |
+
{
|
| 7 |
+
public Guid GraphId { get; set; }
|
| 8 |
+
public Guid FromNodeId { get; set; }
|
| 9 |
+
public Guid ToNodeId { get; set; }
|
| 10 |
+
public EdgeCondition Condition { get; set; } = EdgeCondition.True;
|
| 11 |
+
}
|
| 12 |
+
|
| 13 |
+
public class UpdateEdgeDto
|
| 14 |
+
{
|
| 15 |
+
public EdgeCondition? Condition { get; set; }
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
public class EdgeResponseDto
|
| 19 |
+
{
|
| 20 |
+
public Guid Id { get; set; }
|
| 21 |
+
public Guid GraphId { get; set; }
|
| 22 |
+
public Guid FromNodeId { get; set; }
|
| 23 |
+
public Guid ToNodeId { get; set; }
|
| 24 |
+
public EdgeCondition Condition { get; set; }
|
| 25 |
+
}
|
| 26 |
+
}
|
FlowAPI.Application/DTOs/Graph/GraphDtos.cs
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
using System;
|
| 2 |
+
using System.Collections.Generic;
|
| 3 |
+
|
| 4 |
+
namespace FlowAPI.Application.DTOs.Graph
|
| 5 |
+
{
|
| 6 |
+
public class CreateGraphDto
|
| 7 |
+
{
|
| 8 |
+
public Guid UserId { get; set; }
|
| 9 |
+
public string Title { get; set; } = string.Empty;
|
| 10 |
+
public string? Description { get; set; }
|
| 11 |
+
}
|
| 12 |
+
|
| 13 |
+
public class UpdateGraphDto
|
| 14 |
+
{
|
| 15 |
+
public string? Title { get; set; }
|
| 16 |
+
public string? Description { get; set; }
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
+
public class GraphResponseDto
|
| 20 |
+
{
|
| 21 |
+
public Guid Id { get; set; }
|
| 22 |
+
public Guid UserId { get; set; }
|
| 23 |
+
public string Title { get; set; } = string.Empty;
|
| 24 |
+
public string? Description { get; set; }
|
| 25 |
+
public DateTime CreatedAt { get; set; }
|
| 26 |
+
public bool IsShared { get; set; } = false;
|
| 27 |
+
public string Permission { get; set; } = "Owner"; // "Owner", "Read", "Edit"
|
| 28 |
+
}
|
| 29 |
+
|
| 30 |
+
public class FullGraphResponseDto : GraphResponseDto
|
| 31 |
+
{
|
| 32 |
+
public List<NodeDto> Nodes { get; set; } = new();
|
| 33 |
+
public List<EdgeDto> Edges { get; set; } = new();
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
public class ShareGraphRequestDto
|
| 37 |
+
{
|
| 38 |
+
public string Email { get; set; } = string.Empty;
|
| 39 |
+
public string Permission { get; set; } = "Read"; // "Read" or "Edit"
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
public class SharedUserResponseDto
|
| 43 |
+
{
|
| 44 |
+
public Guid Id { get; set; }
|
| 45 |
+
public Guid SharedWithUserId { get; set; }
|
| 46 |
+
public string Email { get; set; } = string.Empty;
|
| 47 |
+
public string DisplayName { get; set; } = string.Empty;
|
| 48 |
+
public string Permission { get; set; } = "Read";
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
+
public class NodeDto
|
| 52 |
+
{
|
| 53 |
+
public Guid Id { get; set; }
|
| 54 |
+
public string Label { get; set; } = string.Empty;
|
| 55 |
+
public double PosX { get; set; }
|
| 56 |
+
public double PosY { get; set; }
|
| 57 |
+
public string Type { get; set; } = "sketch";
|
| 58 |
+
public int State { get; set; }
|
| 59 |
+
public string? Decision { get; set; }
|
| 60 |
+
}
|
| 61 |
+
|
| 62 |
+
public class EdgeDto
|
| 63 |
+
{
|
| 64 |
+
public Guid Id { get; set; }
|
| 65 |
+
public Guid FromNodeId { get; set; }
|
| 66 |
+
public Guid ToNodeId { get; set; }
|
| 67 |
+
public string? Condition { get; set; }
|
| 68 |
+
}
|
| 69 |
+
}
|
FlowAPI.Application/DTOs/Habit/HabitDtos.cs
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
namespace FlowAPI.Application.DTOs.Habit
|
| 2 |
+
{
|
| 3 |
+
public class CreateHabitRecordDto
|
| 4 |
+
{
|
| 5 |
+
public Guid UserId { get; set; }
|
| 6 |
+
public string HabitName { get; set; } = string.Empty;
|
| 7 |
+
}
|
| 8 |
+
|
| 9 |
+
public class HabitRecordResponseDto
|
| 10 |
+
{
|
| 11 |
+
public Guid Id { get; set; }
|
| 12 |
+
public Guid UserId { get; set; }
|
| 13 |
+
public string HabitName { get; set; } = string.Empty;
|
| 14 |
+
public DateTime CheckedDate { get; set; }
|
| 15 |
+
public int StreakCount { get; set; }
|
| 16 |
+
}
|
| 17 |
+
}
|
FlowAPI.Application/DTOs/Sync/SyncDtos.cs
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
using FlowAPI.Application.DTOs.TaskNode;
|
| 2 |
+
using FlowAPI.Application.DTOs.Edge;
|
| 3 |
+
|
| 4 |
+
namespace FlowAPI.Application.DTOs.Sync
|
| 5 |
+
{
|
| 6 |
+
public class SyncRequestDto
|
| 7 |
+
{
|
| 8 |
+
public List<CreateTaskNodeDto> NewNodes { get; set; } = new();
|
| 9 |
+
public List<UpdateTaskNodeDtoWithId> UpdatedNodes { get; set; } = new();
|
| 10 |
+
public List<Guid> DeletedNodeIds { get; set; } = new();
|
| 11 |
+
|
| 12 |
+
public List<CreateEdgeDto> NewEdges { get; set; } = new();
|
| 13 |
+
public List<Guid> DeletedEdgeIds { get; set; } = new();
|
| 14 |
+
}
|
| 15 |
+
|
| 16 |
+
public class UpdateTaskNodeDtoWithId : UpdateTaskNodeDto
|
| 17 |
+
{
|
| 18 |
+
public Guid Id { get; set; }
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
+
public class SyncResponseDto
|
| 22 |
+
{
|
| 23 |
+
public bool Success { get; set; }
|
| 24 |
+
public string Message { get; set; } = string.Empty;
|
| 25 |
+
public DateTime ServerTime { get; set; } = DateTime.UtcNow;
|
| 26 |
+
}
|
| 27 |
+
}
|
FlowAPI.Application/DTOs/TaskNode/TaskNodeDtos.cs
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
using FlowAPI.Domain.Enums;
|
| 2 |
+
|
| 3 |
+
namespace FlowAPI.Application.DTOs.TaskNode
|
| 4 |
+
{
|
| 5 |
+
public class CreateTaskNodeDto
|
| 6 |
+
{
|
| 7 |
+
public Guid GraphId { get; set; }
|
| 8 |
+
public string Label { get; set; } = string.Empty;
|
| 9 |
+
public double PosX { get; set; }
|
| 10 |
+
public double PosY { get; set; }
|
| 11 |
+
public NodeState State { get; set; } = NodeState.Pending;
|
| 12 |
+
}
|
| 13 |
+
|
| 14 |
+
public class UpdateTaskNodeDto
|
| 15 |
+
{
|
| 16 |
+
public string? Label { get; set; }
|
| 17 |
+
public double? PosX { get; set; }
|
| 18 |
+
public double? PosY { get; set; }
|
| 19 |
+
public NodeState? State { get; set; }
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
+
public class TaskNodeResponseDto
|
| 23 |
+
{
|
| 24 |
+
public Guid Id { get; set; }
|
| 25 |
+
public Guid GraphId { get; set; }
|
| 26 |
+
public string Label { get; set; } = string.Empty;
|
| 27 |
+
public double PosX { get; set; }
|
| 28 |
+
public double PosY { get; set; }
|
| 29 |
+
public NodeState State { get; set; }
|
| 30 |
+
public DateTime CreatedAt { get; set; }
|
| 31 |
+
}
|
| 32 |
+
}
|
FlowAPI.Application/DTOs/User/UserDtos.cs
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
namespace FlowAPI.Application.DTOs.User
|
| 2 |
+
{
|
| 3 |
+
public class CreateUserDto
|
| 4 |
+
{
|
| 5 |
+
public string Email { get; set; } = string.Empty;
|
| 6 |
+
public string Password { get; set; } = string.Empty;
|
| 7 |
+
public string DisplayName { get; set; } = string.Empty;
|
| 8 |
+
}
|
| 9 |
+
|
| 10 |
+
public class UpdateUserDto
|
| 11 |
+
{
|
| 12 |
+
public string? DisplayName { get; set; }
|
| 13 |
+
public bool? IsPremium { get; set; }
|
| 14 |
+
public string? AvatarUrl { get; set; }
|
| 15 |
+
}
|
| 16 |
+
|
| 17 |
+
public class LoginDto
|
| 18 |
+
{
|
| 19 |
+
public string Email { get; set; } = string.Empty;
|
| 20 |
+
public string Password { get; set; } = string.Empty;
|
| 21 |
+
}
|
| 22 |
+
|
| 23 |
+
public class UserResponseDto
|
| 24 |
+
{
|
| 25 |
+
public Guid Id { get; set; }
|
| 26 |
+
public string Email { get; set; } = string.Empty;
|
| 27 |
+
public string DisplayName { get; set; } = string.Empty;
|
| 28 |
+
public bool IsPremium { get; set; }
|
| 29 |
+
public string? AvatarUrl { get; set; }
|
| 30 |
+
public DateTime CreatedAt { get; set; }
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
public class AuthResponseDto
|
| 34 |
+
{
|
| 35 |
+
public UserResponseDto User { get; set; } = null!;
|
| 36 |
+
public string Token { get; set; } = string.Empty;
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
public class GoogleLoginDto
|
| 40 |
+
{
|
| 41 |
+
public string IdToken { get; set; } = string.Empty;
|
| 42 |
+
}
|
| 43 |
+
}
|
FlowAPI.Application/DTOs/UserAchievementDto.cs
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
using System;
|
| 2 |
+
|
| 3 |
+
namespace FlowAPI.Application.DTOs
|
| 4 |
+
{
|
| 5 |
+
public class UserAchievementDto
|
| 6 |
+
{
|
| 7 |
+
public Guid Id { get; set; }
|
| 8 |
+
public Guid AchievementId { get; set; }
|
| 9 |
+
public AchievementDto Achievement { get; set; } = default!;
|
| 10 |
+
public int CurrentValue { get; set; }
|
| 11 |
+
public bool IsUnlocked { get; set; }
|
| 12 |
+
public DateTime? UnlockedAt { get; set; }
|
| 13 |
+
}
|
| 14 |
+
}
|
FlowAPI.Application/FlowAPI.Application.csproj
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<Project Sdk="Microsoft.NET.Sdk">
|
| 2 |
+
|
| 3 |
+
<PropertyGroup>
|
| 4 |
+
<TargetFramework>net10.0</TargetFramework>
|
| 5 |
+
<Nullable>enable</Nullable>
|
| 6 |
+
<ImplicitUsings>enable</ImplicitUsings>
|
| 7 |
+
</PropertyGroup>
|
| 8 |
+
|
| 9 |
+
<ItemGroup>
|
| 10 |
+
<ProjectReference Include="..\FlowAPI.Domain\FlowAPI.Domain.csproj" />
|
| 11 |
+
</ItemGroup>
|
| 12 |
+
|
| 13 |
+
<ItemGroup>
|
| 14 |
+
<PackageReference Include="Google.Apis.Auth" Version="1.74.0" />
|
| 15 |
+
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="10.0.8" />
|
| 16 |
+
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="10.0.8" />
|
| 17 |
+
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="8.18.0" />
|
| 18 |
+
</ItemGroup>
|
| 19 |
+
|
| 20 |
+
</Project>
|
FlowAPI.Application/Interfaces/IAIService.cs
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
using FlowAPI.Application.DTOs.AI;
|
| 2 |
+
using FlowAPI.Application.DTOs.Graph;
|
| 3 |
+
|
| 4 |
+
namespace FlowAPI.Application.Interfaces
|
| 5 |
+
{
|
| 6 |
+
public interface IAIService
|
| 7 |
+
{
|
| 8 |
+
Task<AIPathResponseDto> GenerateAlternativePathAsync(GenerateAlternativeRequestDto request);
|
| 9 |
+
Task<FullGraphResponseDto> GenerateGoalGraphAsync(GenerateGoalGraphRequestDto request);
|
| 10 |
+
Task<AICanvasAssistResponseDto> CanvasAssistAsync(AICanvasAssistRequestDto request);
|
| 11 |
+
}
|
| 12 |
+
}
|
FlowAPI.Application/Interfaces/IAchievementRepository.cs
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
using FlowAPI.Domain.Entities;
|
| 2 |
+
using System.Collections.Generic;
|
| 3 |
+
using System.Threading.Tasks;
|
| 4 |
+
|
| 5 |
+
namespace FlowAPI.Application.Interfaces
|
| 6 |
+
{
|
| 7 |
+
public interface IAchievementRepository : IGenericRepository<Achievement>
|
| 8 |
+
{
|
| 9 |
+
Task<IEnumerable<Achievement>> GetByTriggerEventAsync(string eventName);
|
| 10 |
+
Task<IEnumerable<Achievement>> GetByTitlesAsync(IEnumerable<string> titles);
|
| 11 |
+
Task AddRangeAsync(IEnumerable<Achievement> achievements);
|
| 12 |
+
}
|
| 13 |
+
}
|
FlowAPI.Application/Interfaces/IAchievementService.cs
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
using FlowAPI.Application.DTOs;
|
| 2 |
+
using System;
|
| 3 |
+
using System.Collections.Generic;
|
| 4 |
+
using System.Threading.Tasks;
|
| 5 |
+
|
| 6 |
+
namespace FlowAPI.Application.Interfaces
|
| 7 |
+
{
|
| 8 |
+
public interface IAchievementService
|
| 9 |
+
{
|
| 10 |
+
Task<IEnumerable<UserAchievementDto>> GetUserAchievementsAsync(Guid userId);
|
| 11 |
+
Task SeedAchievementsAsync();
|
| 12 |
+
Task ProcessEventAsync(Guid userId, string eventName, int amount = 1);
|
| 13 |
+
}
|
| 14 |
+
}
|
FlowAPI.Application/Interfaces/IEdgeRepository.cs
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
using FlowAPI.Domain.Entities;
|
| 2 |
+
|
| 3 |
+
namespace FlowAPI.Application.Interfaces
|
| 4 |
+
{
|
| 5 |
+
public interface IEdgeRepository : IGenericRepository<Edge>
|
| 6 |
+
{
|
| 7 |
+
Task<IEnumerable<Edge>> GetAllByGraphIdAsync(Guid graphId);
|
| 8 |
+
}
|
| 9 |
+
}
|
FlowAPI.Application/Interfaces/IEdgeService.cs
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
using FlowAPI.Application.DTOs.Edge;
|
| 2 |
+
|
| 3 |
+
namespace FlowAPI.Application.Interfaces
|
| 4 |
+
{
|
| 5 |
+
public interface IEdgeService
|
| 6 |
+
{
|
| 7 |
+
Task<IEnumerable<EdgeResponseDto>> GetAllByGraphIdAsync(Guid graphId);
|
| 8 |
+
Task<EdgeResponseDto?> GetByIdAsync(Guid id);
|
| 9 |
+
Task<EdgeResponseDto> CreateAsync(CreateEdgeDto dto);
|
| 10 |
+
Task<bool> DeleteAsync(Guid id);
|
| 11 |
+
}
|
| 12 |
+
}
|
FlowAPI.Application/Interfaces/IGeminiBridgeManager.cs
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
using System.Threading.Tasks;
|
| 2 |
+
|
| 3 |
+
namespace FlowAPI.Application.Interfaces
|
| 4 |
+
{
|
| 5 |
+
public interface IGeminiBridgeManager
|
| 6 |
+
{
|
| 7 |
+
Task<string> SendPromptAsync(string prompt, bool isNewChat, int timeoutSeconds = 120);
|
| 8 |
+
}
|
| 9 |
+
}
|
FlowAPI.Application/Interfaces/IGenericRepository.cs
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
namespace FlowAPI.Application.Interfaces
|
| 2 |
+
{
|
| 3 |
+
public interface IGenericRepository<T> where T : class
|
| 4 |
+
{
|
| 5 |
+
Task<IEnumerable<T>> GetAllAsync();
|
| 6 |
+
Task<T?> GetByIdAsync(Guid id);
|
| 7 |
+
Task<T> CreateAsync(T entity);
|
| 8 |
+
Task<T> UpdateAsync(T entity);
|
| 9 |
+
Task<bool> DeleteAsync(Guid id);
|
| 10 |
+
}
|
| 11 |
+
}
|
FlowAPI.Application/Interfaces/IGraphRepository.cs
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
using FlowAPI.Domain.Entities;
|
| 2 |
+
|
| 3 |
+
namespace FlowAPI.Application.Interfaces
|
| 4 |
+
{
|
| 5 |
+
public interface IGraphRepository : IGenericRepository<Graph>
|
| 6 |
+
{
|
| 7 |
+
Task<IEnumerable<Graph>> GetAllByUserIdAsync(Guid userId);
|
| 8 |
+
Task<Graph?> GetByIdWithDetailsAsync(Guid id);
|
| 9 |
+
Task ClearDetailsAsync(Guid id);
|
| 10 |
+
Task<bool> UpsertFullGraphAsync(Guid graphId, IEnumerable<TaskNode> nodes, IEnumerable<Edge> edges);
|
| 11 |
+
}
|
| 12 |
+
}
|
FlowAPI.Application/Interfaces/IGraphService.cs
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
using FlowAPI.Application.DTOs.Graph;
|
| 2 |
+
|
| 3 |
+
namespace FlowAPI.Application.Interfaces
|
| 4 |
+
{
|
| 5 |
+
public interface IGraphService
|
| 6 |
+
{
|
| 7 |
+
Task<IEnumerable<GraphResponseDto>> GetAllByUserIdAsync(Guid userId);
|
| 8 |
+
Task<GraphResponseDto?> GetByIdAsync(Guid id, Guid currentUserId);
|
| 9 |
+
Task<FullGraphResponseDto?> GetFullByIdAsync(Guid id, Guid currentUserId);
|
| 10 |
+
Task<GraphResponseDto> CreateAsync(CreateGraphDto dto, Guid currentUserId);
|
| 11 |
+
Task<GraphResponseDto?> UpdateAsync(Guid id, UpdateGraphDto dto, Guid currentUserId);
|
| 12 |
+
Task<bool> DeleteAsync(Guid id, Guid currentUserId);
|
| 13 |
+
Task<bool> SaveFullGraphAsync(Guid id, FullGraphResponseDto dto, Guid currentUserId);
|
| 14 |
+
|
| 15 |
+
Task<GraphResponseDto?> ShareGraphAsync(Guid graphId, Guid ownerId, ShareGraphRequestDto dto);
|
| 16 |
+
Task<IEnumerable<SharedUserResponseDto>> GetSharedUsersAsync(Guid graphId, Guid ownerId);
|
| 17 |
+
Task<bool> RevokeShareAsync(Guid graphId, Guid ownerId, Guid shareId);
|
| 18 |
+
}
|
| 19 |
+
}
|
FlowAPI.Application/Interfaces/IHabitRecordRepository.cs
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
using FlowAPI.Domain.Entities;
|
| 2 |
+
|
| 3 |
+
namespace FlowAPI.Application.Interfaces
|
| 4 |
+
{
|
| 5 |
+
public interface IHabitRecordRepository : IGenericRepository<HabitRecord>
|
| 6 |
+
{
|
| 7 |
+
Task<IEnumerable<HabitRecord>> GetAllByUserIdAsync(Guid userId);
|
| 8 |
+
Task<HabitRecord?> GetTodayRecordAsync(Guid userId, string habitName);
|
| 9 |
+
}
|
| 10 |
+
}
|
FlowAPI.Application/Interfaces/IHabitService.cs
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
using FlowAPI.Application.DTOs.Habit;
|
| 2 |
+
|
| 3 |
+
namespace FlowAPI.Application.Interfaces
|
| 4 |
+
{
|
| 5 |
+
public interface IHabitService
|
| 6 |
+
{
|
| 7 |
+
Task<IEnumerable<HabitRecordResponseDto>> GetAllByUserIdAsync(Guid userId);
|
| 8 |
+
Task<HabitRecordResponseDto> RecordHabitAsync(CreateHabitRecordDto dto);
|
| 9 |
+
Task<int> GetStreakAsync(Guid userId, string habitName);
|
| 10 |
+
}
|
| 11 |
+
}
|
FlowAPI.Application/Interfaces/IPaymentService.cs
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
namespace FlowAPI.Application.Interfaces
|
| 2 |
+
{
|
| 3 |
+
public interface IPaymentService
|
| 4 |
+
{
|
| 5 |
+
Task<string> CreateCheckoutSessionAsync(Guid userId);
|
| 6 |
+
Task<bool> HandleWebhookAsync(string payload, string signature);
|
| 7 |
+
}
|
| 8 |
+
}
|
FlowAPI.Application/Interfaces/ISharedGraphRepository.cs
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
using FlowAPI.Domain.Entities;
|
| 2 |
+
using System;
|
| 3 |
+
using System.Collections.Generic;
|
| 4 |
+
using System.Threading.Tasks;
|
| 5 |
+
|
| 6 |
+
namespace FlowAPI.Application.Interfaces
|
| 7 |
+
{
|
| 8 |
+
public interface ISharedGraphRepository : IGenericRepository<SharedGraph>
|
| 9 |
+
{
|
| 10 |
+
Task<IEnumerable<SharedGraph>> GetSharedGraphsByUserIdAsync(Guid userId);
|
| 11 |
+
Task<SharedGraph?> GetShareAsync(Guid graphId, Guid sharedWithUserId);
|
| 12 |
+
Task<IEnumerable<SharedGraph>> GetSharesForGraphAsync(Guid graphId);
|
| 13 |
+
}
|
| 14 |
+
}
|
FlowAPI.Application/Interfaces/ISyncService.cs
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
using FlowAPI.Application.DTOs.Sync;
|
| 2 |
+
|
| 3 |
+
namespace FlowAPI.Application.Interfaces
|
| 4 |
+
{
|
| 5 |
+
public interface ISyncService
|
| 6 |
+
{
|
| 7 |
+
Task<SyncResponseDto> SyncChangesAsync(Guid userId, SyncRequestDto request);
|
| 8 |
+
}
|
| 9 |
+
}
|
FlowAPI.Application/Interfaces/ITaskNodeRepository.cs
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
using FlowAPI.Domain.Entities;
|
| 2 |
+
|
| 3 |
+
namespace FlowAPI.Application.Interfaces
|
| 4 |
+
{
|
| 5 |
+
public interface ITaskNodeRepository : IGenericRepository<TaskNode>
|
| 6 |
+
{
|
| 7 |
+
Task<IEnumerable<TaskNode>> GetAllByGraphIdAsync(Guid graphId);
|
| 8 |
+
}
|
| 9 |
+
}
|
FlowAPI.Application/Interfaces/ITaskNodeService.cs
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
using FlowAPI.Application.DTOs.TaskNode;
|
| 2 |
+
|
| 3 |
+
namespace FlowAPI.Application.Interfaces
|
| 4 |
+
{
|
| 5 |
+
public interface ITaskNodeService
|
| 6 |
+
{
|
| 7 |
+
Task<IEnumerable<TaskNodeResponseDto>> GetAllByGraphIdAsync(Guid graphId);
|
| 8 |
+
Task<TaskNodeResponseDto?> GetByIdAsync(Guid id);
|
| 9 |
+
Task<TaskNodeResponseDto> CreateAsync(CreateTaskNodeDto dto);
|
| 10 |
+
Task<TaskNodeResponseDto?> UpdateAsync(Guid id, UpdateTaskNodeDto dto);
|
| 11 |
+
Task<bool> DeleteAsync(Guid id);
|
| 12 |
+
}
|
| 13 |
+
}
|
FlowAPI.Application/Interfaces/ITokenService.cs
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
using FlowAPI.Domain.Entities;
|
| 2 |
+
|
| 3 |
+
namespace FlowAPI.Application.Interfaces
|
| 4 |
+
{
|
| 5 |
+
public interface ITokenService
|
| 6 |
+
{
|
| 7 |
+
string CreateToken(User user);
|
| 8 |
+
}
|
| 9 |
+
}
|
FlowAPI.Application/Interfaces/IUserAchievementRepository.cs
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
using FlowAPI.Domain.Entities;
|
| 2 |
+
using System;
|
| 3 |
+
using System.Collections.Generic;
|
| 4 |
+
using System.Threading.Tasks;
|
| 5 |
+
|
| 6 |
+
namespace FlowAPI.Application.Interfaces
|
| 7 |
+
{
|
| 8 |
+
public interface IUserAchievementRepository : IGenericRepository<UserAchievement>
|
| 9 |
+
{
|
| 10 |
+
Task<IEnumerable<UserAchievement>> GetByUserIdAsync(Guid userId);
|
| 11 |
+
Task<IEnumerable<UserAchievement>> GetByUserAndAchievementIdsAsync(Guid userId, IEnumerable<Guid> achievementIds);
|
| 12 |
+
}
|
| 13 |
+
}
|
FlowAPI.Application/Interfaces/IUserRepository.cs
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
using FlowAPI.Domain.Entities;
|
| 2 |
+
|
| 3 |
+
namespace FlowAPI.Application.Interfaces
|
| 4 |
+
{
|
| 5 |
+
public interface IUserRepository : IGenericRepository<User>
|
| 6 |
+
{
|
| 7 |
+
Task<User?> GetByEmailAsync(string email);
|
| 8 |
+
}
|
| 9 |
+
}
|
FlowAPI.Application/Interfaces/IUserService.cs
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
using FlowAPI.Application.DTOs.User;
|
| 2 |
+
|
| 3 |
+
namespace FlowAPI.Application.Interfaces
|
| 4 |
+
{
|
| 5 |
+
public interface IUserService
|
| 6 |
+
{
|
| 7 |
+
Task<IEnumerable<UserResponseDto>> GetAllAsync();
|
| 8 |
+
Task<UserResponseDto?> GetByIdAsync(Guid id);
|
| 9 |
+
Task<AuthResponseDto> RegisterAsync(CreateUserDto dto);
|
| 10 |
+
Task<AuthResponseDto?> LoginAsync(LoginDto dto);
|
| 11 |
+
Task<AuthResponseDto> GoogleLoginAsync(GoogleLoginDto dto);
|
| 12 |
+
Task<UserResponseDto?> UpdateAsync(Guid id, UpdateUserDto dto);
|
| 13 |
+
Task<bool> DeleteAsync(Guid id);
|
| 14 |
+
}
|
| 15 |
+
}
|
FlowAPI.Application/Services/AIService.cs
ADDED
|
@@ -0,0 +1,740 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
using System;
|
| 2 |
+
using System.Collections.Generic;
|
| 3 |
+
using System.Diagnostics;
|
| 4 |
+
using System.IO;
|
| 5 |
+
using System.Linq;
|
| 6 |
+
using System.Text;
|
| 7 |
+
using System.Text.Json;
|
| 8 |
+
using System.Text.Encodings.Web;
|
| 9 |
+
using System.Text.Unicode;
|
| 10 |
+
using System.Text.RegularExpressions;
|
| 11 |
+
using System.Threading.Tasks;
|
| 12 |
+
using FlowAPI.Application.DTOs.AI;
|
| 13 |
+
using FlowAPI.Application.DTOs.Graph;
|
| 14 |
+
using FlowAPI.Application.Interfaces;
|
| 15 |
+
using Microsoft.Extensions.Configuration;
|
| 16 |
+
|
| 17 |
+
namespace FlowAPI.Application.Services
|
| 18 |
+
{
|
| 19 |
+
public class AIService : IAIService
|
| 20 |
+
{
|
| 21 |
+
private readonly IUserRepository _userRepo;
|
| 22 |
+
private readonly IGraphRepository _graphRepo;
|
| 23 |
+
private readonly IConfiguration _config;
|
| 24 |
+
private readonly IGraphService _graphService;
|
| 25 |
+
private readonly IGeminiBridgeManager _bridgeManager;
|
| 26 |
+
|
| 27 |
+
public AIService(IUserRepository userRepo, IGraphRepository graphRepo, IConfiguration config, IGraphService graphService, IGeminiBridgeManager bridgeManager)
|
| 28 |
+
{
|
| 29 |
+
_userRepo = userRepo;
|
| 30 |
+
_graphRepo = graphRepo;
|
| 31 |
+
_config = config;
|
| 32 |
+
_graphService = graphService;
|
| 33 |
+
_bridgeManager = bridgeManager;
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
public async Task<AIPathResponseDto> GenerateAlternativePathAsync(GenerateAlternativeRequestDto request)
|
| 37 |
+
{
|
| 38 |
+
// 1. Fetch graph to get user context
|
| 39 |
+
var graph = await _graphRepo.GetByIdAsync(request.GraphId);
|
| 40 |
+
if (graph == null) throw new Exception("Graph not found.");
|
| 41 |
+
|
| 42 |
+
// 2. Check Premium Status (REQ-03) - Bypassed for regular users for now
|
| 43 |
+
var user = await _userRepo.GetByIdAsync(graph.UserId);
|
| 44 |
+
if (user == null)
|
| 45 |
+
{
|
| 46 |
+
throw new Exception("User not found.");
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
+
// 3. Prepare AI Prompt
|
| 50 |
+
string prompt = $"Given a goal graph '{graph.Title}', the node {request.FailedNodeId} is blocked. " +
|
| 51 |
+
"Generate an alternative sequence of 3-5 steps to bypass this obstacle.";
|
| 52 |
+
|
| 53 |
+
return new AIPathResponseDto
|
| 54 |
+
{
|
| 55 |
+
SuggestedNodes = new()
|
| 56 |
+
{
|
| 57 |
+
new() { Id = Guid.NewGuid(), Label = "AI: Search for alternative resources", PosX = 100, PosY = 100 },
|
| 58 |
+
new() { Id = Guid.NewGuid(), Label = "AI: Contact external consultant", PosX = 200, PosY = 150 }
|
| 59 |
+
}
|
| 60 |
+
};
|
| 61 |
+
}
|
| 62 |
+
|
| 63 |
+
public async Task<FullGraphResponseDto> GenerateGoalGraphAsync(GenerateGoalGraphRequestDto request)
|
| 64 |
+
{
|
| 65 |
+
var serializeOptions = new JsonSerializerOptions { Encoder = JavaScriptEncoder.Create(UnicodeRanges.All) };
|
| 66 |
+
// 1. Check User Premium Status or just allow AI generation
|
| 67 |
+
var user = await _userRepo.GetByIdAsync(request.UserId);
|
| 68 |
+
if (user == null) throw new Exception("User not found.");
|
| 69 |
+
|
| 70 |
+
// 2. Formulate Prompt for Gemini
|
| 71 |
+
string promptText = $"You are an expert strategist and goal decomposition AI. Your task is to break down the user's goal into a sequential, logical tree or path of actionable tasks in Ukrainian.\n\n" +
|
| 72 |
+
$"User Goal: \"{request.Goal}\"\n\n" +
|
| 73 |
+
$"Create a structured, detailed roadmap of 6 to 12 sequential tasks (nodes) and their directed connections (edges) that lead the user to their goal.\n\n" +
|
| 74 |
+
$"Guidelines:\n" +
|
| 75 |
+
$"- Ensure all labels and description text are written in clear, natural Ukrainian.\n" +
|
| 76 |
+
$"- Avoid short, basic, generic titles. Write detailed, rich, and highly actionable labels for each node.\n" +
|
| 77 |
+
$"- Do NOT use the \"habit\" type under any circumstances. It is strictly forbidden. The allowed node types are \"sketch\" (standard task/goal) and \"condition\" (a decision/choice branch).\n" +
|
| 78 |
+
$"- Use \"condition\" nodes appropriately and dynamically when the path requires making a choice, evaluating options, or taking a different approach depending on criteria (e.g. \"Чи є досвід у програмуванні?\" or \"Вибір платформи\").\n" +
|
| 79 |
+
$"- A \"condition\" node can have custom choices (options) instead of just simple binary branches! To specify custom choices/branches for a condition node, include an \"options\" array containing option objects with custom \"id\" and \"label\" properties (e.g. \"options\": [ {{ \"id\": \"opt-ios\", \"label\": \"iOS\" }}, {{ \"id\": \"opt-android\", \"label\": \"Android\" }}, {{ \"id\": \"opt-web\", \"label\": \"Web\" }} ]).\n" +
|
| 80 |
+
$"- Outgoing edges from a \"condition\" node MUST map to the choice options by using the option's index inside the edge's \"condition\" field:\n" +
|
| 81 |
+
$" * The first option (index 0) corresponds to edge \"condition\": \"True\".\n" +
|
| 82 |
+
$" * The second option (index 1) corresponds to edge \"condition\": \"False\".\n" +
|
| 83 |
+
$" * The third option (index 2) corresponds to edge \"condition\": \"Blocked\".\n" +
|
| 84 |
+
$" * Any subsequent options (indices i >= 3) correspond to edge \"condition\": \"i\" (as a string, e.g. \"3\", \"4\", etc.).\n" +
|
| 85 |
+
$"- For coordinates, lay out the nodes visually flowing from top to bottom. Increase 'posY' by approximately 200-240 units for each sequential step. Vary 'posX' (e.g., between 200 and 700) if there are parallel branches or decision paths.\n" +
|
| 86 |
+
$"- Every node must have a unique string 'id' (like \"node_1\", \"node_2\") and a dynamic 'type' property (defaults to \"sketch\").\n" +
|
| 87 |
+
$"- All nodes must be reachable via edges starting from the initial steps.\n\n" +
|
| 88 |
+
$"Provide the output strictly in a valid JSON format following this schema, with no additional explanation, no markdown text outside the JSON, and no other content:\n" +
|
| 89 |
+
$"{{\n" +
|
| 90 |
+
$" \"title\": \"Коротка та приваблива назва дорожньої карти\",\n" +
|
| 91 |
+
$" \"description\": \"Короткий опис стратегії\",\n" +
|
| 92 |
+
$" \"nodes\": [\n" +
|
| 93 |
+
$" {{ \"id\": \"node_1\", \"label\": \"Детальний перший крок\", \"posX\": 450, \"posY\": 100, \"type\": \"sketch\" }},\n" +
|
| 94 |
+
$" {{ \"id\": \"node_2\", \"label\": \"Оберіть платформу для розробки\", \"posX\": 450, \"posY\": 300, \"type\": \"condition\", \"options\": [ {{ \"id\": \"opt-web\", \"label\": \"Web\" }}, {{ \"id\": \"opt-mobile\", \"label\": \"Mobile\" }}, {{ \"id\": \"opt-desktop\", \"label\": \"Desktop\" }} ] }},\n" +
|
| 95 |
+
$" {{ \"id\": \"node_3\", \"label\": \"Освоєння HTML, CSS та JavaScript\", \"posX\": 200, \"posY\": 520, \"type\": \"sketch\" }},\n" +
|
| 96 |
+
$" {{ \"id\": \"node_4\", \"label\": \"Вивчення Swift або Kotlin\", \"posX\": 450, \"posY\": 520, \"type\": \"sketch\" }},\n" +
|
| 97 |
+
$" {{ \"id\": \"node_5\", \"label\": \"Вивчення C# та .NET для настільних вікон\", \"posX\": 700, \"posY\": 520, \"type\": \"sketch\" }}\n" +
|
| 98 |
+
$" ],\n" +
|
| 99 |
+
$" \"edges\": [\n" +
|
| 100 |
+
$" {{ \"from\": \"node_1\", \"to\": \"node_2\", \"condition\": \"True\" }},\n" +
|
| 101 |
+
$" {{ \"from\": \"node_2\", \"to\": \"node_3\", \"condition\": \"True\" }},\n" +
|
| 102 |
+
$" {{ \"from\": \"node_2\", \"to\": \"node_4\", \"condition\": \"False\" }},\n" +
|
| 103 |
+
$" {{ \"from\": \"node_2\", \"to\": \"node_5\", \"condition\": \"Blocked\" }}\n" +
|
| 104 |
+
$" ]\n" +
|
| 105 |
+
$"}}";
|
| 106 |
+
|
| 107 |
+
// 3. Send Prompt via Persistent Bridge
|
| 108 |
+
string rawResponseJson = await _bridgeManager.SendPromptAsync(promptText, isNewChat: true, timeoutSeconds: 120);
|
| 109 |
+
|
| 110 |
+
// 7. Parse Bridge Message
|
| 111 |
+
using var jsonDoc = JsonDocument.Parse(rawResponseJson);
|
| 112 |
+
var root = jsonDoc.RootElement;
|
| 113 |
+
if (root.GetProperty("status").GetString() != "success")
|
| 114 |
+
{
|
| 115 |
+
string errMsg = root.TryGetProperty("error", out var errVal) ? errVal.GetString() ?? "Unknown error" : "Bridge generation failed";
|
| 116 |
+
throw new Exception("AI bridge returned error: " + errMsg);
|
| 117 |
+
}
|
| 118 |
+
|
| 119 |
+
string aiContent = root.GetProperty("content").GetString() ?? string.Empty;
|
| 120 |
+
string cleanJson = CleanJson(aiContent);
|
| 121 |
+
|
| 122 |
+
if (string.IsNullOrWhiteSpace(cleanJson))
|
| 123 |
+
{
|
| 124 |
+
throw new Exception("The AI bridge returned empty content. Please try generating again.");
|
| 125 |
+
}
|
| 126 |
+
|
| 127 |
+
// 8. Deserialize AI Roadmap Structure
|
| 128 |
+
AIGoalGraphResponse? parsedData;
|
| 129 |
+
try
|
| 130 |
+
{
|
| 131 |
+
parsedData = JsonSerializer.Deserialize<AIGoalGraphResponse>(cleanJson, new JsonSerializerOptions
|
| 132 |
+
{
|
| 133 |
+
PropertyNameCaseInsensitive = true
|
| 134 |
+
});
|
| 135 |
+
}
|
| 136 |
+
catch (JsonException ex)
|
| 137 |
+
{
|
| 138 |
+
string preview = aiContent.Length > 200 ? aiContent.Substring(0, 200) + "..." : aiContent;
|
| 139 |
+
throw new Exception($"Failed to parse AI response as JSON. Content: '{preview}'. Error: {ex.Message}", ex);
|
| 140 |
+
}
|
| 141 |
+
|
| 142 |
+
if (parsedData == null || parsedData.Nodes == null || parsedData.Nodes.Count == 0)
|
| 143 |
+
{
|
| 144 |
+
throw new Exception("The AI response did not contain a valid nodes list.");
|
| 145 |
+
}
|
| 146 |
+
|
| 147 |
+
// 9. Map temporary String IDs to Database GUIDs
|
| 148 |
+
var guidMap = new Dictionary<string, Guid>();
|
| 149 |
+
var nodeDtos = new List<NodeDto>();
|
| 150 |
+
|
| 151 |
+
for (int i = 0; i < parsedData.Nodes.Count; i++)
|
| 152 |
+
{
|
| 153 |
+
var aiNode = parsedData.Nodes[i];
|
| 154 |
+
var newGuid = Guid.NewGuid();
|
| 155 |
+
guidMap[aiNode.Id] = newGuid;
|
| 156 |
+
|
| 157 |
+
// Coordinate layout will be handled by the layout engine later
|
| 158 |
+
double finalX = aiNode.PosX;
|
| 159 |
+
double finalY = aiNode.PosY;
|
| 160 |
+
|
| 161 |
+
string? decisionJson = null;
|
| 162 |
+
if (aiNode.Type == "condition")
|
| 163 |
+
{
|
| 164 |
+
var optionsList = new List<object>();
|
| 165 |
+
if (aiNode.Options != null && aiNode.Options.Count > 0)
|
| 166 |
+
{
|
| 167 |
+
foreach (var opt in aiNode.Options)
|
| 168 |
+
{
|
| 169 |
+
optionsList.Add(new { id = opt.Id, label = opt.Label });
|
| 170 |
+
}
|
| 171 |
+
}
|
| 172 |
+
else
|
| 173 |
+
{
|
| 174 |
+
optionsList.Add(new { id = "opt-yes", label = "Так" });
|
| 175 |
+
optionsList.Add(new { id = "opt-no", label = "Ні" });
|
| 176 |
+
}
|
| 177 |
+
|
| 178 |
+
decisionJson = JsonSerializer.Serialize(new
|
| 179 |
+
{
|
| 180 |
+
options = optionsList,
|
| 181 |
+
selected = new string[] { },
|
| 182 |
+
multiSelect = false
|
| 183 |
+
}, serializeOptions);
|
| 184 |
+
}
|
| 185 |
+
|
| 186 |
+
nodeDtos.Add(new NodeDto
|
| 187 |
+
{
|
| 188 |
+
Id = newGuid,
|
| 189 |
+
Label = aiNode.Label,
|
| 190 |
+
PosX = finalX,
|
| 191 |
+
PosY = finalY,
|
| 192 |
+
Type = aiNode.Type ?? "sketch",
|
| 193 |
+
State = 0,
|
| 194 |
+
Decision = decisionJson
|
| 195 |
+
});
|
| 196 |
+
}
|
| 197 |
+
|
| 198 |
+
var edgeDtos = new List<EdgeDto>();
|
| 199 |
+
if (parsedData.Edges != null)
|
| 200 |
+
{
|
| 201 |
+
foreach (var aiEdge in parsedData.Edges)
|
| 202 |
+
{
|
| 203 |
+
if (guidMap.TryGetValue(aiEdge.From, out var fromGuid) &&
|
| 204 |
+
guidMap.TryGetValue(aiEdge.To, out var toGuid))
|
| 205 |
+
{
|
| 206 |
+
edgeDtos.Add(new EdgeDto
|
| 207 |
+
{
|
| 208 |
+
Id = Guid.NewGuid(),
|
| 209 |
+
FromNodeId = fromGuid,
|
| 210 |
+
ToNodeId = toGuid,
|
| 211 |
+
Condition = aiEdge.Condition ?? "True"
|
| 212 |
+
});
|
| 213 |
+
}
|
| 214 |
+
}
|
| 215 |
+
}
|
| 216 |
+
|
| 217 |
+
// 10. Apply Automated Beautiful Layout
|
| 218 |
+
ApplyBeautifulLayout(nodeDtos, edgeDtos);
|
| 219 |
+
|
| 220 |
+
// 11. Persist using existing GraphService
|
| 221 |
+
var createDto = new CreateGraphDto
|
| 222 |
+
{
|
| 223 |
+
UserId = request.UserId,
|
| 224 |
+
Title = string.IsNullOrWhiteSpace(parsedData.Title) ? request.Goal : parsedData.Title,
|
| 225 |
+
Description = string.IsNullOrWhiteSpace(parsedData.Description) ? "AI generated roadmap" : parsedData.Description
|
| 226 |
+
};
|
| 227 |
+
|
| 228 |
+
var createdGraph = await _graphService.CreateAsync(createDto, request.UserId);
|
| 229 |
+
|
| 230 |
+
var fullGraphDto = new FullGraphResponseDto
|
| 231 |
+
{
|
| 232 |
+
Id = createdGraph.Id,
|
| 233 |
+
UserId = createdGraph.UserId,
|
| 234 |
+
Title = createdGraph.Title,
|
| 235 |
+
Description = createdGraph.Description,
|
| 236 |
+
CreatedAt = createdGraph.CreatedAt,
|
| 237 |
+
Nodes = nodeDtos,
|
| 238 |
+
Edges = edgeDtos
|
| 239 |
+
};
|
| 240 |
+
|
| 241 |
+
bool saveSuccess = await _graphService.SaveFullGraphAsync(createdGraph.Id, fullGraphDto, request.UserId);
|
| 242 |
+
if (!saveSuccess)
|
| 243 |
+
{
|
| 244 |
+
throw new Exception("Graph was created, but failed to save the sequential roadmap steps.");
|
| 245 |
+
}
|
| 246 |
+
|
| 247 |
+
return fullGraphDto;
|
| 248 |
+
}
|
| 249 |
+
|
| 250 |
+
public async Task<AICanvasAssistResponseDto> CanvasAssistAsync(AICanvasAssistRequestDto request)
|
| 251 |
+
{
|
| 252 |
+
// 1. Fetch graph with all details
|
| 253 |
+
var graph = await _graphRepo.GetByIdWithDetailsAsync(request.GraphId);
|
| 254 |
+
if (graph == null) throw new Exception("Graph not found.");
|
| 255 |
+
|
| 256 |
+
// 2. Prepare serialization of active nodes and edges
|
| 257 |
+
var currentNodesList = graph.Nodes.Select(n => new {
|
| 258 |
+
id = n.Id.ToString(),
|
| 259 |
+
label = n.Label,
|
| 260 |
+
posX = n.PosX,
|
| 261 |
+
posY = n.PosY,
|
| 262 |
+
type = n.Type,
|
| 263 |
+
state = n.State.ToString()
|
| 264 |
+
}).ToList();
|
| 265 |
+
|
| 266 |
+
var currentEdgesList = graph.Edges.Select(e => new {
|
| 267 |
+
id = e.Id.ToString(),
|
| 268 |
+
from = e.FromNodeId.ToString(),
|
| 269 |
+
to = e.ToNodeId.ToString(),
|
| 270 |
+
condition = e.Condition.ToString()
|
| 271 |
+
}).ToList();
|
| 272 |
+
|
| 273 |
+
var serializeOptions = new JsonSerializerOptions
|
| 274 |
+
{
|
| 275 |
+
Encoder = JavaScriptEncoder.Create(UnicodeRanges.All)
|
| 276 |
+
};
|
| 277 |
+
|
| 278 |
+
// 4. Formulate Prompt for Gemini
|
| 279 |
+
string promptText = $"You are an expert strategist and goal decomposition AI.\n" +
|
| 280 |
+
$"The user has an active strategy roadmap titled \"{graph.Title}\" with the description \"{graph.Description}\" in Ukrainian.\n\n" +
|
| 281 |
+
$"Current Canvas Structure (in Ukrainian):\n" +
|
| 282 |
+
$"- Nodes: {JsonSerializer.Serialize(currentNodesList, serializeOptions)}\n" +
|
| 283 |
+
$"- Edges: {JsonSerializer.Serialize(currentEdgesList, serializeOptions)}\n\n" +
|
| 284 |
+
$"User Request: \"{request.UserPrompt}\"\n\n" +
|
| 285 |
+
$"Analyze the current roadmap, and satisfy the user's request. Suggest adding new steps, alternative/branching paths, or decision conditions.\n" +
|
| 286 |
+
$"Create a list of NEW nodes and edges that should be added/merged into the canvas to satisfy the request.\n\n" +
|
| 287 |
+
$"Guidelines:\n" +
|
| 288 |
+
$"- Ensure all labels and description text are written in clear, natural Ukrainian.\n" +
|
| 289 |
+
$"- Avoid short, basic, generic titles. Write detailed, rich, and highly actionable labels for each node.\n" +
|
| 290 |
+
$"- Do NOT use the \"habit\" type under any circumstances. It is strictly forbidden. The allowed node types are \"sketch\" (standard task/goal) and \"condition\" (a decision/choice branch).\n" +
|
| 291 |
+
$"- Use \"condition\" nodes appropriately and dynamically when the user request or alternative path requires a choice, criteria evaluation, or conditional gate.\n" +
|
| 292 |
+
$"- A \"condition\" node can have custom choices (options) instead of just simple binary branches! To specify custom choices/branches for a condition node, include an \"options\" array containing option objects with custom \"id\" and \"label\" properties (e.g. \"options\": [ {{ \"id\": \"opt-ios\", \"label\": \"iOS\" }}, {{ \"id\": \"opt-android\", \"label\": \"Android\" }}, {{ \"id\": \"opt-web\", \"label\": \"Web\" }} ]).\n" +
|
| 293 |
+
$"- Outgoing edges from a \"condition\" node MUST map to the choice options by using the option's index inside the edge's \"condition\" field:\n" +
|
| 294 |
+
$" * The first option (index 0) corresponds to edge \"condition\": \"True\".\n" +
|
| 295 |
+
$" * The second option (index 1) corresponds to edge \"condition\": \"False\".\n" +
|
| 296 |
+
$" * The third option (index 2) corresponds to edge \"condition\": \"Blocked\".\n" +
|
| 297 |
+
$" * Any subsequent options (indices i >= 3) correspond to edge \"condition\": \"i\" (as a string, e.g. \"3\", \"4\", etc.).\n" +
|
| 298 |
+
$"- For coordinates of new nodes, calculate 'posX' and 'posY' relative to nearby nodes so they flow nicely. Use values that don't directly overlap existing coordinates.\n" +
|
| 299 |
+
$"- Every new node must have a unique string 'id' starting with \"new_node_\" (like \"new_node_1\", \"new_node_2\") and a dynamic 'type' property (defaults to \"sketch\").\n" +
|
| 300 |
+
$"- You can also use existing node IDs (from the current structure) in the 'from' and 'to' fields of the new edges to link the new nodes into the existing flow!\n" +
|
| 301 |
+
$"- All new edges should connect new nodes to each other, or to existing nodes.\n\n" +
|
| 302 |
+
$"Provide the output strictly in a valid JSON format following this schema, with no explanation, no markdown text outside the JSON, and no other content:\n" +
|
| 303 |
+
$"{{\n" +
|
| 304 |
+
$" \"newNodes\": [\n" +
|
| 305 |
+
$" {{ \"id\": \"new_node_1\", \"label\": \"Детальний новий крок для розширення\", \"posX\": 450, \"posY\": 300, \"type\": \"sketch\" }},\n" +
|
| 306 |
+
$" {{ \"id\": \"new_node_2\", \"label\": \"Нова умова вибору середовища\", \"posX\": 450, \"posY\": 520, \"type\": \"condition\", \"options\": [ {{ \"id\": \"opt-react\", \"label\": \"React\" }}, {{ \"id\": \"opt-vue\", \"label\": \"Vue\" }}, {{ \"id\": \"opt-angular\", \"label\": \"Angular\" }} ] }},\n" +
|
| 307 |
+
$" {{ \"id\": \"new_node_3\", \"label\": \"Вивчення React hooks та JSX\", \"posX\": 200, \"posY\": 740, \"type\": \"sketch\" }},\n" +
|
| 308 |
+
$" {{ \"id\": \"new_node_4\", \"label\": \"Вивчення Composition API у Vue\", \"posX\": 450, \"posY\": 740, \"type\": \"sketch\" }},\n" +
|
| 309 |
+
$" {{ \"id\": \"new_node_5\", \"label\": \"Вивченн�� TypeScript та RxJS в Angular\", \"posX\": 700, \"posY\": 740, \"type\": \"sketch\" }}\n" +
|
| 310 |
+
$" ],\n" +
|
| 311 |
+
$" \"newEdges\": [\n" +
|
| 312 |
+
$" {{ \"from\": \"existing_node_id_here\", \"to\": \"new_node_1\" }},\n" +
|
| 313 |
+
$" {{ \"from\": \"new_node_1\", \"to\": \"new_node_2\" }},\n" +
|
| 314 |
+
$" {{ \"from\": \"new_node_2\", \"to\": \"new_node_3\", \"condition\": \"True\" }},\n" +
|
| 315 |
+
$" {{ \"from\": \"new_node_2\", \"to\": \"new_node_4\", \"condition\": \"False\" }},\n" +
|
| 316 |
+
$" {{ \"from\": \"new_node_2\", \"to\": \"new_node_5\", \"condition\": \"Blocked\" }}\n" +
|
| 317 |
+
$" ]\n" +
|
| 318 |
+
$"}}";
|
| 319 |
+
|
| 320 |
+
// 3. Send Prompt via Persistent Bridge
|
| 321 |
+
string rawResponseJson = await _bridgeManager.SendPromptAsync(promptText, isNewChat: true, timeoutSeconds: 120);
|
| 322 |
+
if (string.IsNullOrEmpty(rawResponseJson))
|
| 323 |
+
{
|
| 324 |
+
throw new Exception("Did not receive a valid response block from the Gemini Bridge.");
|
| 325 |
+
}
|
| 326 |
+
|
| 327 |
+
// Parse Bridge Message
|
| 328 |
+
using var jsonDoc = JsonDocument.Parse(rawResponseJson);
|
| 329 |
+
var root = jsonDoc.RootElement;
|
| 330 |
+
|
| 331 |
+
if (root.GetProperty("status").GetString() != "success")
|
| 332 |
+
{
|
| 333 |
+
string errMsg = root.TryGetProperty("error", out var errVal) ? errVal.GetString() ?? "Unknown error" : "Bridge generation failed";
|
| 334 |
+
throw new Exception("AI bridge returned error: " + errMsg);
|
| 335 |
+
}
|
| 336 |
+
|
| 337 |
+
string aiContent = root.GetProperty("content").GetString() ?? string.Empty;
|
| 338 |
+
string cleanJson = CleanJson(aiContent);
|
| 339 |
+
|
| 340 |
+
if (string.IsNullOrWhiteSpace(cleanJson))
|
| 341 |
+
{
|
| 342 |
+
throw new Exception("The AI bridge returned empty canvas assist content. Please try generating again.");
|
| 343 |
+
}
|
| 344 |
+
|
| 345 |
+
// Deserialize AI Assist Response Structure
|
| 346 |
+
AICanvasAssistResponseInternal? parsedData;
|
| 347 |
+
try
|
| 348 |
+
{
|
| 349 |
+
parsedData = JsonSerializer.Deserialize<AICanvasAssistResponseInternal>(cleanJson, new JsonSerializerOptions
|
| 350 |
+
{
|
| 351 |
+
PropertyNameCaseInsensitive = true
|
| 352 |
+
});
|
| 353 |
+
}
|
| 354 |
+
catch (JsonException ex)
|
| 355 |
+
{
|
| 356 |
+
string preview = aiContent.Length > 200 ? aiContent.Substring(0, 200) + "..." : aiContent;
|
| 357 |
+
throw new Exception($"Failed to parse Canvas Assist AI response as JSON. Content: '{preview}'. Error: {ex.Message}", ex);
|
| 358 |
+
}
|
| 359 |
+
|
| 360 |
+
if (parsedData == null || parsedData.NewNodes == null)
|
| 361 |
+
{
|
| 362 |
+
throw new Exception("The AI response did not contain a valid newNodes list.");
|
| 363 |
+
}
|
| 364 |
+
|
| 365 |
+
// Map temporary IDs to database GUIDs
|
| 366 |
+
var guidMap = new Dictionary<string, Guid>();
|
| 367 |
+
var newNodesEntities = new List<FlowAPI.Domain.Entities.TaskNode>();
|
| 368 |
+
var nodeDtos = new List<NodeDto>();
|
| 369 |
+
|
| 370 |
+
for (int i = 0; i < parsedData.NewNodes.Count; i++)
|
| 371 |
+
{
|
| 372 |
+
var aiNode = parsedData.NewNodes[i];
|
| 373 |
+
var newGuid = Guid.NewGuid();
|
| 374 |
+
guidMap[aiNode.Id] = newGuid;
|
| 375 |
+
|
| 376 |
+
string? decisionJson = null;
|
| 377 |
+
if (aiNode.Type == "condition")
|
| 378 |
+
{
|
| 379 |
+
var optionsList = new List<object>();
|
| 380 |
+
if (aiNode.Options != null && aiNode.Options.Count > 0)
|
| 381 |
+
{
|
| 382 |
+
foreach (var opt in aiNode.Options)
|
| 383 |
+
{
|
| 384 |
+
optionsList.Add(new { id = opt.Id, label = opt.Label });
|
| 385 |
+
}
|
| 386 |
+
}
|
| 387 |
+
else
|
| 388 |
+
{
|
| 389 |
+
optionsList.Add(new { id = "opt-yes", label = "Так" });
|
| 390 |
+
optionsList.Add(new { id = "opt-no", label = "Ні" });
|
| 391 |
+
}
|
| 392 |
+
|
| 393 |
+
decisionJson = JsonSerializer.Serialize(new
|
| 394 |
+
{
|
| 395 |
+
options = optionsList,
|
| 396 |
+
selected = new string[] { },
|
| 397 |
+
multiSelect = false
|
| 398 |
+
}, serializeOptions);
|
| 399 |
+
}
|
| 400 |
+
|
| 401 |
+
var nodeEntity = new FlowAPI.Domain.Entities.TaskNode
|
| 402 |
+
{
|
| 403 |
+
Id = newGuid,
|
| 404 |
+
GraphId = request.GraphId,
|
| 405 |
+
Label = aiNode.Label,
|
| 406 |
+
PosX = aiNode.PosX,
|
| 407 |
+
PosY = aiNode.PosY,
|
| 408 |
+
Type = aiNode.Type ?? "sketch",
|
| 409 |
+
State = FlowAPI.Domain.Enums.NodeState.Pending,
|
| 410 |
+
Decision = decisionJson,
|
| 411 |
+
CreatedAt = DateTime.UtcNow
|
| 412 |
+
};
|
| 413 |
+
newNodesEntities.Add(nodeEntity);
|
| 414 |
+
|
| 415 |
+
nodeDtos.Add(new NodeDto
|
| 416 |
+
{
|
| 417 |
+
Id = newGuid,
|
| 418 |
+
Label = aiNode.Label,
|
| 419 |
+
PosX = aiNode.PosX,
|
| 420 |
+
PosY = aiNode.PosY,
|
| 421 |
+
Type = aiNode.Type ?? "sketch",
|
| 422 |
+
State = 0,
|
| 423 |
+
Decision = decisionJson
|
| 424 |
+
});
|
| 425 |
+
}
|
| 426 |
+
|
| 427 |
+
var newEdgesEntities = new List<FlowAPI.Domain.Entities.Edge>();
|
| 428 |
+
var edgeDtos = new List<EdgeDto>();
|
| 429 |
+
|
| 430 |
+
if (parsedData.NewEdges != null)
|
| 431 |
+
{
|
| 432 |
+
foreach (var aiEdge in parsedData.NewEdges)
|
| 433 |
+
{
|
| 434 |
+
Guid fromGuid;
|
| 435 |
+
if (Guid.TryParse(aiEdge.From, out var fromParsed))
|
| 436 |
+
{
|
| 437 |
+
fromGuid = fromParsed;
|
| 438 |
+
}
|
| 439 |
+
else if (guidMap.TryGetValue(aiEdge.From, out var mappedFrom))
|
| 440 |
+
{
|
| 441 |
+
fromGuid = mappedFrom;
|
| 442 |
+
}
|
| 443 |
+
else
|
| 444 |
+
{
|
| 445 |
+
continue;
|
| 446 |
+
}
|
| 447 |
+
|
| 448 |
+
Guid toGuid;
|
| 449 |
+
if (Guid.TryParse(aiEdge.To, out var toParsed))
|
| 450 |
+
{
|
| 451 |
+
toGuid = toParsed;
|
| 452 |
+
}
|
| 453 |
+
else if (guidMap.TryGetValue(aiEdge.To, out var mappedTo))
|
| 454 |
+
{
|
| 455 |
+
toGuid = mappedTo;
|
| 456 |
+
}
|
| 457 |
+
else
|
| 458 |
+
{
|
| 459 |
+
continue;
|
| 460 |
+
}
|
| 461 |
+
|
| 462 |
+
var edgeGuid = Guid.NewGuid();
|
| 463 |
+
var edgeEntity = new FlowAPI.Domain.Entities.Edge
|
| 464 |
+
{
|
| 465 |
+
Id = edgeGuid,
|
| 466 |
+
GraphId = request.GraphId,
|
| 467 |
+
FromNodeId = fromGuid,
|
| 468 |
+
ToNodeId = toGuid,
|
| 469 |
+
Condition = Enum.TryParse<FlowAPI.Domain.Enums.EdgeCondition>(aiEdge.Condition, true, out var conditionEnum) ? conditionEnum : FlowAPI.Domain.Enums.EdgeCondition.True
|
| 470 |
+
};
|
| 471 |
+
newEdgesEntities.Add(edgeEntity);
|
| 472 |
+
|
| 473 |
+
edgeDtos.Add(new EdgeDto
|
| 474 |
+
{
|
| 475 |
+
Id = edgeGuid,
|
| 476 |
+
FromNodeId = fromGuid,
|
| 477 |
+
ToNodeId = toGuid,
|
| 478 |
+
Condition = aiEdge.Condition ?? "True"
|
| 479 |
+
});
|
| 480 |
+
}
|
| 481 |
+
}
|
| 482 |
+
|
| 483 |
+
// Apply Automated Beautiful Layout to the entire combined graph
|
| 484 |
+
var allNodes = graph.Nodes.Concat(newNodesEntities).ToList();
|
| 485 |
+
var allEdges = graph.Edges.Concat(newEdgesEntities).ToList();
|
| 486 |
+
|
| 487 |
+
ApplyBeautifulLayout(allNodes, allEdges);
|
| 488 |
+
|
| 489 |
+
// Update the DTOs with the beautifully laid out coordinates so the frontend places them perfectly
|
| 490 |
+
foreach (var dto in nodeDtos)
|
| 491 |
+
{
|
| 492 |
+
var entity = allNodes.FirstOrDefault(n => n.Id == dto.Id);
|
| 493 |
+
if (entity != null)
|
| 494 |
+
{
|
| 495 |
+
dto.PosX = entity.PosX;
|
| 496 |
+
dto.PosY = entity.PosY;
|
| 497 |
+
}
|
| 498 |
+
}
|
| 499 |
+
|
| 500 |
+
// Persist directly using repository
|
| 501 |
+
bool saveSuccess = await _graphRepo.UpsertFullGraphAsync(request.GraphId, allNodes, allEdges);
|
| 502 |
+
if (!saveSuccess)
|
| 503 |
+
{
|
| 504 |
+
throw new Exception("Failed to persist newly suggested AI canvas elements to database.");
|
| 505 |
+
}
|
| 506 |
+
|
| 507 |
+
return new AICanvasAssistResponseDto
|
| 508 |
+
{
|
| 509 |
+
NewNodes = nodeDtos,
|
| 510 |
+
NewEdges = edgeDtos
|
| 511 |
+
};
|
| 512 |
+
}
|
| 513 |
+
|
| 514 |
+
private string CleanJson(string text)
|
| 515 |
+
{
|
| 516 |
+
text = text.Trim();
|
| 517 |
+
if (text.StartsWith("```"))
|
| 518 |
+
{
|
| 519 |
+
var lines = text.Split('\n');
|
| 520 |
+
var cleanLines = lines.Where(line => !line.Trim().StartsWith("```")).ToArray();
|
| 521 |
+
text = string.Join("\n", cleanLines).Trim();
|
| 522 |
+
}
|
| 523 |
+
return text;
|
| 524 |
+
}
|
| 525 |
+
|
| 526 |
+
private void ApplyBeautifulLayout(List<NodeDto> nodes, List<EdgeDto> edges)
|
| 527 |
+
{
|
| 528 |
+
if (nodes == null || nodes.Count == 0) return;
|
| 529 |
+
|
| 530 |
+
var inDegree = new Dictionary<Guid, int>();
|
| 531 |
+
var adj = new Dictionary<Guid, List<Guid>>();
|
| 532 |
+
|
| 533 |
+
foreach (var node in nodes)
|
| 534 |
+
{
|
| 535 |
+
inDegree[node.Id] = 0;
|
| 536 |
+
adj[node.Id] = new List<Guid>();
|
| 537 |
+
}
|
| 538 |
+
|
| 539 |
+
foreach (var edge in edges)
|
| 540 |
+
{
|
| 541 |
+
if (inDegree.ContainsKey(edge.ToNodeId) && inDegree.ContainsKey(edge.FromNodeId))
|
| 542 |
+
{
|
| 543 |
+
inDegree[edge.ToNodeId]++;
|
| 544 |
+
adj[edge.FromNodeId].Add(edge.ToNodeId);
|
| 545 |
+
}
|
| 546 |
+
}
|
| 547 |
+
|
| 548 |
+
var layers = new Dictionary<Guid, int>();
|
| 549 |
+
foreach (var node in nodes) { layers[node.Id] = 0; }
|
| 550 |
+
|
| 551 |
+
var queue = new Queue<Guid>();
|
| 552 |
+
foreach (var node in nodes)
|
| 553 |
+
{
|
| 554 |
+
if (inDegree[node.Id] == 0) { queue.Enqueue(node.Id); }
|
| 555 |
+
}
|
| 556 |
+
|
| 557 |
+
if (queue.Count == 0 && nodes.Count > 0)
|
| 558 |
+
{
|
| 559 |
+
queue.Enqueue(nodes[0].Id);
|
| 560 |
+
}
|
| 561 |
+
|
| 562 |
+
var processed = new HashSet<Guid>();
|
| 563 |
+
while (queue.Count > 0)
|
| 564 |
+
{
|
| 565 |
+
var curr = queue.Dequeue();
|
| 566 |
+
processed.Add(curr);
|
| 567 |
+
|
| 568 |
+
int currentLayer = layers[curr];
|
| 569 |
+
foreach (var neighbor in adj[curr])
|
| 570 |
+
{
|
| 571 |
+
layers[neighbor] = Math.Max(layers[neighbor], currentLayer + 1);
|
| 572 |
+
if (!processed.Contains(neighbor) && !queue.Contains(neighbor))
|
| 573 |
+
{
|
| 574 |
+
queue.Enqueue(neighbor);
|
| 575 |
+
}
|
| 576 |
+
}
|
| 577 |
+
}
|
| 578 |
+
|
| 579 |
+
var nodesByLayer = new Dictionary<int, List<NodeDto>>();
|
| 580 |
+
foreach (var node in nodes)
|
| 581 |
+
{
|
| 582 |
+
int layer = layers.ContainsKey(node.Id) ? layers[node.Id] : 0;
|
| 583 |
+
if (!nodesByLayer.ContainsKey(layer)) { nodesByLayer[layer] = new List<NodeDto>(); }
|
| 584 |
+
nodesByLayer[layer].Add(node);
|
| 585 |
+
}
|
| 586 |
+
|
| 587 |
+
double marginY = 100;
|
| 588 |
+
double spacingY = 220;
|
| 589 |
+
double spacingX = 350;
|
| 590 |
+
double centerX = 450;
|
| 591 |
+
|
| 592 |
+
var sortedLayers = nodesByLayer.Keys.OrderBy(l => l).ToList();
|
| 593 |
+
foreach (var layer in sortedLayers)
|
| 594 |
+
{
|
| 595 |
+
var layerNodes = nodesByLayer[layer];
|
| 596 |
+
int count = layerNodes.Count;
|
| 597 |
+
for (int i = 0; i < count; i++)
|
| 598 |
+
{
|
| 599 |
+
var node = layerNodes[i];
|
| 600 |
+
node.PosY = marginY + layer * spacingY;
|
| 601 |
+
node.PosX = centerX + (i - (count - 1) / 2.0) * spacingX;
|
| 602 |
+
}
|
| 603 |
+
}
|
| 604 |
+
}
|
| 605 |
+
|
| 606 |
+
private void ApplyBeautifulLayout(List<FlowAPI.Domain.Entities.TaskNode> nodes, List<FlowAPI.Domain.Entities.Edge> edges)
|
| 607 |
+
{
|
| 608 |
+
if (nodes == null || nodes.Count == 0) return;
|
| 609 |
+
|
| 610 |
+
var inDegree = new Dictionary<Guid, int>();
|
| 611 |
+
var adj = new Dictionary<Guid, List<Guid>>();
|
| 612 |
+
|
| 613 |
+
foreach (var node in nodes)
|
| 614 |
+
{
|
| 615 |
+
inDegree[node.Id] = 0;
|
| 616 |
+
adj[node.Id] = new List<Guid>();
|
| 617 |
+
}
|
| 618 |
+
|
| 619 |
+
foreach (var edge in edges)
|
| 620 |
+
{
|
| 621 |
+
if (inDegree.ContainsKey(edge.ToNodeId) && inDegree.ContainsKey(edge.FromNodeId))
|
| 622 |
+
{
|
| 623 |
+
inDegree[edge.ToNodeId]++;
|
| 624 |
+
adj[edge.FromNodeId].Add(edge.ToNodeId);
|
| 625 |
+
}
|
| 626 |
+
}
|
| 627 |
+
|
| 628 |
+
var layers = new Dictionary<Guid, int>();
|
| 629 |
+
foreach (var node in nodes) { layers[node.Id] = 0; }
|
| 630 |
+
|
| 631 |
+
var queue = new Queue<Guid>();
|
| 632 |
+
foreach (var node in nodes)
|
| 633 |
+
{
|
| 634 |
+
if (inDegree[node.Id] == 0) { queue.Enqueue(node.Id); }
|
| 635 |
+
}
|
| 636 |
+
|
| 637 |
+
if (queue.Count == 0 && nodes.Count > 0)
|
| 638 |
+
{
|
| 639 |
+
queue.Enqueue(nodes[0].Id);
|
| 640 |
+
}
|
| 641 |
+
|
| 642 |
+
var processed = new HashSet<Guid>();
|
| 643 |
+
while (queue.Count > 0)
|
| 644 |
+
{
|
| 645 |
+
var curr = queue.Dequeue();
|
| 646 |
+
processed.Add(curr);
|
| 647 |
+
|
| 648 |
+
int currentLayer = layers[curr];
|
| 649 |
+
foreach (var neighbor in adj[curr])
|
| 650 |
+
{
|
| 651 |
+
layers[neighbor] = Math.Max(layers[neighbor], currentLayer + 1);
|
| 652 |
+
if (!processed.Contains(neighbor) && !queue.Contains(neighbor))
|
| 653 |
+
{
|
| 654 |
+
queue.Enqueue(neighbor);
|
| 655 |
+
}
|
| 656 |
+
}
|
| 657 |
+
}
|
| 658 |
+
|
| 659 |
+
var nodesByLayer = new Dictionary<int, List<FlowAPI.Domain.Entities.TaskNode>>();
|
| 660 |
+
foreach (var node in nodes)
|
| 661 |
+
{
|
| 662 |
+
int layer = layers.ContainsKey(node.Id) ? layers[node.Id] : 0;
|
| 663 |
+
if (!nodesByLayer.ContainsKey(layer)) { nodesByLayer[layer] = new List<FlowAPI.Domain.Entities.TaskNode>(); }
|
| 664 |
+
nodesByLayer[layer].Add(node);
|
| 665 |
+
}
|
| 666 |
+
|
| 667 |
+
double marginY = 100;
|
| 668 |
+
double spacingY = 220;
|
| 669 |
+
double spacingX = 350;
|
| 670 |
+
double centerX = 450;
|
| 671 |
+
|
| 672 |
+
var sortedLayers = nodesByLayer.Keys.OrderBy(l => l).ToList();
|
| 673 |
+
foreach (var layer in sortedLayers)
|
| 674 |
+
{
|
| 675 |
+
var layerNodes = nodesByLayer[layer];
|
| 676 |
+
int count = layerNodes.Count;
|
| 677 |
+
for (int i = 0; i < count; i++)
|
| 678 |
+
{
|
| 679 |
+
var node = layerNodes[i];
|
| 680 |
+
node.PosY = marginY + layer * spacingY;
|
| 681 |
+
node.PosX = centerX + (i - (count - 1) / 2.0) * spacingX;
|
| 682 |
+
}
|
| 683 |
+
}
|
| 684 |
+
}
|
| 685 |
+
|
| 686 |
+
private class AIGoalGraphResponse
|
| 687 |
+
{
|
| 688 |
+
public string Title { get; set; } = string.Empty;
|
| 689 |
+
public string Description { get; set; } = string.Empty;
|
| 690 |
+
public List<AINodeDto> Nodes { get; set; } = new();
|
| 691 |
+
public List<AIEdgeDto> Edges { get; set; } = new();
|
| 692 |
+
}
|
| 693 |
+
|
| 694 |
+
private class AIOptionDto
|
| 695 |
+
{
|
| 696 |
+
public string Id { get; set; } = string.Empty;
|
| 697 |
+
public string Label { get; set; } = string.Empty;
|
| 698 |
+
}
|
| 699 |
+
|
| 700 |
+
private class AINodeDto
|
| 701 |
+
{
|
| 702 |
+
public string Id { get; set; } = string.Empty;
|
| 703 |
+
public string Label { get; set; } = string.Empty;
|
| 704 |
+
public double PosX { get; set; }
|
| 705 |
+
public double PosY { get; set; }
|
| 706 |
+
public string? Type { get; set; }
|
| 707 |
+
public List<AIOptionDto>? Options { get; set; }
|
| 708 |
+
}
|
| 709 |
+
|
| 710 |
+
private class AIEdgeDto
|
| 711 |
+
{
|
| 712 |
+
public string From { get; set; } = string.Empty;
|
| 713 |
+
public string To { get; set; } = string.Empty;
|
| 714 |
+
public string? Condition { get; set; }
|
| 715 |
+
}
|
| 716 |
+
|
| 717 |
+
private class AICanvasAssistResponseInternal
|
| 718 |
+
{
|
| 719 |
+
public List<AINewNodeInternalDto> NewNodes { get; set; } = new();
|
| 720 |
+
public List<AINewEdgeInternalDto> NewEdges { get; set; } = new();
|
| 721 |
+
}
|
| 722 |
+
|
| 723 |
+
private class AINewNodeInternalDto
|
| 724 |
+
{
|
| 725 |
+
public string Id { get; set; } = string.Empty;
|
| 726 |
+
public string Label { get; set; } = string.Empty;
|
| 727 |
+
public double PosX { get; set; }
|
| 728 |
+
public double PosY { get; set; }
|
| 729 |
+
public string? Type { get; set; }
|
| 730 |
+
public List<AIOptionDto>? Options { get; set; }
|
| 731 |
+
}
|
| 732 |
+
|
| 733 |
+
private class AINewEdgeInternalDto
|
| 734 |
+
{
|
| 735 |
+
public string From { get; set; } = string.Empty;
|
| 736 |
+
public string To { get; set; } = string.Empty;
|
| 737 |
+
public string? Condition { get; set; }
|
| 738 |
+
}
|
| 739 |
+
}
|
| 740 |
+
}
|
FlowAPI.Application/Services/AchievementService.cs
ADDED
|
@@ -0,0 +1,161 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
using FlowAPI.Application.DTOs;
|
| 2 |
+
using FlowAPI.Application.Interfaces;
|
| 3 |
+
using FlowAPI.Domain.Entities;
|
| 4 |
+
using FlowAPI.Domain.Enums;
|
| 5 |
+
using System;
|
| 6 |
+
using System.Collections.Generic;
|
| 7 |
+
using System.Linq;
|
| 8 |
+
using System.Threading.Tasks;
|
| 9 |
+
|
| 10 |
+
namespace FlowAPI.Application.Services
|
| 11 |
+
{
|
| 12 |
+
public class AchievementService : IAchievementService
|
| 13 |
+
{
|
| 14 |
+
private readonly IAchievementRepository _achievementRepo;
|
| 15 |
+
private readonly IUserAchievementRepository _userAchievementRepo;
|
| 16 |
+
private readonly IUserRepository _userRepo;
|
| 17 |
+
|
| 18 |
+
public AchievementService(IAchievementRepository achievementRepo, IUserAchievementRepository userAchievementRepo, IUserRepository userRepo)
|
| 19 |
+
{
|
| 20 |
+
_achievementRepo = achievementRepo;
|
| 21 |
+
_userAchievementRepo = userAchievementRepo;
|
| 22 |
+
_userRepo = userRepo;
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
public async Task<IEnumerable<UserAchievementDto>> GetUserAchievementsAsync(Guid userId)
|
| 26 |
+
{
|
| 27 |
+
var allAchievements = await _achievementRepo.GetAllAsync();
|
| 28 |
+
var userAchievements = await _userAchievementRepo.GetByUserIdAsync(userId);
|
| 29 |
+
|
| 30 |
+
var user = await _userRepo.GetByIdAsync(userId);
|
| 31 |
+
var isAvatarUploaded = user != null && !string.IsNullOrEmpty(user.AvatarUrl);
|
| 32 |
+
|
| 33 |
+
var result = new List<UserAchievementDto>();
|
| 34 |
+
|
| 35 |
+
foreach (var achievement in allAchievements)
|
| 36 |
+
{
|
| 37 |
+
var userRecord = userAchievements.FirstOrDefault(ua => ua.AchievementId == achievement.Id);
|
| 38 |
+
|
| 39 |
+
// Retroactively unlock Self Portrait if user already has an avatar set but no record or locked record in DB
|
| 40 |
+
if (achievement.Title == "Self Portrait" && isAvatarUploaded)
|
| 41 |
+
{
|
| 42 |
+
if (userRecord == null)
|
| 43 |
+
{
|
| 44 |
+
var newUa = new UserAchievement
|
| 45 |
+
{
|
| 46 |
+
UserId = userId,
|
| 47 |
+
AchievementId = achievement.Id,
|
| 48 |
+
CurrentValue = 1,
|
| 49 |
+
IsUnlocked = true,
|
| 50 |
+
UnlockedAt = DateTime.UtcNow
|
| 51 |
+
};
|
| 52 |
+
userRecord = await _userAchievementRepo.CreateAsync(newUa);
|
| 53 |
+
}
|
| 54 |
+
else if (!userRecord.IsUnlocked)
|
| 55 |
+
{
|
| 56 |
+
userRecord.CurrentValue = 1;
|
| 57 |
+
userRecord.IsUnlocked = true;
|
| 58 |
+
userRecord.UnlockedAt = DateTime.UtcNow;
|
| 59 |
+
userRecord = await _userAchievementRepo.UpdateAsync(userRecord);
|
| 60 |
+
}
|
| 61 |
+
}
|
| 62 |
+
|
| 63 |
+
result.Add(new UserAchievementDto
|
| 64 |
+
{
|
| 65 |
+
Id = userRecord?.Id ?? Guid.Empty,
|
| 66 |
+
AchievementId = achievement.Id,
|
| 67 |
+
CurrentValue = userRecord?.CurrentValue ?? 0,
|
| 68 |
+
IsUnlocked = userRecord?.IsUnlocked ?? false,
|
| 69 |
+
UnlockedAt = userRecord?.UnlockedAt,
|
| 70 |
+
Achievement = new AchievementDto
|
| 71 |
+
{
|
| 72 |
+
Id = achievement.Id,
|
| 73 |
+
Title = achievement.Title,
|
| 74 |
+
Description = achievement.Description,
|
| 75 |
+
IconUrl = achievement.IconUrl,
|
| 76 |
+
Category = achievement.Category,
|
| 77 |
+
Type = achievement.Type,
|
| 78 |
+
TargetValue = achievement.TargetValue,
|
| 79 |
+
RewardPoints = achievement.RewardPoints
|
| 80 |
+
}
|
| 81 |
+
});
|
| 82 |
+
}
|
| 83 |
+
|
| 84 |
+
return result;
|
| 85 |
+
}
|
| 86 |
+
|
| 87 |
+
public async Task ProcessEventAsync(Guid userId, string eventName, int amount = 1)
|
| 88 |
+
{
|
| 89 |
+
// 1. Find achievements triggered by this event
|
| 90 |
+
var triggeredAchievements = await _achievementRepo.GetByTriggerEventAsync(eventName);
|
| 91 |
+
|
| 92 |
+
if (!triggeredAchievements.Any()) return;
|
| 93 |
+
|
| 94 |
+
// 2. Fetch user's current progress for these achievements
|
| 95 |
+
var achievementIds = triggeredAchievements.Select(a => a.Id).ToList();
|
| 96 |
+
var userAchievements = await _userAchievementRepo.GetByUserAndAchievementIdsAsync(userId, achievementIds);
|
| 97 |
+
|
| 98 |
+
foreach (var achievement in triggeredAchievements)
|
| 99 |
+
{
|
| 100 |
+
var ua = userAchievements.FirstOrDefault(u => u.AchievementId == achievement.Id);
|
| 101 |
+
|
| 102 |
+
// If user doesn't have a tracking record yet, create it
|
| 103 |
+
if (ua == null)
|
| 104 |
+
{
|
| 105 |
+
ua = new UserAchievement
|
| 106 |
+
{
|
| 107 |
+
UserId = userId,
|
| 108 |
+
AchievementId = achievement.Id,
|
| 109 |
+
CurrentValue = 0,
|
| 110 |
+
IsUnlocked = false
|
| 111 |
+
};
|
| 112 |
+
ua = await _userAchievementRepo.CreateAsync(ua);
|
| 113 |
+
}
|
| 114 |
+
|
| 115 |
+
// Skip if already unlocked
|
| 116 |
+
if (ua.IsUnlocked) continue;
|
| 117 |
+
|
| 118 |
+
// Update progress
|
| 119 |
+
if (achievement.Type == AchievementType.Accumulative || achievement.Type == AchievementType.SingleAction)
|
| 120 |
+
{
|
| 121 |
+
ua.CurrentValue += amount;
|
| 122 |
+
}
|
| 123 |
+
|
| 124 |
+
// Check for unlock
|
| 125 |
+
if (ua.CurrentValue >= achievement.TargetValue)
|
| 126 |
+
{
|
| 127 |
+
ua.CurrentValue = achievement.TargetValue;
|
| 128 |
+
ua.IsUnlocked = true;
|
| 129 |
+
ua.UnlockedAt = DateTime.UtcNow;
|
| 130 |
+
}
|
| 131 |
+
|
| 132 |
+
await _userAchievementRepo.UpdateAsync(ua);
|
| 133 |
+
}
|
| 134 |
+
}
|
| 135 |
+
|
| 136 |
+
public async Task SeedAchievementsAsync()
|
| 137 |
+
{
|
| 138 |
+
var seedData = new List<Achievement>
|
| 139 |
+
{
|
| 140 |
+
new Achievement { Title = "Blank Canvas", Description = "Create your first project (Graph).", Category = "Projects", Type = AchievementType.SingleAction, TargetValue = 1, RewardPoints = 10, TriggerEventName = "GraphCreated" },
|
| 141 |
+
new Achievement { Title = "Connecting the Dots", Description = "Create 10 connections (Edges) between tasks.", Category = "Tasks", Type = AchievementType.Accumulative, TargetValue = 10, RewardPoints = 50, TriggerEventName = "EdgeCreated" },
|
| 142 |
+
new Achievement { Title = "Sketch Master", Description = "Create 100 task nodes overall.", Category = "Tasks", Type = AchievementType.Accumulative, TargetValue = 100, RewardPoints = 100, TriggerEventName = "NodeCreated" },
|
| 143 |
+
new Achievement { Title = "Daily Doodler", Description = "Track habits 7 times.", Category = "Habits", Type = AchievementType.Accumulative, TargetValue = 7, RewardPoints = 70, TriggerEventName = "HabitTracked" },
|
| 144 |
+
new Achievement { Title = "Decision Maker", Description = "Complete 50 tasks.", Category = "Tasks", Type = AchievementType.Accumulative, TargetValue = 50, RewardPoints = 50, TriggerEventName = "TaskCompleted" },
|
| 145 |
+
new Achievement { Title = "Clear Desk", Description = "Delete 5 projects.", Category = "System", Type = AchievementType.Accumulative, TargetValue = 5, RewardPoints = 20, TriggerEventName = "GraphDeleted" },
|
| 146 |
+
new Achievement { Title = "First Milestone", Description = "Complete your first task.", Category = "Tasks", Type = AchievementType.SingleAction, TargetValue = 1, RewardPoints = 15, TriggerEventName = "TaskCompleted" },
|
| 147 |
+
new Achievement { Title = "Self Portrait", Description = "Upload your profile avatar.", Category = "System", Type = AchievementType.SingleAction, TargetValue = 1, RewardPoints = 15, TriggerEventName = "AvatarUploaded" }
|
| 148 |
+
};
|
| 149 |
+
|
| 150 |
+
var existing = await _achievementRepo.GetByTitlesAsync(seedData.Select(s => s.Title));
|
| 151 |
+
var existingTitles = existing.Select(e => e.Title).ToList();
|
| 152 |
+
|
| 153 |
+
var toAdd = seedData.Where(s => !existingTitles.Contains(s.Title)).ToList();
|
| 154 |
+
|
| 155 |
+
if (toAdd.Any())
|
| 156 |
+
{
|
| 157 |
+
await _achievementRepo.AddRangeAsync(toAdd);
|
| 158 |
+
}
|
| 159 |
+
}
|
| 160 |
+
}
|
| 161 |
+
}
|
FlowAPI.Application/Services/EdgeService.cs
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
using FlowAPI.Application.DTOs.Edge;
|
| 2 |
+
using FlowAPI.Application.Interfaces;
|
| 3 |
+
using FlowAPI.Domain.Entities;
|
| 4 |
+
|
| 5 |
+
namespace FlowAPI.Application.Services
|
| 6 |
+
{
|
| 7 |
+
public class EdgeService : IEdgeService
|
| 8 |
+
{
|
| 9 |
+
private readonly IEdgeRepository _repo;
|
| 10 |
+
|
| 11 |
+
public EdgeService(IEdgeRepository repo)
|
| 12 |
+
{
|
| 13 |
+
_repo = repo;
|
| 14 |
+
}
|
| 15 |
+
|
| 16 |
+
public async Task<IEnumerable<EdgeResponseDto>> GetAllByGraphIdAsync(Guid graphId)
|
| 17 |
+
{
|
| 18 |
+
var edges = await _repo.GetAllByGraphIdAsync(graphId);
|
| 19 |
+
return edges.Select(MapToDto);
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
+
public async Task<EdgeResponseDto?> GetByIdAsync(Guid id)
|
| 23 |
+
{
|
| 24 |
+
var edge = await _repo.GetByIdAsync(id);
|
| 25 |
+
return edge is null ? null : MapToDto(edge);
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
public async Task<EdgeResponseDto> CreateAsync(CreateEdgeDto dto)
|
| 29 |
+
{
|
| 30 |
+
var edge = new Edge
|
| 31 |
+
{
|
| 32 |
+
Id = Guid.NewGuid(),
|
| 33 |
+
GraphId = dto.GraphId,
|
| 34 |
+
FromNodeId = dto.FromNodeId,
|
| 35 |
+
ToNodeId = dto.ToNodeId,
|
| 36 |
+
Condition = dto.Condition
|
| 37 |
+
};
|
| 38 |
+
|
| 39 |
+
var created = await _repo.CreateAsync(edge);
|
| 40 |
+
return MapToDto(created);
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
public async Task<bool> DeleteAsync(Guid id)
|
| 44 |
+
{
|
| 45 |
+
return await _repo.DeleteAsync(id);
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
private static EdgeResponseDto MapToDto(Edge edge) => new()
|
| 49 |
+
{
|
| 50 |
+
Id = edge.Id,
|
| 51 |
+
GraphId = edge.GraphId,
|
| 52 |
+
FromNodeId = edge.FromNodeId,
|
| 53 |
+
ToNodeId = edge.ToNodeId,
|
| 54 |
+
Condition = edge.Condition
|
| 55 |
+
};
|
| 56 |
+
}
|
| 57 |
+
}
|
FlowAPI.Application/Services/GeminiBridgeManager.cs
ADDED
|
@@ -0,0 +1,203 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
using System;
|
| 2 |
+
using System.Diagnostics;
|
| 3 |
+
using System.IO;
|
| 4 |
+
using System.Text.Json;
|
| 5 |
+
using System.Text.Encodings.Web;
|
| 6 |
+
using System.Text.Unicode;
|
| 7 |
+
using System.Threading;
|
| 8 |
+
using System.Threading.Tasks;
|
| 9 |
+
using FlowAPI.Application.Interfaces;
|
| 10 |
+
using Microsoft.Extensions.Hosting;
|
| 11 |
+
using Microsoft.Extensions.Logging;
|
| 12 |
+
|
| 13 |
+
namespace FlowAPI.Application.Services
|
| 14 |
+
{
|
| 15 |
+
public class GeminiBridgeManager : IGeminiBridgeManager, IDisposable
|
| 16 |
+
{
|
| 17 |
+
private readonly ILogger<GeminiBridgeManager> _logger;
|
| 18 |
+
private readonly SemaphoreSlim _semaphore = new SemaphoreSlim(1, 1);
|
| 19 |
+
private Process? _process;
|
| 20 |
+
private bool _isReady = false;
|
| 21 |
+
private readonly JsonSerializerOptions _serializeOptions;
|
| 22 |
+
|
| 23 |
+
public GeminiBridgeManager(ILogger<GeminiBridgeManager> logger, IHostApplicationLifetime appLifetime)
|
| 24 |
+
{
|
| 25 |
+
_logger = logger;
|
| 26 |
+
_serializeOptions = new JsonSerializerOptions
|
| 27 |
+
{
|
| 28 |
+
Encoder = JavaScriptEncoder.Create(UnicodeRanges.All)
|
| 29 |
+
};
|
| 30 |
+
|
| 31 |
+
appLifetime.ApplicationStopping.Register(() =>
|
| 32 |
+
{
|
| 33 |
+
_logger.LogInformation("Application stopping, closing Gemini Bridge.");
|
| 34 |
+
Dispose();
|
| 35 |
+
});
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
private async Task EnsureProcessStartedAsync()
|
| 39 |
+
{
|
| 40 |
+
if (_process != null && !_process.HasExited && _isReady)
|
| 41 |
+
return;
|
| 42 |
+
|
| 43 |
+
_logger.LogInformation("Starting persistent Gemini Bridge process...");
|
| 44 |
+
|
| 45 |
+
string scriptPath = Path.Combine(AppContext.BaseDirectory, "gemini_bridge.py");
|
| 46 |
+
if (!File.Exists(scriptPath))
|
| 47 |
+
{
|
| 48 |
+
scriptPath = Path.Combine(Directory.GetCurrentDirectory(), "gemini_bridge.py");
|
| 49 |
+
}
|
| 50 |
+
if (!File.Exists(scriptPath))
|
| 51 |
+
{
|
| 52 |
+
scriptPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "..", "..", "..", "gemini_bridge.py");
|
| 53 |
+
}
|
| 54 |
+
|
| 55 |
+
if (!File.Exists(scriptPath))
|
| 56 |
+
{
|
| 57 |
+
throw new FileNotFoundException("Could not locate gemini_bridge.py in application folders.");
|
| 58 |
+
}
|
| 59 |
+
|
| 60 |
+
var startInfo = new ProcessStartInfo
|
| 61 |
+
{
|
| 62 |
+
FileName = "python",
|
| 63 |
+
Arguments = $"\"{scriptPath}\" --headless",
|
| 64 |
+
RedirectStandardInput = true,
|
| 65 |
+
RedirectStandardOutput = true,
|
| 66 |
+
RedirectStandardError = true,
|
| 67 |
+
UseShellExecute = false,
|
| 68 |
+
CreateNoWindow = true,
|
| 69 |
+
WorkingDirectory = Path.GetDirectoryName(scriptPath)
|
| 70 |
+
};
|
| 71 |
+
|
| 72 |
+
_process = new Process { StartInfo = startInfo };
|
| 73 |
+
_process.Start();
|
| 74 |
+
|
| 75 |
+
// Fire and forget error logging
|
| 76 |
+
_ = Task.Run(async () =>
|
| 77 |
+
{
|
| 78 |
+
while (_process != null && !_process.HasExited && !_process.StandardError.EndOfStream)
|
| 79 |
+
{
|
| 80 |
+
var errLine = await _process.StandardError.ReadLineAsync();
|
| 81 |
+
if (!string.IsNullOrEmpty(errLine))
|
| 82 |
+
{
|
| 83 |
+
_logger.LogWarning($"[GeminiBridge Python]: {errLine}");
|
| 84 |
+
}
|
| 85 |
+
}
|
| 86 |
+
});
|
| 87 |
+
|
| 88 |
+
// Wait for bridge to declare itself "ready"
|
| 89 |
+
var readyTimeout = Task.Delay(TimeSpan.FromSeconds(45));
|
| 90 |
+
var readReadyTask = Task.Run(async () =>
|
| 91 |
+
{
|
| 92 |
+
while (_process != null && !_process.HasExited && !_process.StandardOutput.EndOfStream)
|
| 93 |
+
{
|
| 94 |
+
var l = await _process.StandardOutput.ReadLineAsync();
|
| 95 |
+
if (l != null && l.Contains("\"type\": \"ready\""))
|
| 96 |
+
{
|
| 97 |
+
return true;
|
| 98 |
+
}
|
| 99 |
+
}
|
| 100 |
+
return false;
|
| 101 |
+
});
|
| 102 |
+
|
| 103 |
+
var completedTask = await Task.WhenAny(readReadyTask, readyTimeout);
|
| 104 |
+
if (completedTask == readyTimeout || !await readReadyTask)
|
| 105 |
+
{
|
| 106 |
+
try { _process.Kill(); } catch { }
|
| 107 |
+
_process = null;
|
| 108 |
+
throw new Exception("Timeout or failure waiting for persistent Gemini Browser Bridge to initialize.");
|
| 109 |
+
}
|
| 110 |
+
|
| 111 |
+
_isReady = true;
|
| 112 |
+
_logger.LogInformation("Persistent Gemini Bridge initialized successfully.");
|
| 113 |
+
}
|
| 114 |
+
|
| 115 |
+
public async Task<string> SendPromptAsync(string promptText, bool isNewChat, int timeoutSeconds = 120)
|
| 116 |
+
{
|
| 117 |
+
await _semaphore.WaitAsync();
|
| 118 |
+
try
|
| 119 |
+
{
|
| 120 |
+
await EnsureProcessStartedAsync();
|
| 121 |
+
|
| 122 |
+
if (_process == null || _process.HasExited)
|
| 123 |
+
throw new Exception("Gemini Bridge process is not running.");
|
| 124 |
+
|
| 125 |
+
var promptObj = new
|
| 126 |
+
{
|
| 127 |
+
type = "prompt",
|
| 128 |
+
id = Guid.NewGuid().ToString(),
|
| 129 |
+
new_chat = isNewChat,
|
| 130 |
+
content = promptText
|
| 131 |
+
};
|
| 132 |
+
|
| 133 |
+
string promptRequestJson = JsonSerializer.Serialize(promptObj, _serializeOptions);
|
| 134 |
+
|
| 135 |
+
// Send Prompt to standard input
|
| 136 |
+
await _process.StandardInput.WriteLineAsync(promptRequestJson);
|
| 137 |
+
await _process.StandardInput.FlushAsync();
|
| 138 |
+
|
| 139 |
+
// Await Gemini final response
|
| 140 |
+
var responseTimeout = Task.Delay(TimeSpan.FromSeconds(timeoutSeconds));
|
| 141 |
+
var readResponseTask = Task.Run(async () =>
|
| 142 |
+
{
|
| 143 |
+
while (_process != null && !_process.HasExited && !_process.StandardOutput.EndOfStream)
|
| 144 |
+
{
|
| 145 |
+
var l = await _process.StandardOutput.ReadLineAsync();
|
| 146 |
+
if (l != null && l.Contains("\"type\": \"response\"") && l.Contains($"\"id\": \"{promptObj.id}\""))
|
| 147 |
+
{
|
| 148 |
+
return l;
|
| 149 |
+
}
|
| 150 |
+
}
|
| 151 |
+
return null;
|
| 152 |
+
});
|
| 153 |
+
|
| 154 |
+
var responseCompletedTask = await Task.WhenAny(readResponseTask, responseTimeout);
|
| 155 |
+
if (responseCompletedTask == responseTimeout)
|
| 156 |
+
{
|
| 157 |
+
// If timeout occurs, we should probably kill the process to avoid desync
|
| 158 |
+
try { _process.Kill(); } catch { }
|
| 159 |
+
_process = null;
|
| 160 |
+
_isReady = false;
|
| 161 |
+
throw new Exception("Timeout waiting for AI response from Gemini Bridge.");
|
| 162 |
+
}
|
| 163 |
+
|
| 164 |
+
string? rawResponseJson = await readResponseTask;
|
| 165 |
+
|
| 166 |
+
if (string.IsNullOrEmpty(rawResponseJson))
|
| 167 |
+
{
|
| 168 |
+
throw new Exception("Did not receive a valid response block from the Gemini Bridge.");
|
| 169 |
+
}
|
| 170 |
+
|
| 171 |
+
return rawResponseJson;
|
| 172 |
+
}
|
| 173 |
+
finally
|
| 174 |
+
{
|
| 175 |
+
_semaphore.Release();
|
| 176 |
+
}
|
| 177 |
+
}
|
| 178 |
+
|
| 179 |
+
public void Dispose()
|
| 180 |
+
{
|
| 181 |
+
if (_process != null)
|
| 182 |
+
{
|
| 183 |
+
try
|
| 184 |
+
{
|
| 185 |
+
if (!_process.HasExited)
|
| 186 |
+
{
|
| 187 |
+
_process.StandardInput.WriteLine("{\"type\": \"exit\"}");
|
| 188 |
+
_process.StandardInput.Flush();
|
| 189 |
+
Thread.Sleep(500); // Give it a moment to exit gracefully
|
| 190 |
+
if (!_process.HasExited)
|
| 191 |
+
{
|
| 192 |
+
_process.Kill();
|
| 193 |
+
}
|
| 194 |
+
}
|
| 195 |
+
}
|
| 196 |
+
catch { }
|
| 197 |
+
_process.Dispose();
|
| 198 |
+
_process = null;
|
| 199 |
+
}
|
| 200 |
+
_semaphore.Dispose();
|
| 201 |
+
}
|
| 202 |
+
}
|
| 203 |
+
}
|
FlowAPI.Application/Services/GraphService.cs
ADDED
|
@@ -0,0 +1,320 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
using FlowAPI.Application.DTOs.Graph;
|
| 2 |
+
using FlowAPI.Application.Interfaces;
|
| 3 |
+
using FlowAPI.Domain.Entities;
|
| 4 |
+
using System;
|
| 5 |
+
using System.Collections.Generic;
|
| 6 |
+
using System.Linq;
|
| 7 |
+
using System.Threading.Tasks;
|
| 8 |
+
|
| 9 |
+
namespace FlowAPI.Application.Services
|
| 10 |
+
{
|
| 11 |
+
public class GraphService : IGraphService
|
| 12 |
+
{
|
| 13 |
+
private readonly IGraphRepository _repo;
|
| 14 |
+
private readonly ITaskNodeRepository _nodeRepo;
|
| 15 |
+
private readonly IEdgeRepository _edgeRepo;
|
| 16 |
+
private readonly IAchievementService _achievementService;
|
| 17 |
+
private readonly ISharedGraphRepository _sharedGraphRepo;
|
| 18 |
+
private readonly IUserRepository _userRepo;
|
| 19 |
+
|
| 20 |
+
public GraphService(
|
| 21 |
+
IGraphRepository repo,
|
| 22 |
+
ITaskNodeRepository nodeRepo,
|
| 23 |
+
IEdgeRepository edgeRepo,
|
| 24 |
+
IAchievementService achievementService,
|
| 25 |
+
ISharedGraphRepository sharedGraphRepo,
|
| 26 |
+
IUserRepository userRepo)
|
| 27 |
+
{
|
| 28 |
+
_repo = repo;
|
| 29 |
+
_nodeRepo = nodeRepo;
|
| 30 |
+
_edgeRepo = edgeRepo;
|
| 31 |
+
_achievementService = achievementService;
|
| 32 |
+
_sharedGraphRepo = sharedGraphRepo;
|
| 33 |
+
_userRepo = userRepo;
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
public async Task<IEnumerable<GraphResponseDto>> GetAllByUserIdAsync(Guid userId)
|
| 37 |
+
{
|
| 38 |
+
// Owned graphs
|
| 39 |
+
var ownedGraphs = await _repo.GetAllByUserIdAsync(userId);
|
| 40 |
+
var ownedDtos = ownedGraphs.Select(g =>
|
| 41 |
+
{
|
| 42 |
+
var dto = MapToDto(g);
|
| 43 |
+
dto.IsShared = false;
|
| 44 |
+
dto.Permission = "Owner";
|
| 45 |
+
return dto;
|
| 46 |
+
}).ToList();
|
| 47 |
+
|
| 48 |
+
// Shared graphs
|
| 49 |
+
var sharedShares = await _sharedGraphRepo.GetSharedGraphsByUserIdAsync(userId);
|
| 50 |
+
|
| 51 |
+
var sharedDtos = sharedShares.Select(s =>
|
| 52 |
+
{
|
| 53 |
+
var dto = MapToDto(s.Graph);
|
| 54 |
+
dto.IsShared = true;
|
| 55 |
+
dto.Permission = s.Permission;
|
| 56 |
+
return dto;
|
| 57 |
+
}).ToList();
|
| 58 |
+
|
| 59 |
+
return ownedDtos.Concat(sharedDtos);
|
| 60 |
+
}
|
| 61 |
+
|
| 62 |
+
public async Task<GraphResponseDto?> GetByIdAsync(Guid id, Guid currentUserId)
|
| 63 |
+
{
|
| 64 |
+
var graph = await _repo.GetByIdAsync(id);
|
| 65 |
+
if (graph is null) return null;
|
| 66 |
+
|
| 67 |
+
if (graph.UserId == currentUserId)
|
| 68 |
+
{
|
| 69 |
+
var dto = MapToDto(graph);
|
| 70 |
+
dto.IsShared = false;
|
| 71 |
+
dto.Permission = "Owner";
|
| 72 |
+
return dto;
|
| 73 |
+
}
|
| 74 |
+
|
| 75 |
+
var shared = await _sharedGraphRepo.GetShareAsync(id, currentUserId);
|
| 76 |
+
if (shared is not null)
|
| 77 |
+
{
|
| 78 |
+
var dto = MapToDto(graph);
|
| 79 |
+
dto.IsShared = true;
|
| 80 |
+
dto.Permission = shared.Permission;
|
| 81 |
+
return dto;
|
| 82 |
+
}
|
| 83 |
+
|
| 84 |
+
return null;
|
| 85 |
+
}
|
| 86 |
+
|
| 87 |
+
public async Task<FullGraphResponseDto?> GetFullByIdAsync(Guid id, Guid currentUserId)
|
| 88 |
+
{
|
| 89 |
+
var graph = await _repo.GetByIdWithDetailsAsync(id);
|
| 90 |
+
if (graph is null) return null;
|
| 91 |
+
|
| 92 |
+
string permission = "Owner";
|
| 93 |
+
bool isShared = false;
|
| 94 |
+
|
| 95 |
+
if (graph.UserId != currentUserId)
|
| 96 |
+
{
|
| 97 |
+
var shared = await _sharedGraphRepo.GetShareAsync(id, currentUserId);
|
| 98 |
+
if (shared is null) return null;
|
| 99 |
+
permission = shared.Permission;
|
| 100 |
+
isShared = true;
|
| 101 |
+
}
|
| 102 |
+
|
| 103 |
+
return new FullGraphResponseDto
|
| 104 |
+
{
|
| 105 |
+
Id = graph.Id,
|
| 106 |
+
UserId = graph.UserId,
|
| 107 |
+
Title = graph.Title,
|
| 108 |
+
Description = graph.Description,
|
| 109 |
+
CreatedAt = graph.CreatedAt,
|
| 110 |
+
IsShared = isShared,
|
| 111 |
+
Permission = permission,
|
| 112 |
+
Nodes = graph.Nodes.Select(n => new NodeDto
|
| 113 |
+
{
|
| 114 |
+
Id = n.Id,
|
| 115 |
+
Label = n.Label,
|
| 116 |
+
PosX = n.PosX,
|
| 117 |
+
PosY = n.PosY,
|
| 118 |
+
Type = n.Type,
|
| 119 |
+
State = (int)n.State,
|
| 120 |
+
Decision = n.Decision
|
| 121 |
+
}).ToList(),
|
| 122 |
+
Edges = graph.Edges.Select(e => new EdgeDto
|
| 123 |
+
{
|
| 124 |
+
Id = e.Id,
|
| 125 |
+
FromNodeId = e.FromNodeId,
|
| 126 |
+
ToNodeId = e.ToNodeId,
|
| 127 |
+
Condition = e.Condition.ToString()
|
| 128 |
+
}).ToList()
|
| 129 |
+
};
|
| 130 |
+
}
|
| 131 |
+
|
| 132 |
+
public async Task<GraphResponseDto> CreateAsync(CreateGraphDto dto, Guid currentUserId)
|
| 133 |
+
{
|
| 134 |
+
var graph = new Graph
|
| 135 |
+
{
|
| 136 |
+
Id = Guid.NewGuid(),
|
| 137 |
+
UserId = currentUserId, // Secure: always force authenticated user ID
|
| 138 |
+
Title = dto.Title,
|
| 139 |
+
Description = dto.Description,
|
| 140 |
+
CreatedAt = DateTime.UtcNow
|
| 141 |
+
};
|
| 142 |
+
|
| 143 |
+
var created = await _repo.CreateAsync(graph);
|
| 144 |
+
|
| 145 |
+
await _achievementService.ProcessEventAsync(currentUserId, "GraphCreated");
|
| 146 |
+
|
| 147 |
+
var result = MapToDto(created);
|
| 148 |
+
result.IsShared = false;
|
| 149 |
+
result.Permission = "Owner";
|
| 150 |
+
return result;
|
| 151 |
+
}
|
| 152 |
+
|
| 153 |
+
public async Task<GraphResponseDto?> UpdateAsync(Guid id, UpdateGraphDto dto, Guid currentUserId)
|
| 154 |
+
{
|
| 155 |
+
var graph = await _repo.GetByIdAsync(id);
|
| 156 |
+
if (graph is null) return null;
|
| 157 |
+
|
| 158 |
+
// Secure: only allowed for owner or edit shared users
|
| 159 |
+
if (graph.UserId != currentUserId)
|
| 160 |
+
{
|
| 161 |
+
var shared = await _sharedGraphRepo.GetShareAsync(id, currentUserId);
|
| 162 |
+
if (shared is null || shared.Permission != "Edit") return null;
|
| 163 |
+
}
|
| 164 |
+
|
| 165 |
+
if (dto.Title is not null) graph.Title = dto.Title;
|
| 166 |
+
if (dto.Description is not null) graph.Description = dto.Description;
|
| 167 |
+
|
| 168 |
+
var updated = await _repo.UpdateAsync(graph);
|
| 169 |
+
var result = MapToDto(updated);
|
| 170 |
+
if (graph.UserId != currentUserId)
|
| 171 |
+
{
|
| 172 |
+
result.IsShared = true;
|
| 173 |
+
result.Permission = "Edit";
|
| 174 |
+
}
|
| 175 |
+
return result;
|
| 176 |
+
}
|
| 177 |
+
|
| 178 |
+
public async Task<bool> DeleteAsync(Guid id, Guid currentUserId)
|
| 179 |
+
{
|
| 180 |
+
var graph = await _repo.GetByIdAsync(id);
|
| 181 |
+
if (graph == null) return false;
|
| 182 |
+
|
| 183 |
+
// Secure: only owner can delete the graph
|
| 184 |
+
if (graph.UserId != currentUserId) return false;
|
| 185 |
+
|
| 186 |
+
var deleted = await _repo.DeleteAsync(id);
|
| 187 |
+
if (deleted)
|
| 188 |
+
{
|
| 189 |
+
await _achievementService.ProcessEventAsync(graph.UserId, "GraphDeleted");
|
| 190 |
+
}
|
| 191 |
+
return deleted;
|
| 192 |
+
}
|
| 193 |
+
|
| 194 |
+
public async Task<bool> SaveFullGraphAsync(Guid id, FullGraphResponseDto dto, Guid currentUserId)
|
| 195 |
+
{
|
| 196 |
+
var graph = await _repo.GetByIdWithDetailsAsync(id);
|
| 197 |
+
if (graph is null) return false;
|
| 198 |
+
|
| 199 |
+
// Secure: only allowed for owner or shared users with Edit permission
|
| 200 |
+
if (graph.UserId != currentUserId)
|
| 201 |
+
{
|
| 202 |
+
var shared = await _sharedGraphRepo.GetShareAsync(id, currentUserId);
|
| 203 |
+
if (shared is null || shared.Permission != "Edit") return false;
|
| 204 |
+
}
|
| 205 |
+
|
| 206 |
+
// Map DTOs to Domain Entities
|
| 207 |
+
var mappedNodes = dto.Nodes.Select(nodeDto => new TaskNode
|
| 208 |
+
{
|
| 209 |
+
Id = nodeDto.Id == Guid.Empty ? Guid.NewGuid() : nodeDto.Id,
|
| 210 |
+
GraphId = id,
|
| 211 |
+
Label = nodeDto.Label,
|
| 212 |
+
PosX = nodeDto.PosX,
|
| 213 |
+
PosY = nodeDto.PosY,
|
| 214 |
+
Type = nodeDto.Type,
|
| 215 |
+
Decision = nodeDto.Decision,
|
| 216 |
+
State = (FlowAPI.Domain.Enums.NodeState)nodeDto.State
|
| 217 |
+
}).ToList();
|
| 218 |
+
|
| 219 |
+
var mappedEdges = dto.Edges.Select(edgeDto => new Edge
|
| 220 |
+
{
|
| 221 |
+
FromNodeId = edgeDto.FromNodeId,
|
| 222 |
+
ToNodeId = edgeDto.ToNodeId,
|
| 223 |
+
Condition = Enum.TryParse<FlowAPI.Domain.Enums.EdgeCondition>(edgeDto.Condition, true, out var cond) ? cond : FlowAPI.Domain.Enums.EdgeCondition.True
|
| 224 |
+
}).ToList();
|
| 225 |
+
|
| 226 |
+
// Calculate deltas for achievements
|
| 227 |
+
var oldNodes = graph.Nodes ?? new List<TaskNode>();
|
| 228 |
+
var oldEdges = graph.Edges ?? new List<Edge>();
|
| 229 |
+
|
| 230 |
+
int newlyCreatedNodes = mappedNodes.Count(n => !oldNodes.Any(o => o.Id == n.Id));
|
| 231 |
+
int newlyCreatedEdges = mappedEdges.Count(e => !oldEdges.Any(o => o.Id == e.Id));
|
| 232 |
+
|
| 233 |
+
int newlyCompletedNodes = mappedNodes.Count(n =>
|
| 234 |
+
n.State == FlowAPI.Domain.Enums.NodeState.Done &&
|
| 235 |
+
(!oldNodes.Any(o => o.Id == n.Id) || oldNodes.First(o => o.Id == n.Id).State != FlowAPI.Domain.Enums.NodeState.Done)
|
| 236 |
+
);
|
| 237 |
+
|
| 238 |
+
// Perform robust Upsert
|
| 239 |
+
var success = await _repo.UpsertFullGraphAsync(id, mappedNodes, mappedEdges);
|
| 240 |
+
if (!success) return false;
|
| 241 |
+
|
| 242 |
+
// Trigger Achievements based on deltas
|
| 243 |
+
if (newlyCreatedNodes > 0) await _achievementService.ProcessEventAsync(graph.UserId, "NodeCreated", newlyCreatedNodes);
|
| 244 |
+
if (newlyCreatedEdges > 0) await _achievementService.ProcessEventAsync(graph.UserId, "EdgeCreated", newlyCreatedEdges);
|
| 245 |
+
if (newlyCompletedNodes > 0) await _achievementService.ProcessEventAsync(graph.UserId, "TaskCompleted", newlyCompletedNodes);
|
| 246 |
+
|
| 247 |
+
return true;
|
| 248 |
+
}
|
| 249 |
+
|
| 250 |
+
public async Task<GraphResponseDto?> ShareGraphAsync(Guid graphId, Guid ownerId, ShareGraphRequestDto dto)
|
| 251 |
+
{
|
| 252 |
+
var graph = await _repo.GetByIdAsync(graphId);
|
| 253 |
+
if (graph is null || graph.UserId != ownerId) return null;
|
| 254 |
+
|
| 255 |
+
var targetUser = await _userRepo.GetByEmailAsync(dto.Email.ToLower().Trim());
|
| 256 |
+
if (targetUser is null || targetUser.Id == ownerId) return null;
|
| 257 |
+
|
| 258 |
+
var existing = await _sharedGraphRepo.GetShareAsync(graphId, targetUser.Id);
|
| 259 |
+
if (existing is not null)
|
| 260 |
+
{
|
| 261 |
+
existing.Permission = dto.Permission;
|
| 262 |
+
await _sharedGraphRepo.UpdateAsync(existing);
|
| 263 |
+
}
|
| 264 |
+
else
|
| 265 |
+
{
|
| 266 |
+
var share = new SharedGraph
|
| 267 |
+
{
|
| 268 |
+
Id = Guid.NewGuid(),
|
| 269 |
+
GraphId = graphId,
|
| 270 |
+
SharedWithUserId = targetUser.Id,
|
| 271 |
+
Permission = dto.Permission,
|
| 272 |
+
CreatedAt = DateTime.UtcNow
|
| 273 |
+
};
|
| 274 |
+
await _sharedGraphRepo.CreateAsync(share);
|
| 275 |
+
}
|
| 276 |
+
|
| 277 |
+
var res = MapToDto(graph);
|
| 278 |
+
res.IsShared = true;
|
| 279 |
+
res.Permission = dto.Permission;
|
| 280 |
+
return res;
|
| 281 |
+
}
|
| 282 |
+
|
| 283 |
+
public async Task<IEnumerable<SharedUserResponseDto>> GetSharedUsersAsync(Guid graphId, Guid ownerId)
|
| 284 |
+
{
|
| 285 |
+
var graph = await _repo.GetByIdAsync(graphId);
|
| 286 |
+
if (graph is null || graph.UserId != ownerId) return Enumerable.Empty<SharedUserResponseDto>();
|
| 287 |
+
|
| 288 |
+
var shares = await _sharedGraphRepo.GetSharesForGraphAsync(graphId);
|
| 289 |
+
|
| 290 |
+
return shares.Select(s => new SharedUserResponseDto
|
| 291 |
+
{
|
| 292 |
+
Id = s.Id,
|
| 293 |
+
SharedWithUserId = s.SharedWithUserId,
|
| 294 |
+
Email = s.SharedWithUser?.Email ?? string.Empty,
|
| 295 |
+
DisplayName = s.SharedWithUser?.DisplayName ?? string.Empty,
|
| 296 |
+
Permission = s.Permission
|
| 297 |
+
});
|
| 298 |
+
}
|
| 299 |
+
|
| 300 |
+
public async Task<bool> RevokeShareAsync(Guid graphId, Guid ownerId, Guid shareId)
|
| 301 |
+
{
|
| 302 |
+
var graph = await _repo.GetByIdAsync(graphId);
|
| 303 |
+
if (graph is null || graph.UserId != ownerId) return false;
|
| 304 |
+
|
| 305 |
+
var share = await _sharedGraphRepo.GetByIdAsync(shareId);
|
| 306 |
+
if (share is null || share.GraphId != graphId) return false;
|
| 307 |
+
|
| 308 |
+
return await _sharedGraphRepo.DeleteAsync(shareId);
|
| 309 |
+
}
|
| 310 |
+
|
| 311 |
+
private static GraphResponseDto MapToDto(Graph graph) => new()
|
| 312 |
+
{
|
| 313 |
+
Id = graph.Id,
|
| 314 |
+
UserId = graph.UserId,
|
| 315 |
+
Title = graph.Title,
|
| 316 |
+
Description = graph.Description,
|
| 317 |
+
CreatedAt = graph.CreatedAt
|
| 318 |
+
};
|
| 319 |
+
}
|
| 320 |
+
}
|
FlowAPI.Application/Services/HabitService.cs
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
using FlowAPI.Application.DTOs.Habit;
|
| 2 |
+
using FlowAPI.Application.Interfaces;
|
| 3 |
+
using FlowAPI.Domain.Entities;
|
| 4 |
+
|
| 5 |
+
namespace FlowAPI.Application.Services
|
| 6 |
+
{
|
| 7 |
+
public class HabitService : IHabitService
|
| 8 |
+
{
|
| 9 |
+
private readonly IHabitRecordRepository _repo;
|
| 10 |
+
private readonly IAchievementService _achievementService;
|
| 11 |
+
|
| 12 |
+
public HabitService(IHabitRecordRepository repo, IAchievementService achievementService)
|
| 13 |
+
{
|
| 14 |
+
_repo = repo;
|
| 15 |
+
_achievementService = achievementService;
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
public async Task<IEnumerable<HabitRecordResponseDto>> GetAllByUserIdAsync(Guid userId)
|
| 19 |
+
{
|
| 20 |
+
var records = await _repo.GetAllByUserIdAsync(userId);
|
| 21 |
+
return records.Select(MapToDto);
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
public async Task<HabitRecordResponseDto> RecordHabitAsync(CreateHabitRecordDto dto)
|
| 25 |
+
{
|
| 26 |
+
var existing = await _repo.GetTodayRecordAsync(dto.UserId, dto.HabitName);
|
| 27 |
+
if (existing is not null) return MapToDto(existing);
|
| 28 |
+
|
| 29 |
+
var record = new HabitRecord
|
| 30 |
+
{
|
| 31 |
+
Id = Guid.NewGuid(),
|
| 32 |
+
UserId = dto.UserId,
|
| 33 |
+
HabitName = dto.HabitName,
|
| 34 |
+
CheckedDate = DateTime.UtcNow
|
| 35 |
+
};
|
| 36 |
+
|
| 37 |
+
var created = await _repo.CreateAsync(record);
|
| 38 |
+
|
| 39 |
+
await _achievementService.ProcessEventAsync(dto.UserId, "HabitTracked");
|
| 40 |
+
|
| 41 |
+
return MapToDto(created);
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
public async Task<int> GetStreakAsync(Guid userId, string habitName)
|
| 45 |
+
{
|
| 46 |
+
var records = await _repo.GetAllByUserIdAsync(userId);
|
| 47 |
+
var habitRecords = records
|
| 48 |
+
.Where(r => r.HabitName == habitName)
|
| 49 |
+
.OrderByDescending(r => r.CheckedDate)
|
| 50 |
+
.ToList();
|
| 51 |
+
|
| 52 |
+
if (!habitRecords.Any()) return 0;
|
| 53 |
+
|
| 54 |
+
int streak = 0;
|
| 55 |
+
DateTime expectedDate = DateTime.UtcNow.Date;
|
| 56 |
+
|
| 57 |
+
foreach (var record in habitRecords)
|
| 58 |
+
{
|
| 59 |
+
if (record.CheckedDate.Date == expectedDate)
|
| 60 |
+
{
|
| 61 |
+
streak++;
|
| 62 |
+
expectedDate = expectedDate.AddDays(-1);
|
| 63 |
+
}
|
| 64 |
+
else if (record.CheckedDate.Date > expectedDate)
|
| 65 |
+
{
|
| 66 |
+
continue;
|
| 67 |
+
}
|
| 68 |
+
else
|
| 69 |
+
{
|
| 70 |
+
break;
|
| 71 |
+
}
|
| 72 |
+
}
|
| 73 |
+
|
| 74 |
+
return streak;
|
| 75 |
+
}
|
| 76 |
+
|
| 77 |
+
private static HabitRecordResponseDto MapToDto(HabitRecord record) => new()
|
| 78 |
+
{
|
| 79 |
+
Id = record.Id,
|
| 80 |
+
UserId = record.UserId,
|
| 81 |
+
HabitName = record.HabitName,
|
| 82 |
+
CheckedDate = record.CheckedDate
|
| 83 |
+
};
|
| 84 |
+
}
|
| 85 |
+
}
|
FlowAPI.Application/Services/PaymentService.cs
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
using FlowAPI.Application.Interfaces;
|
| 2 |
+
using FlowAPI.Domain.Entities;
|
| 3 |
+
using Microsoft.Extensions.Configuration;
|
| 4 |
+
|
| 5 |
+
namespace FlowAPI.Application.Services
|
| 6 |
+
{
|
| 7 |
+
public class PaymentService : IPaymentService
|
| 8 |
+
{
|
| 9 |
+
private readonly IUserRepository _userRepo;
|
| 10 |
+
private readonly IConfiguration _config;
|
| 11 |
+
|
| 12 |
+
public PaymentService(IUserRepository userRepo, IConfiguration config)
|
| 13 |
+
{
|
| 14 |
+
_userRepo = userRepo;
|
| 15 |
+
_config = config;
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
public async Task<string> CreateCheckoutSessionAsync(Guid userId)
|
| 19 |
+
{
|
| 20 |
+
// Implementation with Stripe.net:
|
| 21 |
+
// var options = new SessionCreateOptions { ... };
|
| 22 |
+
// var service = new SessionService();
|
| 23 |
+
// var session = await service.CreateAsync(options);
|
| 24 |
+
// return session.Url;
|
| 25 |
+
|
| 26 |
+
return "https://checkout.stripe.com/pay/cs_test_placeholder_" + userId;
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
public async Task<bool> HandleWebhookAsync(string payload, string signature)
|
| 30 |
+
{
|
| 31 |
+
try
|
| 32 |
+
{
|
| 33 |
+
// In real app, we'd verify the signature:
|
| 34 |
+
// var stripeEvent = EventUtility.ConstructEvent(payload, signature, _config["Stripe:WebhookSecret"]);
|
| 35 |
+
|
| 36 |
+
// if (stripeEvent.Type == Events.CheckoutSessionCompleted)
|
| 37 |
+
// {
|
| 38 |
+
// var session = stripeEvent.Data.Object as Session;
|
| 39 |
+
// var userId = Guid.Parse(session.ClientReferenceId);
|
| 40 |
+
// await UpgradeUserToPremium(userId);
|
| 41 |
+
// }
|
| 42 |
+
|
| 43 |
+
return true;
|
| 44 |
+
}
|
| 45 |
+
catch
|
| 46 |
+
{
|
| 47 |
+
return false;
|
| 48 |
+
}
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
+
private async Task UpgradeUserToPremium(Guid userId)
|
| 52 |
+
{
|
| 53 |
+
var user = await _userRepo.GetByIdAsync(userId);
|
| 54 |
+
if (user != null)
|
| 55 |
+
{
|
| 56 |
+
user.IsPremium = true;
|
| 57 |
+
await _userRepo.UpdateAsync(user);
|
| 58 |
+
}
|
| 59 |
+
}
|
| 60 |
+
}
|
| 61 |
+
}
|
FlowAPI.Application/Services/SyncService.cs
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
using FlowAPI.Application.DTOs.Sync;
|
| 2 |
+
using FlowAPI.Application.Interfaces;
|
| 3 |
+
using FlowAPI.Domain.Entities;
|
| 4 |
+
|
| 5 |
+
namespace FlowAPI.Application.Services
|
| 6 |
+
{
|
| 7 |
+
public class SyncService : ISyncService
|
| 8 |
+
{
|
| 9 |
+
private readonly ITaskNodeRepository _nodeRepo;
|
| 10 |
+
private readonly IEdgeRepository _edgeRepo;
|
| 11 |
+
|
| 12 |
+
public SyncService(ITaskNodeRepository nodeRepo, IEdgeRepository edgeRepo)
|
| 13 |
+
{
|
| 14 |
+
_nodeRepo = nodeRepo;
|
| 15 |
+
_edgeRepo = edgeRepo;
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
public async Task<SyncResponseDto> SyncChangesAsync(Guid userId, SyncRequestDto request)
|
| 19 |
+
{
|
| 20 |
+
try
|
| 21 |
+
{
|
| 22 |
+
// 1. Process Node Deletions
|
| 23 |
+
foreach (var id in request.DeletedNodeIds)
|
| 24 |
+
{
|
| 25 |
+
await _nodeRepo.DeleteAsync(id);
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
// 2. Process Edge Deletions
|
| 29 |
+
foreach (var id in request.DeletedEdgeIds)
|
| 30 |
+
{
|
| 31 |
+
await _edgeRepo.DeleteAsync(id);
|
| 32 |
+
}
|
| 33 |
+
|
| 34 |
+
// 3. Process New Nodes
|
| 35 |
+
foreach (var dto in request.NewNodes)
|
| 36 |
+
{
|
| 37 |
+
var node = new TaskNode
|
| 38 |
+
{
|
| 39 |
+
Id = Guid.NewGuid(),
|
| 40 |
+
GraphId = dto.GraphId,
|
| 41 |
+
Label = dto.Label,
|
| 42 |
+
PosX = dto.PosX,
|
| 43 |
+
PosY = dto.PosY,
|
| 44 |
+
State = dto.State
|
| 45 |
+
};
|
| 46 |
+
await _nodeRepo.CreateAsync(node);
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
+
// 4. Process Updated Nodes
|
| 50 |
+
foreach (var dto in request.UpdatedNodes)
|
| 51 |
+
{
|
| 52 |
+
var node = await _nodeRepo.GetByIdAsync(dto.Id);
|
| 53 |
+
if (node != null)
|
| 54 |
+
{
|
| 55 |
+
if (dto.Label != null) node.Label = dto.Label;
|
| 56 |
+
if (dto.PosX.HasValue) node.PosX = dto.PosX.Value;
|
| 57 |
+
if (dto.PosY.HasValue) node.PosY = dto.PosY.Value;
|
| 58 |
+
if (dto.State.HasValue) node.State = dto.State.Value;
|
| 59 |
+
await _nodeRepo.UpdateAsync(node);
|
| 60 |
+
}
|
| 61 |
+
}
|
| 62 |
+
|
| 63 |
+
// 5. Process New Edges
|
| 64 |
+
foreach (var dto in request.NewEdges)
|
| 65 |
+
{
|
| 66 |
+
var edge = new Edge
|
| 67 |
+
{
|
| 68 |
+
Id = Guid.NewGuid(),
|
| 69 |
+
GraphId = dto.GraphId,
|
| 70 |
+
FromNodeId = dto.FromNodeId,
|
| 71 |
+
ToNodeId = dto.ToNodeId,
|
| 72 |
+
Condition = dto.Condition
|
| 73 |
+
};
|
| 74 |
+
await _edgeRepo.CreateAsync(edge);
|
| 75 |
+
}
|
| 76 |
+
|
| 77 |
+
return new SyncResponseDto
|
| 78 |
+
{
|
| 79 |
+
Success = true,
|
| 80 |
+
Message = "Synchronization completed successfully.",
|
| 81 |
+
ServerTime = DateTime.UtcNow
|
| 82 |
+
};
|
| 83 |
+
}
|
| 84 |
+
catch (Exception ex)
|
| 85 |
+
{
|
| 86 |
+
return new SyncResponseDto
|
| 87 |
+
{
|
| 88 |
+
Success = false,
|
| 89 |
+
Message = $"Sync failed: {ex.Message}",
|
| 90 |
+
ServerTime = DateTime.UtcNow
|
| 91 |
+
};
|
| 92 |
+
}
|
| 93 |
+
}
|
| 94 |
+
}
|
| 95 |
+
}
|
FlowAPI.Application/Services/TaskNodeService.cs
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
using FlowAPI.Application.DTOs.TaskNode;
|
| 2 |
+
using FlowAPI.Application.Interfaces;
|
| 3 |
+
using FlowAPI.Domain.Entities;
|
| 4 |
+
|
| 5 |
+
namespace FlowAPI.Application.Services
|
| 6 |
+
{
|
| 7 |
+
public class TaskNodeService : ITaskNodeService
|
| 8 |
+
{
|
| 9 |
+
private readonly ITaskNodeRepository _repo;
|
| 10 |
+
|
| 11 |
+
public TaskNodeService(ITaskNodeRepository repo)
|
| 12 |
+
{
|
| 13 |
+
_repo = repo;
|
| 14 |
+
}
|
| 15 |
+
|
| 16 |
+
public async Task<IEnumerable<TaskNodeResponseDto>> GetAllByGraphIdAsync(Guid graphId)
|
| 17 |
+
{
|
| 18 |
+
var nodes = await _repo.GetAllByGraphIdAsync(graphId);
|
| 19 |
+
return nodes.Select(MapToDto);
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
+
public async Task<TaskNodeResponseDto?> GetByIdAsync(Guid id)
|
| 23 |
+
{
|
| 24 |
+
var node = await _repo.GetByIdAsync(id);
|
| 25 |
+
return node is null ? null : MapToDto(node);
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
public async Task<TaskNodeResponseDto> CreateAsync(CreateTaskNodeDto dto)
|
| 29 |
+
{
|
| 30 |
+
if (dto.Label.Length > 30)
|
| 31 |
+
throw new ArgumentException("Label cannot exceed 30 characters.");
|
| 32 |
+
|
| 33 |
+
var node = new TaskNode
|
| 34 |
+
{
|
| 35 |
+
Id = Guid.NewGuid(),
|
| 36 |
+
GraphId = dto.GraphId,
|
| 37 |
+
Label = dto.Label,
|
| 38 |
+
PosX = dto.PosX,
|
| 39 |
+
PosY = dto.PosY,
|
| 40 |
+
State = dto.State
|
| 41 |
+
};
|
| 42 |
+
|
| 43 |
+
var created = await _repo.CreateAsync(node);
|
| 44 |
+
return MapToDto(created);
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
public async Task<TaskNodeResponseDto?> UpdateAsync(Guid id, UpdateTaskNodeDto dto)
|
| 48 |
+
{
|
| 49 |
+
var node = await _repo.GetByIdAsync(id);
|
| 50 |
+
if (node is null) return null;
|
| 51 |
+
|
| 52 |
+
if (dto.Label is not null)
|
| 53 |
+
{
|
| 54 |
+
if (dto.Label.Length > 30) throw new ArgumentException("Label cannot exceed 30 characters.");
|
| 55 |
+
node.Label = dto.Label;
|
| 56 |
+
}
|
| 57 |
+
if (dto.PosX.HasValue) node.PosX = dto.PosX.Value;
|
| 58 |
+
if (dto.PosY.HasValue) node.PosY = dto.PosY.Value;
|
| 59 |
+
if (dto.State.HasValue) node.State = dto.State.Value;
|
| 60 |
+
|
| 61 |
+
var updated = await _repo.UpdateAsync(node);
|
| 62 |
+
return MapToDto(updated);
|
| 63 |
+
}
|
| 64 |
+
|
| 65 |
+
public async Task<bool> DeleteAsync(Guid id)
|
| 66 |
+
{
|
| 67 |
+
return await _repo.DeleteAsync(id);
|
| 68 |
+
}
|
| 69 |
+
|
| 70 |
+
private static TaskNodeResponseDto MapToDto(TaskNode node) => new()
|
| 71 |
+
{
|
| 72 |
+
Id = node.Id,
|
| 73 |
+
GraphId = node.GraphId,
|
| 74 |
+
Label = node.Label,
|
| 75 |
+
PosX = node.PosX,
|
| 76 |
+
PosY = node.PosY,
|
| 77 |
+
State = node.State
|
| 78 |
+
};
|
| 79 |
+
}
|
| 80 |
+
}
|
FlowAPI.Application/Services/TokenService.cs
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
using System.IdentityModel.Tokens.Jwt;
|
| 2 |
+
using System.Security.Claims;
|
| 3 |
+
using System.Text;
|
| 4 |
+
using FlowAPI.Application.Interfaces;
|
| 5 |
+
using FlowAPI.Domain.Entities;
|
| 6 |
+
using Microsoft.Extensions.Configuration;
|
| 7 |
+
using Microsoft.IdentityModel.Tokens;
|
| 8 |
+
|
| 9 |
+
namespace FlowAPI.Application.Services
|
| 10 |
+
{
|
| 11 |
+
public class TokenService : ITokenService
|
| 12 |
+
{
|
| 13 |
+
private readonly SymmetricSecurityKey _key;
|
| 14 |
+
private readonly IConfiguration _config;
|
| 15 |
+
|
| 16 |
+
public TokenService(IConfiguration config)
|
| 17 |
+
{
|
| 18 |
+
_config = config;
|
| 19 |
+
var secretKey = _config["Jwt:Key"] ?? throw new ArgumentNullException("Jwt:Key is missing in configuration.");
|
| 20 |
+
_key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(secretKey));
|
| 21 |
+
}
|
| 22 |
+
|
| 23 |
+
public string CreateToken(User user)
|
| 24 |
+
{
|
| 25 |
+
var claims = new List<Claim>
|
| 26 |
+
{
|
| 27 |
+
new Claim(JwtRegisteredClaimNames.NameId, user.Id.ToString()),
|
| 28 |
+
new Claim(JwtRegisteredClaimNames.Email, user.Email),
|
| 29 |
+
new Claim("displayName", user.DisplayName)
|
| 30 |
+
};
|
| 31 |
+
|
| 32 |
+
var creds = new SigningCredentials(_key, SecurityAlgorithms.HmacSha256Signature);
|
| 33 |
+
|
| 34 |
+
var tokenDescriptor = new SecurityTokenDescriptor
|
| 35 |
+
{
|
| 36 |
+
Subject = new ClaimsIdentity(claims),
|
| 37 |
+
Expires = DateTime.Now.AddDays(double.Parse(_config["Jwt:ExpiryDays"] ?? "14")),
|
| 38 |
+
SigningCredentials = creds,
|
| 39 |
+
Issuer = _config["Jwt:Issuer"],
|
| 40 |
+
Audience = _config["Jwt:Audience"]
|
| 41 |
+
};
|
| 42 |
+
|
| 43 |
+
var tokenHandler = new JwtSecurityTokenHandler();
|
| 44 |
+
var token = tokenHandler.CreateToken(tokenDescriptor);
|
| 45 |
+
|
| 46 |
+
return tokenHandler.WriteToken(token);
|
| 47 |
+
}
|
| 48 |
+
}
|
| 49 |
+
}
|
FlowAPI.Application/Services/UserService.cs
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
using System.Security.Cryptography;
|
| 2 |
+
using System.Text;
|
| 3 |
+
using FlowAPI.Application.DTOs.User;
|
| 4 |
+
using FlowAPI.Application.Interfaces;
|
| 5 |
+
using FlowAPI.Domain.Entities;
|
| 6 |
+
using Microsoft.Extensions.Configuration;
|
| 7 |
+
|
| 8 |
+
namespace FlowAPI.Application.Services
|
| 9 |
+
{
|
| 10 |
+
public class UserService : IUserService
|
| 11 |
+
{
|
| 12 |
+
private readonly IUserRepository _repo;
|
| 13 |
+
private readonly ITokenService _tokenService;
|
| 14 |
+
private readonly IConfiguration _config;
|
| 15 |
+
private readonly IAchievementService _achievementService;
|
| 16 |
+
|
| 17 |
+
public UserService(IUserRepository repo, ITokenService tokenService, IConfiguration config, IAchievementService achievementService)
|
| 18 |
+
{
|
| 19 |
+
_repo = repo;
|
| 20 |
+
_tokenService = tokenService;
|
| 21 |
+
_config = config;
|
| 22 |
+
_achievementService = achievementService;
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
public async Task<IEnumerable<UserResponseDto>> GetAllAsync()
|
| 26 |
+
{
|
| 27 |
+
var users = await _repo.GetAllAsync();
|
| 28 |
+
return users.Select(MapToDto);
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
public async Task<UserResponseDto?> GetByIdAsync(Guid id)
|
| 32 |
+
{
|
| 33 |
+
var user = await _repo.GetByIdAsync(id);
|
| 34 |
+
return user is null ? null : MapToDto(user);
|
| 35 |
+
}
|
| 36 |
+
|
| 37 |
+
public async Task<AuthResponseDto> RegisterAsync(CreateUserDto dto)
|
| 38 |
+
{
|
| 39 |
+
var existing = await _repo.GetByEmailAsync(dto.Email);
|
| 40 |
+
if (existing is not null)
|
| 41 |
+
throw new InvalidOperationException("User with this email already exists.");
|
| 42 |
+
|
| 43 |
+
var user = new User
|
| 44 |
+
{
|
| 45 |
+
Id = Guid.NewGuid(),
|
| 46 |
+
Email = dto.Email,
|
| 47 |
+
PasswordHash = HashPassword(dto.Password),
|
| 48 |
+
DisplayName = dto.DisplayName,
|
| 49 |
+
CreatedAt = DateTime.UtcNow
|
| 50 |
+
};
|
| 51 |
+
|
| 52 |
+
var created = await _repo.CreateAsync(user);
|
| 53 |
+
return new AuthResponseDto
|
| 54 |
+
{
|
| 55 |
+
User = MapToDto(created),
|
| 56 |
+
Token = _tokenService.CreateToken(created)
|
| 57 |
+
};
|
| 58 |
+
}
|
| 59 |
+
|
| 60 |
+
public async Task<AuthResponseDto?> LoginAsync(LoginDto dto)
|
| 61 |
+
{
|
| 62 |
+
var user = await _repo.GetByEmailAsync(dto.Email);
|
| 63 |
+
if (user is null || string.IsNullOrEmpty(user.PasswordHash) || user.PasswordHash != HashPassword(dto.Password))
|
| 64 |
+
return null;
|
| 65 |
+
|
| 66 |
+
return new AuthResponseDto
|
| 67 |
+
{
|
| 68 |
+
User = MapToDto(user),
|
| 69 |
+
Token = _tokenService.CreateToken(user)
|
| 70 |
+
};
|
| 71 |
+
}
|
| 72 |
+
|
| 73 |
+
public async Task<AuthResponseDto> GoogleLoginAsync(GoogleLoginDto dto)
|
| 74 |
+
{
|
| 75 |
+
var settings = new Google.Apis.Auth.GoogleJsonWebSignature.ValidationSettings
|
| 76 |
+
{
|
| 77 |
+
Audience = new List<string> { _config["Google:ClientId"]! }
|
| 78 |
+
};
|
| 79 |
+
|
| 80 |
+
var payload = await Google.Apis.Auth.GoogleJsonWebSignature.ValidateAsync(dto.IdToken, settings);
|
| 81 |
+
|
| 82 |
+
var user = await _repo.GetByEmailAsync(payload.Email);
|
| 83 |
+
|
| 84 |
+
if (user == null)
|
| 85 |
+
{
|
| 86 |
+
user = new User
|
| 87 |
+
{
|
| 88 |
+
Id = Guid.NewGuid(),
|
| 89 |
+
Email = payload.Email,
|
| 90 |
+
DisplayName = payload.Name,
|
| 91 |
+
CreatedAt = DateTime.UtcNow,
|
| 92 |
+
PasswordHash = "" // No password for Google users
|
| 93 |
+
};
|
| 94 |
+
user = await _repo.CreateAsync(user);
|
| 95 |
+
}
|
| 96 |
+
|
| 97 |
+
return new AuthResponseDto
|
| 98 |
+
{
|
| 99 |
+
User = MapToDto(user),
|
| 100 |
+
Token = _tokenService.CreateToken(user)
|
| 101 |
+
};
|
| 102 |
+
}
|
| 103 |
+
|
| 104 |
+
public async Task<UserResponseDto?> UpdateAsync(Guid id, UpdateUserDto dto)
|
| 105 |
+
{
|
| 106 |
+
var user = await _repo.GetByIdAsync(id);
|
| 107 |
+
if (user is null) return null;
|
| 108 |
+
|
| 109 |
+
bool isAvatarUpdated = dto.AvatarUrl is not null && user.AvatarUrl != dto.AvatarUrl;
|
| 110 |
+
|
| 111 |
+
if (dto.DisplayName is not null) user.DisplayName = dto.DisplayName;
|
| 112 |
+
if (dto.IsPremium.HasValue) user.IsPremium = dto.IsPremium.Value;
|
| 113 |
+
if (dto.AvatarUrl is not null) user.AvatarUrl = dto.AvatarUrl;
|
| 114 |
+
|
| 115 |
+
var updated = await _repo.UpdateAsync(user);
|
| 116 |
+
|
| 117 |
+
if (isAvatarUpdated)
|
| 118 |
+
{
|
| 119 |
+
await _achievementService.ProcessEventAsync(id, "AvatarUploaded");
|
| 120 |
+
}
|
| 121 |
+
|
| 122 |
+
return MapToDto(updated);
|
| 123 |
+
}
|
| 124 |
+
|
| 125 |
+
public async Task<bool> DeleteAsync(Guid id)
|
| 126 |
+
{
|
| 127 |
+
return await _repo.DeleteAsync(id);
|
| 128 |
+
}
|
| 129 |
+
|
| 130 |
+
private static string HashPassword(string password)
|
| 131 |
+
{
|
| 132 |
+
var bytes = SHA256.HashData(Encoding.UTF8.GetBytes(password));
|
| 133 |
+
return Convert.ToBase64String(bytes);
|
| 134 |
+
}
|
| 135 |
+
|
| 136 |
+
private static UserResponseDto MapToDto(User user) => new()
|
| 137 |
+
{
|
| 138 |
+
Id = user.Id,
|
| 139 |
+
Email = user.Email,
|
| 140 |
+
DisplayName = user.DisplayName,
|
| 141 |
+
IsPremium = user.IsPremium,
|
| 142 |
+
AvatarUrl = user.AvatarUrl,
|
| 143 |
+
CreatedAt = user.CreatedAt
|
| 144 |
+
};
|
| 145 |
+
}
|
| 146 |
+
}
|
FlowAPI.Domain/Entities/Achievement.cs
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
using System;
|
| 2 |
+
using FlowAPI.Domain.Enums;
|
| 3 |
+
|
| 4 |
+
namespace FlowAPI.Domain.Entities
|
| 5 |
+
{
|
| 6 |
+
public class Achievement
|
| 7 |
+
{
|
| 8 |
+
public Guid Id { get; set; }
|
| 9 |
+
|
| 10 |
+
public string Title { get; set; } = string.Empty;
|
| 11 |
+
public string Description { get; set; } = string.Empty;
|
| 12 |
+
public string IconUrl { get; set; } = string.Empty;
|
| 13 |
+
|
| 14 |
+
public string Category { get; set; } = string.Empty;
|
| 15 |
+
public AchievementType Type { get; set; }
|
| 16 |
+
public int TargetValue { get; set; }
|
| 17 |
+
public int RewardPoints { get; set; }
|
| 18 |
+
|
| 19 |
+
// This tells the backend what event triggers this achievement
|
| 20 |
+
// e.g. "NodeCreated", "HabitCompleted", "EdgeCreated", "GraphDeleted"
|
| 21 |
+
public string TriggerEventName { get; set; } = string.Empty;
|
| 22 |
+
}
|
| 23 |
+
}
|
FlowAPI.Domain/Entities/Edge.cs
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
using FlowAPI.Domain.Enums;
|
| 2 |
+
|
| 3 |
+
namespace FlowAPI.Domain.Entities
|
| 4 |
+
{
|
| 5 |
+
public class Edge
|
| 6 |
+
{
|
| 7 |
+
public Guid Id { get; set; }
|
| 8 |
+
public Guid GraphId { get; set; }
|
| 9 |
+
public Guid FromNodeId { get; set; }
|
| 10 |
+
public Guid ToNodeId { get; set; }
|
| 11 |
+
public EdgeCondition Condition { get; set; } = EdgeCondition.True;
|
| 12 |
+
|
| 13 |
+
// Navigation properties
|
| 14 |
+
public Graph Graph { get; set; } = null!;
|
| 15 |
+
public TaskNode FromNode { get; set; } = null!;
|
| 16 |
+
public TaskNode ToNode { get; set; } = null!;
|
| 17 |
+
}
|
| 18 |
+
}
|
FlowAPI.Domain/Entities/Graph.cs
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
namespace FlowAPI.Domain.Entities
|
| 2 |
+
{
|
| 3 |
+
public class Graph
|
| 4 |
+
{
|
| 5 |
+
public Guid Id { get; set; }
|
| 6 |
+
public Guid UserId { get; set; }
|
| 7 |
+
public string Title { get; set; } = string.Empty;
|
| 8 |
+
public string? Description { get; set; }
|
| 9 |
+
public DateTime CreatedAt { get; set; } = DateTime.UtcNow;
|
| 10 |
+
public DateTime UpdatedAt { get; set; } = DateTime.UtcNow;
|
| 11 |
+
|
| 12 |
+
// Navigation properties
|
| 13 |
+
public User User { get; set; } = null!;
|
| 14 |
+
public ICollection<TaskNode> Nodes { get; set; } = new List<TaskNode>();
|
| 15 |
+
public ICollection<Edge> Edges { get; set; } = new List<Edge>();
|
| 16 |
+
public ICollection<SharedGraph> SharedGraphs { get; set; } = new List<SharedGraph>();
|
| 17 |
+
}
|
| 18 |
+
}
|
FlowAPI.Domain/Entities/HabitRecord.cs
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
namespace FlowAPI.Domain.Entities
|
| 2 |
+
{
|
| 3 |
+
public class HabitRecord
|
| 4 |
+
{
|
| 5 |
+
public Guid Id { get; set; }
|
| 6 |
+
public Guid UserId { get; set; }
|
| 7 |
+
public string HabitName { get; set; } = string.Empty;
|
| 8 |
+
public DateTime CheckedDate { get; set; }
|
| 9 |
+
public int StreakCount { get; set; }
|
| 10 |
+
|
| 11 |
+
// Navigation properties
|
| 12 |
+
public User User { get; set; } = null!;
|
| 13 |
+
}
|
| 14 |
+
}
|
FlowAPI.Domain/Entities/SharedGraph.cs
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
using System;
|
| 2 |
+
|
| 3 |
+
namespace FlowAPI.Domain.Entities
|
| 4 |
+
{
|
| 5 |
+
public class SharedGraph
|
| 6 |
+
{
|
| 7 |
+
public Guid Id { get; set; }
|
| 8 |
+
public Guid GraphId { get; set; }
|
| 9 |
+
public Guid SharedWithUserId { get; set; }
|
| 10 |
+
public string Permission { get; set; } = "Read"; // "Read" or "Edit"
|
| 11 |
+
public DateTime CreatedAt { get; set; } = DateTime.UtcNow;
|
| 12 |
+
|
| 13 |
+
// Navigation properties
|
| 14 |
+
public Graph Graph { get; set; } = null!;
|
| 15 |
+
public User SharedWithUser { get; set; } = null!;
|
| 16 |
+
}
|
| 17 |
+
}
|
FlowAPI.Domain/Entities/TaskNode.cs
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
using FlowAPI.Domain.Enums;
|
| 2 |
+
|
| 3 |
+
namespace FlowAPI.Domain.Entities
|
| 4 |
+
{
|
| 5 |
+
public class TaskNode
|
| 6 |
+
{
|
| 7 |
+
public Guid Id { get; set; }
|
| 8 |
+
public Guid GraphId { get; set; }
|
| 9 |
+
public string Label { get; set; } = string.Empty;
|
| 10 |
+
public double PosX { get; set; }
|
| 11 |
+
public double PosY { get; set; }
|
| 12 |
+
public string Type { get; set; } = "sketch";
|
| 13 |
+
public string? Decision { get; set; }
|
| 14 |
+
public NodeState State { get; set; } = NodeState.Pending;
|
| 15 |
+
public DateTime CreatedAt { get; set; } = DateTime.UtcNow;
|
| 16 |
+
|
| 17 |
+
// Navigation properties
|
| 18 |
+
public Graph Graph { get; set; } = null!;
|
| 19 |
+
public ICollection<Edge> OutgoingEdges { get; set; } = new List<Edge>();
|
| 20 |
+
public ICollection<Edge> IncomingEdges { get; set; } = new List<Edge>();
|
| 21 |
+
}
|
| 22 |
+
}
|
FlowAPI.Domain/Entities/User.cs
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
namespace FlowAPI.Domain.Entities
|
| 2 |
+
{
|
| 3 |
+
public class User
|
| 4 |
+
{
|
| 5 |
+
public Guid Id { get; set; }
|
| 6 |
+
public string Email { get; set; } = string.Empty;
|
| 7 |
+
public string PasswordHash { get; set; } = string.Empty;
|
| 8 |
+
public string DisplayName { get; set; } = string.Empty;
|
| 9 |
+
public bool IsPremium { get; set; }
|
| 10 |
+
public string? AvatarUrl { get; set; }
|
| 11 |
+
public DateTime CreatedAt { get; set; } = DateTime.UtcNow;
|
| 12 |
+
|
| 13 |
+
// Navigation properties
|
| 14 |
+
public ICollection<Graph> Graphs { get; set; } = new List<Graph>();
|
| 15 |
+
public ICollection<HabitRecord> HabitRecords { get; set; } = new List<HabitRecord>();
|
| 16 |
+
public ICollection<UserAchievement> UserAchievements { get; set; } = new List<UserAchievement>();
|
| 17 |
+
public ICollection<SharedGraph> SharedGraphs { get; set; } = new List<SharedGraph>();
|
| 18 |
+
}
|
| 19 |
+
}
|