public void FailsValidationWithInvalidInput() { ITestService proxy = host.Proxy; AddCustomerRequest request = new AddCustomerRequest(); request.FirstName = "John"; request.LastName = "Doe"; request.SSN = "This is not a valid SSN"; proxy.AddCustomer(request); }
public async Task <ActionResult <CustomerResponse> > AddCustomer(AddCustomerRequest customer) { try { var result = await _customerService.AddAsync(customer); return(Ok(result)); } catch (Exception ex) { _logger.LogError(ex, "Error in CustomersController->AddCustomer()"); return(StatusCode(StatusCodes.Status500InternalServerError, "There is some internal issue. Please try again later.")); } }
public ResponseBase AddCustomer(AddCustomerRequest request) { if (ModelState.IsValid) { return(RequestManager.Execute(request)); } else { AddCustomerResponse responseObject = new AddCustomerResponse(); responseObject.Message = "Validation Failed"; responseObject.Errors = ModelState.Keys .SelectMany(key => ModelState[key].Errors.Select(x => new ValidationError(key, x.ErrorMessage))) .ToList(); return(responseObject); } }
public IActionResult AddCustomer(AddCustomerRequest request) { try { return(Ok(_customerService.AddCustomer(request))); } catch (ArgumentException exception) { _logger.LogWarning($"Argument Exception in AddCustomer: {exception.Message}"); return(BadRequest(exception.Message)); } catch (Exception ex) { _logger.LogError($"Error in DeleteCustomer: {ex.Message}"); return(StatusCode(500, ex)); } }
public async Task GetAllCustomers_ReturnCustomers_WhenCustomersExistsInDatabase() { // Arrange var firstRequest = new AddCustomerRequest { Name = "R N Jha", ContactNumber = "0989898098", Email = "*****@*****.**", Address = "Test Address" }; var secondRequest = new AddCustomerRequest { Name = "M N Jha", ContactNumber = "0989898099", Email = "*****@*****.**", Address = "Test Address2" }; var firstAddedCustomer = await AddCustomerAsync(firstRequest); var secondAddedCustomer = await AddCustomerAsync(secondRequest); // Act var responses = await TestClient.GetAsync("/api/v1/customers"); // Assert responses.StatusCode.Should().Be(HttpStatusCode.OK); var customers = await responses.Content.ReadAsAsync <List <CustomerResponse> >(); customers.Count.Should().BeGreaterThan(1); var firstCustomer = customers.SingleOrDefault(c => c.Id == firstAddedCustomer.Id); firstCustomer.Should().NotBeNull(); firstCustomer.Name.Should().Be(firstRequest.Name); firstCustomer.Email.Should().Be(firstRequest.Email); firstCustomer.ContactNumber.Should().Be(firstRequest.ContactNumber); firstCustomer.Address.Should().Be(firstRequest.Address); var secondCustomer = customers.SingleOrDefault(c => c.Id == secondAddedCustomer.Id); secondCustomer.Should().NotBeNull(); secondCustomer.Name.Should().Be(secondRequest.Name); secondCustomer.Email.Should().Be(secondRequest.Email); secondCustomer.ContactNumber.Should().Be(secondRequest.ContactNumber); secondCustomer.Address.Should().Be(secondRequest.Address); }
public void ShouldGiveCorrectFaultDetails() { AddCustomerRequest request = new AddCustomerRequest("First", "Last", "Invalid SSN"); try { host.Proxy.AddCustomer(request); Assert.Fail("If you got here, the validation exception didn't happen"); } catch (FaultException <ValidationFault> e) { ValidationFault yourFault = e.Detail; Assert.IsFalse(yourFault.IsValid); Assert.AreEqual(1, yourFault.Details.Count); Assert.AreEqual("SSN", yourFault.Details[0].Key); Assert.AreEqual("request", yourFault.Details[0].Tag); } }
public string AddCustomer(AddCustomerRequest request) { try { using (var connection = _connectionProvider.GetDbConnection()) { connection.Open(); connection.Execute( @"INSERT INTO Customer ( name, surname, phone_number ) VALUES ( @Name, @Surname, @PhoneNumber );", new { Name = request.Name, Surname = request.Surname, PhoneNumber = request.PhoneNumber }); } return(null); } catch (Exception e) { return(e.Message); } }
public ValidationResult ValidateRequest(AddCustomerRequest request) { var result = new ValidationResult(true); if (MissingRequiredFields(request, ref result)) { return(result); } if (ValidateEmail(request, ref result)) { return(result); } if (CustomerAlreadyInDb(request, ref result)) { return(result); } return(result); }
public AddCustomerResponse AddCustomer([FromBody] AddCustomerRequest request) { try { if (request.Name == null || request.Surname == null || request.PhoneNumber == null) { throw new ArgumentException("Name, Surname or Phone_number can't be empty."); } return(new AddCustomerResponse { Error = _customerService.AddCustomer(request) }); } catch (Exception e) { return(new AddCustomerResponse { Error = e.Message }); } }
public async Task <ActionResult> Post([FromBody] AddCustomerRequest request) { /* * Assumption: Errors that I return from here are not really in a proper structure, in order to handle more complex case, I will need to define a proper json for Error return * For example: * { * "errors": [ * { * "code": "001", * "message": "some error here" * }, * { * "code": "002", * "message": "some error here" * } * ] * } */ var validator = new AddCustomerRequestValidator(); var validationResult = validator.Validate(request); if (!validationResult.IsValid) { return(BadRequest(new { ErrorMessage = validationResult.ErrorMessage })); } var customerEntity = _mapper.Map <CustomerEntity>(request); var result = await _customerService.AddCustomerAsync(customerEntity); if (!result.IsSuccessful) { return(BadRequest(new { ErrorMessage = result.ErrorMessage })); } var customer = _mapper.Map <Customer>(result.Result); return(Created($"{Request.Path}/{result.Result.Id}", customer)); }
private bool MissingRequiredFields(AddCustomerRequest request, ref ValidationResult result) { var errors = new List <string>(); if (string.IsNullOrEmpty(request.FirstName)) { errors.Add("Customer First Name must be populated"); } if (string.IsNullOrEmpty(request.LastName)) { errors.Add("Customer Last Name must be populated"); } if (errors.Any()) { result.PassedValidation = false; result.Errors.AddRange(errors); return(true); } return(false); }
private bool ValidateEmail(AddCustomerRequest request, ref ValidationResult result) { var errors = new List <string>(); if (string.IsNullOrEmpty(request.Email)) { errors.Add("Email must be populated"); } if (!RegexValidation.IsEmailValid(request.Email)) { errors.Add("Email must be a valid email address"); } if (errors.Any()) { result.PassedValidation = false; result.Errors.AddRange(errors); return(true); } return(false); }
public async Task AddCustomer_ShouldCreateCustomerInDatabase_AndReturnCustomerDetails() { // Arrange var request = new AddCustomerRequest { Name = "L N Jha", ContactNumber = "0989898098", Email = "*****@*****.**", Address = "Test Address" }; // Act var responses = await TestClient.PostAsJsonAsync("/api/v1/customers", request); // Assert responses.StatusCode.Should().Be(HttpStatusCode.OK); var returnedCustomer = await responses.Content.ReadAsAsync <CustomerResponse>(); returnedCustomer.Name.Should().Be(request.Name); returnedCustomer.Email.Should().Be(request.Email); returnedCustomer.ContactNumber.Should().Be(request.ContactNumber); returnedCustomer.Address.Should().Be(request.Address); }
public Response <Customer> UpdateCustomer(AddCustomerRequest request) { Response <Customer> response = new Response <Customer>(); if (request == null || request.Customer == null) { ArgumentNullException ex = new ArgumentNullException("OperateCustomer request"); LogError(ex); response.ErrorCode = ErrorCode.Argument; response.Exception = ex; return(response); } try { WMS_CustomerAccessor WMS_CustomerAccessor = new WMS_CustomerAccessor(); response.Result = WMS_CustomerAccessor.UpdateCustomers(request.Customer); if (response.Result.ID > 0) { response.IsSuccess = true; } else { response.IsSuccess = false; response.ErrorCode = ErrorCode.Technical; } } catch (Exception ex) { LogError(ex); response.IsSuccess = false; response.Exception = ex; response.ErrorCode = ErrorCode.Technical; throw; } return(response); }
public async Task <ApiResponse> DoesCustomerExist(AddCustomerRequest request) { ApiResponse taskResponse = await PostRequest <AddCustomerRequest, ApiResponse>("DoesCustomerExist", request); return(taskResponse); }
public async Task <ActionResult <CustomerDto> > AddCustomer([FromBody] AddCustomerRequest request) { var result = await _mediator.Send(new AddCustomerCommand(request.Name)); return(Ok(result)); }
public Customer Customer(AddCustomerRequest request) { return(new Customer(request.Username, request.FirstName, request.LastName, request.Email)); }
public long AddCustomer(AddCustomerRequest request) { return(persistence.AddCustomer(request)); }
public long DoesCustomerExist(AddCustomerRequest request) { return(persistence.DoesCustomerExist(request)); }
private void SaveCustomer(AddCustomerRequest request, ApiResponse response) { try { if (response.Success && response.Id != 0) { //duplicate customer Device.BeginInvokeOnMainThread(() => { DisplayAlert("Error", "This user is already in the database.", "Ok"); }); } else { HttpClient client = new HttpClient(); client.BaseAddress = new Uri(((App)App.Current).LAN_Address); client.DefaultRequestHeaders.Accept.Add( new MediaTypeWithQualityHeaderValue("application/json")); client.DefaultRequestHeaders.Add("EO-Header", ((App)(App.Current)).User + " : " + ((App)(App.Current)).Pwd); string jsonData = JsonConvert.SerializeObject(request); var content = new StringContent(jsonData, Encoding.UTF8, "application/json"); HttpResponseMessage httpResponse = client.PostAsync("api/Login/AddCustomer", content).Result; if (httpResponse.IsSuccessStatusCode) { Stream streamData = httpResponse.Content.ReadAsStreamAsync().Result; StreamReader strReader = new StreamReader(streamData); string strData = strReader.ReadToEnd(); //strReader.Close(); ApiResponse apiResponse = JsonConvert.DeserializeObject <ApiResponse>(strData); if (apiResponse.Messages.Count > 0) { StringBuilder sb = new StringBuilder(); foreach (KeyValuePair <string, List <string> > messages in apiResponse.Messages) { foreach (string msg in messages.Value) { sb.AppendLine(msg); } } } else { if (Initiator != null && Initiator as WorkOrderPage != null) { Device.BeginInvokeOnMainThread(() => { request.Customer.Person.person_id = apiResponse.Id; MessagingCenter.Send <PersonAndAddressDTO>(request.Customer, "SearchCustomer"); Navigation.PopAsync(); }); } else { Device.BeginInvokeOnMainThread(() => { ClearForm(); DisplayAlert("Success", "Customer saved!", "Ok"); }); } } } else { Device.BeginInvokeOnMainThread(() => { DisplayAlert("Error", "Error adding Work Order", "Ok"); }); } } } catch (Exception ex) { Exception ex2 = new Exception("AddCustomer", ex); ((App)App.Current).LogError(ex2.Message, JsonConvert.SerializeObject(request)); } }
protected async Task <CustomerResponse> AddCustomerAsync(AddCustomerRequest request) { var response = await TestClient.PostAsJsonAsync("/api/v1/customers", request); return(await response.Content.ReadAsAsync <CustomerResponse>()); }
public async Task <IActionResult> RegisterCustomer([FromBody] AddCustomerRequest request) { return(await HandleNoContent(request)); }
public Task <IActionResult> AddCustomer([FromBody] AddCustomerRequest request) { return(this.HandleRequest <AddCustomerRequest, AddCustomerResponse>(request)); }