public IActionResult Put(int id, [FromBody] Applicant a) { ApplicantValidator v = new ApplicantValidator(); ValidationResult result = v.Validate(a); if (result.IsValid) { var queryApplicant = _applicantContext.Applicants.Find(id); if (queryApplicant != null) { queryApplicant.Address = a.Address; queryApplicant.Age = a.Age; queryApplicant.CountryOfOrigin = a.CountryOfOrigin; queryApplicant.EMailAdress = a.EMailAdress; queryApplicant.FamilyName = a.FamilyName; queryApplicant.Hired = a.Hired; queryApplicant.Name = a.Name; _applicantContext.SaveChanges(); return(Ok()); } else { return(NotFound()); } } else { return(BadRequest()); } }
public async Task <IActionResult> Add(ApplicantViewModel applicantViewModel) { try { var validator = new ApplicantValidator(); var result = validator.Validate(applicantViewModel); if (!result.IsValid) { return(BadRequest(result.Errors)); } var addTuple = await _applicantService.Add(applicantViewModel); if (string.IsNullOrEmpty(addTuple.errorMessage)) { return(CreatedAtAction(nameof(Get), new { id = addTuple.applicantViewModel.Id }, addTuple.applicantViewModel)); } else { return(BadRequest(addTuple.errorMessage)); } } catch (Exception ex) { _logger.LogError(ex.Message); return(StatusCode((int)HttpStatusCode.InternalServerError)); } }
public bool UpdateApplicant(ApplicantObj applicant) { try { ApplicantValidator validator = new ApplicantValidator(); ValidationResult results = validator.Validate(applicant); if (!results.IsValid) { return(false); } Applicant app = _context.Applicants.Where(a => a.ID == applicant.ID).FirstOrDefault(); if (app != null) { app.Address = applicant.Address; app.Age = applicant.Age; app.CountryOfOrigin = applicant.CountryOfOrigin; app.EMailAdress = applicant.EMailAdress; app.FamilyName = applicant.FamilyName; app.Hired = applicant.Hired; app.Name = applicant.Name; _context.SaveChangesAsync(); return(true); } return(false); } catch (Exception ex) { _logger.LogError(ex.Message + " " + ex.StackTrace); return(false); } }
public void WhenFamilyNameIsEmpty_ShouldHaveError() { _fakeHttpMessageHandler.Setup(f => f.Send(It.IsAny <HttpRequestMessage>())).Returns(new HttpResponseMessage { StatusCode = HttpStatusCode.OK, Content = new StringContent(@"{ 'name': 'Germany' }") }); var model = new ApplicantModel { ID = 1, Age = 24, Hired = true, Name = "John Doe", FamilyName = "", CountryOfOrigin = "Germany", EmailAddress = "*****@*****.**", Address = "Block 10A New State Avenue", }; var sut = new ApplicantValidator(_httpClient); var isValid = sut.Validate(model).IsValid; Assert.False(isValid); }
public void WhenModelIsValid_ShouldNotHaveError() { _fakeHttpMessageHandler.Setup(f => f.Send(It.IsAny <HttpRequestMessage>())).Returns(new HttpResponseMessage { StatusCode = HttpStatusCode.OK, Content = new StringContent(@"{ 'name': 'Nigeria', 'message': 'Not Found' }") }); var model = new ApplicantModel { ID = 1, Age = 24, Hired = true, Name = "James", FamilyName = "Smith", CountryOfOrigin = "Nigeria", EmailAddress = "*****@*****.**", Address = "Block 10A New State Avenue", }; var sut = new ApplicantValidator(_httpClient); var isValid = sut.Validate(model).IsValid; Assert.True(isValid); }
public List <string> HandleErrors(ApplicantViewModel applicant) { var errors = new List <string>(); var result = _validator.Validate(applicant); if (!result.IsValid) { foreach (var error in result.Errors) { errors.Add(error.ErrorMessage); } } return(errors); }
public IActionResult Post([FromBody] Applicant a) { ApplicantValidator v = new ApplicantValidator(); ValidationResult result = v.Validate(a); if (result.IsValid) { var qu = _applicantContext.Applicants.Add(a); var user = _applicantContext.SaveChanges(); return(Created("/" + a.ID, user)); } else { return(BadRequest()); } }
public ActionResult <IEnumerable <Applicant> > Post(Applicant applicant) { ApplicantValidator validator = new ApplicantValidator(); var validateResult = validator.Validate(applicant); if (validateResult.IsValid) { dbContext.Applicants.Add(applicant); dbContext.SaveChanges(); return(CreatedAtAction(nameof(Get), new { id = applicant.ID }, applicant)); } else { string allMessages = validateResult.ToString("~"); return(BadRequest(new { Message = allMessages })); } }
public async Task <int> Update(int id, Applicant applicant) { var validator = new ApplicantValidator(); ValidationResult result = validator.Validate(applicant); if (result.IsValid) { return(await _repository.Update(id, applicant)); } var exceptionMessage = String.Join(",", result.Errors.Select(x => $"{x.ErrorMessage}").ToArray()); _logger.LogError(exceptionMessage); throw new Exception(exceptionMessage); }
public async Task <ApplicantObjResponse> AddApplicant(ApplicantObj applicant) { try { ApplicantValidator validator = new ApplicantValidator(); ValidationResult results = validator.Validate(applicant); if (!results.IsValid) { IList <ValidationFailure> failures = results.Errors; return(new ApplicantObjResponse() { ValidationResults = failures }); } var app = new Applicant { Address = applicant.Address, Age = applicant.Age, EMailAdress = applicant.EMailAdress, FamilyName = applicant.FamilyName, Hired = applicant.Hired, Name = applicant.Name, CountryOfOrigin = applicant.CountryOfOrigin }; _logger.LogInformation($"request from applicant--- {JsonConvert.SerializeObject(app)}"); var res = await _context.Applicants.AddAsync(app); await _context.SaveChangesAsync(); return(new ApplicantObjResponse() { Success = true }); } catch (Exception ex) { _logger.LogError(ex.Message + " " + ex.StackTrace); return(new ApplicantObjResponse() { Success = false }); } }
public ActionResult Put(int id, Applicant newApplicant) { ApplicantValidator validator = new ApplicantValidator(); var validateResult = validator.Validate(newApplicant); if (validateResult.IsValid) { newApplicant.ID = id; dbContext.Attach(newApplicant); dbContext.Entry(newApplicant).State = EntityState.Modified; dbContext.SaveChanges(); return(Ok()); } else { string allMessages = validateResult.ToString("~"); return(BadRequest(new { Message = allMessages })); } }
public ActionResult <ApplicantModel> CreateApplicant([FromBody] ApplicantModel applicantModel) { try { ApplicantValidator validator = new ApplicantValidator(); ValidationResult result = validator.Validate(applicantModel); return(Ok()); } catch (Exception ex) { return(BadRequest()); } //pet.Id = _petsInMemoryStore.Any() ? // _petsInMemoryStore.Max(p => p.Id) + 1 : 1; //_petsInMemoryStore.Add(pet); //return CreatedAtAction(nameof(GetById), new { id = pet.Id }, pet); }