public PhysicalPersonCardListResponse Sync(SyncPhysicalPersonCardRequest request)
        {
            PhysicalPersonCardListResponse response = new PhysicalPersonCardListResponse();

            try
            {
                response.PhysicalPersonCards = new List <PhysicalPersonCardViewModel>();

                if (request.LastUpdatedAt != null)
                {
                    response.PhysicalPersonCards.AddRange(unitOfWork.GetPhysicalPersonCardRepository()
                                                          .GetPhysicalPersonCardsNewerThen(request.CompanyId, (DateTime)request.LastUpdatedAt)
                                                          ?.ConvertToPhysicalPersonCardViewModelList() ?? new List <PhysicalPersonCardViewModel>());
                }
                else
                {
                    response.PhysicalPersonCards.AddRange(unitOfWork.GetPhysicalPersonCardRepository()
                                                          .GetPhysicalPersonCards(request.CompanyId)
                                                          ?.ConvertToPhysicalPersonCardViewModelList() ?? new List <PhysicalPersonCardViewModel>());
                }

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

            return(response);
        }
        public PhysicalPersonCardListResponse Sync(SyncPhysicalPersonCardRequest request)
        {
            PhysicalPersonCardListResponse response = new PhysicalPersonCardListResponse();

            try
            {
                response = WpfApiHandler.SendToApi <SyncPhysicalPersonCardRequest, PhysicalPersonCardViewModel, PhysicalPersonCardListResponse>(request, "Sync");
            }
            catch (Exception ex)
            {
                response.PhysicalPersonCards = new List <PhysicalPersonCardViewModel>();
                response.Success             = false;
                response.Message             = ex.Message;
            }

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

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

            return(Json(response, new Newtonsoft.Json.JsonSerializerSettings()
            {
                Formatting = Newtonsoft.Json.Formatting.Indented
            }));
        }
Esempio n. 4
0
        public PhysicalPersonCardListResponse GetPhysicalPersonCardsByPhysicalPerson(int companyId, Guid PhysicalPersonIdentifier)
        {
            PhysicalPersonCardListResponse     response            = new PhysicalPersonCardListResponse();
            List <PhysicalPersonCardViewModel> PhysicalPersonCards = new List <PhysicalPersonCardViewModel>();

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

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

                    SqliteDataReader query = selectCommand.ExecuteReader();

                    while (query.Read())
                    {
                        PhysicalPersonCardViewModel dbEntry = Read(query);
                        PhysicalPersonCards.Add(dbEntry);
                    }
                }
                catch (SqliteException error)
                {
                    MainWindow.ErrorMessage      = error.Message;
                    response.Success             = false;
                    response.Message             = error.Message;
                    response.PhysicalPersonCards = new List <PhysicalPersonCardViewModel>();
                    return(response);
                }
                db.Close();
            }
            response.Success             = true;
            response.PhysicalPersonCards = PhysicalPersonCards;
            return(response);
        }
Esempio n. 5
0
        public void Sync(IPhysicalPersonCardService PhysicalPersonCardService, Action <int, int> callback = null)
        {
            try
            {
                SyncPhysicalPersonCardRequest request = new SyncPhysicalPersonCardRequest();
                request.CompanyId     = MainWindow.CurrentCompanyId;
                request.LastUpdatedAt = GetLastUpdatedAt(MainWindow.CurrentCompanyId);

                int toSync      = 0;
                int syncedItems = 0;

                PhysicalPersonCardListResponse response = PhysicalPersonCardService.Sync(request);
                if (response.Success)
                {
                    toSync = response?.PhysicalPersonCards?.Count ?? 0;
                    List <PhysicalPersonCardViewModel> items = response.PhysicalPersonCards;

                    using (SqliteConnection db = new SqliteConnection("Filename=SirmiumERPGFC.db"))
                    {
                        db.Open();
                        using (var transaction = db.BeginTransaction())
                        {
                            SqliteCommand deleteCommand = db.CreateCommand();
                            deleteCommand.CommandText = "DELETE FROM PhysicalPersonCards 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;
            }
        }