public async Task UpdateActivityCodeAsync(ActivityCodeUpdateDto activityCode) { var activityCodeEntity = await DbSet .FirstAsync(ac => ac.ActivityCodeId == activityCode.ActivityCodeId); activityCode.EndDate = activityCode.EndDate?.Date; Mapper.Map(activityCode, activityCodeEntity); }
public async Task UpdateActivityCodeAsync(ActivityCodeUpdateDto activityCode) { var activityCodeEntity = await DbSet .Include(x => x.HmrServiceAreaActivities) .FirstAsync(ac => ac.ActivityCodeId == activityCode.ActivityCodeId); activityCode.EndDate = activityCode.EndDate?.Date; Mapper.Map(activityCode, activityCodeEntity); SyncActivityCodeServiceAreas(activityCode, activityCodeEntity); }
public async Task <ActionResult> UpdateActivityCode(decimal id, ActivityCodeUpdateDto activityCode) { if (id != activityCode.ActivityCodeId) { throw new Exception($"The Activity Code ID from the query string does not match that of the body."); } var response = await _activityCodeSvc.UpdateActivityCodeAsync(activityCode); if (response.NotFound) { return(NotFound()); } if (response.Errors.Count > 0) { return(ValidationUtils.GetValidationErrorResult(response.Errors, ControllerContext)); } return(NoContent()); }
private void SyncActivityCodeServiceAreas(ActivityCodeUpdateDto activityCodeUpdateDto, HmrActivityCode activityCodeEntity) { var areasToDelete = activityCodeEntity.HmrServiceAreaActivities.Where(s => !activityCodeUpdateDto.ServiceAreaNumbers.Contains(s.ServiceAreaNumber)).ToList(); for (var i = areasToDelete.Count() - 1; i >= 0; i--) { DbContext.Remove(areasToDelete[i]); } var existingAreaNumbers = activityCodeEntity.HmrServiceAreaActivities.Select(s => s.ServiceAreaNumber); var newAreaNumbers = activityCodeUpdateDto.ServiceAreaNumbers.Where(r => !existingAreaNumbers.Contains(r)); foreach (var areaNumber in newAreaNumbers) { activityCodeEntity.HmrServiceAreaActivities .Add(new HmrServiceAreaActivity { ServiceAreaNumber = areaNumber, ActivityCodeId = activityCodeEntity.ActivityCodeId }); } }