"Improve-product-UI-with-individual-bubbles-and-fix-admin-authentication"
This commit is contained in:
parent
5748ed4a09
commit
7e364b2a44
@ -43,7 +43,8 @@ public class AccountController : Controller
|
|||||||
var claims = new List<Claim>
|
var claims = new List<Claim>
|
||||||
{
|
{
|
||||||
new(ClaimTypes.Name, "admin"),
|
new(ClaimTypes.Name, "admin"),
|
||||||
new(ClaimTypes.NameIdentifier, Guid.NewGuid().ToString())
|
new(ClaimTypes.NameIdentifier, Guid.NewGuid().ToString()),
|
||||||
|
new(ClaimTypes.Role, "Admin")
|
||||||
};
|
};
|
||||||
|
|
||||||
var identity = new ClaimsIdentity(claims, "Cookies");
|
var identity = new ClaimsIdentity(claims, "Cookies");
|
||||||
|
|||||||
36
LittleShop/Areas/Admin/Views/Account/AccessDenied.cshtml
Normal file
36
LittleShop/Areas/Admin/Views/Account/AccessDenied.cshtml
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
@{
|
||||||
|
ViewData["Title"] = "Access Denied";
|
||||||
|
}
|
||||||
|
|
||||||
|
<div class="container-fluid">
|
||||||
|
<div class="row justify-content-center">
|
||||||
|
<div class="col-md-6">
|
||||||
|
<div class="card shadow-sm">
|
||||||
|
<div class="card-body text-center">
|
||||||
|
<div class="mb-4">
|
||||||
|
<i class="fas fa-lock fa-3x text-danger mb-3"></i>
|
||||||
|
<h2 class="text-danger">Access Denied</h2>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="alert alert-danger" role="alert">
|
||||||
|
<strong>Unauthorized Access</strong><br>
|
||||||
|
You don't have permission to access this resource.
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<p class="text-muted">
|
||||||
|
You need to log in with an administrator account to access the admin panel.
|
||||||
|
</p>
|
||||||
|
|
||||||
|
<div class="mt-4">
|
||||||
|
<a href="/Admin/Account/Login" class="btn btn-primary">
|
||||||
|
<i class="fas fa-sign-in-alt"></i> Login
|
||||||
|
</a>
|
||||||
|
<a href="/" class="btn btn-outline-secondary">
|
||||||
|
<i class="fas fa-home"></i> Home
|
||||||
|
</a>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
@ -1,6 +1,8 @@
|
|||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
using LittleShop.Services;
|
using LittleShop.Services;
|
||||||
using LittleShop.DTOs;
|
using LittleShop.DTOs;
|
||||||
|
using LittleShop.Data;
|
||||||
|
|
||||||
namespace LittleShop.Controllers;
|
namespace LittleShop.Controllers;
|
||||||
|
|
||||||
@ -10,11 +12,13 @@ public class TestController : ControllerBase
|
|||||||
{
|
{
|
||||||
private readonly ICategoryService _categoryService;
|
private readonly ICategoryService _categoryService;
|
||||||
private readonly IProductService _productService;
|
private readonly IProductService _productService;
|
||||||
|
private readonly LittleShopContext _context;
|
||||||
|
|
||||||
public TestController(ICategoryService categoryService, IProductService productService)
|
public TestController(ICategoryService categoryService, IProductService productService, LittleShopContext context)
|
||||||
{
|
{
|
||||||
_categoryService = categoryService;
|
_categoryService = categoryService;
|
||||||
_productService = productService;
|
_productService = productService;
|
||||||
|
_context = context;
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpPost("create-product")]
|
[HttpPost("create-product")]
|
||||||
@ -86,4 +90,52 @@ public class TestController : ControllerBase
|
|||||||
return BadRequest(new { error = ex.Message });
|
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 });
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Binary file not shown.
Binary file not shown.
@ -219,13 +219,40 @@ namespace TeleBot.Handlers
|
|||||||
categoryName = categories.FirstOrDefault(c => c.Id == categoryId)?.Name;
|
categoryName = categories.FirstOrDefault(c => c.Id == categoryId)?.Name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Edit the original message to show category header
|
||||||
|
var headerText = !string.IsNullOrEmpty(categoryName)
|
||||||
|
? $"📦 *Products in {categoryName}*\n\nBrowse products below:"
|
||||||
|
: "📦 *All Products*\n\nBrowse products below:";
|
||||||
|
|
||||||
await bot.EditMessageTextAsync(
|
await bot.EditMessageTextAsync(
|
||||||
message.Chat.Id,
|
message.Chat.Id,
|
||||||
message.MessageId,
|
message.MessageId,
|
||||||
MessageFormatter.FormatProductList(products, categoryName),
|
headerText,
|
||||||
parseMode: Telegram.Bot.Types.Enums.ParseMode.Markdown,
|
parseMode: Telegram.Bot.Types.Enums.ParseMode.Markdown,
|
||||||
replyMarkup: MenuBuilder.ProductListMenu(products, categoryId, page)
|
replyMarkup: MenuBuilder.CategoryNavigationMenu(categoryId)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// Send individual product bubbles
|
||||||
|
if (products.Items.Any())
|
||||||
|
{
|
||||||
|
foreach (var product in products.Items)
|
||||||
|
{
|
||||||
|
await bot.SendTextMessageAsync(
|
||||||
|
message.Chat.Id,
|
||||||
|
MessageFormatter.FormatSingleProduct(product),
|
||||||
|
parseMode: Telegram.Bot.Types.Enums.ParseMode.Markdown,
|
||||||
|
replyMarkup: MenuBuilder.SingleProductMenu(product.Id)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
await bot.SendTextMessageAsync(
|
||||||
|
message.Chat.Id,
|
||||||
|
"No products available in this category.",
|
||||||
|
replyMarkup: MenuBuilder.BackToCategoriesMenu()
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task HandleProductDetail(ITelegramBotClient bot, Message message, UserSession session, Guid productId)
|
private async Task HandleProductDetail(ITelegramBotClient bot, Message message, UserSession session, Guid productId)
|
||||||
|
|||||||
@ -320,5 +320,37 @@ namespace TeleBot.UI
|
|||||||
_ => "📋"
|
_ => "📋"
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static InlineKeyboardMarkup SingleProductMenu(Guid productId)
|
||||||
|
{
|
||||||
|
return new InlineKeyboardMarkup(new[]
|
||||||
|
{
|
||||||
|
new[] {
|
||||||
|
InlineKeyboardButton.WithCallbackData("🛒 Quick Buy", $"add:{productId}:1"),
|
||||||
|
InlineKeyboardButton.WithCallbackData("📄 Details", $"product:{productId}")
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public static InlineKeyboardMarkup CategoryNavigationMenu(Guid? categoryId)
|
||||||
|
{
|
||||||
|
return new InlineKeyboardMarkup(new[]
|
||||||
|
{
|
||||||
|
new[] {
|
||||||
|
InlineKeyboardButton.WithCallbackData("⬅️ Back to Categories", "browse"),
|
||||||
|
InlineKeyboardButton.WithCallbackData("🏠 Main Menu", "menu")
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public static InlineKeyboardMarkup BackToCategoriesMenu()
|
||||||
|
{
|
||||||
|
return new InlineKeyboardMarkup(new[]
|
||||||
|
{
|
||||||
|
new[] {
|
||||||
|
InlineKeyboardButton.WithCallbackData("⬅️ Back to Categories", "browse")
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -89,6 +89,25 @@ namespace TeleBot.UI
|
|||||||
return sb.ToString();
|
return sb.ToString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static string FormatSingleProduct(Product product)
|
||||||
|
{
|
||||||
|
var sb = new StringBuilder();
|
||||||
|
|
||||||
|
sb.AppendLine($"🛍️ *{product.Name}*");
|
||||||
|
sb.AppendLine($"💰 £{product.Price:F2}");
|
||||||
|
|
||||||
|
if (!string.IsNullOrEmpty(product.Description))
|
||||||
|
{
|
||||||
|
// Truncate description for bubble format
|
||||||
|
var desc = product.Description.Length > 100
|
||||||
|
? product.Description.Substring(0, 100) + "..."
|
||||||
|
: product.Description;
|
||||||
|
sb.AppendLine($"\n_{desc}_");
|
||||||
|
}
|
||||||
|
|
||||||
|
return sb.ToString();
|
||||||
|
}
|
||||||
|
|
||||||
public static string FormatProductDetail(Product product)
|
public static string FormatProductDetail(Product product)
|
||||||
{
|
{
|
||||||
var sb = new StringBuilder();
|
var sb = new StringBuilder();
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user