Spaces:
Paused
Paused
| using Application.Abstractions.Interfaces; | |
| using Application.DTOs.AdminDTOs; | |
| using Application.DTOs.UserDTOs; | |
| using Application.DTOs.OrderDTOs; | |
| using Microsoft.AspNetCore.Authorization; | |
| using Microsoft.AspNetCore.Mvc; | |
| namespace RobDeliveryAPI.Controllers | |
| { | |
| [] | |
| [] | |
| [] | |
| public class AdminController : ControllerBase | |
| { | |
| private readonly IAdminService _adminService; | |
| private readonly ISettingsService _settingsService; | |
| private readonly IUserService _userService; | |
| private readonly IOrderService _orderService; | |
| public AdminController( | |
| IAdminService adminService, | |
| ISettingsService settingsService, | |
| IUserService userService, | |
| IOrderService orderService) | |
| { | |
| _adminService = adminService; | |
| _settingsService = settingsService; | |
| _userService = userService; | |
| _orderService = orderService; | |
| } | |
| /// <summary> | |
| /// Get system statistics for dashboard | |
| /// </summary> | |
| [] | |
| public async Task<IActionResult> GetSystemStats() | |
| { | |
| try | |
| { | |
| var stats = await _adminService.GetSystemStatsAsync(); | |
| return Ok(stats); | |
| } | |
| catch (Exception ex) | |
| { | |
| return StatusCode(500, new { error = "Failed to retrieve system stats", details = ex.Message }); | |
| } | |
| } | |
| /// <summary> | |
| /// Export delivery history as JSON | |
| /// </summary> | |
| [] | |
| public async Task<IActionResult> ExportDeliveryHistory() | |
| { | |
| try | |
| { | |
| var historyJson = await _adminService.ExportDeliveryHistoryAsync(); | |
| var fileName = $"DeliveryHistory_{DateTime.Now:yyyyMMdd_HHmmss}.json"; | |
| return File( | |
| System.Text.Encoding.UTF8.GetBytes(historyJson), | |
| "application/json", | |
| fileName | |
| ); | |
| } | |
| catch (Exception ex) | |
| { | |
| return StatusCode(500, new { error = "Failed to export delivery history", details = ex.Message }); | |
| } | |
| } | |
| /// <summary> | |
| /// Create database backup | |
| /// </summary> | |
| [] | |
| public async Task<IActionResult> CreateBackup([FromBody] BackupRequestDTO request) | |
| { | |
| try | |
| { | |
| string backupPath = request.BackupPath ?? "Backups"; | |
| var success = await _adminService.CreateDatabaseBackupAsync(backupPath); | |
| if (success) | |
| { | |
| return Ok(new { message = "Backup created successfully", path = backupPath }); | |
| } | |
| else | |
| { | |
| return StatusCode(500, new { error = "Failed to create backup" }); | |
| } | |
| } | |
| catch (Exception ex) | |
| { | |
| return StatusCode(500, new { error = "Failed to create backup", details = ex.Message }); | |
| } | |
| } | |
| /// <summary> | |
| /// Get robot efficiency analytics | |
| /// </summary> | |
| [] | |
| public async Task<IActionResult> GetRobotEfficiency() | |
| { | |
| try | |
| { | |
| var efficiency = await _adminService.GetRobotEfficiencyAsync(); | |
| return Ok(efficiency); | |
| } | |
| catch (Exception ex) | |
| { | |
| return StatusCode(500, new { error = "Failed to retrieve robot efficiency", details = ex.Message }); | |
| } | |
| } | |
| /// <summary> | |
| /// Generate a new admin registration key | |
| /// </summary> | |
| [] | |
| public async Task<IActionResult> GenerateAdminKey([FromBody] CreateAdminKeyDTO request) | |
| { | |
| try | |
| { | |
| var userIdClaim = User.FindFirst("Id")?.Value; | |
| if (userIdClaim == null || !int.TryParse(userIdClaim, out int adminId)) | |
| { | |
| return Unauthorized(new { error = "Invalid token" }); | |
| } | |
| var adminKey = await _adminService.GenerateAdminKeyAsync( | |
| adminId, | |
| request.ExpiresAt, | |
| request.Description | |
| ); | |
| return Ok(new | |
| { | |
| message = "Admin key generated successfully", | |
| key = adminKey | |
| }); | |
| } | |
| catch (Exception ex) | |
| { | |
| return StatusCode(500, new { error = "Failed to generate admin key", details = ex.Message }); | |
| } | |
| } | |
| /// <summary> | |
| /// Get all admin keys | |
| /// </summary> | |
| [] | |
| public async Task<IActionResult> GetAllAdminKeys() | |
| { | |
| try | |
| { | |
| var keys = await _adminService.GetAllAdminKeysAsync(); | |
| return Ok(keys); | |
| } | |
| catch (Exception ex) | |
| { | |
| return StatusCode(500, new { error = "Failed to retrieve admin keys", details = ex.Message }); | |
| } | |
| } | |
| /// <summary> | |
| /// Get unused admin keys | |
| /// </summary> | |
| [] | |
| public async Task<IActionResult> GetUnusedAdminKeys() | |
| { | |
| try | |
| { | |
| var keys = await _adminService.GetUnusedAdminKeysAsync(); | |
| return Ok(keys); | |
| } | |
| catch (Exception ex) | |
| { | |
| return StatusCode(500, new { error = "Failed to retrieve unused admin keys", details = ex.Message }); | |
| } | |
| } | |
| /// <summary> | |
| /// Revoke an admin key | |
| /// </summary> | |
| [] | |
| public async Task<IActionResult> RevokeAdminKey(int keyId) | |
| { | |
| try | |
| { | |
| var success = await _adminService.RevokeAdminKeyAsync(keyId); | |
| if (success) | |
| { | |
| return Ok(new { message = "Admin key revoked successfully" }); | |
| } | |
| else | |
| { | |
| return NotFound(new { error = "Admin key not found or already used" }); | |
| } | |
| } | |
| catch (Exception ex) | |
| { | |
| return StatusCode(500, new { error = "Failed to revoke admin key", details = ex.Message }); | |
| } | |
| } | |
| /// <summary> | |
| /// Get current delivery pricing settings | |
| /// </summary> | |
| [] | |
| public async Task<IActionResult> GetPricingSettings() | |
| { | |
| try | |
| { | |
| var settings = await _settingsService.GetPricingSettingsAsync(); | |
| return Ok(settings); | |
| } | |
| catch (Exception ex) | |
| { | |
| return StatusCode(500, new { error = "Failed to retrieve pricing settings", details = ex.Message }); | |
| } | |
| } | |
| /// <summary> | |
| /// Update delivery pricing settings | |
| /// </summary> | |
| [] | |
| public async Task<IActionResult> UpdatePricingSettings([FromBody] UpdatePricingDTO request) | |
| { | |
| try | |
| { | |
| var success = await _settingsService.UpdatePricingSettingsAsync(request); | |
| if (success) | |
| { | |
| return Ok(new { message = "Pricing settings updated successfully" }); | |
| } | |
| return BadRequest(new { error = "Failed to update pricing settings" }); | |
| } | |
| catch (Exception ex) | |
| { | |
| return StatusCode(500, new { error = "Failed to update pricing settings", details = ex.Message }); | |
| } | |
| } | |
| /// <summary> | |
| /// Download database backup file | |
| /// </summary> | |
| [] | |
| public async Task<IActionResult> DownloadBackup() | |
| { | |
| try | |
| { | |
| var (content, contentType, fileName) = await _adminService.DownloadDatabaseAsync(); | |
| return File(content, contentType, fileName); | |
| } | |
| catch (Exception ex) | |
| { | |
| return StatusCode(500, new { error = "Failed to download backup", details = ex.Message }); | |
| } | |
| } | |
| /// <summary> | |
| /// Restore database from uploaded file | |
| /// </summary> | |
| [] | |
| [] | |
| public async Task<IActionResult> RestoreBackup(IFormFile file) | |
| { | |
| if (file == null || file.Length == 0) | |
| { | |
| return BadRequest(new { error = "No file uploaded" }); | |
| } | |
| try | |
| { | |
| using (var stream = file.OpenReadStream()) | |
| { | |
| var success = await _adminService.RestoreDatabaseAsync(stream); | |
| if (success) | |
| { | |
| return Ok(new { message = "Database restored successfully. Application may need to restart to apply changes completely." }); | |
| } | |
| } | |
| return StatusCode(500, new { error = "Failed to restore database" }); | |
| } | |
| catch (Exception ex) | |
| { | |
| return StatusCode(500, new { error = "Failed to restore database", details = ex.Message }); | |
| } | |
| } | |
| // --- USER MANAGEMENT --- | |
| [] | |
| public async Task<IActionResult> GetAllUsers() | |
| { | |
| var users = await _userService.GetAllUsersAsync(); | |
| return Ok(users); | |
| } | |
| [] | |
| public async Task<IActionResult> UpdateUser([FromBody] AdminUpdateUserDTO updateDto) | |
| { | |
| var success = await _userService.AdminUpdateUserAsync(updateDto); | |
| return success ? Ok(new { message = "User updated successfully" }) : NotFound(new { error = "User not found" }); | |
| } | |
| [] | |
| public async Task<IActionResult> DeleteUser(int userId) | |
| { | |
| var success = await _userService.DeleteUserAsync(userId); | |
| return success ? Ok(new { message = "User deleted successfully" }) : NotFound(new { error = "User not found" }); | |
| } | |
| // --- ORDER MANAGEMENT --- | |
| [] | |
| public async Task<IActionResult> GetAllOrders() | |
| { | |
| var orders = await _orderService.GetAllOrdersAsync(); | |
| return Ok(orders); | |
| } | |
| [] | |
| public async Task<IActionResult> UpdateOrder([FromBody] AdminUpdateOrderDTO updateDto) | |
| { | |
| var success = await _orderService.AdminUpdateOrderAsync(updateDto); | |
| return success ? Ok(new { message = "Order updated successfully" }) : NotFound(new { error = "Order not found" }); | |
| } | |
| [] | |
| public async Task<IActionResult> DeleteOrder(int orderId) | |
| { | |
| var success = await _orderService.DeleteOrderAsync(orderId); | |
| return success ? Ok(new { message = "Order deleted successfully" }) : NotFound(new { error = "Order not found" }); | |
| } | |
| // --- KEY MANAGEMENT (EXPANDED) --- | |
| [] | |
| public async Task<IActionResult> UpdateAdminKey([FromBody] AdminUpdateKeyDTO updateDto) | |
| { | |
| var success = await _adminService.UpdateAdminKeyAsync(updateDto); | |
| return success ? Ok(new { message = "Admin key updated successfully" }) : NotFound(new { error = "Admin key not found" }); | |
| } | |
| [] | |
| public async Task<IActionResult> DeleteAdminKey(int keyId) | |
| { | |
| var success = await _adminService.DeleteAdminKeyAsync(keyId); | |
| return success ? Ok(new { message = "Admin key deleted successfully" }) : NotFound(new { error = "Admin key not found" }); | |
| } | |
| } | |
| public class BackupRequestDTO | |
| { | |
| public string? BackupPath { get; set; } | |
| } | |
| } | |