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 })); } }
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)); }
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); }
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)); }
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()); } }