CheckPoint: Avances Varios
This commit is contained in:
@@ -25,8 +25,16 @@ public class ListingRepository : IListingRepository
|
||||
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);
|
||||
INSERT INTO Listings (
|
||||
CategoryId, OperationId, Title, Description, Price, Currency,
|
||||
CreatedAt, Status, UserId, PrintText, PrintStartDate, PrintDaysCount,
|
||||
IsBold, IsFrame, PrintFontSize, PrintAlignment
|
||||
)
|
||||
VALUES (
|
||||
@CategoryId, @OperationId, @Title, @Description, @Price, @Currency,
|
||||
@CreatedAt, @Status, @UserId, @PrintText, @PrintStartDate, @PrintDaysCount,
|
||||
@IsBold, @IsFrame, @PrintFontSize, @PrintAlignment
|
||||
);
|
||||
SELECT CAST(SCOPE_IDENTITY() as int);";
|
||||
|
||||
var listingId = await conn.QuerySingleAsync<int>(sqlListing, listing, transaction);
|
||||
@@ -59,15 +67,90 @@ public class ListingRepository : IListingRepository
|
||||
return await conn.QuerySingleOrDefaultAsync<Listing>("SELECT * FROM Listings WHERE Id = @Id", new { Id = id });
|
||||
}
|
||||
|
||||
public async Task<SIGCM.Domain.Models.ListingDetail?> GetDetailByIdAsync(int id)
|
||||
{
|
||||
using var conn = _connectionFactory.CreateConnection();
|
||||
var sql = @"
|
||||
SELECT * FROM Listings WHERE Id = @Id;
|
||||
|
||||
SELECT lav.*, ad.Name as AttributeName
|
||||
FROM ListingAttributeValues lav
|
||||
JOIN AttributeDefinitions ad ON lav.AttributeDefinitionId = ad.Id
|
||||
WHERE lav.ListingId = @Id;
|
||||
|
||||
SELECT * FROM ListingImages WHERE ListingId = @Id ORDER BY DisplayOrder;
|
||||
";
|
||||
|
||||
using var multi = await conn.QueryMultipleAsync(sql, new { Id = id });
|
||||
var listing = await multi.ReadSingleOrDefaultAsync<Listing>();
|
||||
if (listing == null) return null;
|
||||
|
||||
var attributes = await multi.ReadAsync<SIGCM.Domain.Models.ListingAttributeValueWithName>();
|
||||
var images = await multi.ReadAsync<ListingImage>();
|
||||
|
||||
return new SIGCM.Domain.Models.ListingDetail
|
||||
{
|
||||
Listing = listing,
|
||||
Attributes = attributes,
|
||||
Images = images
|
||||
};
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<Listing>> GetAllAsync()
|
||||
{
|
||||
using var conn = _connectionFactory.CreateConnection();
|
||||
// A simple query for now
|
||||
// Subquery para obtener la imagen principal
|
||||
var sql = @"
|
||||
SELECT l.*
|
||||
SELECT TOP 20 l.*,
|
||||
(SELECT TOP 1 Url FROM ListingImages li WHERE li.ListingId = l.Id ORDER BY IsMainInfo DESC, DisplayOrder ASC) as MainImageUrl
|
||||
FROM Listings l
|
||||
ORDER BY l.CreatedAt DESC";
|
||||
|
||||
|
||||
return await conn.QueryAsync<Listing>(sql);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<Listing>> SearchAsync(string? query, int? categoryId)
|
||||
{
|
||||
using var conn = _connectionFactory.CreateConnection();
|
||||
var sql = @"
|
||||
SELECT l.*,
|
||||
(SELECT TOP 1 Url FROM ListingImages li WHERE li.ListingId = l.Id ORDER BY IsMainInfo DESC, DisplayOrder ASC) as MainImageUrl
|
||||
FROM Listings l
|
||||
WHERE 1=1";
|
||||
|
||||
var parameters = new DynamicParameters();
|
||||
|
||||
if (!string.IsNullOrEmpty(query))
|
||||
{
|
||||
sql += " AND (l.Title LIKE @Query OR l.Description LIKE @Query)";
|
||||
parameters.Add("Query", $"%{query}%");
|
||||
}
|
||||
|
||||
if (categoryId.HasValue)
|
||||
{
|
||||
sql += " AND l.CategoryId = @CategoryId";
|
||||
parameters.Add("CategoryId", categoryId);
|
||||
}
|
||||
|
||||
sql += " ORDER BY l.CreatedAt DESC";
|
||||
|
||||
return await conn.QueryAsync<Listing>(sql, parameters);
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<Listing>> GetListingsForPrintAsync(DateTime targetDate)
|
||||
{
|
||||
using var conn = _connectionFactory.CreateConnection();
|
||||
// La lógica: El aviso debe haber empezado antes o en la fecha target
|
||||
// Y la fecha target debe ser menor a la fecha de inicio + duración
|
||||
var sql = @"
|
||||
SELECT l.*, c.Name as CategoryName
|
||||
FROM Listings l
|
||||
JOIN Categories c ON l.CategoryId = c.Id
|
||||
WHERE l.PrintStartDate IS NOT NULL
|
||||
AND @TargetDate >= CAST(l.PrintStartDate AS DATE)
|
||||
AND @TargetDate < DATEADD(day, l.PrintDaysCount, CAST(l.PrintStartDate AS DATE))
|
||||
ORDER BY c.Name, l.Title"; // Ordenado por Rubro y luego alfabético
|
||||
|
||||
return await conn.QueryAsync<Listing>(sql, new { TargetDate = targetDate.Date });
|
||||
}
|
||||
}
|
||||
94
src/SIGCM.Infrastructure/Repositories/PricingRepository.cs
Normal file
94
src/SIGCM.Infrastructure/Repositories/PricingRepository.cs
Normal file
@@ -0,0 +1,94 @@
|
||||
using Dapper;
|
||||
using SIGCM.Domain.Entities;
|
||||
using SIGCM.Infrastructure.Data;
|
||||
|
||||
namespace SIGCM.Infrastructure.Repositories;
|
||||
|
||||
public class PricingRepository
|
||||
{
|
||||
private readonly IDbConnectionFactory _db;
|
||||
|
||||
public PricingRepository(IDbConnectionFactory db)
|
||||
{
|
||||
_db = db;
|
||||
}
|
||||
|
||||
public async Task<CategoryPricing?> GetByCategoryIdAsync(int categoryId)
|
||||
{
|
||||
using var conn = _db.CreateConnection();
|
||||
return await conn.QuerySingleOrDefaultAsync<CategoryPricing>(
|
||||
"SELECT * FROM CategoryPricing WHERE CategoryId = @Id", new { Id = categoryId });
|
||||
}
|
||||
|
||||
public async Task UpsertPricingAsync(CategoryPricing pricing)
|
||||
{
|
||||
using var conn = _db.CreateConnection();
|
||||
// Lógica de "Si existe actualiza, sino inserta"
|
||||
var exists = await conn.ExecuteScalarAsync<int>(
|
||||
"SELECT COUNT(1) FROM CategoryPricing WHERE CategoryId = @CategoryId", new { pricing.CategoryId });
|
||||
|
||||
if (exists > 0)
|
||||
{
|
||||
var updateSql = @"
|
||||
UPDATE CategoryPricing
|
||||
SET BasePrice = @BasePrice, BaseWordCount = @BaseWordCount,
|
||||
ExtraWordPrice = @ExtraWordPrice, SpecialChars = @SpecialChars,
|
||||
SpecialCharPrice = @SpecialCharPrice, BoldSurcharge = @BoldSurcharge,
|
||||
FrameSurcharge = @FrameSurcharge
|
||||
WHERE CategoryId = @CategoryId";
|
||||
await conn.ExecuteAsync(updateSql, pricing);
|
||||
}
|
||||
else
|
||||
{
|
||||
var insertSql = @"
|
||||
INSERT INTO CategoryPricing
|
||||
(CategoryId, BasePrice, BaseWordCount, ExtraWordPrice, SpecialChars, SpecialCharPrice, BoldSurcharge, FrameSurcharge)
|
||||
VALUES
|
||||
(@CategoryId, @BasePrice, @BaseWordCount, @ExtraWordPrice, @SpecialChars, @SpecialCharPrice, @BoldSurcharge, @FrameSurcharge)";
|
||||
await conn.ExecuteAsync(insertSql, pricing);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<Promotion>> GetActivePromotionsAsync()
|
||||
{
|
||||
using var conn = _db.CreateConnection();
|
||||
return await conn.QueryAsync<Promotion>(
|
||||
"SELECT * FROM Promotions WHERE IsActive = 1");
|
||||
}
|
||||
|
||||
// --- SECCIÓN PROMOCIONES ---
|
||||
|
||||
public async Task<IEnumerable<Promotion>> GetAllPromotionsAsync()
|
||||
{
|
||||
using var conn = _db.CreateConnection();
|
||||
return await conn.QueryAsync<Promotion>("SELECT * FROM Promotions ORDER BY Id DESC");
|
||||
}
|
||||
|
||||
public async Task<int> CreatePromotionAsync(Promotion promo)
|
||||
{
|
||||
using var conn = _db.CreateConnection();
|
||||
var sql = @"
|
||||
INSERT INTO Promotions (Name, CategoryId, MinDays, DaysOfWeek, DiscountPercentage, DiscountFixedAmount, IsActive)
|
||||
VALUES (@Name, @CategoryId, @MinDays, @DaysOfWeek, @DiscountPercentage, @DiscountFixedAmount, @IsActive);
|
||||
SELECT CAST(SCOPE_IDENTITY() as int);";
|
||||
return await conn.QuerySingleAsync<int>(sql, promo);
|
||||
}
|
||||
|
||||
public async Task UpdatePromotionAsync(Promotion promo)
|
||||
{
|
||||
using var conn = _db.CreateConnection();
|
||||
var sql = @"
|
||||
UPDATE Promotions
|
||||
SET Name = @Name, CategoryId = @CategoryId, MinDays = @MinDays,
|
||||
DaysOfWeek = @DaysOfWeek, DiscountPercentage = @DiscountPercentage,
|
||||
DiscountFixedAmount = @DiscountFixedAmount, IsActive = @IsActive
|
||||
WHERE Id = @Id";
|
||||
await conn.ExecuteAsync(sql, promo);
|
||||
}
|
||||
|
||||
public async Task DeletePromotionAsync(int id)
|
||||
{
|
||||
using var conn = _db.CreateConnection();
|
||||
await conn.ExecuteAsync("DELETE FROM Promotions WHERE Id = @Id", new { Id = id });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user