public async Task <OS_FunctionSkillTemplate_Dto> AddSkill(OS_FunctionSkillTemplate_Dto functionSkillTemplate) { OS_FunctionSkillTemplate toAdd = ObjectMapper.Map <OS_FunctionSkillTemplate_Dto, OS_FunctionSkillTemplate>(functionSkillTemplate); return(ObjectMapper.Map <OS_FunctionSkillTemplate, OS_FunctionSkillTemplate_Dto>(await SkillsRepository.InsertAsync(toAdd))); }
public async Task <IActionResult> OnPostFunctionTemplate() { if (ModelState.IsValid) { try { var FormData = Request.Form; OS_FunctionTemplate_Dto functionTemplate_Dto = JsonSerializer.Deserialize <OS_FunctionTemplate_Dto>(FormData["info"]); bool IsEditing = functionTemplate_Dto.Id > 0; if (IsEditing) { OS_FunctionTemplate curFunctionTemplate = await OS_FunctionTemplateAppService.Repository.GetAsync(functionTemplate_Dto.Id); if (AuditingManager.Current != null) { EntityChangeInfo entityChangeInfo = new EntityChangeInfo(); entityChangeInfo.EntityId = functionTemplate_Dto.Id.ToString(); entityChangeInfo.EntityTenantId = functionTemplate_Dto.TenantId; entityChangeInfo.ChangeTime = DateTime.Now; entityChangeInfo.ChangeType = EntityChangeType.Updated; entityChangeInfo.EntityTypeFullName = typeof(OS_FunctionTemplate).FullName; entityChangeInfo.PropertyChanges = new List <EntityPropertyChangeInfo>(); List <EntityPropertyChangeInfo> entityPropertyChanges = new List <EntityPropertyChangeInfo>(); var auditProps = typeof(OS_FunctionTemplate).GetProperties().Where(x => Attribute.IsDefined(x, typeof(CustomAuditedAttribute))).ToList(); OS_FunctionTemplate mappedInput = ObjectMapper.Map <OS_FunctionTemplate_Dto, OS_FunctionTemplate>(functionTemplate_Dto); foreach (var prop in auditProps) { EntityPropertyChangeInfo propertyChange = new EntityPropertyChangeInfo(); object origVal = prop.GetValue(curFunctionTemplate); propertyChange.OriginalValue = origVal == null ? "" : origVal.ToString(); object newVal = prop.GetValue(mappedInput); propertyChange.NewValue = newVal == null ? "" : newVal.ToString(); if (propertyChange.OriginalValue == propertyChange.NewValue) { continue; } propertyChange.PropertyName = prop.Name; if (prop.Name.EndsWith("Id")) { try { string valuePropName = prop.Name.TrimEnd('d', 'I'); propertyChange.PropertyName = valuePropName; var valueProp = typeof(OS_FunctionTemplate).GetProperty(valuePropName); DictionaryValue _origValObj = (DictionaryValue)valueProp.GetValue(functionTemplate_Dto); if (_origValObj == null) { _origValObj = await DictionaryValuesRepo.GetAsync((Guid)origVal); } string _origVal = _origValObj.Value; propertyChange.OriginalValue = origVal == null ? "" : _origVal; DictionaryValue _newValObj = (DictionaryValue)valueProp.GetValue(mappedInput); if (_newValObj == null) { _newValObj = await DictionaryValuesRepo.GetAsync((Guid)newVal); } string _newVal = _newValObj.Value; propertyChange.NewValue = _newValObj == null ? "" : _newVal; } catch (Exception ex) { } } propertyChange.PropertyTypeFullName = prop.Name.GetType().FullName; entityChangeInfo.PropertyChanges.Add(propertyChange); } #region ExtraProperties //List<EmployeeExtraPropertyHistory> allExtraPropertyHistories = new List<EmployeeExtraPropertyHistory>(); //if (departmentTemplate_Dto.ExtraProperties != curFunctionTemplate.ExtraProperties) //{ // //GeneralInfo oldGeneralInfo = department.GetProperty<GeneralInfo>("generalInfo"); // //List<EmployeeExtraPropertyHistory> physicalIdsHistory = new List<EmployeeExtraPropertyHistory>(); // //var generalInfoPhysicalIdAuditProps = typeof(PhysicalID).GetProperties().Where(x => Attribute.IsDefined(x, typeof(CustomAuditedAttribute))).ToList(); // //List<PhysicalId<Guid>> NewPhysicalIds = generalInfo.PhysicalIds.Where(x => !oldGeneralInfo.PhysicalIds.Any(y => y.Id == x.Id)).ToList(); // //List<PhysicalId<Guid>> UpdatedPhysicalIds = generalInfo.PhysicalIds.Where(x => oldGeneralInfo.PhysicalIds.Any(y => y.Id == x.Id)).ToList(); // //List<PhysicalId<Guid>> DeletedPhysicalIds = oldGeneralInfo.PhysicalIds.Where(x => !generalInfo.PhysicalIds.Any(y => y.Id == x.Id)).ToList(); // //for (int i = 0; i < NewPhysicalIds.Count; i++) // //{ // // PhysicalId<Guid> curPhId = generalInfo.PhysicalIds[i]; // // EmployeeExtraPropertyHistory newPhIdHistory = new EmployeeExtraPropertyHistory(2, "Physical Id", curPhId.IDNumber, "Created"); // // physicalIdsHistory.Add(newPhIdHistory); // //} // //for (int i = 0; i < UpdatedPhysicalIds.Count; i++) // //{ // // PhysicalId<Guid> curPhId = generalInfo.PhysicalIds[i]; // // PhysicalId<Guid> oldPhId = oldGeneralInfo.PhysicalIds.First(x => x.Id == curPhId.Id); // // EmployeeExtraPropertyHistory updatedPhIdHistory = new EmployeeExtraPropertyHistory(2, "Physical Id", curPhId.IDNumber, "Updated"); // // foreach (var prop in generalInfoPhysicalIdAuditProps) // // { // // updatedPhIdHistory.PropertyChanges = new List<EmployeeTypePropertyChange>(); // // EmployeeTypePropertyChange propertyChange = new EmployeeTypePropertyChange(); // // object origVal = prop.GetValue(oldPhId); // // propertyChange.OriginalValue = origVal == null ? "" : origVal.ToString(); // // object newVal = prop.GetValue(curPhId); // // propertyChange.NewValue = newVal == null ? "" : newVal.ToString(); // // if (propertyChange.OriginalValue == propertyChange.NewValue) continue; // // propertyChange.PropertyName = prop.Name; // // if (prop.Name.EndsWith("Id")) // // { // // try // // { // // string valuePropName = prop.Name.TrimEnd('d', 'I'); // // propertyChange.PropertyName = valuePropName; // // var valueProp = typeof(PhysicalID).GetProperty(valuePropName); // // DictionaryValue _origValObj = (DictionaryValue)valueProp.GetValue(oldPhId); // // if (_origValObj == null) _origValObj = await DictionaryValuesRepo.GetAsync((Guid)origVal); // // string _origVal = _origValObj.Value; // // propertyChange.OriginalValue = origVal == null ? "" : _origVal; // // DictionaryValue _newValObj = (DictionaryValue)valueProp.GetValue(curPhId); // // if (_newValObj == null) _newValObj = await DictionaryValuesRepo.GetAsync((Guid)newVal); // // string _newVal = _newValObj.Value; // // propertyChange.NewValue = _newValObj == null ? "" : _newVal; // // } // // catch (Exception ex) // // { // // } // // } // // propertyChange.PropertyTypeFullName = prop.Name.GetType().FullName; // // updatedPhIdHistory.PropertyChanges.Add(propertyChange); // // } // // physicalIdsHistory.Add(updatedPhIdHistory); // //} // //for (int i = 0; i < DeletedPhysicalIds.Count; i++) // //{ // // PhysicalId<Guid> curPhId = generalInfo.PhysicalIds[i]; // // EmployeeExtraPropertyHistory deletedPhIdHistory = new EmployeeExtraPropertyHistory(2, "Physical Id", curPhId.IDNumber, "Deleted"); // // physicalIdsHistory.Add(deletedPhIdHistory); // //} // entityChangeInfo.SetProperty("extraPropertiesHistory", allExtraPropertyHistories); //} #endregion AuditingManager.Current.Log.EntityChanges.Add(entityChangeInfo); } curFunctionTemplate.Name = functionTemplate_Dto.Name; curFunctionTemplate.NameLocalized = functionTemplate_Dto.NameLocalized; curFunctionTemplate.Code = functionTemplate_Dto.Code; curFunctionTemplate.ReviewPeriod = functionTemplate_Dto.ReviewPeriod; curFunctionTemplate.ReviewPeriodDays = functionTemplate_Dto.ReviewPeriodDays; curFunctionTemplate.ValidityFromDate = functionTemplate_Dto.ValidityFromDate; curFunctionTemplate.ValidityToDate = functionTemplate_Dto.ValidityToDate; curFunctionTemplate.Description = functionTemplate_Dto.Description; curFunctionTemplate.DoesKPI = functionTemplate_Dto.DoesKPI; curFunctionTemplate.CompensationMatrix = null; curFunctionTemplate.CompensationMatrixId = functionTemplate_Dto.CompensationMatrixId; #region Child Entities OS_FunctionSkillTemplate_Dto[] functionSkills = functionTemplate_Dto.FunctionSkillTemplates.ToArray(); int[] curFunctionSkillsIds = curFunctionTemplate.FunctionSkillTemplates != null && curFunctionTemplate.FunctionSkillTemplates.Count > 0 ? curFunctionTemplate.FunctionSkillTemplates.Select(x => x.SkillTemplate.Id).ToArray() : new int[0]; List <int> toDeleteSkills = new List <int>(); for (int i = 0; i < curFunctionSkillsIds.Length; i++) { OS_FunctionSkillTemplate curFunctionSkill = curFunctionTemplate.FunctionSkillTemplates.First(x => x.SkillTemplate.Id == curFunctionSkillsIds[i]); if (!functionSkills.Any(x => x.SkillTemplate.Id == curFunctionSkillsIds[i])) { curFunctionTemplate.FunctionSkillTemplates.Remove(curFunctionTemplate.FunctionSkillTemplates.First(x => x.SkillTemplate.Id == curFunctionSkillsIds[i])); toDeleteSkills.Add(curFunctionSkillsIds[i]); } } for (int i = 0; i < functionSkills.Length; i++) { if (!curFunctionTemplate.FunctionSkillTemplates.Any(x => x.SkillTemplateId == functionSkills[i].SkillTemplate.Id)) { curFunctionTemplate.FunctionSkillTemplates.Add(new OS_FunctionSkillTemplate() { SkillTemplateId = functionSkills[i].SkillTemplate.Id }); } else { var _functionSkill = curFunctionTemplate.FunctionSkillTemplates.First(x => x.SkillTemplateId == functionSkills[i].SkillTemplate.Id); //_functionLoc.FunctionValidityStart = posFunctions[i].FunctionValidityStart; //_functionLoc.FunctionValidityEnd = posFunctions[i].FunctionValidityEnd; //_functionLoc.Name = posFunctions[i].Name; //curFunction.FunctionSkillTemplates.Remove(curFunction.FunctionSkillTemplates.First(x => x.FunctionTemplateId == _functionLoc.FunctionTemplateId)); await OS_FunctionTemplateAppService.SkillsRepository.UpdateAsync(_functionSkill); } } for (int i = 0; i < toDeleteSkills.Count; i++) { await OS_FunctionTemplateAppService.SkillsRepository.DeleteAsync(x => x.SkillTemplateId == toDeleteSkills[i]); } OS_FunctionAcademiaTemplate_Dto[] functionAcademia = functionTemplate_Dto.FunctionAcademiaTemplates.ToArray(); int[] curFunctionAcademiaIds = curFunctionTemplate.FunctionAcademiaTemplates != null && curFunctionTemplate.FunctionAcademiaTemplates.Count > 0 ? curFunctionTemplate.FunctionAcademiaTemplates.Select(x => x.AcademiaTemplate.Id).ToArray() : new int[0]; List <int> toDeleteAcademia = new List <int>(); for (int i = 0; i < curFunctionAcademiaIds.Length; i++) { OS_FunctionAcademiaTemplate curFunctionAcademia = curFunctionTemplate.FunctionAcademiaTemplates.First(x => x.AcademiaTemplate.Id == curFunctionAcademiaIds[i]); if (!functionAcademia.Any(x => x.AcademiaTemplate.Id == curFunctionAcademiaIds[i])) { curFunctionTemplate.FunctionAcademiaTemplates.Remove(curFunctionTemplate.FunctionAcademiaTemplates.First(x => x.AcademiaTemplate.Id == curFunctionAcademiaIds[i])); toDeleteSkills.Add(curFunctionAcademiaIds[i]); } } for (int i = 0; i < functionAcademia.Length; i++) { if (!curFunctionTemplate.FunctionAcademiaTemplates.Any(x => x.AcademiaTemplateId == functionAcademia[i].AcademiaTemplate.Id)) { curFunctionTemplate.FunctionAcademiaTemplates.Add(new OS_FunctionAcademiaTemplate() { AcademiaTemplateId = functionAcademia[i].AcademiaTemplate.Id }); } else { var _functionAcademia = curFunctionTemplate.FunctionAcademiaTemplates.First(x => x.AcademiaTemplateId == functionAcademia[i].AcademiaTemplate.Id); //_functionLoc.FunctionValidityStart = posFunctions[i].FunctionValidityStart; //_functionLoc.FunctionValidityEnd = posFunctions[i].FunctionValidityEnd; //_functionLoc.Name = posFunctions[i].Name; //curFunction.FunctionAcademiaTemplates.Remove(curFunction.FunctionAcademiaTemplates.First(x => x.FunctionTemplateId == _functionLoc.FunctionTemplateId)); await OS_FunctionTemplateAppService.AcademiaRepository.UpdateAsync(_functionAcademia); } } for (int i = 0; i < toDeleteAcademia.Count; i++) { await OS_FunctionTemplateAppService.AcademiaRepository.DeleteAsync(x => x.AcademiaTemplateId == toDeleteAcademia[i]); } #endregion OS_FunctionTemplate_Dto updated = ObjectMapper.Map <OS_FunctionTemplate, OS_FunctionTemplate_Dto>(await OS_FunctionTemplateAppService.Repository.UpdateAsync(curFunctionTemplate)); updated.FunctionSkillTemplates = ObjectMapper.Map <List <OS_FunctionSkillTemplate>, List <OS_FunctionSkillTemplate_Dto> >(curFunctionTemplate.FunctionSkillTemplates.ToList()); updated.FunctionAcademiaTemplates = ObjectMapper.Map <List <OS_FunctionAcademiaTemplate>, List <OS_FunctionAcademiaTemplate_Dto> >(curFunctionTemplate.FunctionAcademiaTemplates.ToList()); updated.CompensationMatrix = await OS_FunctionTemplateAppService.GetCompensationMatrixAsync(updated.CompensationMatrixId); return(StatusCode(200, updated)); } else { functionTemplate_Dto.Id = 0; if (functionTemplate_Dto.FunctionSkillTemplates != null) { functionTemplate_Dto.FunctionSkillTemplates.ForEach(x => { x.Id = 0; x.SkillTemplateId = x.SkillTemplate.Id; x.SkillTemplate = null; }); } if (functionTemplate_Dto.FunctionAcademiaTemplates != null) { functionTemplate_Dto.FunctionAcademiaTemplates.ForEach(x => { x.Id = 0; x.AcademiaTemplateId = x.AcademiaTemplate.Id; x.AcademiaTemplate = null; }); } functionTemplate_Dto.CompensationMatrix = null; OS_FunctionTemplate_Dto added = await OS_FunctionTemplateAppService.CreateAsync(functionTemplate_Dto); added = await OS_FunctionTemplateAppService.GetAsync(added.Id); if (AuditingManager.Current != null) { EntityChangeInfo entityChangeInfo = new EntityChangeInfo(); entityChangeInfo.EntityId = added.Id.ToString(); entityChangeInfo.EntityTenantId = added.TenantId; entityChangeInfo.ChangeTime = DateTime.Now; entityChangeInfo.ChangeType = EntityChangeType.Created; entityChangeInfo.EntityTypeFullName = typeof(OS_FunctionTemplate).FullName; AuditingManager.Current.Log.EntityChanges.Add(entityChangeInfo); } return(StatusCode(200, added)); } } catch (Exception ex) { } } return(StatusCode(500)); }
public async Task <OS_FunctionSkillTemplate_Dto> AddSkillTemplate(OS_FunctionSkillTemplate functionSkillTemplate) { return(ObjectMapper.Map <OS_FunctionSkillTemplate, OS_FunctionSkillTemplate_Dto>(await SkillsRepository.InsertAsync(functionSkillTemplate))); }