public ProfessionListResponse GetProfessionsNewerThen(int companyId, DateTime?lastUpdateTime)
        {
            ProfessionListResponse response = new ProfessionListResponse();

            try
            {
                if (lastUpdateTime != null)
                {
                    response.Professions = unitOfWork.GetProfessionRepository()
                                           .GetProfessionsNewerThen(companyId, (DateTime)lastUpdateTime)
                                           .ConvertToProfessionViewModelList();
                }
                else
                {
                    response.Professions = unitOfWork.GetProfessionRepository()
                                           .GetProfessions(companyId)
                                           .ConvertToProfessionViewModelList();
                }
                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.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 GetProfessionsForPopup(int companyId, string filterString)
        {
            ProfessionListResponse     response    = new ProfessionListResponse();
            List <ProfessionViewModel> Professions = new List <ProfessionViewModel>();

            using (SqliteConnection db = new SqliteConnection("Filename=SirmiumERPGFC.db"))
            {
                db.Open();
                try
                {
                    SqliteCommand selectCommand = new SqliteCommand(
                        SqlCommandSelectPart +
                        "FROM Professions " +
                        "WHERE ((@SecondCode IS NULL OR @SecondCode = '' OR SecondCode LIKE @SecondCode) " +
                        "OR (@Name IS NULL OR @Name = '' OR Name LIKE @Name) " +
                        "OR (@Country IS NULL OR @Country = '' OR CountryCode LIKE @Country OR CountryName LIKE @Country)) " +
                        "AND CompanyId = @CompanyId " +
                        "ORDER BY IsSynced, Id DESC " +
                        "LIMIT @ItemsPerPage;", db);

                    selectCommand.Parameters.AddWithValue("@SecondCode", ((object)filterString) != null ? "%" + filterString + "%" : "");
                    selectCommand.Parameters.AddWithValue("@Name", ((object)filterString) != null ? "%" + filterString + "%" : "");
                    selectCommand.Parameters.AddWithValue("@Country", ((object)filterString) != null ? "%" + filterString + "%" : "");
                    selectCommand.Parameters.AddWithValue("@CompanyId", companyId);
                    selectCommand.Parameters.AddWithValue("@ItemsPerPage", 100);

                    SqliteDataReader query = selectCommand.ExecuteReader();

                    while (query.Read())
                    {
                        ProfessionViewModel dbEntry = Read(query);
                        Professions.Add(dbEntry);
                    }
                }
                catch (SqliteException error)
                {
                    MainWindow.ErrorMessage = error.Message;
                    response.Success        = false;
                    response.Message        = error.Message;
                    response.Professions    = new List <ProfessionViewModel>();
                    return(response);
                }
                db.Close();
            }
            response.Success     = true;
            response.Professions = Professions;
            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 GetProfessions(int companyId)
        {
            ProfessionListResponse response = new ProfessionListResponse();

            try
            {
                response = professionService.GetProfessions(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 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 ProfessionListResponse GetProfessions(int companyId)
        {
            ProfessionListResponse response = new ProfessionListResponse();

            try
            {
                response.Professions = unitOfWork.GetProfessionRepository().GetProfessions(companyId)
                                       .ConvertToProfessionViewModelList();
                response.Success = true;
            }
            catch (Exception ex)
            {
                response.Professions = new List <ProfessionViewModel>();
                response.Success     = false;
                response.Message     = ex.Message;
            }

            return(response);
        }
        public ProfessionListResponse GetProfessions(int companyId)
        {
            ProfessionListResponse response = new ProfessionListResponse();

            try
            {
                response = WpfApiHandler.GetFromApi <List <ProfessionViewModel>, ProfessionListResponse>("GetProfessions", new Dictionary <string, string>()
                {
                    { "CompanyId", companyId.ToString() }
                });
            }
            catch (Exception ex)
            {
                response.Professions = new List <ProfessionViewModel>();
                response.Success     = false;
                response.Message     = ex.Message;
            }

            return(response);
        }
        public ProfessionListResponse GetProfessionsByPage(int companyId, ProfessionViewModel professionSearchObject, int currentPage = 1, int itemsPerPage = 50)
        {
            ProfessionListResponse     response    = new ProfessionListResponse();
            List <ProfessionViewModel> Professions = new List <ProfessionViewModel>();

            using (SqliteConnection db = new SqliteConnection("Filename=SirmiumERPGFC.db"))
            {
                db.Open();
                try
                {
                    SqliteCommand selectCommand = new SqliteCommand(
                        SqlCommandSelectPart +
                        "FROM Professions " +
                        "WHERE (@SecondCode IS NULL OR @SecondCode = '' OR SecondCode LIKE @SecondCode) " +
                        "AND (@Name IS NULL OR @Name = '' OR Name LIKE @Name) " +
                        //"AND (@Country IS NULL OR @Country = '' OR CountryCode LIKE @Country) " +
                        "AND (@Country IS NULL OR @Country = '' OR CountryName LIKE @Country) " +
                        "AND CompanyId = @CompanyId " +
                        "ORDER BY IsSynced, Id DESC " +
                        "LIMIT @ItemsPerPage OFFSET @Offset;", db);

                    selectCommand.Parameters.AddWithValue("@SecondCode", ((object)professionSearchObject.Search_SecondCode) != null ? "%" + professionSearchObject.Search_SecondCode + "%" : "");
                    selectCommand.Parameters.AddWithValue("@Name", ((object)professionSearchObject.Search_Name) != null ? "%" + professionSearchObject.Search_Name + "%" : "");
                    selectCommand.Parameters.AddWithValue("@Country", ((object)professionSearchObject.Search_Country) != null ? "%" + professionSearchObject.Search_Country + "%" : "");
                    selectCommand.Parameters.AddWithValue("@CompanyId", companyId);
                    selectCommand.Parameters.AddWithValue("@ItemsPerPage", itemsPerPage);
                    selectCommand.Parameters.AddWithValue("@Offset", (currentPage - 1) * itemsPerPage);

                    SqliteDataReader query = selectCommand.ExecuteReader();

                    while (query.Read())
                    {
                        ProfessionViewModel dbEntry = Read(query);
                        Professions.Add(dbEntry);
                    }


                    selectCommand = new SqliteCommand(
                        "SELECT Count(*) " +
                        "FROM Professions " +
                        "WHERE (@SecondCode IS NULL OR @SecondCode = '' OR SecondCode LIKE @SecondCode) " +
                        "AND (@Name IS NULL OR @Name = '' OR Name LIKE @Name) " +
                        //"AND (@Country IS NULL OR @Country = '' OR CountryCode LIKE @Country) " +
                        "AND (@Country IS NULL OR @Country = '' OR CountryName LIKE @Country) " +
                        "AND CompanyId = @CompanyId;", db);

                    selectCommand.Parameters.AddWithValue("@SecondCode", ((object)professionSearchObject.Search_SecondCode) != null ? "%" + professionSearchObject.Search_SecondCode + "%" : "");
                    selectCommand.Parameters.AddWithValue("@Name", ((object)professionSearchObject.Search_Name) != null ? "%" + professionSearchObject.Search_Name + "%" : "");
                    selectCommand.Parameters.AddWithValue("@Country", ((object)professionSearchObject.Search_Country) != null ? "%" + professionSearchObject.Search_Country + "%" : "");

                    selectCommand.Parameters.AddWithValue("@CompanyId", companyId);

                    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.Professions    = new List <ProfessionViewModel>();
                    return(response);
                }
                db.Close();
            }
            response.Success     = true;
            response.Professions = Professions;
            return(response);
        }
        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;
            }
        }