public async Task <IActionResult> Put(int id, [FromBody] IssueUpdateDto item, CancellationToken ct) { try { // return non content response if item is null if (item == null) { return(NoContent()); } // retrieve the issue that is going to be updated var original = await _issueRepository.GetBugSingle(id, ct); // return not found response if existing item cannot be found if (original == null) { return(NotFound()); } var originalStatus = original.Status; // map the update dto to model Issue toUpdate = Mapper.Map(item, original); // add closed datetime is issue transitions from open to closed if (originalStatus == IssueStatus.Open && item.Status == IssueStatus.Closed) { toUpdate.DateTimeClosed = DateTime.UtcNow; } // remove closed datetime is issue transitions from open to closed if (originalStatus == IssueStatus.Closed && item.Status == IssueStatus.Open) { toUpdate.DateTimeClosed = null; } // update the issue await _issueRepository.UpdateBug(toUpdate, ct); // map to dto var result = Mapper.Map <IssueDto>(toUpdate); // return successful response return(Ok(new SuccessResult { Results = new[] { result }, Status = "Successful" })); } catch (ArgumentException ex) { return(BadRequest(ex.Message)); } }
public ActionResult UpdateIssue(int id, IssueUpdateDto issueUpdateDto) { var issueModelFromRepo = _repository.GetIssueById(id); if (issueModelFromRepo == null) { return(NotFound()); } _mapper.Map(issueUpdateDto, issueModelFromRepo); _repository.UpdateIssue(issueModelFromRepo); _repository.SaveChanges(); return(NoContent()); }
public async Task <IActionResult> Update([FromRoute] string id, [FromBody] IssueUpdateDto dto) { var issue = await _repo.GetIssue(id); if (dto.Labels != null) { issue.IssueLabels = new List <IssueLabel>(); foreach (var labelId in dto.Labels) { var label = await _repo.GetLabel(labelId); issue.IssueLabels.Add(new IssueLabel { IssueId = id, LabelId = labelId, Label = label }); } } var phase = await _repo.GetPhase(dto.PhaseId); if (phase == null) { return(BadRequest()); } issue.Name = dto.Name; issue.Description = dto.Description; issue.IssueType = dto.IssueType; issue.DueDate = dto.DueDate; issue.PhaseId = dto.PhaseId; if (await _repo.SaveAll()) { return(Ok()); } return(BadRequest(new { message = "Error updating the issue." })); }