Exemplo n.º 1
0
        public ConstructionSiteCalculationListResponse Sync(SyncConstructionSiteCalculationRequest request)
        {
            ConstructionSiteCalculationListResponse response = new ConstructionSiteCalculationListResponse();

            try
            {
                response.ConstructionSiteCalculations = new List <ConstructionSiteCalculationViewModel>();

                if (request.LastUpdatedAt != null)
                {
                    response.ConstructionSiteCalculations.AddRange(unitOfWork.GetConstructionSiteCalculationRepository()
                                                                   .GetConstructionSiteCalculationsNewerThen(request.CompanyId, (DateTime)request.LastUpdatedAt)
                                                                   ?.ConvertToConstructionSiteCalculationViewModelList() ?? new List <ConstructionSiteCalculationViewModel>());
                }
                else
                {
                    response.ConstructionSiteCalculations.AddRange(unitOfWork.GetConstructionSiteCalculationRepository()
                                                                   .GetConstructionSiteCalculations(request.CompanyId)
                                                                   ?.ConvertToConstructionSiteCalculationViewModelList() ?? new List <ConstructionSiteCalculationViewModel>());
                }

                unitOfWork.Save();

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

            return(response);
        }
Exemplo n.º 2
0
        public ConstructionSiteCalculationListResponse Sync(SyncConstructionSiteCalculationRequest request)
        {
            ConstructionSiteCalculationListResponse response = new ConstructionSiteCalculationListResponse();

            try
            {
                response = WpfApiHandler.SendToApi <SyncConstructionSiteCalculationRequest, ConstructionSiteCalculationViewModel, ConstructionSiteCalculationListResponse>(request, "Sync");
            }
            catch (Exception ex)
            {
                response.ConstructionSiteCalculations = new List <ConstructionSiteCalculationViewModel>();
                response.Success = false;
                response.Message = ex.Message;
            }

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

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

            return(Json(response, new Newtonsoft.Json.JsonSerializerSettings()
            {
                Formatting = Newtonsoft.Json.Formatting.Indented
            }));
        }
Exemplo n.º 4
0
        //public ConstructionSiteCalculationListResponse GetUnSyncedItems(int companyId)
        //{
        //    ConstructionSiteCalculationListResponse response = new ConstructionSiteCalculationListResponse();
        //    List<ConstructionSiteCalculationViewModel> viewModels = new List<ConstructionSiteCalculationViewModel>();

        //    using (SqliteConnection db = new SqliteConnection("Filename=SirmiumERPGFC.db"))
        //    {
        //        db.Open();
        //        try
        //        {
        //            SqliteCommand selectCommand = new SqliteCommand(
        //                SqlCommandSelectPart +
        //                "FROM  ConstructionSiteCalculations " +
        //                "WHERE CompanyId = @CompanyId AND IsSynced = 0 " +
        //                "ORDER BY Id DESC;", db);
        //            selectCommand.Parameters.AddWithValue("@CompanyId", companyId);

        //            SqliteDataReader query = selectCommand.ExecuteReader();

        //            while (query.Read())
        //            {
        //                int counter = 0;
        //                ConstructionSiteCalculationViewModel dbEntry = new ConstructionSiteCalculationViewModel();
        //                dbEntry.Id = SQLiteHelper.GetInt(query, ref counter);
        //                dbEntry.Identifier = SQLiteHelper.GetGuid(query, ref counter);
        //                dbEntry.ConstructionSite = SQLiteHelper.GetConstructionSite(query, ref counter);
        //                dbEntry.NumOfEmployees = SQLiteHelper.GetInt(query, ref counter);
        //                dbEntry.EmployeePrice = SQLiteHelper.GetDecimal(query, ref counter);
        //                dbEntry.NumOfMonths = SQLiteHelper.GetInt(query, ref counter);
        //                dbEntry.OldValue = SQLiteHelper.GetDecimal(query, ref counter);
        //                dbEntry.NewValue = SQLiteHelper.GetDecimal(query, ref counter);
        //                dbEntry.ValueDifference = SQLiteHelper.GetDecimal(query, ref counter);
        //                dbEntry.PlusMinus = SQLiteHelper.GetString(query, ref counter);
        //                dbEntry.IsSynced = SQLiteHelper.GetBoolean(query, ref counter);
        //                dbEntry.UpdatedAt = SQLiteHelper.GetDateTime(query, ref counter);
        //                dbEntry.CreatedBy = SQLiteHelper.GetCreatedBy(query, ref counter);
        //                dbEntry.Company = SQLiteHelper.GetCompany(query, ref counter);
        //                viewModels.Add(dbEntry);
        //            }

        //        }
        //        catch (SqliteException error)
        //        {
        //            MainWindow.ErrorMessage = error.Message;
        //            response.Success = false;
        //            response.Message = error.Message;
        //            response.ConstructionSiteCalculations = new List<ConstructionSiteCalculationViewModel>();
        //            return response;
        //        }
        //        db.Close();
        //    }
        //    response.Success = true;
        //    response.ConstructionSiteCalculations = viewModels;
        //    return response;
        //}

        #endregion

        #region Sync

        public void Sync(IConstructionSiteCalculationService ConstructionSiteCalculationService, Action <int, int> callback = null)
        {
            try
            {
                SyncConstructionSiteCalculationRequest request = new SyncConstructionSiteCalculationRequest();
                request.CompanyId     = MainWindow.CurrentCompanyId;
                request.LastUpdatedAt = GetLastUpdatedAt(MainWindow.CurrentCompanyId);

                int toSync      = 0;
                int syncedItems = 0;

                ConstructionSiteCalculationListResponse response = ConstructionSiteCalculationService.Sync(request);
                if (response.Success)
                {
                    toSync = response?.ConstructionSiteCalculations?.Count ?? 0;
                    List <ConstructionSiteCalculationViewModel> items = response.ConstructionSiteCalculations;

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