Fase 3: Implementación de Listings (Avisos) - Entidades, Repositorio, API y Frontend Wizard integración
This commit is contained in:
73
src/SIGCM.Infrastructure/Repositories/ListingRepository.cs
Normal file
73
src/SIGCM.Infrastructure/Repositories/ListingRepository.cs
Normal file
@@ -0,0 +1,73 @@
|
||||
using System.Data;
|
||||
using Dapper;
|
||||
using SIGCM.Application.DTOs;
|
||||
using SIGCM.Domain.Entities;
|
||||
using SIGCM.Domain.Interfaces;
|
||||
using SIGCM.Infrastructure.Data;
|
||||
|
||||
namespace SIGCM.Infrastructure.Repositories;
|
||||
|
||||
public class ListingRepository : IListingRepository
|
||||
{
|
||||
private readonly IDbConnectionFactory _connectionFactory;
|
||||
|
||||
public ListingRepository(IDbConnectionFactory connectionFactory)
|
||||
{
|
||||
_connectionFactory = connectionFactory;
|
||||
}
|
||||
|
||||
public async Task<int> CreateAsync(Listing listing, Dictionary<int, string> attributes)
|
||||
{
|
||||
using var conn = _connectionFactory.CreateConnection();
|
||||
conn.Open();
|
||||
using var transaction = conn.BeginTransaction();
|
||||
|
||||
try
|
||||
{
|
||||
var sqlListing = @"
|
||||
INSERT INTO Listings (CategoryId, OperationId, Title, Description, Price, Currency, CreatedAt, Status, UserId)
|
||||
VALUES (@CategoryId, @OperationId, @Title, @Description, @Price, @Currency, @CreatedAt, @Status, @UserId);
|
||||
SELECT CAST(SCOPE_IDENTITY() as int);";
|
||||
|
||||
var listingId = await conn.QuerySingleAsync<int>(sqlListing, listing, transaction);
|
||||
|
||||
if (attributes != null && attributes.Any())
|
||||
{
|
||||
var sqlAttr = @"
|
||||
INSERT INTO ListingAttributeValues (ListingId, AttributeDefinitionId, Value)
|
||||
VALUES (@ListingId, @AttributeDefinitionId, @Value)";
|
||||
|
||||
foreach (var attr in attributes)
|
||||
{
|
||||
await conn.ExecuteAsync(sqlAttr, new { ListingId = listingId, AttributeDefinitionId = attr.Key, Value = attr.Value }, transaction);
|
||||
}
|
||||
}
|
||||
|
||||
transaction.Commit();
|
||||
return listingId;
|
||||
}
|
||||
catch
|
||||
{
|
||||
transaction.Rollback();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<Listing?> GetByIdAsync(int id)
|
||||
{
|
||||
using var conn = _connectionFactory.CreateConnection();
|
||||
return await conn.QuerySingleOrDefaultAsync<Listing>("SELECT * FROM Listings WHERE Id = @Id", new { Id = id });
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<Listing>> GetAllAsync()
|
||||
{
|
||||
using var conn = _connectionFactory.CreateConnection();
|
||||
// A simple query for now
|
||||
var sql = @"
|
||||
SELECT l.*
|
||||
FROM Listings l
|
||||
ORDER BY l.CreatedAt DESC";
|
||||
|
||||
return await conn.QueryAsync<Listing>(sql);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user