Exemplo n.º 1
0
        public async Task <ExhibitDTO> LoadExhibitAsync(string hallId, string standId, string id, ExhibitDTO exhibitCached, CancellationToken cancellationToken)
        {
            if (CheckConnection())
            {
                try
                {
                    string content = await LoadAsync(new Uri($"{App.UriBase}/api/Exhibits/{hallId}/{standId}/{id}?hash={exhibitCached.GetHashCode()}"), cancellationToken);

                    if (string.IsNullOrEmpty(content))
                    {
                        return(exhibitCached);
                    }
                    ExhibitDTO exhibit = JsonConvert.DeserializeObject <ExhibitDTO>(content);
                    await DependencyService.Get <CachingService>().WriteExhibitAsync(exhibit);

                    return(exhibit);
                }
                catch (Exception ex)
                {
                    if (ex is HttpRequestException || ex is WebException)
                    {
                        return(exhibitCached);
                    }
                    else if (ex is OperationCanceledException)
                    {
                        throw ex;
                    }
                }
            }
            return(exhibitCached);
        }
Exemplo n.º 2
0
        public async Task <ExhibitDTO> ReadExhibitAsync(string id)
        {
            if (!App.Settings.UseCache)
            {
                return(null);
            }

            string language = Thread.CurrentThread.CurrentUICulture.Name.Substring(0, 2);
            var    keys     = await BlobCache.LocalMachine.GetAllKeys();

            if (keys.Contains($"{language}{id}"))
            {
                _logger.Info($"Read exhibit {language}-{id} from cache");

                // Read exhibit
                ExhibitDTO exhibit = await BlobCache.LocalMachine.GetObject <ExhibitDTO>($"{language}{id}");

                // Read photos description
                exhibit.Photos = await BlobCache.LocalMachine.GetObject <List <PhotoInfoDTO> >($"{language}{id}description");

                // Read photos
                var photosBytes = await BlobCache.LocalMachine.GetObject <List <byte[]> >($"photo{id}");

                int index = 0;
                foreach (var photo in photosBytes)
                {
                    exhibit.Photos[index++].Photo = photo;
                }
                return(exhibit);
            }
            return(null);
        }
Exemplo n.º 3
0
        public async void GetAsync_RecordDoesNotExist_ShouldReturnNull()
        {
            const string id = "111111112111111111111113";

            // Arrange
            ExhibitDTO expected = null;

            // Act
            var actual = await _service.GetAsync(httpRequest, HallId, StandId, id);

            // Assert
            Assert.Equal(expected, actual);
        }
Exemplo n.º 4
0
        public async Task <ExhibitDTO> GetAsync(HttpRequest request, string hallId, string standId, string id)
        {
            // Get language from header
            StringValues language = LanguageConstants.LanguageDefault;

            request?.Headers?.TryGetValue("Accept-Language", out language);

            var exhibit = await _exhibitsRepository.GetAsync(hallId, standId, id);

            MapperConfiguration mapperConfiguration      = null;
            MapperConfiguration mapperConfigurationPhoto = null;

            ExhibitDTO exhibitDTO = null;

            if (exhibit != null)
            {
                // Create mapping depending on language
                switch (language)
                {
                case LanguageConstants.LanguageRu:
                    mapperConfiguration      = ExhibitsMappingConfigurations.GetRuConfiguration;
                    mapperConfigurationPhoto = ExhibitsMappingConfigurations.GetRuPhotoConfiguration;
                    break;

                case LanguageConstants.LanguageEn:
                    mapperConfiguration      = ExhibitsMappingConfigurations.GetEnConfiguration;
                    mapperConfigurationPhoto = ExhibitsMappingConfigurations.GetEnPhotoConfiguration;
                    break;

                case LanguageConstants.LanguageBy:
                    mapperConfiguration      = ExhibitsMappingConfigurations.GetByConfiguration;
                    mapperConfigurationPhoto = ExhibitsMappingConfigurations.GetByPhotoConfiguration;
                    break;

                default:
                    mapperConfiguration      = ExhibitsMappingConfigurations.GetEnConfiguration;
                    mapperConfigurationPhoto = ExhibitsMappingConfigurations.GetEnPhotoConfiguration;
                    break;
                }
                var mapper = new Mapper(mapperConfiguration);
                exhibitDTO = mapper.Map <ExhibitDTO>(exhibit);

                mapper = new Mapper(mapperConfigurationPhoto);
                var photoInfoDTO = mapper.Map <List <PhotoInfoDTO> >(exhibit.Photos);
                exhibitDTO.Photos = photoInfoDTO;
            }
            return(exhibitDTO);
        }
Exemplo n.º 5
0
        public async Task <ExhibitDTO> LoadExhibitAsync(string hallId, string standId, string id, CancellationToken cancellationToken)
        {
            if (CheckConnection())
            {
                string content = await LoadAsync(new Uri($"{App.UriBase}/api/Exhibits/{hallId}/{standId}/{id}"), cancellationToken);

                ExhibitDTO exhibit = JsonConvert.DeserializeObject <ExhibitDTO>(content);
                await DependencyService.Get <CachingService>().WriteExhibitAsync(exhibit);

                return(exhibit);
            }
            throw new Error()
                  {
                      ErrorCode = Errors.Failed_Connection, Info = AppResources.ErrorMessage_LoadingFaild
                  };
        }
Exemplo n.º 6
0
        public async Task WriteExhibitAsync(ExhibitDTO exhibit)
        {
            if (!App.Settings.UseCache)
            {
                return;
            }

            string language = Thread.CurrentThread.CurrentUICulture.Name.Substring(0, 2);

            _logger.Info($"Write exhibit {language}-{exhibit.Id} to cache");

            // Copy photos
            List <PhotoInfoDTO> photos = new List <PhotoInfoDTO>(exhibit.Photos);

            // Copy bytes
            var photosBytes = new List <byte[]>();

            foreach (var photo in exhibit.Photos)
            {
                photosBytes.Add(photo.Photo);
                photo.Photo = null;
            }

            // Save images themselves
            await BlobCache.LocalMachine.InsertObject($"photo{exhibit.Id}", photosBytes);

            // Save images description
            await BlobCache.LocalMachine.InsertObject($"{language}{exhibit.Id}description", exhibit.Photos);

            exhibit.Photos = null;

            // Save exhibit description
            await BlobCache.LocalMachine.InsertObject($"{language}{exhibit.Id}", exhibit);

            for (int i = 0; i < photos.Count; i++)
            {
                photos[i].Photo = photosBytes[i];
            }
            exhibit.Photos = photos;
        }
Exemplo n.º 7
0
        public void WriteExhibitAsync(ExhibitDTO exhibit, Dictionary <string, object> cache, Dictionary <string, object> cachePhotos, string language)
        {
            // Copy bytes
            var photosBytes = new List <byte[]>();

            foreach (var photo in exhibit.Photos)
            {
                photosBytes.Add(photo.Photo);
                photo.Photo = null;
            }

            if (!_isPhotosSaved)
            {
                // Save images themselves
                cachePhotos.Add($"photo{exhibit.Id}", photosBytes);
            }

            // Save images description
            cache.Add($"{language}{exhibit.Id}description", exhibit.Photos);
            exhibit.Photos = null;

            // Save exhibit description
            cache.Add($"{language}{exhibit.Id}", exhibit);
        }
Exemplo n.º 8
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="exhibit">Exhibit to display</param>
 /// <param name="navigation">Instance of navigation</param>
 /// <param name="navigation">Instance of navigation</param>
 public ExhibitGalleryViewModel(ExhibitDTO exhibit, INavigation navigation) : base(navigation)
 {
     Exhibit = exhibit;
     Photos  = new ObservableCollection <PhotoInfoDTO>();
 }
Exemplo n.º 9
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="exhibit">Exhibit to display</param>
 public ExhibitsArticle(ExhibitDTO exhibit)
 {
     InitializeComponent();
     BindingContext = new ExhibitsArticleViewModel(exhibit, Navigation);
 }
Exemplo n.º 10
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="exhibit">Exhibit to display</param>
 public ExhibitGallery(ExhibitDTO exhibit)
 {
     InitializeComponent();
     BindingContext = new ExhibitGalleryViewModel(exhibit, Navigation);
 }
Exemplo n.º 11
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="exhibit">Exhibit to display</param>
 /// <param name="navigation">Instance of navigation</param>
 /// <param name="hallId">Id of the hall</param>
 /// <param name="standId">Id of the stand</param>
 public ExhibitsArticleViewModel(ExhibitDTO exhibit, INavigation navigation) : base(navigation)
 {
     Exhibit = exhibit;
     Photos  = new ObservableCollection <ImageSource>();
 }