public EmployeeDocumentListResponse Sync(SyncEmployeeDocumentRequest request) { EmployeeDocumentListResponse response = new EmployeeDocumentListResponse(); try { response.EmployeeDocuments = new List <EmployeeDocumentViewModel>(); if (request.LastUpdatedAt != null) { response.EmployeeDocuments.AddRange(unitOfWork.GetEmployeeDocumentRepository() .GetEmployeeDocumentsNewerThen(request.CompanyId, (DateTime)request.LastUpdatedAt) ?.ConvertToEmployeeDocumentViewModelList() ?? new List <EmployeeDocumentViewModel>()); } else { response.EmployeeDocuments.AddRange(unitOfWork.GetEmployeeDocumentRepository() .GetEmployeeDocuments(request.CompanyId) ?.ConvertToEmployeeDocumentViewModelList() ?? new List <EmployeeDocumentViewModel>()); } response.Success = true; } catch (Exception ex) { response.EmployeeDocuments = new List <EmployeeDocumentViewModel>(); response.Success = false; response.Message = ex.Message; } return(response); }
public EmployeeDocumentListResponse Sync(SyncEmployeeDocumentRequest request) { EmployeeDocumentListResponse response = new EmployeeDocumentListResponse(); try { response = WpfApiHandler.SendToApi <SyncEmployeeDocumentRequest, EmployeeDocumentViewModel, EmployeeDocumentListResponse>(request, "Sync"); } catch (Exception ex) { response.EmployeeDocuments = new List <EmployeeDocumentViewModel>(); response.Success = false; response.Message = ex.Message; } return(response); }
public JsonResult Sync([FromBody] SyncEmployeeDocumentRequest request) { EmployeeDocumentListResponse response = new EmployeeDocumentListResponse(); try { response = this.EmployeeDocumentService.Sync(request); } catch (Exception ex) { response.Success = false; response.Message = ex.Message; } return(Json(response, new Newtonsoft.Json.JsonSerializerSettings() { Formatting = Newtonsoft.Json.Formatting.Indented })); }
public void Sync(IEmployeeDocumentService EmployeeDocumentService, Action <int, int> callback = null) { try { SyncEmployeeDocumentRequest request = new SyncEmployeeDocumentRequest(); request.CompanyId = MainWindow.CurrentCompanyId; request.LastUpdatedAt = GetLastUpdatedAt(MainWindow.CurrentCompanyId); int toSync = 0; int syncedItems = 0; EmployeeDocumentListResponse response = EmployeeDocumentService.Sync(request); if (response.Success) { toSync = response?.EmployeeDocuments?.Count ?? 0; List <EmployeeDocumentViewModel> employeeDocumentsFromDB = response.EmployeeDocuments; using (SqliteConnection db = new SqliteConnection(SQLiteHelper.SqLiteTableName)) { db.Open(); using (var transaction = db.BeginTransaction()) { SqliteCommand deleteCommand = db.CreateCommand(); deleteCommand.CommandText = "DELETE FROM EmployeeDocuments WHERE Identifier = @Identifier"; SqliteCommand insertCommand = db.CreateCommand(); insertCommand.CommandText = SqlCommandInsertPart; foreach (var employeeDocument in employeeDocumentsFromDB) { deleteCommand.Parameters.AddWithValue("@Identifier", employeeDocument.Identifier); deleteCommand.ExecuteNonQuery(); deleteCommand.Parameters.Clear(); if (employeeDocument.IsActive) { employeeDocument.IsSynced = true; insertCommand = AddCreateParameters(insertCommand, employeeDocument); insertCommand.ExecuteNonQuery(); insertCommand.Parameters.Clear(); syncedItems++; callback?.Invoke(syncedItems, toSync); } } transaction.Commit(); } db.Close(); } } else { throw new Exception(response.Message); } } catch (Exception ex) { MainWindow.ErrorMessage = ex.Message; } }