예제 #1
0
        public async Task <JsonResult> OnPostCreateOrEditAsync(int id, Customer customer)
        {
            if (ModelState.IsValid)
            {
                if (id == 0)
                {
                    await _customer.AddAsync(customer);

                    await _unitOfWork.Commit();
                }
                else
                {
                    await _customer.UpdateAsync(customer);

                    await _unitOfWork.Commit();
                }
                Customers = await _customer.GetAllAsync();

                var html = await _renderService.ToStringAsync("_ViewAll", Customers);

                return(new JsonResult(new { isValid = true, html = html }));
            }
            else
            {
                var html = await _renderService.ToStringAsync("_CreateOrEdit", customer);

                return(new JsonResult(new { isValid = false, html = html }));
            }
        }
예제 #2
0
        public async Task <Response <int> > Handle(CreateCustomerCommand request, CancellationToken cancellationToken)
        {
            var customerInfo = _mapper.Map <CustomerInfo>(request);
            await _customerRepository.AddAsync(customerInfo);

            return(new Response <int>(customerInfo.Id));
        }
예제 #3
0
        private static async Task AddCustomerTest(ICustomerRepositoryAsync customerRepository)
        {
            //  ICustomerRepositoryAsync customerRepository = new ApiCustomerRepository(new HttpClient());

            CustomerFaker customerFaker = new CustomerFaker(new AddressFaker());
            Customer      customer      = customerFaker.Generate();
            await customerRepository.AddAsync(customer);
        }
예제 #4
0
        public async Task <IActionResult> Post([FromBody] Customer customer)
        {
            if (!this.ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            await customerRepository.AddAsync(customer);

            return(CreatedAtAction(nameof(GetById), new { Id = customer.Id }, customer));
        }
예제 #5
0
        public async Task <IActionResult> AddAsync([FromBody] Customer customer)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            try
            {
                var newCustomer = await _customerRepository.AddAsync(customer);

                if (newCustomer == null)
                {
                    return(BadRequest());
                }
                return(CreatedAtRoute("GetCustomerById", new { id = newCustomer.CustomerId }));
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.Message);
                return(BadRequest());
            }
        }