public LimitationListResponse Sync(SyncLimitationRequest request)
        {
            LimitationListResponse response = new LimitationListResponse();

            try
            {
                response.Limitations = new List <LimitationViewModel>();

                if (request.LastUpdatedAt != null)
                {
                    response.Limitations.AddRange(unitOfWork.GetLimitationRepository()
                                                  .GetLimitationsNewerThen(request.CompanyId, (DateTime)request.LastUpdatedAt)
                                                  ?.ConvertToLimitationViewModelList() ?? new List <LimitationViewModel>());
                }
                else
                {
                    response.Limitations.AddRange(unitOfWork.GetLimitationRepository()
                                                  .GetLimitations(request.CompanyId)
                                                  ?.ConvertToLimitationViewModelList() ?? new List <LimitationViewModel>());
                }

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

            return(response);
        }
        public LimitationListResponse GetLimitationsNewerThen(int companyId, DateTime?lastUpdateTime)
        {
            LimitationListResponse response = new LimitationListResponse();

            try
            {
                if (lastUpdateTime != null)
                {
                    response.Limitations = unitOfWork.GetLimitationRepository()
                                           .GetLimitationsNewerThen(companyId, (DateTime)lastUpdateTime)
                                           .ConvertToLimitationViewModelList();
                }
                else
                {
                    response.Limitations = unitOfWork.GetLimitationRepository()
                                           .GetLimitations(companyId)
                                           .ConvertToLimitationViewModelList();
                }
                response.Success = true;
            }
            catch (Exception ex)
            {
                response.Limitations = new List <LimitationViewModel>();
                response.Success     = false;
                response.Message     = ex.Message;
            }

            return(response);
        }
        public LimitationListResponse Sync(SyncLimitationRequest request)
        {
            LimitationListResponse response = new LimitationListResponse();

            try
            {
                response = WpfApiHandler.SendToApi <SyncLimitationRequest, LimitationViewModel, LimitationListResponse>(request, "Sync");
            }
            catch (Exception ex)
            {
                response.Limitations = new List <LimitationViewModel>();
                response.Success     = false;
                response.Message     = ex.Message;
            }

            return(response);
        }
        public LimitationListResponse GetLimitations(int companyId)
        {
            LimitationListResponse response = new LimitationListResponse();

            try
            {
                response.Limitations = unitOfWork.GetLimitationRepository().GetLimitations(companyId)
                                       .ConvertToLimitationViewModelList();
                response.Success = true;
            }
            catch (Exception ex)
            {
                response.Limitations = new List <LimitationViewModel>();
                response.Success     = false;
                response.Message     = ex.Message;
            }

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

            try
            {
                response = this.limitationService.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 JsonResult GetLimitations(int companyId)
        {
            LimitationListResponse response = new LimitationListResponse();

            try
            {
                response = limitationService.GetLimitations(companyId);
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.Message = ex.Message;
                Console.WriteLine(ex.Message);
            }
            return(Json(response, new Newtonsoft.Json.JsonSerializerSettings()
            {
                Formatting = Newtonsoft.Json.Formatting.Indented
            }));
        }
        public LimitationListResponse GetLimitations(int companyId)
        {
            LimitationListResponse response = new LimitationListResponse();

            try
            {
                response = WpfApiHandler.GetFromApi <List <LimitationViewModel>, LimitationListResponse>("GetLimitations", new Dictionary <string, string>()
                {
                    { "CompanyId", companyId.ToString() }
                });
            }
            catch (Exception ex)
            {
                response.Limitations = new List <LimitationViewModel>();
                response.Success     = false;
                response.Message     = ex.Message;
            }

            return(response);
        }
        public void Sync(ILimitationService limitationService, Action <int, int> callback = null)
        {
            try
            {
                SyncLimitationRequest request = new SyncLimitationRequest();
                request.CompanyId     = MainWindow.CurrentCompanyId;
                request.LastUpdatedAt = GetLastUpdatedAt(MainWindow.CurrentCompanyId);

                int toSync      = 0;
                int syncedItems = 0;

                LimitationListResponse response = limitationService.Sync(request);
                if (response.Success)
                {
                    toSync = response?.Limitations?.Count ?? 0;
                    List <LimitationViewModel> limitationsFromDB = response.Limitations;

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

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

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

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

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