Fase 2: Gestión de Atributos Dinámicos (Back & Front EAV Definition)
This commit is contained in:
39
src/SIGCM.API/Controllers/AttributeDefinitionsController.cs
Normal file
39
src/SIGCM.API/Controllers/AttributeDefinitionsController.cs
Normal file
@@ -0,0 +1,39 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using SIGCM.Domain.Entities;
|
||||
using SIGCM.Domain.Interfaces;
|
||||
|
||||
namespace SIGCM.API.Controllers;
|
||||
|
||||
[ApiController]
|
||||
[Route("api/[controller]")]
|
||||
public class AttributeDefinitionsController : ControllerBase
|
||||
{
|
||||
private readonly IAttributeDefinitionRepository _repository;
|
||||
|
||||
public AttributeDefinitionsController(IAttributeDefinitionRepository repository)
|
||||
{
|
||||
_repository = repository;
|
||||
}
|
||||
|
||||
[HttpGet("category/{categoryId}")]
|
||||
public async Task<IActionResult> GetByCategoryId(int categoryId)
|
||||
{
|
||||
var attributes = await _repository.GetByCategoryIdAsync(categoryId);
|
||||
return Ok(attributes);
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> Create(AttributeDefinition attribute)
|
||||
{
|
||||
var id = await _repository.AddAsync(attribute);
|
||||
attribute.Id = id;
|
||||
return Ok(attribute);
|
||||
}
|
||||
|
||||
[HttpDelete("{id}")]
|
||||
public async Task<IActionResult> Delete(int id)
|
||||
{
|
||||
await _repository.DeleteAsync(id);
|
||||
return NoContent();
|
||||
}
|
||||
}
|
||||
10
src/SIGCM.Domain/Entities/AttributeDefinition.cs
Normal file
10
src/SIGCM.Domain/Entities/AttributeDefinition.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
namespace SIGCM.Domain.Entities;
|
||||
|
||||
public class AttributeDefinition
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public int CategoryId { get; set; }
|
||||
public required string Name { get; set; }
|
||||
public required string DataType { get; set; } // text, number, boolean, date
|
||||
public bool Required { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
namespace SIGCM.Domain.Interfaces;
|
||||
using SIGCM.Domain.Entities;
|
||||
|
||||
public interface IAttributeDefinitionRepository
|
||||
{
|
||||
Task<IEnumerable<AttributeDefinition>> GetByCategoryIdAsync(int categoryId);
|
||||
Task<int> AddAsync(AttributeDefinition attribute);
|
||||
Task DeleteAsync(int id);
|
||||
}
|
||||
@@ -111,6 +111,18 @@ BEGIN
|
||||
FOREIGN KEY (OperationId) REFERENCES Operations(Id) ON DELETE CASCADE
|
||||
);
|
||||
END
|
||||
|
||||
IF NOT EXISTS (SELECT * FROM sys.tables WHERE name = 'AttributeDefinitions')
|
||||
BEGIN
|
||||
CREATE TABLE AttributeDefinitions (
|
||||
Id INT IDENTITY(1,1) PRIMARY KEY,
|
||||
CategoryId INT NOT NULL,
|
||||
Name NVARCHAR(100) NOT NULL,
|
||||
DataType NVARCHAR(20) NOT NULL,
|
||||
Required BIT DEFAULT 0,
|
||||
FOREIGN KEY (CategoryId) REFERENCES Categories(Id) ON DELETE CASCADE
|
||||
);
|
||||
END
|
||||
";
|
||||
await connection.ExecuteAsync(schemaSql);
|
||||
}
|
||||
|
||||
@@ -17,9 +17,11 @@ public static class DependencyInjection
|
||||
services.AddScoped<IUserRepository, UserRepository>();
|
||||
services.AddScoped<ITokenService, Services.TokenService>();
|
||||
services.AddScoped<IAuthService, Services.AuthService>();
|
||||
services.AddScoped<IAttributeDefinitionRepository, AttributeDefinitionRepository>();
|
||||
return services;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
using Dapper;
|
||||
using Microsoft.Data.SqlClient;
|
||||
using SIGCM.Domain.Entities;
|
||||
using SIGCM.Domain.Interfaces;
|
||||
using SIGCM.Infrastructure.Data;
|
||||
|
||||
namespace SIGCM.Infrastructure.Repositories;
|
||||
|
||||
public class AttributeDefinitionRepository : IAttributeDefinitionRepository
|
||||
{
|
||||
private readonly IDbConnectionFactory _connectionFactory;
|
||||
|
||||
public AttributeDefinitionRepository(IDbConnectionFactory connectionFactory)
|
||||
{
|
||||
_connectionFactory = connectionFactory;
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<AttributeDefinition>> GetByCategoryIdAsync(int categoryId)
|
||||
{
|
||||
using var conn = _connectionFactory.CreateConnection();
|
||||
return await conn.QueryAsync<AttributeDefinition>(
|
||||
"SELECT * FROM AttributeDefinitions WHERE CategoryId = @CategoryId",
|
||||
new { CategoryId = categoryId });
|
||||
}
|
||||
|
||||
public async Task<int> AddAsync(AttributeDefinition attribute)
|
||||
{
|
||||
using var conn = _connectionFactory.CreateConnection();
|
||||
var sql = @"
|
||||
INSERT INTO AttributeDefinitions (CategoryId, Name, DataType, Required)
|
||||
VALUES (@CategoryId, @Name, @DataType, @Required);
|
||||
SELECT CAST(SCOPE_IDENTITY() as int);";
|
||||
return await conn.QuerySingleAsync<int>(sql, attribute);
|
||||
}
|
||||
|
||||
public async Task DeleteAsync(int id)
|
||||
{
|
||||
using var conn = _connectionFactory.CreateConnection();
|
||||
await conn.ExecuteAsync("DELETE FROM AttributeDefinitions WHERE Id = @Id", new { Id = id });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user