Skip to content

Open-source eCommerce platform for C# and ASP.NET Core 10 with MongoDB. Build multi-store shops and multi-vendor marketplaces with B2B/B2C, REST API, plugins, Vue 3 and Docker.

5.0 KB
Raw Blame
using Grand.Business.Core.Interfaces.Catalog.Discounts;
using Grand.Business.Core.Interfaces.Common.Directory;
using Grand.Business.Core.Interfaces.Common.Localization;
using Grand.Business.Core.Interfaces.Customers;
using Grand.Domain;
using Grand.Domain.Discounts;
using Grand.Domain.Permissions;
using Grand.Domain.Vendors;
using Grand.Mediator;
using Grand.Web.Admin.Extensions;
using Grand.Web.AdminShared.Controllers;
using Grand.Web.AdminShared.Extensions.Mapping;
using Grand.Web.AdminShared.Interfaces;
using Grand.Web.AdminShared.Models.Discounts;
using Grand.Web.Common.DataSource;
using Grand.Web.Common.Filters;
using Grand.Web.Common.Security.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace Grand.Web.Admin.Controllers;

[AuthorizeAdmin]
[AutoValidateAntiforgeryToken]
[Area(Constants.AreaAdmin)]
[AuthorizeMenu]
public class DiscountController(
    IDiscountViewModelService discountViewModelService,
    IDiscountService discountService,
    ITranslationService translationService,
    IDateTimeService dateTimeService,
    IMediator mediator,
    IDiscountProviderLoader discountProviderLoader,
    IAdminDataScope<Discount> scope)
    : BaseDiscountController(discountViewModelService, discountService, translationService, dateTimeService,
        mediator, discountProviderLoader, scope)
{
    #region Applied to vendors (Admin-only — no Store equivalent, see Task 7b)

    [PermissionAuthorizeAction(PermissionActionName.Preview)]
    [HttpPost]
    public async Task<IActionResult> VendorList(DataSourceRequest command, string discountId,
        [FromServices] IVendorService vendorService)
    {
        var discount = await DiscountService.GetDiscountById(discountId);
        if (discount == null)
            throw new Exception("No discount found with the specified id");
        if (!await Scope.CanView(discount))
            return new JsonResult(new DataSourceResult { Errors = "Access denied" });

        var vendors = await vendorService.GetAllVendorsByDiscount(discount.Id);
        return Json(new DataSourceResult {
            Data = vendors.Select(x => new DiscountModel.AppliedToVendorModel { VendorId = x.Id, VendorName = x.Name }),
            Total = vendors.Count
        });
    }

    [PermissionAuthorizeAction(PermissionActionName.Edit)]
    public async Task<IActionResult> VendorDelete(string discountId, string vendorId,
        [FromServices] IVendorService vendorService)
    {
        var discount = await DiscountService.GetDiscountById(discountId);
        if (discount == null)
            throw new Exception("No discount found with the specified id");
        if (!await Scope.HasAccess(discount))
            return new JsonResult(new DataSourceResult { Errors = "Access denied" });

        var vendor = await vendorService.GetVendorById(vendorId);
        if (vendor == null)
            throw new Exception("No vendor found with the specified id");
        if (ModelState.IsValid)
        {
            await DiscountViewModelService.DeleteVendor(discount, vendor);
            return new JsonResult("");
        }
        return ErrorForKendoGridJson(ModelState);
    }

    [PermissionAuthorizeAction(PermissionActionName.Edit)]
    public async Task<IActionResult> VendorAddPopup(string discountId)
    {
        var discount = await DiscountService.GetDiscountById(discountId);
        if (discount == null)
            throw new Exception("No discount found with the specified id");
        if (!await Scope.HasAccess(discount))
            return new JsonResult(new DataSourceResult { Errors = "Access denied" });

        return View(new DiscountModel.AddVendorToDiscountModel());
    }

    [PermissionAuthorizeAction(PermissionActionName.Edit)]
    [HttpPost]
    public async Task<IActionResult> VendorAddPopupList(DataSourceRequest command,
        DiscountModel.AddVendorToDiscountModel model, [FromServices] IVendorService vendorService)
    {
        var vendors = await vendorService.GetAllVendors(model.SearchVendorName, command.Page - 1, command.PageSize, true);

        if (!string.IsNullOrEmpty(model.SearchVendorEmail))
        {
            var tempVendors = vendors.Where(x => x.Email.ToLowerInvariant().Contains(model.SearchVendorEmail.Trim()));
            vendors = new PagedList<Vendor>(tempVendors, command.Page - 1, command.PageSize);
        }

        return Json(new DataSourceResult { Data = vendors.Select(x => x.ToModel()), Total = vendors.TotalCount });
    }

    [PermissionAuthorizeAction(PermissionActionName.Edit)]
    [HttpPost]
    public async Task<IActionResult> VendorAddPopup(DiscountModel.AddVendorToDiscountModel model)
    {
        var discount = await DiscountService.GetDiscountById(model.DiscountId);
        if (discount == null)
            throw new Exception("No discount found with the specified id");
        if (!await Scope.HasAccess(discount))
            return Content("Access denied");

        if (model.SelectedVendorIds != null) await DiscountViewModelService.InsertVendorToDiscountModel(model);
        return Content("");
    }

    #endregion
}