예제 #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));
        }
        public void Update(Dealership dealership, DealershipDto dealershipDto)
        {
            dealership.DealershipName = dealershipDto.DealershipName;
            dealership.WebsiteUrl     = dealershipDto.WebsiteUrl.ToLower();
            dealership.IsActive       = dealershipDto.IsActive;

            _repository.Update(dealership);
        }
        public void Add(DealershipDto dealershipDto)
        {
            var dealership = new Dealership
            {
                DealershipName = dealershipDto.DealershipName,
                WebsiteUrl     = dealershipDto.WebsiteUrl.ToLower(),
                IsActive       = true,
                DateAdded      = DateTime.Now
            };

            _repository.Add(dealership);
        }