public async Task <LabelManageModel> UpdateAsync(string user, LabelManageModel model) { if (model == null) { return(null); } var labelModel = await _repository.GetByIdAsync(model.Id, true); if (labelModel == null) { throw new KeyNotFoundException(model.Id.ToString()); } if (!labelModel.Group.GroupUser.Any(gu => gu.UserId.Equals(user))) { throw new ForbidException(); } var objToUpdate = _mapper.Map <LabelModel>(model); objToUpdate.GroupId = labelModel.GroupId; _unitOfWork.BeginTransaction(); var updatedModel = await _repository.UpdateAsync(objToUpdate); var result = await _unitOfWork.CommitAsync(); return(result > 0 ? _mapper.Map <LabelManageModel>(updatedModel) : null); }
public async Task Label_PutWithInvalidLabel2_ShouldForbid() { var model = new LabelManageModel { Id = DefaultLabelOtherGroup, Name = "New name" }; var results = await _controller.Put(model); results.Should().BeOfType <ForbidResult>(); }
public async Task Label_PutWithInvalidLabel_ShouldNotFound() { var model = new LabelManageModel { Id = DefaultInvalidLabel, Name = "New name" }; var results = await _controller.Put(model); results.Should().BeOfType <NotFoundResult>(); }
public async Task Label_PutWithInvalidUser_ShouldForbid() { MockUser(_controller, DefaultInvalidUser); var model = new LabelManageModel { Id = DefaultLabel, Name = "New name" }; var results = await _controller.Put(model); results.Should().BeOfType <ForbidResult>(); }
public async Task Label_Put_ShouldReturnData() { var model = new LabelManageModel { Id = DefaultLabel, Name = "New name" }; var results = await _controller.Put(model); results .Should().BeOfType <OkObjectResult>() .Which.Value.Should().BeAssignableTo <LabelManageModel>() .Which.Should().NotBeNull(); }
public async Task <IActionResult> Put([FromBody] LabelManageModel value) { var userId = _validateHelper.GetUserId(HttpContext); try { var model = await _labelService.UpdateAsync(userId, value); if (model == null) { return(BadRequest()); } return(Ok(model)); } catch (KeyNotFoundException) { return(NotFound()); } catch (ForbidException) { return(Forbid()); } }