public CallCentarListResponse Sync(SyncCallCentarRequest request)
        {
            CallCentarListResponse response = new CallCentarListResponse();

            try
            {
                response.CallCentars = new List <CallCentarViewModel>();

                if (request.LastUpdatedAt != null)
                {
                    response.CallCentars.AddRange(unitOfWork.GetCallCentarRepository()
                                                  .GetCallCentarsNewerThen(request.CompanyId, (DateTime)request.LastUpdatedAt)
                                                  ?.ConvertToCallCentarViewModelList() ?? new List <CallCentarViewModel>());
                }
                else
                {
                    response.CallCentars.AddRange(unitOfWork.GetCallCentarRepository()
                                                  .GetCallCentars(request.CompanyId)
                                                  ?.ConvertToCallCentarViewModelList() ?? new List <CallCentarViewModel>());
                }

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

            return(response);
        }
        public CallCentarListResponse GetCallCentarsByPage(int companyId, CallCentarViewModel CallCentarSearchObject, int currentPage = 1, int itemsPerPage = 50, int?userId = null)
        {
            CallCentarListResponse     response    = new CallCentarListResponse();
            List <CallCentarViewModel> CallCentars = new List <CallCentarViewModel>();

            using (SqliteConnection db = new SqliteConnection("Filename=SirmiumERPGFC.db"))
            {
                db.Open();
                try
                {
                    SqliteCommand selectCommand = new SqliteCommand(
                        SqlCommandSelectPart +
                        "FROM CallCentars " +
                        "WHERE CompanyId = @CompanyId " +
                        "AND (@UserId IS NULL OR @UserId = '' OR UserId = @UserId) " +
                        "ORDER BY IsSynced, ServerId " +
                        "LIMIT @ItemsPerPage OFFSET @Offset;", db);

                    selectCommand.Parameters.AddWithValue("@CompanyId", companyId);
                    selectCommand.Parameters.AddWithValue("@ItemsPerPage", itemsPerPage);
                    selectCommand.Parameters.AddWithValue("@Offset", (currentPage - 1) * itemsPerPage);
                    selectCommand.Parameters.AddWithValue("@UserId", ((object)userId ?? DBNull.Value));

                    SqliteDataReader query = selectCommand.ExecuteReader();

                    while (query.Read())
                    {
                        CallCentarViewModel dbEntry = Read(query);
                        CallCentars.Add(dbEntry);
                    }
                    selectCommand = new SqliteCommand(
                        "SELECT Count(*) " +
                        "FROM CallCentars " +
                        "WHERE CompanyId = @CompanyId " +
                        "AND (@UserId IS NULL OR @UserId = '' OR UserId = @UserId) ", db);

                    selectCommand.Parameters.AddWithValue("@CompanyId", companyId);
                    selectCommand.Parameters.AddWithValue("@UserId", ((object)userId ?? DBNull.Value));

                    query = selectCommand.ExecuteReader();

                    if (query.Read())
                    {
                        response.TotalItems = query.GetInt32(0);
                    }
                }
                catch (SqliteException error)
                {
                    MainWindow.ErrorMessage = error.Message;
                    response.Success        = false;
                    response.Message        = error.Message;
                    response.CallCentars    = new List <CallCentarViewModel>();
                    return(response);
                }
                db.Close();
            }
            response.Success     = true;
            response.CallCentars = CallCentars;
            return(response);
        }
        public CallCentarListResponse Sync(SyncCallCentarRequest request)
        {
            CallCentarListResponse response = new CallCentarListResponse();

            try
            {
                response = WpfApiHandler.SendToApi <SyncCallCentarRequest, CallCentarViewModel, CallCentarListResponse>(request, "Sync");
            }
            catch (Exception ex)
            {
                response.CallCentars = new List <CallCentarViewModel>();
                response.Success     = false;
                response.Message     = ex.Message;
            }

            return(response);
        }
        public CallCentarListResponse GetCallCentars(int companyId)
        {
            CallCentarListResponse response = new CallCentarListResponse();

            try
            {
                response.CallCentars = unitOfWork.GetCallCentarRepository().GetCallCentars(companyId)
                                       .ConvertToCallCentarViewModelList();
                response.Success = true;
            }
            catch (Exception ex)
            {
                response.CallCentars = new List <CallCentarViewModel>();
                response.Success     = false;
                response.Message     = ex.Message;
            }

            return(response);
        }
        public CallCentarListResponse GetCallCentarsForPopup(int companyId, string filterString)
        {
            CallCentarListResponse     response    = new CallCentarListResponse();
            List <CallCentarViewModel> CallCentars = new List <CallCentarViewModel>();

            using (SqliteConnection db = new SqliteConnection("Filename=SirmiumERPGFC.db"))
            {
                db.Open();
                try
                {
                    SqliteCommand selectCommand = new SqliteCommand(
                        SqlCommandSelectPart +
                        "FROM CallCentars " +
                        "WHERE CompanyId = @CompanyId " +
                        "ORDER BY IsSynced, Id DESC " +
                        "LIMIT @ItemsPerPage;", db);

                    selectCommand.Parameters.AddWithValue("@CompanyId", ((object)filterString) != null ? companyId : 0);
                    selectCommand.Parameters.AddWithValue("@ItemsPerPage", 100);

                    SqliteDataReader query = selectCommand.ExecuteReader();

                    while (query.Read())
                    {
                        CallCentarViewModel dbEntry = Read(query);
                        CallCentars.Add(dbEntry);
                    }
                }
                catch (SqliteException error)
                {
                    MainWindow.ErrorMessage = error.Message;
                    response.Success        = false;
                    response.Message        = error.Message;
                    response.CallCentars    = new List <CallCentarViewModel>();
                    return(response);
                }
                db.Close();
            }
            response.Success     = true;
            response.CallCentars = CallCentars;
            return(response);
        }
        public CallCentarListResponse GetCallCentars(int companyId)
        {
            CallCentarListResponse response = new CallCentarListResponse();

            try
            {
                response = WpfApiHandler.GetFromApi <List <CallCentarViewModel>, CallCentarListResponse>("GetCallCentars", new Dictionary <string, string>()
                {
                    { "CompanyId", companyId.ToString() }
                });
            }
            catch (Exception ex)
            {
                response.CallCentars = new List <CallCentarViewModel>();
                response.Success     = false;
                response.Message     = ex.Message;
            }

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

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

            JsonResult result = Json(response, new Newtonsoft.Json.JsonSerializerSettings()
            {
                Formatting            = Newtonsoft.Json.Formatting.Indented,
                ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Serialize
            });

            return(result);
        }
        public JsonResult GetCallCentars(int companyId)
        {
            CallCentarListResponse response = new CallCentarListResponse();

            try
            {
                response = callCentarService.GetCallCentars(companyId);
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.Message = ex.Message;
                Console.WriteLine(ex.Message);
            }

            JsonResult result = Json(response, new Newtonsoft.Json.JsonSerializerSettings()
            {
                Formatting            = Newtonsoft.Json.Formatting.Indented,
                ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Serialize
            });

            return(result);
        }
        public void Sync(ICallCentarService CallCentarService, Action <int, int> callback = null)
        {
            try
            {
                SyncCallCentarRequest request = new SyncCallCentarRequest();
                request.CompanyId     = MainWindow.CurrentCompanyId;
                request.LastUpdatedAt = GetLastUpdatedAt(MainWindow.CurrentCompanyId);

                int toSync      = 0;
                int syncedItems = 0;

                CallCentarListResponse response = CallCentarService.Sync(request);
                if (response.Success)
                {
                    toSync = response?.CallCentars?.Count ?? 0;
                    List <CallCentarViewModel> CallCentarsFromDB = response.CallCentars;

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

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

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

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

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