public IActionResult Put(int id, [FromBody] ContactDto dto, [FromServices] UpdateContactValidator validator) { dto.Id = id; var contact = _context.Contact.Find(id); if (contact == null) { return(NotFound()); } var result = validator.Validate(dto); if (!result.IsValid) { throw new Exception();// prepraviti sa klasom error/ medelja 5-subota termin } _mapper.Map(dto, contact); try { _context.SaveChanges(); return(NoContent()); } catch (Exception e) { return(StatusCode(StatusCodes.Status500InternalServerError)); } }
public ContactController(IContactService contactService, ContactValidator contactValidator, UpdateContactValidator updateContactValidator, DeleteContactValidator deleteContactValidator) { _contactService = contactService; _contactValidator = contactValidator; _updateContactValidator = updateContactValidator; _deleteContactValidator = deleteContactValidator; }
public void Throws_ValidationException_Id() { // Arrange var sut = new UpdateContactValidator(); var cmd = new UpdateContactCommand { Id = default }; // Act var result = sut.Validate(cmd); // Assert Assert.False(result.IsValid); Assert.Contains(result.Errors, e => e.PropertyName == nameof(cmd.Id)); }
public void Throws_ValidationException_DateOfBirth_Default() { // Arrange var sut = new UpdateContactValidator(); var cmd = new UpdateContactCommand { Id = int.MaxValue, Name = "A", Address = "A" }; // Act var result = sut.Validate(cmd); // Assert Assert.False(result.IsValid); Assert.Contains(result.Errors, e => e.PropertyName == nameof(cmd.DateOfBirth)); }
public void Throws_ValidationException_Address_Empty() { // Arrange var sut = new UpdateContactValidator(); var cmd = new UpdateContactCommand { Id = int.MaxValue, Name = "A", Address = string.Empty, DateOfBirth = DateTime.Now }; // Act var result = sut.Validate(cmd); // Assert Assert.False(result.IsValid); Assert.Contains(result.Errors, e => e.PropertyName == nameof(cmd.Address)); }
public void Throws_ValidationException_Name_Too_Long() { // Arrange var sut = new UpdateContactValidator(); var cmd = new UpdateContactCommand { Id = int.MaxValue, Name = new String('0', Constants.MaxLengths.ContactName + 1), Address = "A", DateOfBirth = DateTime.Now }; // Act var result = sut.Validate(cmd); // Assert Assert.False(result.IsValid); Assert.Contains(result.Errors, e => e.PropertyName == nameof(cmd.Name)); }
public void Validation_OK() { // Arrange var sut = new UpdateContactValidator(); var cmd = new UpdateContactCommand { Id = int.MaxValue, Name = "A", Address = "A", DateOfBirth = DateTime.Now.AddDays(-1) }; // Act var result = sut.Validate(cmd); // Assert Assert.True(result.IsValid); Assert.Empty(result.Errors); }
public EfUpdateContactCommand(DentaCareContext context, UpdateContactValidator validator) { this._context = context; this._validator = validator; }