public ProfessionListResponse Sync(SyncProfessionRequest request)
        {
            ProfessionListResponse response = new ProfessionListResponse();

            try
            {
                response.Professions = new List <ProfessionViewModel>();

                if (request.LastUpdatedAt != null)
                {
                    response.Professions.AddRange(unitOfWork.GetProfessionRepository()
                                                  .GetProfessionsNewerThen(request.CompanyId, (DateTime)request.LastUpdatedAt)
                                                  ?.ConvertToProfessionViewModelList() ?? new List <ProfessionViewModel>());
                }
                else
                {
                    response.Professions.AddRange(unitOfWork.GetProfessionRepository()
                                                  .GetProfessions(request.CompanyId)
                                                  ?.ConvertToProfessionViewModelList() ?? new List <ProfessionViewModel>());
                }

                response.Success = true;
            }
            catch (Exception ex)
            {
                response.Professions = new List <ProfessionViewModel>();
                response.Success     = false;
                response.Message     = ex.Message;
            }

            return(response);
        }
        public ProfessionListResponse Sync(SyncProfessionRequest request)
        {
            ProfessionListResponse response = new ProfessionListResponse();

            try
            {
                response = WpfApiHandler.SendToApi <SyncProfessionRequest, ProfessionViewModel, ProfessionListResponse>(request, "Sync");
            }
            catch (Exception ex)
            {
                response.Professions = new List <ProfessionViewModel>();
                response.Success     = false;
                response.Message     = ex.Message;
            }

            return(response);
        }
        public JsonResult Sync([FromBody] SyncProfessionRequest request)
        {
            ProfessionListResponse response = new ProfessionListResponse();

            try
            {
                response = this.professionService.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(IProfessionService professionService, Action <int, int> callback = null)
        {
            try
            {
                SyncProfessionRequest request = new SyncProfessionRequest();
                request.CompanyId     = MainWindow.CurrentCompanyId;
                request.LastUpdatedAt = GetLastUpdatedAt(MainWindow.CurrentCompanyId);

                int toSync      = 0;
                int syncedItems = 0;

                ProfessionListResponse response = professionService.Sync(request);
                if (response.Success)
                {
                    toSync = response?.Professions?.Count ?? 0;
                    List <ProfessionViewModel> professionsFromDB = response.Professions;

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

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

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

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

                                    insertCommand = AddCreateParameters(insertCommand, profession);
                                    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;
            }
        }