| | | 1 | | using Chronicis.Api.Infrastructure; |
| | | 2 | | using Chronicis.Api.Services; |
| | | 3 | | using Chronicis.Shared.DTOs; |
| | | 4 | | using Chronicis.Shared.Extensions; |
| | | 5 | | using Microsoft.AspNetCore.Authorization; |
| | | 6 | | using Microsoft.AspNetCore.Mvc; |
| | | 7 | | |
| | | 8 | | namespace Chronicis.Api.Controllers; |
| | | 9 | | |
| | | 10 | | /// <summary> |
| | | 11 | | /// API endpoints for Arc management. |
| | | 12 | | /// </summary> |
| | | 13 | | [ApiController] |
| | | 14 | | [Route("arcs")] |
| | | 15 | | [Authorize] |
| | | 16 | | public class ArcsController : ControllerBase |
| | | 17 | | { |
| | | 18 | | private readonly IArcService _arcService; |
| | | 19 | | private readonly ICurrentUserService _currentUserService; |
| | | 20 | | private readonly ILogger<ArcsController> _logger; |
| | | 21 | | |
| | 0 | 22 | | public ArcsController( |
| | 0 | 23 | | IArcService arcService, |
| | 0 | 24 | | ICurrentUserService currentUserService, |
| | 0 | 25 | | ILogger<ArcsController> logger) |
| | | 26 | | { |
| | 0 | 27 | | _arcService = arcService; |
| | 0 | 28 | | _currentUserService = currentUserService; |
| | 0 | 29 | | _logger = logger; |
| | 0 | 30 | | } |
| | | 31 | | |
| | | 32 | | /// <summary> |
| | | 33 | | /// GET /api/arcs/{id} - Get a specific arc. |
| | | 34 | | /// </summary> |
| | | 35 | | [HttpGet("{id:guid}")] |
| | | 36 | | public async Task<ActionResult<ArcDto>> GetArc(Guid id) |
| | | 37 | | { |
| | 0 | 38 | | var user = await _currentUserService.GetRequiredUserAsync(); |
| | 0 | 39 | | _logger.LogDebug("Getting arc {ArcId} for user {UserId}", id, user.Id); |
| | | 40 | | |
| | 0 | 41 | | var arc = await _arcService.GetArcAsync(id, user.Id); |
| | | 42 | | |
| | 0 | 43 | | if (arc == null) |
| | | 44 | | { |
| | 0 | 45 | | return NotFound(new { error = "Arc not found or access denied" }); |
| | | 46 | | } |
| | | 47 | | |
| | 0 | 48 | | return Ok(arc); |
| | 0 | 49 | | } |
| | | 50 | | |
| | | 51 | | /// <summary> |
| | | 52 | | /// POST /api/arcs - Create a new arc. |
| | | 53 | | /// </summary> |
| | | 54 | | [HttpPost] |
| | | 55 | | public async Task<ActionResult<ArcDto>> CreateArc([FromBody] ArcCreateDto dto) |
| | | 56 | | { |
| | 0 | 57 | | var user = await _currentUserService.GetRequiredUserAsync(); |
| | | 58 | | |
| | 0 | 59 | | if (dto == null || string.IsNullOrWhiteSpace(dto.Name)) |
| | | 60 | | { |
| | 0 | 61 | | return BadRequest(new { error = "Name is required" }); |
| | | 62 | | } |
| | | 63 | | |
| | 0 | 64 | | if (dto.CampaignId == Guid.Empty) |
| | | 65 | | { |
| | 0 | 66 | | return BadRequest(new { error = "CampaignId is required" }); |
| | | 67 | | } |
| | | 68 | | |
| | 0 | 69 | | _logger.LogDebugSanitized("Creating arc '{Name}' in campaign {CampaignId} for user {UserId}", |
| | 0 | 70 | | dto.Name, dto.CampaignId, user.Id); |
| | | 71 | | |
| | 0 | 72 | | var arc = await _arcService.CreateArcAsync(dto, user.Id); |
| | | 73 | | |
| | 0 | 74 | | if (arc == null) |
| | | 75 | | { |
| | 0 | 76 | | return StatusCode(403, new { error = "Access denied or failed to create arc" }); |
| | | 77 | | } |
| | | 78 | | |
| | 0 | 79 | | return CreatedAtAction(nameof(GetArc), new { id = arc.Id }, arc); |
| | 0 | 80 | | } |
| | | 81 | | |
| | | 82 | | /// <summary> |
| | | 83 | | /// PUT /api/arcs/{id} - Update an arc. |
| | | 84 | | /// </summary> |
| | | 85 | | [HttpPut("{id:guid}")] |
| | | 86 | | public async Task<ActionResult<ArcDto>> UpdateArc(Guid id, [FromBody] ArcUpdateDto dto) |
| | | 87 | | { |
| | 0 | 88 | | var user = await _currentUserService.GetRequiredUserAsync(); |
| | | 89 | | |
| | 0 | 90 | | if (dto == null || string.IsNullOrWhiteSpace(dto.Name)) |
| | | 91 | | { |
| | 0 | 92 | | return BadRequest(new { error = "Name is required" }); |
| | | 93 | | } |
| | | 94 | | |
| | 0 | 95 | | _logger.LogDebug("Updating arc {ArcId} for user {UserId}", id, user.Id); |
| | | 96 | | |
| | 0 | 97 | | var arc = await _arcService.UpdateArcAsync(id, dto, user.Id); |
| | | 98 | | |
| | 0 | 99 | | if (arc == null) |
| | | 100 | | { |
| | 0 | 101 | | return NotFound(new { error = "Arc not found or access denied" }); |
| | | 102 | | } |
| | | 103 | | |
| | 0 | 104 | | return Ok(arc); |
| | 0 | 105 | | } |
| | | 106 | | |
| | | 107 | | /// <summary> |
| | | 108 | | /// DELETE /api/arcs/{id} - Delete an arc (only if empty). |
| | | 109 | | /// </summary> |
| | | 110 | | [HttpDelete("{id:guid}")] |
| | | 111 | | public async Task<IActionResult> DeleteArc(Guid id) |
| | | 112 | | { |
| | 0 | 113 | | var user = await _currentUserService.GetRequiredUserAsync(); |
| | 0 | 114 | | _logger.LogDebug("Deleting arc {ArcId} for user {UserId}", id, user.Id); |
| | | 115 | | |
| | 0 | 116 | | var success = await _arcService.DeleteArcAsync(id, user.Id); |
| | | 117 | | |
| | 0 | 118 | | if (!success) |
| | | 119 | | { |
| | 0 | 120 | | return BadRequest(new { error = "Arc not found, access denied, or arc is not empty" }); |
| | | 121 | | } |
| | | 122 | | |
| | 0 | 123 | | return NoContent(); |
| | 0 | 124 | | } |
| | | 125 | | |
| | | 126 | | /// <summary> |
| | | 127 | | /// POST /api/arcs/{id}/activate - Activate an arc for quick session creation. |
| | | 128 | | /// </summary> |
| | | 129 | | [HttpPost("{id:guid}/activate")] |
| | | 130 | | public async Task<IActionResult> ActivateArc(Guid id) |
| | | 131 | | { |
| | 0 | 132 | | var user = await _currentUserService.GetRequiredUserAsync(); |
| | 0 | 133 | | _logger.LogDebug("Activating arc {ArcId} for user {UserId}", id, user.Id); |
| | | 134 | | |
| | 0 | 135 | | var success = await _arcService.ActivateArcAsync(id, user.Id); |
| | | 136 | | |
| | 0 | 137 | | if (!success) |
| | | 138 | | { |
| | 0 | 139 | | return BadRequest(new { error = "Unable to activate arc. Arc not found or you don't have permission." }); |
| | | 140 | | } |
| | | 141 | | |
| | 0 | 142 | | return NoContent(); |
| | 0 | 143 | | } |
| | | 144 | | } |
| | | 145 | | |
| | | 146 | | /// <summary> |
| | | 147 | | /// Campaign-scoped arc endpoints. |
| | | 148 | | /// </summary> |
| | | 149 | | [ApiController] |
| | | 150 | | [Route("campaigns/{campaignId:guid}/arcs")] |
| | | 151 | | [Authorize] |
| | | 152 | | public class CampaignArcsController : ControllerBase |
| | | 153 | | { |
| | | 154 | | private readonly IArcService _arcService; |
| | | 155 | | private readonly ICurrentUserService _currentUserService; |
| | | 156 | | private readonly ILogger<CampaignArcsController> _logger; |
| | | 157 | | |
| | | 158 | | public CampaignArcsController( |
| | | 159 | | IArcService arcService, |
| | | 160 | | ICurrentUserService currentUserService, |
| | | 161 | | ILogger<CampaignArcsController> logger) |
| | | 162 | | { |
| | | 163 | | _arcService = arcService; |
| | | 164 | | _currentUserService = currentUserService; |
| | | 165 | | _logger = logger; |
| | | 166 | | } |
| | | 167 | | |
| | | 168 | | /// <summary> |
| | | 169 | | /// GET /api/campaigns/{campaignId}/arcs - Get all arcs for a campaign. |
| | | 170 | | /// </summary> |
| | | 171 | | [HttpGet] |
| | | 172 | | public async Task<ActionResult<List<ArcDto>>> GetArcsByCampaign(Guid campaignId) |
| | | 173 | | { |
| | | 174 | | var user = await _currentUserService.GetRequiredUserAsync(); |
| | | 175 | | _logger.LogDebug("Getting arcs for campaign {CampaignId} for user {UserId}", campaignId, user.Id); |
| | | 176 | | |
| | | 177 | | var arcs = await _arcService.GetArcsByCampaignAsync(campaignId, user.Id); |
| | | 178 | | return Ok(arcs); |
| | | 179 | | } |
| | | 180 | | } |