public IActionResult CreateStand([FromBody] StandDTO stand) { try { if (stand == null) { logger.LogError("Stand object sent from client is null"); return(BadRequest("Stand object is null")); } if (!ModelState.IsValid) { logger.LogError("Invalid stand object sent from client"); return(BadRequest("Invalid stand object")); } var standEntity = mapper.Map <Stand>(stand); repositoryWrapper.Stand.CreateStand(standEntity); repositoryWrapper.Save(); var createdStand = mapper.Map <StandDTO>(standEntity); return(CreatedAtAction("CreateStand", createdStand)); } catch (Exception ex) { logger.LogError($"Something went wrong inside CreateStand action: {ex.Message}"); return(StatusCode(500, "Internal Server Error")); } }
public async Task <StandDTO> ReadStandAsync(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 stand {language}-{id} from cache"); // Read stand StandDTO stand = await BlobCache.LocalMachine.GetObject <StandDTO>($"{language}{id}"); // Read photo var photosBytes = await BlobCache.LocalMachine.GetObject <List <byte[]> >($"photo{id}"); for (int i = 0; i < photosBytes.Count; i++) { if (stand.Exhibits[i].Photos != null) { stand.Exhibits[i].Photos[0].Photo = photosBytes[i]; } } return(stand); } return(null); }
public void WriteStandAsync(StandDTO stand, Dictionary <string, object> cache, Dictionary <string, object> cachePhotos, string language) { // Save photos List <byte[]> photosBytes = new List <byte[]>(); foreach (var exhibit in stand.Exhibits) { if (exhibit.Photos?.Count == 0 || exhibit.Photos == null) { photosBytes.Add(null); } else { photosBytes.Add(exhibit.Photos[0]?.Photo); exhibit.Photos[0].Photo = null; } } if (!_isPhotosSaved) { // Write photos cachePhotos.Add($"photo{stand.Id}", photosBytes); } // Write text cache.Add($"{language}{stand.Id}", stand); }
public async Task <StandDTO> LoadStandAsync(string hallId, string id, StandDTO standCached, CancellationToken cancellationToken) { if (CheckConnection()) { try { string content = await LoadAsync(new Uri($"{App.UriBase}/api/Stands/{hallId}/{id}?hash={standCached.GetHashCode()}"), cancellationToken); if (string.IsNullOrEmpty(content)) { return(standCached); } StandDTO stand = JsonConvert.DeserializeObject <StandDTO>(content); await DependencyService.Get <CachingService>().WriteStandAsync(stand); return(stand); } catch (Exception ex) { if (ex is HttpRequestException || ex is WebException) { return(standCached); } else if (ex is OperationCanceledException) { throw ex; } } } return(standCached); }
public async void GetAsync_RecordDoesNotExist_ShouldReturnNull() { const string id = "111111112111111111111113"; // Arrange StandDTO expected = null; // Act var actual = await _service.GetAsync(httpRequest, HallId, id); // Assert Assert.Equal(expected, actual); }
public async Task <StandDTO> LoadStandAsync(string hallId, string id, CancellationToken cancellationToken) { if (CheckConnection()) { string content = await LoadAsync(new Uri($"{App.UriBase}/api/Stands/{hallId}/{id}"), cancellationToken); StandDTO stand = JsonConvert.DeserializeObject <StandDTO>(content); await DependencyService.Get <CachingService>().WriteStandAsync(stand); return(stand); } throw new Error() { ErrorCode = Errors.Failed_Connection, Info = AppResources.ErrorMessage_LoadingFaild }; }
public async Task <StandDTO> GetAsync(HttpRequest request, string hallId, string id) { // Get language from header StringValues language = LanguageConstants.LanguageDefault; request?.Headers?.TryGetValue("Accept-Language", out language); var stand = await _standsRepository.GetAsync(hallId, id); MapperConfiguration mapperConfiguration = null; StandDTO standDTO = null; if (stand != null) { // Create mapping depending on language switch (language) { case LanguageConstants.LanguageRu: mapperConfiguration = StandsMappingConfigurations.GetRuConfiguration; break; case LanguageConstants.LanguageEn: mapperConfiguration = StandsMappingConfigurations.GetEnConfiguration; break; case LanguageConstants.LanguageBy: mapperConfiguration = StandsMappingConfigurations.GetByConfiguration; break; default: mapperConfiguration = StandsMappingConfigurations.GetEnConfiguration; break; } var mapper = new Mapper(mapperConfiguration); standDTO = mapper.Map <StandDTO>(stand); var exhibits = await _exhibitsService.GetAllAsync(request, hallId, id); standDTO.Exhibits = exhibits; } return(standDTO); }
public async Task WriteStandAsync(StandDTO stand) { if (!App.Settings.UseCache) { return; } string language = Thread.CurrentThread.CurrentUICulture.Name.Substring(0, 2); _logger.Info($"Write stand {language}-{stand.Id} to cache"); // Save photos List <byte[]> photosBytes = new List <byte[]>(); foreach (var exhibit in stand.Exhibits) { if (exhibit.Photos?.Count == 0 || exhibit.Photos == null) { photosBytes.Add(null); } else { photosBytes.Add(exhibit.Photos[0]?.Photo); exhibit.Photos[0].Photo = null; } } // Write photos await BlobCache.LocalMachine.InsertObject($"photo{stand.Id}", photosBytes); // Write text await BlobCache.LocalMachine.InsertObject($"{language}{stand.Id}", stand); for (int i = 0; i < photosBytes.Count; i++) { if (stand.Exhibits[i].Photos != null) { stand.Exhibits[i].Photos[0].Photo = photosBytes[i]; } } }