| 435 | } |
| 436 | |
| 437 | public async Task<TaskActivityDto> AddCommentAsync(int taskId, string text, int userId) |
| 438 | { |
| 439 | if (!await VerifyTaskAccessAsync(taskId, userId)) |
| 440 | throw new UnauthorizedAccessException("User does not have access to this task."); |
| 441 | |
| 442 | var activity = new TaskActivity |
| 443 | { |
| 444 | TaskCardId = taskId, |
| 445 | UserId = userId, |
| 446 | Action = "Commented", |
| 447 | Details = text, |
| 448 | Timestamp = DateTime.UtcNow |
| 449 | }; |
| 450 | |
| 451 | _db.TaskActivities.Add(activity); |
| 452 | await _db.SaveChangesAsync(); |
| 453 | |
| 454 | Console.WriteLine($"[TaskService] Comment saved. ActivityId: {activity.Id}, TaskId: {taskId}, UserId: {userId}"); |
| 455 | |
| 456 | // Detect mentions @username |
| 457 | await DetectMentionsAsync(taskId, text, userId); |
| 458 | |
| 459 | // Load user info for the DTO |
| 460 | var user = await _db.Users.AsNoTracking().FirstOrDefaultAsync(u => u.Id == userId); |
| 461 | |
| 462 | if (user == null) |
| 463 | { |
| 464 | Console.WriteLine($"[TaskService] WARNING: User not found in database for userId: {userId}"); |
| 465 | } |
| 466 | else |
| 467 | { |
| 468 | Console.WriteLine($"[TaskService] User found: {user.Username} (ID: {user.Id})"); |
| 469 | } |
| 470 | |
| 471 | return new TaskActivityDto |
| 472 | { |
| 473 | Id = activity.Id, |
| 474 | TaskCardId = activity.TaskCardId, |
| 475 | UserId = activity.UserId, |
| 476 | Username = user?.Username ?? "Unknown", |
| 477 | UserAvatarUrl = user?.AvatarUrl, |
| 478 | Action = activity.Action, |
| 479 | Details = activity.Details, |
| 480 | Timestamp = activity.Timestamp, |
| 481 | Reactions = new List<ActivityReactionDto>() |
| 482 | }; |
| 483 | } |
| 484 | |
| 485 | public async Task<TaskActivityDto?> UpdateActivityAsync(int activityId, string text, int userId) |
| 486 | { |