public IActionResult UpdatePersonInWorld(NameChangeRequest nameChangeRequest) { if (nameChangeRequest == null) { return(BadRequest("No name change request was given")); } if (!CheckIfOwnerName(nameChangeRequest.OldName)) { if (!_peopleRepository.CheckIfNameExistsInRepository(nameChangeRequest.OldName)) { return(BadRequest($"{nameChangeRequest.OldName} does not exist in the world, and was not updated")); } if (_peopleRepository.CheckIfNameExistsInRepository(nameChangeRequest.NewName)) { return(BadRequest($"{nameChangeRequest.NewName} already exists in the world")); } _peopleRepository.UpdatePersonInRepository(nameChangeRequest); return(new OkObjectResult( $"{nameChangeRequest.OldName} has been changed to {nameChangeRequest.NewName} in the world")); } return(BadRequest("Can't change the owner's name")); }
public async Task <IActionResult> NameChangeRequest(NameChangeRequestViewModel model) { if (model.fileBase != null && model.fileBase.Length <= 25000000) { var allowedExtensions = new[] { ".pdf", ".png", ".docx", ".xlsx", ".jpg" }; if (!allowedExtensions.Contains(Path.GetExtension(model.fileBase.FileName).ToLower())) { TempData["AlertWarningMsg"] = "Attachment not added to travel. Invalid File Format. File must be .pdf, .png, .docx, .xlsx, .jpg"; } else { byte[] bytes; model.Attachement = new NameChangeAttachements(); using (var memoryStream = new MemoryStream()) { await model.fileBase.CopyToAsync(memoryStream); bytes = memoryStream.ToArray(); } model.Attachement.FileName = Path.GetFileName(model.fileBase.FileName); model.Attachement.FileType = model.fileBase.ContentType; model.Attachement.File = bytes; NameChangeRequest nameChange = new NameChangeRequest { oldFirstName = model.oldFirstName, oldMiddleName = model.oldMiddleName, oldLastName = model.oldLastName, newFirstName = model.newFirstName, newMiddleName = model.newMiddleName, newLastName = model.newLastName, UserId = _userManager.GetUserId(User) }; await _context.NameChangeRequests.AddAsync(nameChange); await _context.SaveChangesAsync(); NameChangeAttachements attachement = new NameChangeAttachements { NameChangeId = nameChange.Id, FileName = model.Attachement.FileName, FileType = model.Attachement.FileType, File = model.Attachement.File, Status = true }; await _context.NameChangeAttachements.AddAsync(attachement); await _context.SaveChangesAsync(); } StatusMessage = "Your request has been submitted for review."; return(RedirectToAction(nameof(NameChangeRequest))); } else { return(View()); } }
public void UpdatePersonName_DoesNotCall_UpdatePersonInRepository_WhenOldNameIsOwner() { var testRequest = new NameChangeRequest("Anton", "John"); var mockRepo = new Mock <IPeopleRepository>(); mockRepo.Setup(repo => repo.UpdatePersonInRepository(testRequest)); var controller = new PeopleController(mockRepo.Object, new Mock <IMessageBuilder>().Object); controller.UpdatePersonInWorld(testRequest); mockRepo.Verify(repo => repo.UpdatePersonInRepository(testRequest), Times.Never); }
public void UpdatePersonName_DoesNotCall_UpdatePersonInRepository_WhenOldNameDoesNotExistInRepo() { var testRequest = new NameChangeRequest("Greg", "John"); var mockRepo = new Mock <IPeopleRepository>(); mockRepo.Setup(repo => repo.CheckIfNameExistsInRepository(testRequest.OldName)).Returns(false); mockRepo.Setup(repo => repo.CheckIfNameExistsInRepository(testRequest.NewName)).Returns(false); mockRepo.Setup(repo => repo.UpdatePersonInRepository(testRequest)); var controller = new PeopleController(mockRepo.Object, new Mock <IMessageBuilder>().Object); controller.UpdatePersonInWorld(testRequest); mockRepo.Verify(repo => repo.UpdatePersonInRepository(testRequest), Times.Never); }
public void UpdatePersonName_Returns400_WhenOldNameIsOwner() { var testRequest = new NameChangeRequest("Anton", "John"); var mockRepo = new Mock <IPeopleRepository>(); mockRepo.Setup(repo => repo.UpdatePersonInRepository(testRequest)); var controller = new PeopleController(mockRepo.Object, new Mock <IMessageBuilder>().Object); controller.UpdatePersonInWorld(testRequest); IActionResult result = controller.UpdatePersonInWorld(testRequest); var badRequest = result as BadRequestObjectResult; Assert.Equal(400, badRequest.StatusCode); }
public void UpdatePersonName_Returns400_WhenOldNameDoesNotExistInRepo() { var testRequest = new NameChangeRequest("Greg", "John"); var mockRepo = new Mock <IPeopleRepository>(); mockRepo.Setup(repo => repo.CheckIfNameExistsInRepository(testRequest.OldName)).Returns(false); mockRepo.Setup(repo => repo.CheckIfNameExistsInRepository(testRequest.NewName)).Returns(false); mockRepo.Setup(repo => repo.UpdatePersonInRepository(testRequest)); var controller = new PeopleController(mockRepo.Object, new Mock <IMessageBuilder>().Object); IActionResult result = controller.UpdatePersonInWorld(testRequest); var badRequest = result as BadRequestObjectResult; Assert.Equal(400, badRequest.StatusCode); }
public Request(NameChangeRequest nameChangeRequest) { NameChangeRequest = nameChangeRequest; }
public void UpdatePersonInRepository(NameChangeRequest request) { _context.People.FirstOrDefault(person => person.Name == request.OldName).Name = request.NewName; _context.SaveChangesAsync(); }