public PhonebookPhoneListResponse Sync(SyncPhonebookPhoneRequest request)
        {
            PhonebookPhoneListResponse response = new PhonebookPhoneListResponse();

            try
            {
                response.PhonebookPhones = new List <PhonebookPhoneViewModel>();

                if (request.LastUpdatedAt != null)
                {
                    response.PhonebookPhones.AddRange(unitOfWork.GetPhonebookPhoneRepository()
                                                      .GetPhonebookPhonesNewerThen(request.CompanyId, (DateTime)request.LastUpdatedAt)
                                                      ?.ConvertToPhonebookPhoneViewModelList() ?? new List <PhonebookPhoneViewModel>());
                }
                else
                {
                    response.PhonebookPhones.AddRange(unitOfWork.GetPhonebookPhoneRepository()
                                                      .GetPhonebookPhones(request.CompanyId)
                                                      ?.ConvertToPhonebookPhoneViewModelList() ?? new List <PhonebookPhoneViewModel>());
                }

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

            return(response);
        }
Example #2
0
        public PhonebookPhoneListResponse Sync(SyncPhonebookPhoneRequest request)
        {
            PhonebookPhoneListResponse response = new PhonebookPhoneListResponse();

            try
            {
                response = WpfApiHandler.SendToApi <SyncPhonebookPhoneRequest, PhonebookPhoneViewModel, PhonebookPhoneListResponse>(request, "Sync");
            }
            catch (Exception ex)
            {
                response.PhonebookPhones = new List <PhonebookPhoneViewModel>();
                response.Success         = false;
                response.Message         = ex.Message;
            }

            return(response);
        }
Example #3
0
        public JsonResult Sync([FromBody] SyncPhonebookPhoneRequest request)
        {
            PhonebookPhoneListResponse response = new PhonebookPhoneListResponse();

            try
            {
                response = this.PhonebookPhoneService.Sync(request);
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.Message = ex.Message;
            }

            return(Json(response, new Newtonsoft.Json.JsonSerializerSettings()
            {
                Formatting = Newtonsoft.Json.Formatting.Indented
            }));
        }
Example #4
0
        public void Sync(IPhonebookPhoneService PhonebookPhoneservice, Action <int, int> callback = null)
        {
            try
            {
                SyncPhonebookPhoneRequest request = new SyncPhonebookPhoneRequest();
                request.CompanyId     = MainWindow.CurrentCompanyId;
                request.LastUpdatedAt = GetLastUpdatedAt(MainWindow.CurrentCompanyId);

                int toSync      = 0;
                int syncedItems = 0;

                PhonebookPhoneListResponse response = PhonebookPhoneservice.Sync(request);
                if (response.Success)
                {
                    toSync = response?.PhonebookPhones?.Count ?? 0;
                    List <PhonebookPhoneViewModel> items = response.PhonebookPhones;

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

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

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

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

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