public async Task <ActionResult> UpdateDemoCustomer([FromRoute] Guid merchantGuid, [FromRoute] Guid demoCustomerGuid, [FromBody] NewDemoCustomerMBE updatedDemoCustomer) { // query the db for the merchant var dbMerchant = await _dbContext.GetMerchantAsync(merchantGuid); // if we did not find a matching merchant if (dbMerchant == null) { return(BadRequest(new ArgumentException($"MerchantGuid: [{merchantGuid}] not found", nameof(merchantGuid)))); } // trims Customer name so that it doesn't have trailing characters updatedDemoCustomer.CustomerName = updatedDemoCustomer.CustomerName.Trim(); // validate the input params (bool isValidPhoneNo, string formatedPhoneNo, string normalizedPhoneNo) = Utilities.PhoneNoHelpers.NormalizePhoneNo(updatedDemoCustomer.CustomerPhoneNo); if (!isValidPhoneNo) { return(BadRequest(new ArgumentNullException($"[{updatedDemoCustomer.CustomerPhoneNo}] is NOT a supported Phone No format.", nameof(updatedDemoCustomer.CustomerPhoneNo)))); } // get the existing demo customer var dbDemoCustomers = await _dbContext.GetDemoCustomersAsync(dbMerchant.MerchantId); var dbDemoCustomer = dbDemoCustomers.Where(c => c.DemoCustomerGuid == demoCustomerGuid).FirstOrDefault(); if (dbDemoCustomer == null) { return(NotFound($"CustomerID: [{demoCustomerGuid}] on MerchantID: [{merchantGuid}] not found")); } // grab the current phone no string existingCustomerPhoneNo = dbDemoCustomer.CustomerPhoneNo; try { //Save the updated customer dbDemoCustomer.CustomerName = updatedDemoCustomer.CustomerName; dbDemoCustomer.CustomerPhoneNo = formatedPhoneNo; await _dbContext.UpdateDemoCustomerAsync(dbDemoCustomer); } catch (Exception ex) { return(BadRequest(new ApplicationException($"Error: [{ex.Message}] trying to add Phone No: [{existingCustomerPhoneNo}] to merchant: [{merchantGuid}]"))); } return(NoContent()); }
public async Task <ActionResult> UpdateDemoCustomer(Guid merchantGuid, Guid demoCustomerGuid, [FromBody] NewDemoCustomerMBE updatedDemoCustomer) { if (merchantGuid != GeneralConstants.MERCHANT_1_GUID) { return(NotFound($"Merchant with ID: {merchantGuid} not found")); } else if (demoCustomerGuid != GeneralConstants.MERCHANT_1_CUSTOMER_1_GUID) { return(NotFound($"Customer with ID: {demoCustomerGuid} on Merchant with ID: {merchantGuid} not found")); } await Task.Delay(100); return(NoContent()); }
public async Task <ActionResult <DemoCustomerMBE> > AddDemoCustomer([FromRoute] Guid merchantGuid, [FromBody] NewDemoCustomerMBE newDemoCustomer) { //trims Customer name so that it doesn't have trailing characters newDemoCustomer.CustomerName = newDemoCustomer.CustomerName.Trim(); // validate request data (bool isValidPhoneNo, string formatedPhoneNo, string normalizedPhoneNo) = Utilities.PhoneNoHelpers.NormalizePhoneNo(newDemoCustomer.CustomerPhoneNo); if (!isValidPhoneNo) { ModelState.AddModelError(nameof(newDemoCustomer.CustomerPhoneNo), $"[{newDemoCustomer.CustomerPhoneNo}] is NOT a supported Phone No format."); return(BadRequest(ModelState)); } //query the db for the merchant var dbMerchant = await _dbContext.GetMerchantAsync(merchantGuid); // if we did not find a matching merchant if (dbMerchant == null) { return(BadRequest(new ArgumentException($"MerchantGuid: [{merchantGuid}] not found", nameof(merchantGuid)))); } else { newDemoCustomer.CustomerPhoneNo = formatedPhoneNo; } try { //Store the new customer var newDBDemoCustomer = new DemoCustomerDBE() { MerchantId = dbMerchant.MerchantId, CustomerName = newDemoCustomer.CustomerName, CustomerPhoneNo = newDemoCustomer.CustomerPhoneNo }; await _dbContext.InsertDemoCustomerAsync(newDBDemoCustomer); // convert DB entity to the public entity type var customer = (DemoCustomerMBE)newDBDemoCustomer; // return the response return(CreatedAtAction(nameof(GetDemoCustomer), new { merchantGuid = merchantGuid, demoCustomerGuid = customer.CustomerGuid }, customer)); } catch (Exception ex) { return(BadRequest(new ApplicationException($"Error: [{ex.Message}] trying to add Phone No: [{newDemoCustomer.CustomerPhoneNo}] to merchant: [{merchantGuid}]"))); } }
public async Task <ActionResult <DemoCustomerMBE> > AddDemoCustomer(Guid merchantGuid, [FromBody] NewDemoCustomerMBE newDemoCustomer) { if (merchantGuid != GeneralConstants.MERCHANT_1_GUID) { return(NotFound($"Merchant with ID: {merchantGuid} not found")); } var customer = new DemoCustomerMBE { CustomerGuid = GeneralConstants.MERCHANT_1_CUSTOMER_1_GUID, CustomerName = newDemoCustomer.CustomerName, CustomerPhoneNo = newDemoCustomer.CustomerPhoneNo }; await Task.Delay(100); return(CreatedAtAction(nameof(GetDemoCustomer), new { merchantGuid = merchantGuid, customerGuid = customer.CustomerGuid }, customer)); }