Ejemplo n.º 1
0
        public async Task <IActionResult> CreateCustomer(CreateCustomerInputModel createCustomerInputModel)
        {
            if (!ModelState.IsValid)
            {
                return(this.View(StringConstants.CreateCustomerViewName, createCustomerInputModel));
            }
            User newCustomer = this.mapper.Map <User>(createCustomerInputModel);
            bool success     = await this.userService.RegisterUserAsync(newCustomer, createCustomerInputModel.Password);

            if (!success)
            {
                throw new ApplicationException();
            }
            if (createCustomerInputModel.IsCorporateCustomer)
            {
                success = await this.userService.AddUserToRoleAsync(newCustomer, StringConstants.CorporateCustomerUserRole);
            }
            else
            {
                success = await this.userService.AddUserToRoleAsync(newCustomer, StringConstants.CustomerUserRole);
            }
            if (!success)
            {
                throw new ApplicationException();
            }
            return(this.RedirectToAction(StringConstants.ActionNameIndex, StringConstants.HomeControllerName, new { area = StringConstants.AreaNameAdministration }));
        }
        public async Task <IActionResult> CreateAsync([FromBody] CreateCustomerInputModel inputModel,
                                                      CancellationToken cancellationToken = default)
        {
            var customerCreated = await _customerService.CreateCustomerAsync(inputModel, cancellationToken);

            return(Created($"api/customers/{customerCreated.Id}", customerCreated));
        }
Ejemplo n.º 3
0
        public void Create_ValidInputModel_ReturnsCreatedCustomer()
        {
            var customer = new Customer()
            {
                Id             = 1,
                UserId         = "aaaa",
                User           = new AppUser(),
                Gender         = Gender.Female,
                AgeGroup       = AgeGroup.Adult,
                EducationLevel = EducationLevel.Master,
                CoursesOrdered = new List <Order>(),
                ExamsTaken     = new List <Exam>(),
                Feedbacks      = new List <Feedback>()
            };

            this.repository.Setup(r => r.AddAsync(customer)).Returns(Task.FromResult(customer));

            var model = new CreateCustomerInputModel()
            {
                UserId         = "aaaa",
                Gender         = Gender.Female,
                AgeGroup       = AgeGroup.Adult,
                EducationLevel = EducationLevel.Master,
            };

            var result = this.customerService.Create(model);

            Assert.That(result.UserId, Is.EqualTo("aaaa"));
            Assert.That(result.Gender, Is.EqualTo(Gender.Female));
            Assert.That(result, Is.TypeOf <Customer>());
        }
 public ServiceResult <CustomerViewModel> Create(CreateCustomerInputModel request)
 {
     return(new ServiceResult <CustomerViewModel>()
     {
         StatusCode = StatusCode.Created,
         Data = new CustomerViewModel()
     });
 }
        public Customer Create(CreateCustomerInputModel model)
        {
            var customer = Mapper.Map <Customer>(model);

            this.customerRepository.AddAsync(customer).GetAwaiter().GetResult();
            this.customerRepository.SaveChangesAsync().GetAwaiter().GetResult();

            return(customer);
        }
Ejemplo n.º 6
0
        public void CatchCreateCustomerWithInvalidEmail(string email)
        {
            var customerService = new CustomerService(Mock.Of <IRepository>());

            var inputModel = new CreateCustomerInputModel()
            {
                Name = "test", Email = email
            };

            Assert.ThrowsAsync <ModelValidationException>(async() => await customerService.CreateCustomerAsync(inputModel));
        }
Ejemplo n.º 7
0
 public static CreateCustomerModel MapToCreateCustomerModel(this CreateCustomerInputModel inputModel) => new CreateCustomerModel
 {
     FirstName   = inputModel.FirstName,
     Insertion   = inputModel.Insertion,
     LastName    = inputModel.LastName,
     Email       = inputModel.Email,
     Password    = inputModel.Password,
     PostalCode  = inputModel.PostalCode,
     HouseNumber = inputModel.HouseNumber,
     Address     = inputModel.Address,
     City        = inputModel.City
 };
Ejemplo n.º 8
0
        public async Task <IActionResult> Create()
        {
            var model = new CreateCustomerInputModel
            {
                Address = new CustomersAddressInputModel
                {
                    Cities = await this.addressService.GetCitiesByCountryAsync <CityViewModel>(GlobalConstants.CountryOfUsing) as ICollection <CityViewModel>,
                },
            };

            return(this.View(model));
        }
Ejemplo n.º 9
0
        public ServiceResult <CustomerViewModel> Create(CreateCustomerInputModel request)
        {
            IRestRequest CreateCustomer = new RestRequest("api/CustomerManagement", method: Method.POST);

            CreateCustomer.AddJsonBody(request);
            IRestResponse <ServiceResult <CustomerViewModel> > Result = RestProvider.GetInstance().Post <ServiceResult <CustomerViewModel> >(CreateCustomer);

            if (Result != null)
            {
                return(Result.Data);
            }

            throw new Exception();
        }
Ejemplo n.º 10
0
        public async Task <IActionResult> Create(CreateCustomerInputModel model)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.View(model));
            }

            var    customer   = model.To <CustomerInputModel>();
            string customerId = await this.customerService.CreateAsync <CustomerInputModel>(customer);

            var address = model.Address;

            address.CustomerId = customerId;
            await this.addressService.CreateAsync <CustomersAddressInputModel>(address);

            return(this.RedirectToAction("Search"));
        }
        public ActionResult Create(CreateCustomerInputModel model)
        {
            try
            {
                if (!this.ModelState.IsValid)
                {
                    this.ViewBag.UserId = model.UserId;
                    return(this.View(model));
                }

                var customer = this.customerService.Create(model);
                this.accountService.SetRole("User", model.UserId);

                return(this.RedirectToAction("Index", "Home"));
            }
            catch (Exception e)
            {
                return(this.View("_Error", e.Message));
            }
        }
Ejemplo n.º 12
0
        public async Task CreateCustomerWithSuccess()
        {
            using (var context = CreateContext())
            {
                var customerService = new CustomerService(new RepositoryBase(context));

                var inputModel = new CreateCustomerInputModel()
                {
                    Name = "Test", Email = "*****@*****.**"
                };

                var result = await customerService.CreateCustomerAsync(inputModel);

                Assert.AreEqual(inputModel.Name, result.Name);
                Assert.AreEqual(inputModel.Email, result.Email);
                Assert.IsTrue(result.Id != default);

                ClearContext(context);
            }
        }
Ejemplo n.º 13
0
        public async Task <CustomerDTO> CreateCustomerAsync(CreateCustomerInputModel inputModel, CancellationToken cancellationToken = default)
        {
            inputModel.EnsureIsValid();

            var customerPersisted = await _repository.Query <Customer>().FirstOrDefaultAsync(c => c.Email.Equals(inputModel.Email), cancellationToken);

            if (customerPersisted != null)
            {
                throw new BusinessException("Customer already exists with this email");
            }

            var customer = new Customer(inputModel.Name, inputModel.Email);

            await _repository.AddAsync(customer, cancellationToken);

            return(new CustomerDTO()
            {
                Id = customer.Id,
                Name = customer.Name,
                Email = customer.Email
            });
        }
Ejemplo n.º 14
0
        public async Task CatchCreateCustomerWithExistingEmail()
        {
            using (var context = CreateContext())
            {
                var inputModel = new CreateCustomerInputModel()
                {
                    Name = "Test", Email = "*****@*****.**"
                };

                await context.AddAsync(new Customer()
                {
                    Email = inputModel.Email
                });

                await context.SaveChangesAsync();

                var customerService = new CustomerService(new RepositoryBase(context));

                Assert.ThrowsAsync <BusinessException>(async() => await customerService.CreateCustomerAsync(inputModel));

                ClearContext(context);
            }
        }
 public async Task CreateCustomerAsync(CreateCustomerInputModel model)
 {
     await _customerService.CreateCustomerAsync(model.MapToCreateCustomerModel());
 }
Ejemplo n.º 16
0
 public ServiceResult <CustomerViewModel> Post(CreateCustomerInputModel request)
 {
     return(_customerManagementCommand.Create(request));
 }