public async Task <int> UpdateAsync(WorkForUpdateDto workForUpdate) { var entity = await FindByCondition(x => x.Id == workForUpdate.Id, true) .Include(i => i.NextWorks) .Include(i => i.PrevWorks) .FirstOrDefaultAsync(); if (entity.FactStartDate.HasValue && workForUpdate.FactStartDate.HasValue && entity.FactStartDate.Value != workForUpdate.FactStartDate) { return(0); } var parentWorks = entity.PrevWorks; if (parentWorks != null && parentWorks.Any()) { var possibleDateList = entity.PrevWorks.Select(s => s.PlannedStartDate.AddDays(s.NormDuration)).ToList(); var maxPossibleDate = possibleDateList.Min(i => i); if (workForUpdate.NewPlannedStartDate.HasValue && workForUpdate.NewPlannedStartDate < maxPossibleDate) { return(0); } } entity.NewPlannedStartDate = workForUpdate.FactStartDate ?? workForUpdate.NewPlannedStartDate; if (workForUpdate.FactStartDate.HasValue) { entity.FactStartDate = workForUpdate.FactStartDate; } await SaveChanges(); if (workForUpdate.NewPlannedStartDate != null) { foreach (var p in entity.NextWorks) { await GetNext(p.Id, workForUpdate.NewPlannedStartDate.Value.AddDays(entity.NormDuration)); } } return(1); }
public async Task <IActionResult> Update([FromBody] WorkForUpdateDto workForUpdate) { try { var result = await _repository.WorkRepository.UpdateAsync(workForUpdate); if (result == 0) { return(BadRequest()); } await _repository.WorkRepository.Recalculate(); return(NoContent()); } catch (Exception e) { _logger.LogError($"Something went wrong in the {nameof(Update)} action {e}"); return(StatusCode(500, "Internal server error")); } }