public async Task <ActionResult> Put(string name, [FromBody] SimpleTaskDto value) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } bool isTaskInProgress = true; do { isTaskInProgress = _tasksInProgress.Any(x => x == value.Name); if (isTaskInProgress) { Thread.Sleep(1000); } else { _tasksInProgress.Add(value.Name); } } while (!isTaskInProgress); var task = _iMapper.Map <SimpleTaskDto, SimpleTask>(value); SimpleTask existingTask = null; existingTask = _simpleTaskRepository.Get(name); if (existingTask == null) { return(NotFound($"Task with name {name} does not exist.")); } // check for uniqness of name bool isValid = await Validate(existingTask); if (!isValid) { _tasksInProgress.Remove(value.Name); return(BadRequest($"Task name {name} already exists in database.")); } // Actually, this should not be used at all - PUT is just to take whole object and "put" it somewhere else. That's the theory at least :) // No fields should be changed, including last update time and number of updates. // For updating task please use "PATCH". existingTask.LastUpdateDateTime = DateTime.Now; existingTask.LastUpdateBy = "Mikk"; existingTask.NumberOfUpdates++; existingTask.Update(task); _simpleTaskRepository.Update(task); IncrementNumberOfTotalOperations(); _tasksInProgress.Remove(value.Name); return(Ok(existingTask)); }
public async Task <ActionResult> Patch(string name, [FromBody] SimpleTaskDto value) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } bool isTaskInProgress = true; do { isTaskInProgress = _tasksInProgress.Any(x => x == value.Name); if (isTaskInProgress) { Thread.Sleep(1000); } else { _tasksInProgress.Add(value.Name); } } while (!isTaskInProgress); var task = _iMapper.Map <SimpleTaskDto, SimpleTask>(value); SimpleTask existingTask = null; existingTask = _simpleTaskRepository.Get(name); if (existingTask == null) { _tasksInProgress.Remove(value.Name); return(NotFound($"Task with name {name} does not exist.")); } bool isValid = await Validate(existingTask); if (!isValid) { _tasksInProgress.Remove(value.Name); return(BadRequest($"Task with name {name} already exists in database.")); } existingTask.LastUpdateDateTime = DateTime.Now; existingTask.LastUpdateBy = "Mikk"; existingTask.NumberOfUpdates++; existingTask.Patch(task); _simpleTaskRepository.Update(task); IncrementNumberOfTotalOperations(); _tasksInProgress.Remove(value.Name); return(Ok(existingTask)); }