public EmployeeDTO SaveUpdatedEmployee(EmployeeDTO dto) { var emp = IMSDB.Employees.Find(dto.BasicInfo.BadgeNumber); if (!UserAddData?.IMSEmployeesBusinessUnitsEditRights?.Contains(emp.BusinessUnitID) ?? false) { throw new Exception("You have no rights to edit employees of this business unit"); } emp.FromDTO(dto, UserAddData?.IMSEmployeeSectionEditRights); var results = new List <ValidationResult>(); var context = new ValidationContext(emp); if (!Validator.TryValidateObject(emp, context, results, true)) { throw new Exception(string.Join(", ", results.Select(er => er.ErrorMessage))); } IMSDB.SaveChanges(); IMSDB.Entry(emp).Reload(); dto.FromEF(emp, User.IsInRole(AppRole.HREmployeesCreatorRole) ? null : UserAddData?.IMSEmployeeSectionViewRights); return(dto); }
public EmployeeDTO SaveNewEmployee(EmployeeDTO dto) { if (!UserAddData?.IMSEmployeesBusinessUnitsEditRights?.Contains(dto.BasicCompanyInfo?.BusinessUnitID) ?? false) { throw new Exception("You have no rights to edit employees of this business unit"); } var emp = IMSDB.Employees.Add(new DataModels.Models.IMS.Employee()); emp.FromDTO(dto, UserAddData?.IMSEmployeeSectionEditRights); var results = new List <ValidationResult>(); var context = new ValidationContext(emp); if (!Validator.TryValidateObject(emp, context, results, true)) { throw new Exception(string.Join(", ", results.Select(er => er.ErrorMessage))); } IMSDB.SaveChanges(); if (dto.BasicInfo.PhotoBase64 != null) { App.PhotoRepository.SaveEmployeePhotoFromBase64(dto.BasicInfo.BadgeNumber, dto.BasicInfo.PhotoBase64); } else { App.PhotoRepository.DeleteEmployeePhotoIfExists(dto.BasicInfo.BadgeNumber); } IMSDB.Entry(emp).Reload(); dto.FromEF(emp); return(dto); }