Esempio n. 1
0
        public IActionResult Create(DealershipFormViewModel dealershipViewModel)
        {
            try
            {
                dealershipViewModel.SaveButtonText = "Create";

                if (ModelState.IsValid)
                {
                    var dealerships = _businessLogics.DealershipBusinessLogic.Get(
                        d => new
                    {
                        d.DealershipName,
                        d.WebsiteUrl
                    },
                        d => d.DealershipName.Equals(dealershipViewModel.DealershipName) ||
                        d.WebsiteUrl.Equals(dealershipViewModel.WebsiteUrl));

                    if (dealerships.Count() > 0)
                    {
                        foreach (var dealership in dealerships)
                        {
                            if (dealership.DealershipName.Equals(dealershipViewModel.DealershipName))
                            {
                                ModelState.AddModelError(nameof(dealershipViewModel.DealershipName), "Dealership name already exists");
                            }

                            if (dealership.WebsiteUrl.Equals(dealershipViewModel.WebsiteUrl))
                            {
                                ModelState.AddModelError(nameof(dealershipViewModel.WebsiteUrl), "Website URL already exists");
                            }
                        }

                        return(View("DealershipForm", dealershipViewModel));
                    }

                    var dealershipDto = new DealershipDto
                    {
                        DealershipName = dealershipViewModel.DealershipName,
                        WebsiteUrl     = dealershipViewModel.WebsiteUrl
                    };

                    _businessLogics.DealershipBusinessLogic.Add(dealershipDto);

                    _businessLogics.Save();

                    dealershipViewModel.SuccessMessage = "Dealership successfully added";
                    dealershipViewModel.DealershipName = string.Empty;
                    dealershipViewModel.WebsiteUrl     = string.Empty;

                    ModelState.Clear();
                }
            }
            catch (Exception ex)
            {
                dealershipViewModel.ErrorMessage = "An error has occurred";
            }

            return(View("DealershipForm", dealershipViewModel));
        }
Esempio n. 2
0
        public IActionResult Create(CarFormViewModel carFormViewModel)
        {
            carFormViewModel.Heading        = "Add Car";
            carFormViewModel.SaveButtonText = "Create";

            try
            {
                carFormViewModel.Dealerships = GetDealerships();

                if (!ModelState.IsValid)
                {
                    carFormViewModel.DealershipAddresses = GetDealershipAddresses(carFormViewModel.DealershipId);
                    return(View("CarForm", carFormViewModel));
                }

                _businessLogics.CarBusinessLogic.Add(new CarDto
                {
                    Year                = carFormViewModel.Year.Value,
                    Make                = carFormViewModel.Make,
                    Model               = carFormViewModel.Model,
                    Trim                = carFormViewModel.Trim,
                    Mileage             = carFormViewModel.Mileage.Value,
                    Price               = carFormViewModel.Price.Value,
                    IsNew               = carFormViewModel.IsNew,
                    DealershipAddressId = carFormViewModel.DealershipAddressId
                });

                _businessLogics.Save();

                carFormViewModel.SuccessMessage = "Car successfully added";

                ModelState.Clear();

                carFormViewModel.DealershipId        = 0;
                carFormViewModel.DealershipAddressId = 0;
                carFormViewModel.DealershipAddresses = GetDealershipAddresses();
                carFormViewModel.Year    = null;
                carFormViewModel.Make    = string.Empty;
                carFormViewModel.Model   = string.Empty;
                carFormViewModel.Trim    = string.Empty;
                carFormViewModel.Mileage = null;
                carFormViewModel.Price   = null;
                carFormViewModel.IsNew   = false;

                return(View("CarForm", carFormViewModel));
            }
            catch (Exception ex)
            {
                carFormViewModel.DealershipAddresses = GetDealershipAddresses(carFormViewModel.DealershipId);
                carFormViewModel.ErrorMessage        = "An error has occurred";
            }

            return(View("CarForm", carFormViewModel));
        }