Exemplo n.º 1
0
        private Rate(Datamodel.StruckRate sr)
        {
            rateId   = sr.rate;
            struckId = sr.id;
            rank     = sr.rank;

            Datamodel.Rate r = null;
            if (!Datamodel.Rate.FetchById(ref r, rateId))
            {
                throw new ArgumentException(
                          "Struck rate does not have a valid rate id");
            }

            name   = r.name;
            abbrev = r.abrv;

            earnedDate =
                DateTimeOffset.FromUnixTimeSeconds(sr.earnedDate).DateTime;
            if (sr.expirationDate != -1)
            {
                hasExpirationDate = true;
                expirationDate    = DateTimeOffset.FromUnixTimeSeconds(
                    sr.expirationDate).DateTime;
            }
            else
            {
                hasExpirationDate = false;
            }
        }
Exemplo n.º 2
0
 /// <summary>
 /// Gets a rate struck by a user
 /// </summary>
 /// <param name="userId"></param>
 /// <param name="rateId">The rate ID, NOT the struck ID</param>
 /// <returns></returns>
 public static Rate FetchUsersRate(int userId, int rateId)
 {
     Datamodel.StruckRate sr = null;
     if (Datamodel.StruckRate.FetchByUserStruckId(ref sr, userId, rateId))
     {
         return(new Rate(sr));
     }
     else
     {
         return(Rate.UNDESIGNATED);
     }
 }
Exemplo n.º 3
0
        public static StruckRate Factory(SQLiteDataReader reader)
        {
            StruckRate result = new StruckRate(
                id: Convert.ToInt32(reader["id"]),
                user: Convert.ToInt32(reader["user"]),
                rate: Convert.ToInt32(reader["rate"]),
                rank: Convert.ToInt32(reader["rank"]),
                earned: Convert.ToInt64(reader["earned"]),
                expires: Convert.ToInt64(reader["expires"])
                );

            return(result);
        }
Exemplo n.º 4
0
        public static StruckRate Factory()
        {
            StruckRate result = new StruckRate(
                id: -1,
                user: -1,
                rate: -1,
                rank: 0,
                earned: -1,
                expires: -1
                );

            return(result);
        }
Exemplo n.º 5
0
        public static StruckRate Factory(int id, int user, int rate, int rank,
                                         long earned, bool hasExp, long expires)
        {
            StruckRate result = new StruckRate(
                id: id,
                user: user,
                rate: rate,
                rank: rank,
                earned: earned,
                expires: expires
                );

            return(result);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Gets a rating by ID
        /// </summary>
        /// <param name="output"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public static bool FetchById(ref StruckRate output, int id)
        {
            SQLiteDataReader reader = DBI.DoPreparedQuery(
                @"SELECT id, user, rate, rank, earned, 
				COALESCE(expires, -1) AS expires 
				FROM StruckRate 
				WHERE id = @id LIMIT 1;"                ,
                new Tuple <string, object>("@id", id));

            if (reader != null && reader.Read())
            {
                output = StruckRate.Factory(reader);
                return(true);
            }
            return(false);
        }
Exemplo n.º 7
0
        ProcessAddRate(ANWI.Messaging.IMessagePayload p)
        {
            ANWI.Messaging.AddRate ar = p as ANWI.Messaging.AddRate;

            bool success = false;

            Datamodel.StruckRate sr = null;
            if (Datamodel.StruckRate.FetchByUserRateId(
                    ref sr, ar.userId, ar.rateId))
            {
                sr.rank = ar.rank;
                if (Datamodel.StruckRate.Store(sr))
                {
                    logger.Info($"Updated rate {ar.rateId} to rank {ar.rank}" +
                                $" to user {ar.userId}");
                    success = true;
                }
                else
                {
                    logger.Error($"Failed to update rate {ar.rateId} for user" +
                                 $" {ar.userId}");
                }
            }
            else
            {
                if (Datamodel.StruckRate.Create(
                        ref sr, ar.userId, ar.rateId, ar.rank))
                {
                    logger.Info($"Added rate {ar.rateId} at rank {ar.rank} " +
                                $"to user {ar.userId}");
                    success = true;
                }
                else
                {
                    logger.Error($"Failed to add rate {ar.rateId} to " +
                                 $"user {ar.userId}");
                }
            }

            if (ar.userId == GetUser().profile.id)
            {
                GetUser().RefreshProfile();
            }

            return(new ANWI.Messaging.ConfirmUpdate(p, success, ar.userId));
        }
Exemplo n.º 8
0
        /// <summary>
        /// Gets a user's primary rate based on their id and rate id
        /// </summary>
        /// <param name="output"></param>
        /// <param name="uid"></param>
        /// <param name="rid"></param>
        /// <returns></returns>
        public static bool FetchByUserRateId(ref StruckRate output, int uid,
                                             int rid)
        {
            SQLiteDataReader reader = DBI.DoPreparedQuery(
                @"SELECT id, user, rate, rank, earned, 
				COALESCE(expires, -1) AS expires 
				FROM StruckRate 
				WHERE user = @user AND rate = @rate;"                ,
                new Tuple <string, object>("@user", uid),
                new Tuple <string, object>("@rate", rid));

            if (reader != null && reader.Read())
            {
                output = StruckRate.Factory(reader);
                return(true);
            }
            return(false);
        }
Exemplo n.º 9
0
        /// <summary>
        /// Updated a rate strike.  Automatically moves the earned time up to
        /// today and moves the expiration date accordingly.
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static bool Store(StruckRate input)
        {
            string expQuery = getExpirationQuery(input.rank);
            int    result   = DBI.DoPreparedAction(
                $@"UPDATE StruckRate SET user = @user, 
				rate = @rate, rank = @rank, 
				earned = strftime('%s', 'now'), expires = {expQuery} 
				WHERE id = @id;"                ,
                new Tuple <string, object>("@user", input.user),
                new Tuple <string, object>("@rate", input.rate),
                new Tuple <string, object>("@rank", input.rank),
                new Tuple <string, object>("@id", input.id));

            if (result == 1)
            {
                return(true);
            }
            return(false);
        }
Exemplo n.º 10
0
        /// <summary>
        /// Fetches a rating by ID
        /// </summary>
        /// <param name="output"></param>
        /// <param name="rate"></param>
        /// <returns></returns>
        public static bool FetchByRateId(ref List <StruckRate> output,
                                         int rate)
        {
            output = new List <StruckRate>();

            SQLiteDataReader reader = DBI.DoPreparedQuery(
                @"SELECT id, user, rate, rank, earned, 
				COALESCE(expires, -1) AS expires 
				FROM StruckRate 
				WHERE id = @id;"                ,
                new Tuple <string, object>("@id", rate));

            while (reader != null && reader.Read())
            {
                output.Add(StruckRate.Factory(reader));
            }

            return(true);
        }
Exemplo n.º 11
0
        /// <summary>
        /// Creates a new rate.  Automatically sets the earned date to today.
        /// Calculates the expiration date if necessary.
        /// </summary>
        /// <param name="output"></param>
        /// <param name="user"></param>
        /// <param name="rate"></param>
        /// <param name="rank"></param>
        /// <returns></returns>
        public static bool Create(ref StruckRate output, int user, int rate,
                                  int rank)
        {
            string expQuery = getExpirationQuery(rank);
            int    result   = DBI.DoPreparedAction(
                $@"INSERT INTO StruckRate (id, user, rate, 
				rank, earned, expires) 
				VALUES (COALESCE((SELECT max(id) FROM StruckRate),0) + 1, @user, @rate, 
				@rank, strftime('%s', 'now'), {expQuery});"                ,
                new Tuple <string, object>("@user", user),
                new Tuple <string, object>("@rate", rate),
                new Tuple <string, object>("@rank", rank));

            if (result == 1)
            {
                return(StruckRate.FetchById(ref output, DBI.LastInsertRowId));
            }
            return(false);
        }