/// <summary> /// Updates label for a todo item. /// </summary> /// <param name="userId">User id</param> /// <param name="updateObj">label todo item to be updated</param> /// <returns>Updated label</returns> public Label UpdateLabel(int userId, UpdateLabelDto updateObj) { var existingItem = dbContext.TodoItems .Include(l => l.Labels) .FirstOrDefault(u => u.Id == userId && u.Id == updateObj.ParentId); if (existingItem == null) { return(null); } var existingLabel = existingItem.Labels?.FirstOrDefault(l => l.Name == updateObj.CurrentValue); if (existingLabel == null) { return(null); } existingLabel.Name = updateObj.NewValue; existingLabel.LastModified = DateTime.Now; dbContext.SaveChanges(); return(existingLabel); }
public void UpdateLabel_ShouldMapResultToDto() { // Arrange var input = new UpdateLabelDto(); // Act logic.UpdateLabel(1, input); // Assert mapper.Verify(t => t.Map <LabelDto>(It.IsAny <Label>())); }
public void UpdateLabel_ShouldCallUpdateLabel() { // Arrange var input = new UpdateLabelDto(); // Act logic.UpdateLabel(1, input); // Assert todoItemRepository.Verify(t => t.UpdateLabel(1, input)); }
public void UpdateLabel_ShouldCallUpdateLabel() { // Arrange var input = new UpdateLabelDto(); // Act controller.UpdateLabel(input); // Assert todoItemLogic.Verify(u => u.UpdateLabel(1, It.Is <UpdateLabelDto>(c => c == input))); }
public Label UpdateLabel(Guid id, UpdateLabelDto labelDto) { var label = _unitOfWork.Repository <Label>().Find(id); label.Name = labelDto.Name; label.Color = labelDto.Color; _unitOfWork.Repository <Label>().Update(label); _unitOfWork.Complete(); return(label); }
public void UpdateLabel_ShouldReturnBadRequestWhenUpdateLabelReturnsNull() { // Arrange var input = new UpdateLabelDto(); // Act var result = controller.UpdateLabel(input); // Assert Assert.IsType <BadRequestObjectResult>(result); var response = (result as BadRequestObjectResult).Value as ErrorResponse; Assert.Equal("Item or label not found in the database.", response.Message); }
public void UpdateLabel_ShouldReturnNullIfItemIsNotFound() { // Arrange var dbContext = SetupDatabase(nameof(UpdateLabel_ShouldReturnNullIfItemIsNotFound)); var repository = new TodoItemRepository(dbContext); var input = new UpdateLabelDto { ParentId = 30 }; // Act var result = repository.UpdateLabel(3, input); // Assert Assert.Null(result); }
public void UpdateLabel_ShouldReturnUpdatedLabel() { // Arrange var input = new UpdateLabelDto(); var model = new LabelDto(); todoItemLogic.Setup(u => u.UpdateLabel(1, It.Is <UpdateLabelDto>(c => c == input))).Returns(model); // Act var result = controller.UpdateLabel(input); // Assert Assert.IsType <OkObjectResult>(result); var response = (result as OkObjectResult).Value as Response <LabelDto>; Assert.Equal(model, response.Model); }
/// <summary> /// 修改商品标签 /// </summary> /// <param name="dto"></param> /// <returns></returns> public bool Update(UpdateLabelDto dto) { using (var commodityDbContext = new CommodityDbContext()) { //若重名,抛出异常 if (commodityDbContext.LabelRepos.Any(o => o.Name == dto.Name)) { throw new Exception("标签名重复"); } var entity = commodityDbContext.LabelRepos.Where(o => o.Id == dto.Id).FirstOrDefault(); if (entity == null) { throw new Exception("未选择标签"); } entity.Name = dto.Name; return(commodityDbContext.SaveChanges() > 0); } }
public void UpdateLabel_ShouldReturnNullIfLabelIsNotFound() { // Arrange var dbContext = SetupDatabase(nameof(UpdateLabel_ShouldReturnNullIfLabelIsNotFound)); var repository = new TodoItemRepository(dbContext); var input = new UpdateLabelDto { ParentId = 1, CurrentValue = "test1", NewValue = "test2" }; // Act var result = repository.UpdateLabel(1, input); // Assert Assert.Null(result); }
public void UpdateLabel_ShouldUpdateLabel() { // Arrange var dbContext = SetupDatabase(nameof(UpdateLabel_ShouldUpdateLabel)); var repository = new TodoItemRepository(dbContext); var input = new UpdateLabelDto { ParentId = 1, CurrentValue = "Today", NewValue = "tomorrow" }; // Act var result = repository.UpdateLabel(1, input); // Assert Assert.NotNull(result); var item = dbContext.Labels.FirstOrDefault(t => t.Name == "tomorrow"); Assert.NotNull(item); }
public IActionResult UpdateLabel(UpdateLabelDto updateDto) { int userId = int.Parse(httpContextAccessor.HttpContext.User.FindFirst(Constants.UserIdClaim)?.Value); var updatedResult = todoItemLogic.UpdateLabel(userId, updateDto); if (updatedResult == null) { return(BadRequest(new ErrorResponse { Status = false, Message = "Item or label not found in the database." })); } else { return(Ok(new Response <LabelDto> { Status = true, Model = updatedResult })); } }
public Label UpdateItemLabel(UpdateLabelDto updateLabelDto) { var userId = CheckAuthentication(); return(todoItemRepository.UpdateLabel(userId, updateLabelDto)); }
public BaseResponse <LabelDto> UpdateLabel([FromBody] UpdateLabelDto labelDto, Guid id) { var res = _labelService.UpdateLabel(id, labelDto); return(new SuccessResponse <LabelDto>(_mapper.Map <LabelDto>(res))); }
/// <summary> /// Updates label for a todo item. /// </summary> /// <param name="userId">User id</param> /// <param name="updateObj">label to be updated</param> /// <returns>Updated label dto</returns> public LabelDto UpdateLabel(int userId, UpdateLabelDto updateObj) { var dbLabel = todoItemRepository.UpdateLabel(userId, updateObj); return(mapper.Map <LabelDto>(dbLabel)); }