public void Update_ShouldFailWithNull_WhenWrongValues() { // Arrange int id = _generalDataPoints[0].Id; int userId = _generalDataPoints[0].User.Id; int sectionId = _generalDataPoints[0].Section.Id; var newStringValue = "NewUpdate"; var newGeneralDataPointRequest = new GeneralDataPointUpdateRequest { Text = newStringValue, }; // Act & Assert Assert.Throws <ArgumentNullException>(() => _service.Update( id, userId, sectionId, null)); Assert.Throws <ArgumentNullException>(() => _service.Update( 999, userId, sectionId, newGeneralDataPointRequest)); Assert.Throws <ArgumentNullException>(() => _service.Update( id, 999, sectionId, newGeneralDataPointRequest)); Assert.Throws <ArgumentNullException>(() => _service.Update( id, userId, 999, newGeneralDataPointRequest)); _repository.Verify(mock => mock.Update(It.IsAny <GeneralDataPoint>()), Times.Never); }
public void Update_ShouldFailWithConflict_WhenConflictValues() { // Arrange int id = _generalDataPoints[0].Id; int userId = _generalDataPoints[0].User.Id; int sectionId = _generalDataPoints[0].Section.Id; var newGeneralDataPointRequest = new GeneralDataPointUpdateRequest { Text = _generalDataPoints[6].Text, }; // Act & Assert Assert.Throws <ConflictException>(() => _service.Update( id, userId, sectionId, newGeneralDataPointRequest)); _repository.Verify(mock => mock.Update(It.IsAny <GeneralDataPoint>()), Times.Never); }
public ActionResult Update(int userId, int sectionId, int id, [FromBody] GeneralDataPointUpdateRequest generalDataPointRequest) { if (!generalDataPointRequest.Validate()) { return(BadRequest()); } try { _service.Update(id, userId, sectionId, generalDataPointRequest); } catch (ArgumentNullException) { return(NotFound()); } catch (ConflictException) { return(Conflict()); } return(NoContent()); }
public void Update_ShouldUpdateuserGeneralDataPoint() { // Arrange int id = _generalDataPoints[0].Id; int userId = _generalDataPoints[0].User.Id; int sectionId = _generalDataPoints[0].Section.Id; var newStringValue = "NewUpdate"; var newGeneralDataPointRequest = new GeneralDataPointUpdateRequest { Text = newStringValue, }; // Act _service.Update( id, userId, sectionId, newGeneralDataPointRequest); // Assert _repository.Verify(mock => mock.Update(It.IsAny <GeneralDataPoint>()), Times.Once); var v = _generalDataPoints.SingleOrDefault(v => v.Id == id); Assert.Equal(newStringValue, v.Text); }
public void Update( int id, int userId, int sectionId, GeneralDataPointUpdateRequest generalDataPoint) { if (generalDataPoint == null) { throw new ArgumentNullException(nameof(generalDataPoint)); } var foundGeneralDataPoint = _repository.GetById(id); if (foundGeneralDataPoint == null) { throw new ArgumentNullException(nameof(foundGeneralDataPoint)); } var foundUser = _userRepo.GetById(userId); if (foundUser == null) { throw new ArgumentNullException(nameof(foundUser)); } var foundSection = _generalDataSectionRepo.GetById(sectionId); if (foundSection == null) { throw new ArgumentNullException(nameof(foundSection)); } if (generalDataPoint.Text != null) { var uniqueConstraintViolationCheck = _repository.GetByUniqueKey( foundGeneralDataPoint.User.Id, foundGeneralDataPoint.Section.Id, generalDataPoint.Text); if (uniqueConstraintViolationCheck != null && uniqueConstraintViolationCheck.Id != id) { throw new ConflictException(uniqueConstraintViolationCheck.Id.ToString()); } foundGeneralDataPoint.Text = generalDataPoint.Text; } if (generalDataPoint.OrderNum != null && generalDataPoint.OrderNum != foundGeneralDataPoint.OrderNum) { var orderNum = generalDataPoint.OrderNum.GetValueOrDefault(); foundGeneralDataPoint.OrderNum = orderNum; var num = 1; foreach (var p in _repository.GetAllByUserAndSectionId(userId, sectionId)) { if (p.Id == id) { continue; } if (num == generalDataPoint.OrderNum) { num = num + 1; p.OrderNum = num; _repository.Update(p); num = num + 1; continue; } p.OrderNum = num; _repository.Update(p); num = num + 1; } } _repository.Update(foundGeneralDataPoint); }