Fase 2: Implementación de la Matriz de Operaciones (Backend & Frontend)

This commit is contained in:
2025-12-17 13:21:50 -03:00
parent 523a8394ae
commit fae9101c63
8 changed files with 251 additions and 1 deletions

View File

@@ -52,4 +52,26 @@ public class CategoriesController : ControllerBase
await _repository.DeleteAsync(id);
return NoContent();
}
[HttpGet("{id}/operations")]
public async Task<IActionResult> GetOperations(int id)
{
var operations = await _repository.GetOperationsAsync(id);
return Ok(operations);
}
[HttpPost("{id}/operations/{operationId}")]
public async Task<IActionResult> AddOperation(int id, int operationId)
{
await _repository.AddOperationAsync(id, operationId);
return Ok();
}
[HttpDelete("{id}/operations/{operationId}")]
public async Task<IActionResult> RemoveOperation(int id, int operationId)
{
await _repository.RemoveOperationAsync(id, operationId);
return NoContent();
}
}

View File

@@ -9,4 +9,7 @@ public interface ICategoryRepository
Task UpdateAsync(Category category);
Task DeleteAsync(int id);
Task<IEnumerable<Category>> GetSubCategoriesAsync(int parentId);
Task<IEnumerable<Operation>> GetOperationsAsync(int categoryId);
Task AddOperationAsync(int categoryId, int operationId);
Task RemoveOperationAsync(int categoryId, int operationId);
}

View File

@@ -1,4 +1,5 @@
using Dapper;
using Microsoft.Data.SqlClient;
using SIGCM.Domain.Entities;
using SIGCM.Domain.Interfaces;
using SIGCM.Infrastructure.Data;
@@ -57,4 +58,38 @@ public class CategoryRepository : ICategoryRepository
using var conn = _connectionFactory.CreateConnection();
return await conn.QueryAsync<Category>("SELECT * FROM Categories WHERE ParentId = @ParentId", new { ParentId = parentId });
}
public async Task<IEnumerable<Operation>> GetOperationsAsync(int categoryId)
{
using var conn = _connectionFactory.CreateConnection();
var sql = @"
SELECT o.*
FROM Operations o
INNER JOIN CategoryOperations co ON o.Id = co.OperationId
WHERE co.CategoryId = @CategoryId";
return await conn.QueryAsync<Operation>(sql, new { CategoryId = categoryId });
}
public async Task AddOperationAsync(int categoryId, int operationId)
{
using var conn = _connectionFactory.CreateConnection();
var sql = "INSERT INTO CategoryOperations (CategoryId, OperationId) VALUES (@CategoryId, @OperationId)";
try
{
await conn.ExecuteAsync(sql, new { CategoryId = categoryId, OperationId = operationId });
}
catch (SqlException)
{
// Ignore duplicate key errors if it already exists
}
}
public async Task RemoveOperationAsync(int categoryId, int operationId)
{
using var conn = _connectionFactory.CreateConnection();
await conn.ExecuteAsync(
"DELETE FROM CategoryOperations WHERE CategoryId = @CategoryId AND OperationId = @OperationId",
new { CategoryId = categoryId, OperationId = operationId });
}
}