Example #1
0
        public async Task <IActionResult> Create(CustomerCreateInputModel customerCreate)
        {
            try
            {
                if (!this.ModelState.IsValid)
                {
                    return(this.View(customerCreate));
                }

                var result = await this.customersService.CreateAsync(customerCreate);

                return(this.RedirectToAction(nameof(this.Index)));
            }
            catch (ArgumentException e)
            {
                // TODO: Error message that item name exist
                this.ModelState.AddModelError(e.ParamName, e.Message);
                return(this.View(customerCreate));
            }
            catch (NullReferenceException e)
            {
                // TODO: Error message that item name exist
                this.ModelState.AddModelError(e.InnerException.Message, e.Message);
                return(this.View(customerCreate));
            }
        }
Example #2
0
        public async Task <IActionResult> Create(CustomerCreateInputModel customerCreate)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.View(AutoMapper.Mapper.Map <CustomerCreateViewModel>(customerCreate)));
            }

            var hasAnyCustomerWithPhoneNumber = await this.customersService.GetAllCustomersAsync <CustomerCreateViewModel>().AnyAsync(x => x.PhoneNumber == customerCreate.PhoneNumber);

            // If customer with phone number exists return existing view model
            if (hasAnyCustomerWithPhoneNumber)
            {
                this.ModelState.AddModelError(string.Empty, string.Format(CustomerConstants.ArgumentExceptionCustomerExistAddAddress));
                return(this.View(AutoMapper.Mapper.Map <CustomerCreateViewModel>(customerCreate)));
            }

            if (string.IsNullOrEmpty(customerCreate.DeliveryAddress))
            {
                customerCreate.DeliveryAddress = customerCreate.PickUpAddress;
            }

            var result = await this.customersService.CreateAsync(customerCreate);

            return(this.RedirectToAction(nameof(this.Index)));
        }
Example #3
0
        public async Task <CustomerCreateViewModel> CreateAsync(CustomerCreateInputModel customerFromView)
        {
            var customerToDb = customerFromView.To <Customer>();

            await this.customerRepository.AddAsync(customerToDb);

            await this.customerRepository.SaveChangesAsync();

            return(customerToDb.To <CustomerCreateViewModel>());
        }
Example #4
0
        public async Task <CustomerIndexViewModel> CreateAsync(CustomerCreateInputModel customerFromView)
        {
            var checkForPhoneNumber = this.customerRepository.All().FirstOrDefault(x => x.PhoneNumber == customerFromView.PhoneNumber);

            // If item exists return existing view model
            if (checkForPhoneNumber != null)
            {
                throw new ArgumentException(string.Format(CustomerConstants.ArgumentExceptionCustomerPhone, customerFromView.PhoneNumber), nameof(customerFromView.PhoneNumber));
            }

            var customerToDb = customerFromView.To <Customer>();

            await this.customerRepository.AddAsync(customerToDb);

            await this.customerRepository.SaveChangesAsync();

            return(customerToDb.To <CustomerIndexViewModel>());
        }