"Improve-product-UI-with-individual-bubbles-and-fix-admin-authentication"
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using LittleShop.Services;
|
||||
using LittleShop.DTOs;
|
||||
using LittleShop.Data;
|
||||
|
||||
namespace LittleShop.Controllers;
|
||||
|
||||
@@ -10,11 +12,13 @@ public class TestController : ControllerBase
|
||||
{
|
||||
private readonly ICategoryService _categoryService;
|
||||
private readonly IProductService _productService;
|
||||
private readonly LittleShopContext _context;
|
||||
|
||||
public TestController(ICategoryService categoryService, IProductService productService)
|
||||
public TestController(ICategoryService categoryService, IProductService productService, LittleShopContext context)
|
||||
{
|
||||
_categoryService = categoryService;
|
||||
_productService = productService;
|
||||
_context = context;
|
||||
}
|
||||
|
||||
[HttpPost("create-product")]
|
||||
@@ -86,4 +90,52 @@ public class TestController : ControllerBase
|
||||
return BadRequest(new { error = ex.Message });
|
||||
}
|
||||
}
|
||||
|
||||
[HttpPost("cleanup-bots")]
|
||||
public async Task<IActionResult> CleanupBots()
|
||||
{
|
||||
try
|
||||
{
|
||||
// Get count before cleanup
|
||||
var totalBots = await _context.Bots.CountAsync();
|
||||
|
||||
// Keep only the most recent active bot per platform
|
||||
var keepBots = await _context.Bots
|
||||
.Where(b => b.IsActive && b.Status == Enums.BotStatus.Active)
|
||||
.GroupBy(b => b.PlatformId)
|
||||
.Select(g => g.OrderByDescending(b => b.LastSeenAt ?? b.CreatedAt).First())
|
||||
.ToListAsync();
|
||||
|
||||
var keepBotIds = keepBots.Select(b => b.Id).ToList();
|
||||
|
||||
// Delete old/inactive bots and related data
|
||||
var botsToDelete = await _context.Bots
|
||||
.Where(b => !keepBotIds.Contains(b.Id))
|
||||
.ToListAsync();
|
||||
|
||||
_context.Bots.RemoveRange(botsToDelete);
|
||||
await _context.SaveChangesAsync();
|
||||
|
||||
var deletedCount = botsToDelete.Count;
|
||||
var remainingCount = keepBots.Count;
|
||||
|
||||
return Ok(new {
|
||||
message = "Bot cleanup completed",
|
||||
totalBots = totalBots,
|
||||
deletedBots = deletedCount,
|
||||
remainingBots = remainingCount,
|
||||
keptBots = keepBots.Select(b => new {
|
||||
id = b.Id,
|
||||
name = b.Name,
|
||||
platformUsername = b.PlatformUsername,
|
||||
lastSeen = b.LastSeenAt,
|
||||
created = b.CreatedAt
|
||||
})
|
||||
});
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return BadRequest(new { error = ex.Message });
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user