public void AddProductToList(string productName) { var key = PrefixConstants.GetProductsListKey(); byte[] bytesKey = Encoding.UTF8.GetBytes(key); try { byte[] value = _database.Read(bytesKey); List <string> currentList = value == null ? new List <string>() : JsonSerializer.Deserialize <List <string> >(Encoding.UTF8.GetString(value)); if (!currentList.Contains(productName)) { currentList.Add(productName); } string stringData = JsonSerializer.Serialize(currentList); byte[] bytesValue = Encoding.UTF8.GetBytes(stringData); _database.Put(bytesKey, bytesValue); } catch (Exception e) { _logger.Error(e, "Failed to add prodct to list"); } }
public List <string> GetProductsList() { string listKey = PrefixConstants.GetProductsListKey(); byte[] bytesKey = Encoding.UTF8.GetBytes(listKey); List <string> result = new List <string>(); try { bool isRead = _database.TryRead(bytesKey, out byte[] value); if (!isRead) { throw new ServerDatabaseException("Failed to read products list!"); } List <string> products = JsonSerializer.Deserialize <List <string> >(Encoding.UTF8.GetString(value)); result.AddRange(products); } catch (Exception e) { _logger.Error(e, "Failed to get products list"); } return(result); }
public void RemoveProductFromList(string productName) { var key = PrefixConstants.GetProductsListKey(); byte[] bytesKey = Encoding.UTF8.GetBytes(key); try { bool result = _database.TryRead(bytesKey, out byte[] value); if (!result) { throw new ServerDatabaseException("Failed to read products list!"); } string stringValue = Encoding.UTF8.GetString(value); List <string> currentList = string.IsNullOrEmpty(stringValue) ? new List <string>() : JsonSerializer.Deserialize <List <string> >(stringValue); currentList.Remove(productName); string stringData = JsonSerializer.Serialize(currentList); byte[] bytesValue = Encoding.UTF8.GetBytes(stringData); _database.Put(bytesKey, bytesValue); } catch (Exception e) { _logger.Error(e, "Failed to add prodct to list"); } }