Ejemplo n.º 1
0
        /// <summary>
        /// Delete product by id.
        /// </summary>
        /// <returns></returns>
        public async Task DeleteAsync(string id)
        {
            IDbTransaction transaction = null;

            try
            {
                using (var conn = await _dbConnectionStrategy.CreateConnectionAsync(Models.DatabaseType.Sqlite))
                {
                    transaction = conn.BeginTransaction();

                    var query = @"
                                    delete
                                    from ProductOptions
                                    where ProductId = @ProductId collate nocase
                                ";

                    await conn.ExecuteAsync(query,
                                            new
                    {
                        ProductId = id
                    }, transaction);

                    query = @"
                                delete
                                from Products
                                where Id = @ProductId collate nocase
                            ";
                    await conn.ExecuteAsync(query, new
                    {
                        ProductId = id
                    }, transaction);

                    transaction.Commit();
                }
            }
            catch (Exception ex)
            {
                _logger.LogError($"An error occurred when trying to delete a product id {id}. Exception: {ex}");
                _logger.LogInformation($"Roll back is being invoked...");

                try
                {
                    transaction?.Rollback();
                    _logger.LogInformation($"Roll back is complete.");
                }
                catch (SqliteException sqlEx)
                {
                    if (transaction.Connection != null)
                    {
                        _logger.LogError($"Failed to roll back. Exception: {sqlEx}");
                    }
                }
                throw ex;
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task DeleteAsync(string id)
        {
            try
            {
                using (var conn = await _dbConnectionStrategy.CreateConnectionAsync(DatabaseType.Sqlite))
                {
                    var query = @"
                                    delete
                                    from ProductOptions
                                    where Id = @Id collate nocase
                                ";

                    await conn.ExecuteAsync(query, new { Id = id.ToString() });
                }
            }
            catch (Exception ex)
            {
                _logger.LogError($"An error occurred when trying to delete the product option id {id}. Exception: {ex}");
                throw ex;
            }
        }