Exemplo n.º 1
0
        public async Task <TResult> UpdateAsync <TResult>(long id, string name, string description, bool isComplete, string username,
                                                          Func <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 = "UPDATE Tasks SET" +
                                "[Name] = @Name," +
                                "[Description] = @Description," +
                                "[IsComplete] = @IsComplete WHERE Id = @Id";


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

                    cmd.Parameters.Add(new SqlParameter("@Id", SqlDbType.BigInt)
                    {
                        Value = id
                    });
                    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.ExecuteNonQueryAsync(); var rows = Convert.ToInt64(rowsItems);
                    try
                    {
                        var oldName        = default(string);
                        var oldDescription = default(string);
                        var oldIsComplete  = default(bool);
                        await FindTaskByIdAsync(id, (s, description1, u) =>
                        {
                            oldName        = s;
                            oldDescription = description1;
                            oldIsComplete  = u;
                            return(true);
                        }, () => true, () => false);

                        await transaction.LogUpdateAsync(entityName, "Name", id, oldName, name, username);

                        await transaction.LogUpdateAsync(entityName, "Description", id, oldDescription, description, username);

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

                    transaction.Commit();

                    return(rows > 0 ? success() : 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());
            }
        }