Esempio n. 1
0
        public Int32 UpdateUserLastActiveDate(Guid userId, DateTimeOffset lastActiveDate)
        {
            s_log.LogInfo($@"Data provider's method '{nameof(UpdateUserLastActiveDate)}({userId}, {lastActiveDate})' is called");

            #region SQL

            var sql = @"
UPDATE users

SET LastActiveDate = (CASE 
        WHEN LastActiveDate < @LastActiveDate 
        THEN @LastActiveDate 
        ELSE LastActiveDate 
    END) 
    WHERE Id = @UserId
";

            #endregion

            using (IDbConnection connection = new SqlConnection(_dbSettings.DbConnectionString))
            {
                using (CDbTransactionQueryExecutor executor = new CDbTransactionQueryExecutor(connection))
                {
                    try
                    {
                        var result = executor.UpdateItem(sql,
                                                         SSqlParameterCreator.Create(
                                                             "@UserId", userId, System.Data.SqlDbType.UniqueIdentifier, false
                                                             ),
                                                         SSqlParameterCreator.Create(
                                                             "@LastActiveDate", lastActiveDate, SqlDbType.DateTimeOffset, true
                                                             )
                                                         );

                        executor.Commit();
                        return(result);
                    }
                    catch (SqlException e)
                    {
                        s_log.LogError($@"{nameof(UpdateUserLastActiveDate)}({userId}, {lastActiveDate}): Error occured during SQL query execution", e);
                        s_log.LogInfo($@"{nameof(UpdateUserLastActiveDate)}({userId}, {lastActiveDate}): Operation was rolled back because of error");
                        Console.WriteLine($@"{nameof(UpdateUserLastActiveDate)}({userId}, {lastActiveDate}): Error occured during SQL query execution");
                        Console.WriteLine("Operation was rolled back because of error");
                        return(0);
                    }
                }
            }
        }
Esempio n. 2
0
        public Int32 UpdateUserStatus(Guid userId, Int32 currentStatus)
        {
            s_log.LogInfo($@"Data provider's method '{nameof(UpdateUserStatus)}({userId}, {currentStatus})' is called");

            #region Sql

            var sql = @"
UPDATE users
SET ActivityStatus = @CurrentStatus
    WHERE Id = @UserId
";

            #endregion

            using (IDbConnection connection = new SqlConnection(_dbSettings.DbConnectionString))
            {
                using (CDbTransactionQueryExecutor executor = new CDbTransactionQueryExecutor(connection))
                {
                    try
                    {
                        var result = executor.UpdateItem(sql,
                                                         SSqlParameterCreator.Create(
                                                             "@UserId", userId, System.Data.SqlDbType.UniqueIdentifier, false
                                                             ),
                                                         SSqlParameterCreator.Create(
                                                             "@CurrentStatus", (Byte)currentStatus, SqlDbType.TinyInt, false
                                                             )
                                                         );

                        executor.Commit();
                        return(result);
                    }
                    catch (SqlException e)
                    {
                        s_log.LogError($@"{nameof(UpdateUserStatus)}({userId}, {currentStatus}): Error occured during SQL query execution", e);
                        s_log.LogInfo($@"{nameof(UpdateUserStatus)}({userId}, {currentStatus}): Operation was rolled back because of error");
                        Console.WriteLine($@"{nameof(UpdateUserStatus)}({userId}, {currentStatus}): Error occured during SQL query execution");
                        Console.WriteLine("Operation was rolled back because of error");
                        return(0);
                    }
                }
            }
        }