Esempio n. 1
0
        public void MapCustomerWith1AddressToGetViewModel_ShouldBeValid()
        {
            Customer customer = new Customer
            {
                CustomerId   = 19,
                ModifiedDate = DateTime.UtcNow
            };

            Address address = new Address
            {
                AddressId    = 29,
                City         = "Auburn",
                ModifiedDate = DateTime.UtcNow.AddMinutes(10.0)
            };

            customer.CustomerAddress.Add(new CustomerAddress
            {
                CustomerId   = customer.CustomerId,
                AddressId    = address.AddressId,
                AddressType  = "Work",
                ModifiedDate = DateTime.UtcNow.AddMinutes(20.0),
                Customer     = customer,
                Address      = address
            });

            CustomerGetViewModel vm = mapper.Map <CustomerGetViewModel>(customer);

            Assert.IsType <CustomerGetViewModel>(vm);
            Assert.NotEmpty(vm.Addresses);
        }
        public async Task <ActionResult <CustomerGetViewModel> > GetById(int id)
        {
            logger.LogInformation($"Get customer where id = {id}");
            Customer customer = await repository.GetByIdAsync(id);

            if (null == customer)
            {
                return(NotFound(id));
            }

            CustomerGetViewModel vm = mapper.Map <CustomerGetViewModel>(customer);

            return(Ok(vm));
        }
        public async Task <ActionResult <CustomerGetViewModel> > Create([FromBody] CustomerUpdateViewModel updateVm)
        {
            Customer customer = mapper.Map <Customer>(updateVm);

            customer = PasswordService.GenerateNewHash(customer, customer.FirstName + customer.LastName);
            try
            {
                customer = await repository.AddAsync(customer);

                CustomerGetViewModel getVm = mapper.Map <CustomerGetViewModel>(customer);
                return(CreatedAtAction(nameof(GetById), new { id = getVm.Id }, getVm));
            }
            catch (Exception e)
            {
                logger.LogError(e, "Saving changes for new customer failed.");
            }
            return(BadRequest());
        }
 public ActionResult getCustomer(string username)
 {
     try
     {
         Customers            customers            = _customersService.Get(_ => _.Username == username);
         CustomerGetViewModel customerGetViewModel = new CustomerGetViewModel
         {
             Username   = customers.Username,
             FirstName  = customers.FirstName,
             LastName   = customers.LastName,
             DayOfBirth = customers.DayOfBirth,
             Email      = customers.Email,
             Active     = customers.Active,
             Gender     = customers.Gender,
         };
         return(Ok(customerGetViewModel));
     }
     catch (Exception e)
     {
         return(BadRequest(e.Message));
     }
 }