Files

53 lines
1.8 KiB
C#
Raw Permalink Normal View History

2026-06-01 22:08:43 +03:30
using AsadiTools.Data;
using AsadiTools.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using System.ComponentModel.DataAnnotations;
namespace AsadiTools.Pages.Admin.Products;
[Authorize(AuthenticationSchemes = "AdminCookie")]
public class CreateModel(AppDbContext db) : PageModel
{
[BindProperty] public ProductInput Input { get; set; } = new();
public void OnGet() { }
public async Task<IActionResult> OnPostAsync()
{
if (!ModelState.IsValid) return Page();
db.Products.Add(new Product
{
NameFa = Input.NameFa,
NameEn = Input.NameEn,
Description = Input.Description,
Price = Input.Price,
DiscountPrice = Input.DiscountPrice,
Category = Input.Category,
Brand = string.IsNullOrEmpty(Input.Brand) ? null : Input.Brand,
Sku = Input.Sku,
Stock = Input.Stock,
IsActive = Input.IsActive,
ImageUrl = string.IsNullOrWhiteSpace(Input.ImageUrl) ? null : Input.ImageUrl,
});
await db.SaveChangesAsync();
return RedirectToPage("/Admin/Products/Index");
}
}
public class ProductInput
{
[Required] public string NameFa { get; set; } = string.Empty;
public string? NameEn { get; set; }
public string? Description { get; set; }
[Required, Range(1, int.MaxValue)] public decimal Price { get; set; }
public decimal? DiscountPrice { get; set; }
[Required] public string Category { get; set; } = "carbon";
public string? Brand { get; set; }
public string? Sku { get; set; }
public int Stock { get; set; }
public bool IsActive { get; set; } = true;
public string? ImageUrl { get; set; }
}