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

            try
            {
                response.Cities = new List <CityViewModel>();

                if (request.LastUpdatedAt != null)
                {
                    response.Cities.AddRange(unitOfWork.GetCityRepository()
                                             .GetCitiesNewerThen(request.CompanyId, (DateTime)request.LastUpdatedAt)
                                             ?.ConvertToCityViewModelList() ?? new List <CityViewModel>());
                }
                else
                {
                    response.Cities.AddRange(unitOfWork.GetCityRepository()
                                             .GetCities(request.CompanyId)
                                             ?.ConvertToCityViewModelList() ?? new List <CityViewModel>());
                }

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

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

            try
            {
                response.Cities = new List <CityViewModel>();

                if (request.LastUpdatedAt != null)
                {
                    response.Cities.AddRange(unitOfWork.GetCityRepository()
                                             .GetCitiesNewerThen((DateTime)request.LastUpdatedAt)
                                             ?.ConvertToCityViewModelList() ?? new List <CityViewModel>());
                }
                else
                {
                    response.Cities.AddRange(unitOfWork.GetCityRepository()
                                             .GetCities()
                                             ?.ConvertToCityViewModelList() ?? new List <CityViewModel>());
                }

                List <City> addedCities = new List <City>();
                foreach (var City in request.UnSyncedCities)
                {
                    addedCities.Add(unitOfWork.GetCityRepository().Create(City.ConvertToCity()));
                }

                unitOfWork.Save();

                foreach (var item in addedCities)
                {
                    response.Cities.Add(unitOfWork.GetCityRepository()
                                        .GetCity(item.Id)
                                        .ConvertToCityViewModel());
                }

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

            return(response);
        }
        public CityListResponse Sync(SyncCityRequest request)
        {
            CityListResponse response = new CityListResponse();

            try
            {
                response = WpfApiHandler.SendToApi <SyncCityRequest, CityViewModel, CityListResponse>(request, "Sync");
            }
            catch (Exception ex)
            {
                response.Cities  = new List <CityViewModel>();
                response.Success = false;
                response.Message = ex.Message;
            }

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

            try
            {
                response = this.cityService.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 void Sync(ICityService cityService, Action <int, int> callback = null)
        {
            try
            {
                SyncCityRequest request = new SyncCityRequest();
                request.CompanyId     = MainWindow.CurrentCompanyId;
                request.LastUpdatedAt = GetLastUpdatedAt(MainWindow.CurrentCompanyId);

                int toSync      = 0;
                int syncedItems = 0;

                CityListResponse response = cityService.Sync(request);
                if (response.Success)
                {
                    toSync = response?.Cities?.Count ?? 0;
                    List <CityViewModel> citiesFromDB = response.Cities;

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

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

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

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

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