Spaces:
Sleeping
Sleeping
Commit ·
d6581ab
1
Parent(s): eb6d2f6
Fix: add robust user existence checks to prevent SQLite Foreign Key errors
Browse files
FlowAPI/Controllers/AgentCanvasController.cs
CHANGED
|
@@ -18,23 +18,33 @@ namespace FlowAPI.Controllers
|
|
| 18 |
{
|
| 19 |
private readonly IGraphService _graphService;
|
| 20 |
private readonly ITokenService _tokenService;
|
|
|
|
| 21 |
|
| 22 |
-
public AgentCanvasController(IGraphService graphService, ITokenService tokenService)
|
| 23 |
{
|
| 24 |
_graphService = graphService;
|
| 25 |
_tokenService = tokenService;
|
|
|
|
| 26 |
}
|
| 27 |
|
| 28 |
[AllowAnonymous]
|
| 29 |
[HttpGet("token")]
|
| 30 |
-
public IActionResult GetToken()
|
| 31 |
{
|
| 32 |
-
var
|
|
|
|
|
|
|
| 33 |
{
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
var token = _tokenService.CreateToken(user);
|
| 39 |
return Ok(new { token });
|
| 40 |
}
|
|
@@ -52,6 +62,21 @@ namespace FlowAPI.Controllers
|
|
| 52 |
|
| 53 |
try
|
| 54 |
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
// 1. Create the Graph record in the database
|
| 56 |
var createDto = new CreateGraphDto
|
| 57 |
{
|
|
@@ -81,7 +106,7 @@ namespace FlowAPI.Controllers
|
|
| 81 |
}
|
| 82 |
catch (Exception ex)
|
| 83 |
{
|
| 84 |
-
return BadRequest(new { message = $"Internal error during plan generation: {ex
|
| 85 |
}
|
| 86 |
}
|
| 87 |
|
|
|
|
| 18 |
{
|
| 19 |
private readonly IGraphService _graphService;
|
| 20 |
private readonly ITokenService _tokenService;
|
| 21 |
+
private readonly IUserRepository _userRepo;
|
| 22 |
|
| 23 |
+
public AgentCanvasController(IGraphService graphService, ITokenService tokenService, IUserRepository userRepo)
|
| 24 |
{
|
| 25 |
_graphService = graphService;
|
| 26 |
_tokenService = tokenService;
|
| 27 |
+
_userRepo = userRepo;
|
| 28 |
}
|
| 29 |
|
| 30 |
[AllowAnonymous]
|
| 31 |
[HttpGet("token")]
|
| 32 |
+
public async Task<IActionResult> GetToken()
|
| 33 |
{
|
| 34 |
+
var userId = Guid.Parse("33d1ae31-bf51-4b12-89d0-8e574e7916ab");
|
| 35 |
+
var user = await _userRepo.GetByIdAsync(userId);
|
| 36 |
+
if (user == null)
|
| 37 |
{
|
| 38 |
+
user = new User
|
| 39 |
+
{
|
| 40 |
+
Id = userId,
|
| 41 |
+
Email = "dan9228335@gmail.com",
|
| 42 |
+
DisplayName = "Dodikuser",
|
| 43 |
+
PasswordHash = "",
|
| 44 |
+
CreatedAt = DateTime.UtcNow
|
| 45 |
+
};
|
| 46 |
+
await _userRepo.CreateAsync(user);
|
| 47 |
+
}
|
| 48 |
var token = _tokenService.CreateToken(user);
|
| 49 |
return Ok(new { token });
|
| 50 |
}
|
|
|
|
| 62 |
|
| 63 |
try
|
| 64 |
{
|
| 65 |
+
// Ensure user exists in database to prevent Foreign Key constraints failure
|
| 66 |
+
var user = await _userRepo.GetByIdAsync(CurrentUserId);
|
| 67 |
+
if (user == null)
|
| 68 |
+
{
|
| 69 |
+
user = new User
|
| 70 |
+
{
|
| 71 |
+
Id = CurrentUserId,
|
| 72 |
+
Email = User.FindFirst(ClaimTypes.Email)?.Value ?? $"{CurrentUserId}@questboard.ai",
|
| 73 |
+
DisplayName = User.FindFirst("displayName")?.Value ?? "Questboard User",
|
| 74 |
+
PasswordHash = "",
|
| 75 |
+
CreatedAt = DateTime.UtcNow
|
| 76 |
+
};
|
| 77 |
+
await _userRepo.CreateAsync(user);
|
| 78 |
+
}
|
| 79 |
+
|
| 80 |
// 1. Create the Graph record in the database
|
| 81 |
var createDto = new CreateGraphDto
|
| 82 |
{
|
|
|
|
| 106 |
}
|
| 107 |
catch (Exception ex)
|
| 108 |
{
|
| 109 |
+
return BadRequest(new { message = $"Internal error during plan generation: {ex}" });
|
| 110 |
}
|
| 111 |
}
|
| 112 |
|