Added order image and description upload
This commit is contained in:
@@ -3,6 +3,7 @@ using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using OneForMe.Data;
|
||||
using OneForMe.Models;
|
||||
using System.IO;
|
||||
|
||||
namespace OneForMe.Controllers;
|
||||
|
||||
@@ -24,7 +25,7 @@ public class OrderController : Controller
|
||||
|
||||
// POST: Order/Create
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> Create(Order order, string[] itemNames, string[] itemPrices)
|
||||
public async Task<IActionResult> Create(Order order, string[] itemNames, string[] itemPrices, IFormFile? ImageFile)
|
||||
{
|
||||
if (!ModelState.IsValid)
|
||||
return View();
|
||||
@@ -32,6 +33,34 @@ public class OrderController : Controller
|
||||
order.OrderCode = GenerateOrderCode();
|
||||
order.CreatorName = User.Identity?.Name ?? "Unknown";
|
||||
|
||||
// Handle image upload
|
||||
if (ImageFile != null && ImageFile.Length > 0)
|
||||
{
|
||||
try
|
||||
{
|
||||
var uploadsFolder = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "uploads", "orders");
|
||||
if (!Directory.Exists(uploadsFolder))
|
||||
{
|
||||
Directory.CreateDirectory(uploadsFolder);
|
||||
}
|
||||
|
||||
var uniqueFileName = $"{order.OrderCode}_{Guid.NewGuid()}_{Path.GetFileName(ImageFile.FileName)}";
|
||||
var filePath = Path.Combine(uploadsFolder, uniqueFileName);
|
||||
|
||||
using (var fileStream = new FileStream(filePath, FileMode.Create))
|
||||
{
|
||||
await ImageFile.CopyToAsync(fileStream);
|
||||
}
|
||||
|
||||
order.ImagePath = $"/uploads/orders/{uniqueFileName}";
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
ModelState.AddModelError("ImageFile", $"Error uploading file: {ex.Message}");
|
||||
return View();
|
||||
}
|
||||
}
|
||||
|
||||
_context.Orders.Add(order);
|
||||
await _context.SaveChangesAsync();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user