Esempio n. 1
0
    public async Task <IActionResult> BookOperation(Guid?id)
    {
        if (id == null)
        {
            return(RedirectToAction(IndexAction, DefaultController));
        }

        var productToBook = _productRepository.GetOne(id.Value);

        if (productToBook == null)
        {
            return(View("ProductNotFound", id.Value));                       //ProductNotFound
        }
        var customer = await _userManager.GetUserAsync(User);

        if (customer == null)
        {
            return(View("CustomerNotFound"));                  //CustomerNotFound
        }
        var productCustomerViewModel = new ProductCustomerViewModel
        {
            CustomerId = customer.CustomerDbModelId, Product = productToBook
        };

        return(View(productCustomerViewModel));
    }
Esempio n. 2
0
    public async Task <IActionResult> PurchaseOperation(Guid?id)
    {
        if (id == null)
        {
            return(RedirectToAction(IndexAction, DefaultController));
        }

        var productToBuy = _productRepository.GetOne(id.Value);

        if (productToBuy == null)
        {
            return(View("ProductNotFound", id.Value));
        }

        var user = await _userManager.GetUserAsync(User);

        if (user == null)
        {
            return(View("CustomerNotFound"));
        }

        var customer = _customerRepository.GetOne(user.CustomerDbModelId);

        if (customer != null)
        {
            var productCustomerViewModel = new ProductCustomerViewModel
            {
                CustomerId = customer.Id,
                Balance    = customer.Balance,
                Product    = productToBuy,
                CashBack   = _propertyGetter.GetProperty <double>(EntityNames.Customer, nameof(VipCustomer.CashBack),
                                                                  EntityNames.CustomerId,
                                                                  customer.Id),
                CashBackPercent = _propertyGetter.GetProperty <int>(EntityNames.Customer,
                                                                    nameof(VipCustomer.CashBackPercent), EntityNames.CustomerId,
                                                                    customer.Id),
                DiscountPercent = _propertyGetter.GetProperty <int>(EntityNames.Customer,
                                                                    nameof(VipCustomer.DiscountPercent), EntityNames.CustomerId,
                                                                    customer.Id),
                Points = _propertyGetter.GetProperty <double>(EntityNames.Customer, nameof(VipCustomer.Points),
                                                              EntityNames.CustomerId, customer.Id)
            };
            return(View(productCustomerViewModel));
        }

        return(RedirectToAction(IndexAction, DefaultController));
    }
Esempio n. 3
0
    public IActionResult BookOperation(ProductCustomerViewModel productCustomerViewModel)
    {
        var customer = _customerRepository.GetOne(productCustomerViewModel.CustomerId);

        if (customer == null)
        {
            return(View("CustomerNotFound", productCustomerViewModel.CustomerId));                  //CustomerNotFound
        }
        var product = _productRepository.GetOne(productCustomerViewModel.ProductId);

        if (product == null)
        {
            return(View("ProductNotFound", productCustomerViewModel.ProductId));                 //ProductNotFound
        }
        var checkForLimit = _bookRepository.GetCustomerBooks(productCustomerViewModel.CustomerId);

        if (checkForLimit == null)
        {
            return(View("CustomerNotFound", productCustomerViewModel.CustomerId));
        }

        if (productCustomerViewModel.BookDaysCount is > 7 or < 1)
        {
            ModelState.AddModelError(string.Empty, "Books Days Count | Max: 7 Min: 1");
            return(View(new ProductCustomerViewModel
            {
                CustomerId = customer.Id, Product = product, BookDaysCount = productCustomerViewModel.BookDaysCount
            }));
        }

        if (checkForLimit.Count >= 3)
        {
            ModelState.AddModelError(string.Empty,
                                     $"Maximum Limit Of Books: 3 | Your Quantity Of Books: {checkForLimit.Count}");
            return(View(new ProductCustomerViewModel
            {
                CustomerId = customer.Id, Product = product, BookDaysCount = productCustomerViewModel.BookDaysCount
            }));
        }

        _reservationService.HandleOperation(productCustomerViewModel.CustomerId, productCustomerViewModel.ProductId,
                                            productCustomerViewModel.BookDaysCount);
        return(RedirectToAction(IndexAction, DefaultController));
    }
Esempio n. 4
0
    public IActionResult PurchaseOperation(ProductCustomerViewModel productCustomerViewModel)
    {
        var customer = _customerRepository.GetOne(productCustomerViewModel.CustomerId);

        if (customer == null)
        {
            return(View("CustomerNotFound", productCustomerViewModel.CustomerId));
        }
        var product = _productRepository.GetOne(productCustomerViewModel.ProductId);

        if (product == null)
        {
            return(View("ProductNotFound", productCustomerViewModel.ProductId));
        }
        string message;
        var    productDiscount =
            _propertyGetter.GetProperty <int>(EntityNames.Customer, nameof(VipCustomer.DiscountPercent),
                                              EntityNames.CustomerId, customer.Id);

        var priceToCompareWith =
            product.PriceInUAH - product.PriceInUAH * productDiscount / 100;

        var customerPoints =
            _propertyGetter.GetProperty <double>(EntityNames.Customer, nameof(VipCustomer.Points),
                                                 EntityNames.CustomerId, customer.Id);

        var customerCashBack =
            _propertyGetter.GetProperty <double>(EntityNames.Customer, nameof(VipCustomer.CashBack),
                                                 EntityNames.CustomerId, customer.Id);

        var productCustomerViewModelToReturnIfNotSucceed = new ProductCustomerViewModel
        {
            CustomerId      = customer.Id,
            Balance         = customer.Balance,
            Product         = product,
            CashBack        = customerCashBack,
            CashBackPercent =
                _propertyGetter.GetProperty <int>(EntityNames.Customer, nameof(VipCustomer.CashBackPercent),
                                                  EntityNames.CustomerId, customer.Id),
            DiscountPercent = productDiscount,
            Points          = customerPoints,
            UseCashBack     = productCustomerViewModel.UseCashBack,
            UsePoints       = productCustomerViewModel.UsePoints
        };

        if (productCustomerViewModel.UsePoints && customerPoints < priceToCompareWith / 1000)
        {
            message = "Not Enough Points";
            ModelState.AddModelError(string.Empty, message);
            return(View(productCustomerViewModelToReturnIfNotSucceed));
        }

        if (productCustomerViewModel.UseCashBack &&
            customer.Balance + customerCashBack < priceToCompareWith)
        {
            message = "Not Enough Money & Cash Back";
            ModelState.AddModelError(string.Empty, message);
            return(View(productCustomerViewModelToReturnIfNotSucceed));
        }

        if (!productCustomerViewModel.UsePoints && !productCustomerViewModel.UseCashBack &&
            customer.Balance < priceToCompareWith)
        {
            message = "Not Enough Money";
            ModelState.AddModelError(string.Empty, message);
            return(View(productCustomerViewModelToReturnIfNotSucceed));
        }

        _purchaseService.HandleOperation(productCustomerViewModel.CustomerId, productCustomerViewModel.ProductId,
                                         productCustomerViewModel.UseCashBack, productCustomerViewModel.UsePoints);
        return(RedirectToAction(IndexAction, DefaultController));
    }