Example #1
0
        /*  Audit Logs are wrapped in try catch to keep things running if an error when logging occurs  */


        public async Task <TResult> InsertAsync <TResult>(string name, string description, bool isComplete, string username, Func <long, TResult> success, Func <string, TResult> failed, Func <TResult> unAuthorized)
        {
            using (var internalConnection = new SqlConnection(connectionString))
            {
                SqlTransaction transaction = null;

                isComplete = false;

                try
                {
                    await internalConnection.OpenAsync();

                    transaction = internalConnection.BeginTransaction();

                    var query =
                        "INSERT INTO Tasks([Name], [Description], [IsComplete]) VALUES (@Name, @Description, @IsComplete); SELECT SCOPE_IDENTITY()";

                    var cmd = new SqlCommand(string.Empty, internalConnection)
                    {
                        CommandText = query
                    };
                    cmd.Parameters.Clear();
                    cmd.Transaction = transaction;

                    cmd.Parameters.Add(new SqlParameter("@Name", SqlDbType.VarChar)
                    {
                        Value = name
                    });
                    cmd.Parameters.Add(new SqlParameter("@Description", SqlDbType.VarChar)
                    {
                        Value = description
                    });
                    cmd.Parameters.Add(new SqlParameter("@IsComplete", SqlDbType.Bit)
                    {
                        Value = isComplete
                    });

                    var rowsItems = await cmd.ExecuteScalarAsync(); var rows = Convert.ToInt64(rowsItems);

                    try
                    {
                        await transaction.LogInsertAsync(entityName, "Name", rows, name, username);

                        await transaction.LogInsertAsync(entityName, "Description", rows, description, username);

                        await transaction.LogInsertAsync(entityName, "IsComplete", rows, isComplete.ToString(), username);
                    }
                    catch (Exception ex)
                    {
                        _log.Error(ex, ex.Message, ex.StackTrace);
                    }

                    transaction.Commit();

                    return(rows > 0 ? success(rows) : failed("No rows saved. Error occurred"));
                }
                catch (SqlException sqlEx)
                {
                    transaction?.Rollback();
                    _log.Error(sqlEx, sqlEx.Message, sqlEx.StackTrace);
                }
                catch (Exception ex)
                {
                    transaction?.Rollback();
                    _log.Error(ex, ex.Message, ex.StackTrace);
                }
                return(unAuthorized());
            }
        }
Example #2
0
        public async Task <TResult> InsertAsync <TResult>(string name, string description, string projectSponsor,
                                                          string executiveSponsor, string productSponsor, long projectTypeId, bool newPricingRules, long volume,
                                                          decimal revenueAtList, bool dealFormEligible, string newTitles, string newAccounts, string projectDetails,
                                                          string businessCase, string comments, DateTime createdDate, string username,
                                                          Func <long, TResult> success,
                                                          Func <string, TResult> failed,
                                                          Func <TResult> unAuthorized)
        {
            using (var internalConnection = new SqlConnection(connectionString))
            {
                SqlTransaction transaction = null;

                try
                {
                    await internalConnection.OpenAsync();

                    transaction = internalConnection.BeginTransaction();

                    var query = "INSERT INTO Projects([Name],[Description],[ProjectSponsor],[ExecutiveSponsor],[ProductSponsor],[ProjectTypeId],[NewPricingRules]," +
                                "[Volume],[RevenueAtList],[DealFormEligible],[NewTitles],[NewAccounts],[ProjectDetails],[BusinessCase],[Comments],[CreatedDate]) " +
                                "VALUES (@Name, @Description, @ProjectSponsor, @ExecutiveSponsor, @ProductSponsor, @ProjectTypeId, @NewPricingRules, " +
                                "@Volume, @RevenueAtList, @DealFormEligible, @NewTitles, @NewAccounts, @ProjectDetails, @BusinessCase, @Comments, @CreatedDate); " +
                                "SELECT SCOPE_IDENTITY()";

                    var cmd = new SqlCommand(string.Empty, internalConnection)
                    {
                        CommandText = query
                    };
                    cmd.Parameters.Clear();
                    cmd.Transaction = transaction;

                    cmd.Parameters.Add(new SqlParameter("@Name", SqlDbType.VarChar)
                    {
                        Value = name
                    });
                    cmd.Parameters.Add(new SqlParameter("@Description", SqlDbType.VarChar)
                    {
                        Value = description
                    });
                    cmd.Parameters.Add(new SqlParameter("@ProjectSponsor", SqlDbType.VarChar)
                    {
                        Value = projectSponsor
                    });
                    cmd.Parameters.Add(new SqlParameter("@ExecutiveSponsor", SqlDbType.VarChar)
                    {
                        Value = executiveSponsor
                    });
                    cmd.Parameters.Add(new SqlParameter("@ProductSponsor", SqlDbType.VarChar)
                    {
                        Value = productSponsor
                    });
                    cmd.Parameters.Add(new SqlParameter("@ProjectTypeId", SqlDbType.Int)
                    {
                        Value = projectTypeId
                    });
                    cmd.Parameters.Add(new SqlParameter("@NewPricingRules", SqlDbType.Bit)
                    {
                        Value = newPricingRules
                    });
                    cmd.Parameters.Add(new SqlParameter("@Volume", SqlDbType.Int)
                    {
                        Value = volume
                    });
                    cmd.Parameters.Add(new SqlParameter("@RevenueAtList", SqlDbType.Decimal)
                    {
                        Value = revenueAtList
                    });
                    cmd.Parameters.Add(new SqlParameter("@DealFormEligible", SqlDbType.Bit)
                    {
                        Value = dealFormEligible
                    });
                    cmd.Parameters.Add(new SqlParameter("@NewTitles", SqlDbType.VarChar)
                    {
                        Value = newTitles
                    });
                    cmd.Parameters.Add(new SqlParameter("@NewAccounts", SqlDbType.VarChar)
                    {
                        Value = newAccounts
                    });
                    cmd.Parameters.Add(new SqlParameter("@ProjectDetails", SqlDbType.VarChar)
                    {
                        Value = projectDetails
                    });
                    cmd.Parameters.Add(new SqlParameter("@BusinessCase", SqlDbType.VarChar)
                    {
                        Value = businessCase
                    });
                    cmd.Parameters.Add(new SqlParameter("@Comments", SqlDbType.VarChar)
                    {
                        Value = comments
                    });
                    cmd.Parameters.Add(new SqlParameter("@CreatedDate", SqlDbType.DateTime)
                    {
                        Value = DateTime.Now
                    });

                    var rowsItems = await cmd.ExecuteScalarAsync(); var rows = Convert.ToInt64(rowsItems);

                    try
                    {
                        await transaction.LogInsertAsync(entityName, "Name", rows, name, username);

                        await transaction.LogInsertAsync(entityName, "Description", rows, description, username);

                        await transaction.LogInsertAsync(entityName, "ProjectSponsor", rows, projectSponsor, username);

                        await transaction.LogInsertAsync(entityName, "ExecutiveSponsor", rows, executiveSponsor, username);

                        await transaction.LogInsertAsync(entityName, "ProductSponsor", rows, productSponsor, username);

                        await transaction.LogInsertAsync(entityName, "NewPricingRules", rows, newPricingRules.ToString(), username);

                        await transaction.LogInsertAsync(entityName, "Volume", rows, volume.ToString(), username);

                        await transaction.LogInsertAsync(entityName, "RevenueAtList", rows, revenueAtList.ToString(), username);

                        await transaction.LogInsertAsync(entityName, "DealFormEligible", rows, dealFormEligible.ToString(), username);

                        await transaction.LogInsertAsync(entityName, "NewTitles", rows, newTitles, username);

                        await transaction.LogInsertAsync(entityName, "NewAccounts", rows, newAccounts, username);

                        await transaction.LogInsertAsync(entityName, "ProjectDetails", rows, projectDetails, username);

                        await transaction.LogInsertAsync(entityName, "BusinessCase", rows, businessCase, username);

                        await transaction.LogInsertAsync(entityName, "Comments", rows, comments, username);

                        await transaction.LogInsertAsync(entityName, "CreatedDate", rows, createdDate.ToString(), username);
                    }
                    catch (Exception ex)
                    {
                        _log.Error(ex, ex.Message, ex.StackTrace);
                    }

                    transaction.Commit();
                    return(rows > 0 ? success(rows) : failed("No rows saved. Error Occured"));
                }
                catch (SqlException sqlEx)
                {
                    transaction?.Rollback();
                    _log.Error(sqlEx, sqlEx.Message, sqlEx.StackTrace);
                }
                catch (Exception ex)
                {
                    transaction?.Rollback();
                    _log.Error(ex, ex.Message, ex.StackTrace);
                }
                return(unAuthorized());
            }
        }