public async Task<long> SavePurchase(PurchaseModel purchase)
 {
     long purchaseId = purchase.Id;
     try
     {
         using (var db = dbManager.GetConnection())
         using (var transaction = db.Connection.BeginTransaction())
         using (var command = db.Connection.CreateCommand())
         {
             if (purchaseId == 0)
             {
                 command.CommandText = string.Format(
                 "INSERT INTO PURCHASE(ProductId, Timestamp, TotalCost, ItemCost, ItemsNumber, StoreId) VALUES ({0}, {1}, {2}, {3}, {4}, {5}); SELECT last_insert_rowid() FROM PURCHASE",
                 purchase.ProductId,
                 purchase.Timestamp,
                 purchase.TotalCost.ToString(formatCulture),
                 purchase.ItemCost.ToString(formatCulture),
                 purchase.ItemsNumber.ToString(formatCulture),
                 purchase.StoreId);
                 purchaseId = (long)(await command.ExecuteScalarAsync().ConfigureAwait(false));
                 logger.Debug("Saving new purchase with name={0} and id={1}", purchase.ProductName, purchaseId);
             }
             else
             {
                 command.CommandText = string.Format(
                 "UPDATE PURCHASE SET ProductId = {0}, Timestamp = {1}, TotalCost = {2}, ItemCost = {3}, ItemsNumber = {4}, StoreId = {5} WHERE Id = {6}",
                 purchase.ProductId,
                 purchase.Timestamp,
                 purchase.TotalCost.ToString(formatCulture),
                 purchase.ItemCost.ToString(formatCulture),
                 purchase.ItemsNumber.ToString(formatCulture),
                 purchase.StoreId,
                 purchase.Id);
                 await command.ExecuteNonQueryAsync().ConfigureAwait(false);
                 logger.Debug("Updating existed purchase with name={0} and id={1}", purchase.ProductName, purchaseId);
             }
             
             transaction.Commit();
         }
     }
     catch (Exception ex)
     {
         purchaseId = -1;
         logger.Error("Exception during execution method \"SavePurchase\": {0}", ex.Message);
     }
     return purchaseId;
 }
 private async Task<Purchase> ModelToPurchase(PurchaseModel model)
 {
     logger.Debug("StorageService: converting purchase storage model to purchase");
     if (model == null)
     {
         return null;
     }
     return new Purchase
     {
         Id = (int)model.Id,
         Date = new DateTime(model.Timestamp),
         Name = model.ProductName,
         ItemCost = model.ItemCost,
         TotalCost = model.TotalCost,
         ItemsNumber = model.ItemsNumber,
         PurchaseComment = model.Comment,
         PurchaseRate = model.Rate,
         StoreName = model.StoreName,
         IsMonthly = model.IsMonthly,
         Type = await ResolveProductType(model.TypeId),
         SubType = await ResolveProductSubType(model.SubTypeId)
     };
 }