Spaces:
Sleeping
Sleeping
Download Controllers/CustomersController.cs from azizproject/Oilshop: direct link, hf CLI and curl.
- Browser
- Download file 3.43 kB
-
https://huggingface.co/spaces/azizproject/Oilshop/resolve/main/Controllers/CustomersController.cs
- Command line
-
hf download hf://spaces/azizproject/Oilshop/Controllers/CustomersController.cs
-
curl -L -o CustomersController.cs https://huggingface.co/spaces/azizproject/Oilshop/resolve/main/Controllers/CustomersController.cs
3.43 kB
| using Microsoft.AspNetCore.Authorization; | |
| using Microsoft.AspNetCore.Mvc; | |
| using Microsoft.EntityFrameworkCore; | |
| using OilShopManagement.Data; | |
| using OilShopManagement.Models; | |
| namespace OilShopManagement.Controllers; | |
| [] | |
| public class CustomersController : Controller | |
| { | |
| private readonly ApplicationDbContext _context; | |
| private readonly ILogger<CustomersController> _logger; | |
| public CustomersController(ApplicationDbContext context, ILogger<CustomersController> logger) | |
| { | |
| _context = context; | |
| _logger = logger; | |
| } | |
| public async Task<IActionResult> Index(string? search) | |
| { | |
| var query = _context.Customers.Where(c => c.IsActive).AsQueryable(); | |
| if (!string.IsNullOrWhiteSpace(search)) | |
| query = query.Where(c => c.Name.Contains(search) || (c.Phone != null && c.Phone.Contains(search))); | |
| ViewBag.Search = search; | |
| return View(await query.OrderBy(c => c.Name).ToListAsync()); | |
| } | |
| public async Task<IActionResult> Details(int id) | |
| { | |
| var customer = await _context.Customers | |
| .Include(c => c.Invoices.OrderByDescending(i => i.InvoiceDate).Take(10)) | |
| .FirstOrDefaultAsync(c => c.Id == id); | |
| if (customer == null) return NotFound(); | |
| return View(customer); | |
| } | |
| public IActionResult Create() => View(); | |
| [] | |
| [] | |
| public async Task<IActionResult> Create(Customer customer) | |
| { | |
| if (ModelState.IsValid) | |
| { | |
| customer.CreatedAt = DateTime.UtcNow; | |
| _context.Customers.Add(customer); | |
| await _context.SaveChangesAsync(); | |
| TempData["Success"] = "طھظ… ط¥ط¶ط§ظپط© ط§ظ„ط¹ظ…ظٹظ„ ط¨ظ†ط¬ط§ط"; | |
| return RedirectToAction(nameof(Index)); | |
| } | |
| return View(customer); | |
| } | |
| public async Task<IActionResult> Edit(int id) | |
| { | |
| var customer = await _context.Customers.FindAsync(id); | |
| if (customer == null) return NotFound(); | |
| return View(customer); | |
| } | |
| [] | |
| [] | |
| public async Task<IActionResult> Edit(int id, Customer customer) | |
| { | |
| if (id != customer.Id) return BadRequest(); | |
| if (ModelState.IsValid) | |
| { | |
| _context.Customers.Update(customer); | |
| await _context.SaveChangesAsync(); | |
| TempData["Success"] = "طھظ… طھط¹ط¯ظٹظ„ ط§ظ„ط¹ظ…ظٹظ„ ط¨ظ†ط¬ط§ط"; | |
| return RedirectToAction(nameof(Index)); | |
| } | |
| return View(customer); | |
| } | |
| [] | |
| [] | |
| public async Task<IActionResult> Delete(int id) | |
| { | |
| var customer = await _context.Customers.FindAsync(id); | |
| if (customer == null) return NotFound(); | |
| customer.IsActive = false; | |
| await _context.SaveChangesAsync(); | |
| TempData["Success"] = "طھظ… طط°ظپ ط§ظ„ط¹ظ…ظٹظ„"; | |
| return RedirectToAction(nameof(Index)); | |
| } | |
| [] | |
| public async Task<IActionResult> Search(string term) | |
| { | |
| var customers = await _context.Customers | |
| .Where(c => c.IsActive && (c.Name.Contains(term) || (c.Phone != null && c.Phone.Contains(term)))) | |
| .Select(c => new { c.Id, c.Name, c.Phone, c.CarModel, c.PlateNumber }) | |
| .Take(10).ToListAsync(); | |
| return Json(customers); | |
| } | |
| } | |