using Microsoft.AspNetCore.Mvc; using Nop.Web.Areas.Admin.Controllers; using Nop.Web.Framework.Mvc.Filters; using Nop.Web.Framework; using Nop.Services.Security; using Microsoft.AspNetCore.Http; using Nop.Plugin.Misc.FruitBankPlugin.Areas.Admin.Models; using Nop.Services.Messages; namespace Nop.Plugin.Misc.FruitBankPlugin.Areas.Admin.Controllers { [Area(AreaNames.ADMIN)] [AuthorizeAdmin] public class ShipmentController : BaseAdminController { private readonly IPermissionService _permissionService; protected readonly INotificationService _notificationService; public ShipmentController(IPermissionService permissionService, INotificationService notificationService) { _permissionService = permissionService; _notificationService = notificationService; } public async Task List() { if (!await _permissionService.AuthorizeAsync(StandardPermission.Security.ACCESS_ADMIN_PANEL)) return AccessDeniedView(); return View("~/Plugins/Misc.FruitBankPlugin/Areas/Admin/Views/Shipment/List.cshtml"); } [HttpGet] public async Task Create() { if (!await _permissionService.AuthorizeAsync(StandardPermission.Security.ACCESS_ADMIN_PANEL)) return AccessDeniedView(); var model = new CreateShipmentModel { ShipmentDate = DateTime.Now }; return View("~/Plugins/Misc.FruitBankPlugin/Areas/Admin/Views/Shipment/Create.cshtml", model); } [HttpPost] public async Task Create(CreateShipmentModel model) { if (!await _permissionService.AuthorizeAsync(StandardPermission.Security.ACCESS_ADMIN_PANEL)) return AccessDeniedView(); if (!ModelState.IsValid) { return View("~/Plugins/Misc.FruitBankPlugin/Areas/Admin/Views/Shipment/Create.cshtml", model); } try { // TODO: Save shipment to database // var shipment = new Shipment // { // Name = model.ShipmentName, // Description = model.Description, // ShipmentDate = model.ShipmentDate, // TrackingNumber = model.TrackingNumber, // CreatedOnUtc = DateTime.UtcNow // }; // await _shipmentService.InsertShipmentAsync(shipment); _notificationService.SuccessNotification("Shipment created successfully"); return RedirectToAction("List"); } catch (Exception ex) { _notificationService.ErrorNotification($"Error creating shipment: {ex.Message}"); return View("~/Plugins/Misc.FruitBankPlugin/Areas/Admin/Views/Shipment/Create.cshtml", model); } } [HttpPost] public async Task UploadFile(IFormFile file) { try { if (file == null || file.Length == 0) return Json(new FileUploadResult { Success = false, ErrorMessage = "No file selected" }); // Validate file type (PDF only) if (!file.ContentType.Equals("application/pdf", StringComparison.OrdinalIgnoreCase)) return Json(new FileUploadResult { Success = false, ErrorMessage = "Only PDF files are allowed" }); // Validate file size (e.g., max 10MB) if (file.Length > 10 * 1024 * 1024) return Json(new FileUploadResult { Success = false, ErrorMessage = "File size must be less than 10MB" }); // Create upload directory if it doesn't exist var uploadsPath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "uploads", "shipments"); Directory.CreateDirectory(uploadsPath); // Generate unique filename var fileName = $"{Guid.NewGuid()}_{file.FileName}"; var filePath = Path.Combine(uploadsPath, fileName); // Save file using (var stream = new FileStream(filePath, FileMode.Create)) { await file.CopyToAsync(stream); } return Json(new FileUploadResult { Success = true, FileName = file.FileName, FilePath = $"/uploads/shipments/{fileName}" }); } catch (Exception ex) { return Json(new FileUploadResult { Success = false, ErrorMessage = ex.Message }); } } [HttpPost] public IActionResult DeleteUploadedFile(string filePath) { try { if (string.IsNullOrEmpty(filePath)) return Json(new { success = false, message = "Invalid file path" }); var fullPath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", filePath.TrimStart('/')); if (System.IO.File.Exists(fullPath)) { System.IO.File.Delete(fullPath); return Json(new { success = true }); } return Json(new { success = false, message = "File not found" }); } catch (Exception ex) { return Json(new { success = false, message = ex.Message }); } } } }