public void Sync(IPhysicalPersonAttachmentService PhysicalPersonAttachmentService, Action <int, int> callback = null)
        {
            try
            {
                SyncPhysicalPersonAttachmentRequest request = new SyncPhysicalPersonAttachmentRequest();
                request.CompanyId     = MainWindow.CurrentCompanyId;
                request.LastUpdatedAt = GetLastUpdatedAt(MainWindow.CurrentCompanyId);

                int toSync      = 0;
                int syncedItems = 0;

                PhysicalPersonAttachmentListResponse response = PhysicalPersonAttachmentService.Sync(request);
                if (response.Success)
                {
                    toSync = response?.PhysicalPersonAttachments?.Count ?? 0;
                    List <PhysicalPersonAttachmentViewModel> PhysicalPersonsFromDB = response.PhysicalPersonAttachments;

                    using (SqliteConnection db = new SqliteConnection(SQLiteHelper.SqLiteTableName))
                    {
                        db.Open();
                        using (var transaction = db.BeginTransaction())
                        {
                            SqliteCommand deleteCommand = db.CreateCommand();
                            deleteCommand.CommandText = "DELETE FROM PhysicalPersonAttachments WHERE Identifier = @Identifier";

                            SqliteCommand insertCommand = db.CreateCommand();
                            insertCommand.CommandText = SqlCommandInsertPart;

                            foreach (var PhysicalPerson in PhysicalPersonsFromDB)
                            {
                                deleteCommand.Parameters.AddWithValue("@Identifier", PhysicalPerson.Identifier);
                                deleteCommand.ExecuteNonQuery();
                                deleteCommand.Parameters.Clear();

                                if (PhysicalPerson.IsActive)
                                {
                                    PhysicalPerson.IsSynced = true;

                                    insertCommand = AddCreateParameters(insertCommand, PhysicalPerson);
                                    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;
            }
        }
Ejemplo n.º 2
0
 public PhysicalPersonAttachmentController(IServiceProvider serviceProvider)
 {
     PhysicalPersonAttachmentService = serviceProvider.GetRequiredService <IPhysicalPersonAttachmentService>();
 }