예제 #1
0
        public override ActionStatus SaveItem(MatchForRoundModel model, DBActionType action)
        {
            var date = this.Context.Matches.Where(e => e.MatchID == model.ID).Select(e => e.Date).FirstOrDefault();

            //add bet only if round is not expired
            if (IsExpired(date))
            {
                return(new ActionStatus
                {
                    Success = false,
                    Message = "Bets for this match have expired",
                    Result = Extensions.GetBetCutoffDate(date)
                });
            }

            return(this.CallStoredProcedure
                   (
                       action,
                       () =>
            {
                return new SPResult {
                    Result = this.Context.SaveMatchBet(model.ID, this.UserID, model.FirstTeamGoals, model.SecondTeamGoals, model.Bonus, DataConfig.HoursBeforeBet)
                };
            }
                   ));
        }
예제 #2
0
        protected ActionStatus CallStoredProcedure(DBActionType DBActionType, Func <SPResult> save, Action afterSave = null)
        {
            StoredProcResult spResult;
            string           spError = null;

            try
            {
                var dbResult = save.Invoke();
                spError = dbResult.Error;

                if (afterSave != null)
                {
                    afterSave.Invoke();
                }

                spResult = (StoredProcResult)Enum.ToObject(typeof(StoredProcResult), dbResult.Result.Value);
            }
            catch (Exception ex)
            {
                Logger.Log(ex);

                spResult = StoredProcResult.ErrUnknown;
            }

            return(this.GetDBResult(spResult, spError, DBActionType));
        }
예제 #3
0
 public override ActionStatus SaveItem(UserModel model, DBActionType action)
 {
     return(this.CallStoredProcedure
            (
                action,
                () =>
     {
         var result = this.Context.UpdateUser(this.UserID, model.DisplayName, model.Email, model.OldPassword, model.Password);
         return new SPResult {
             Result = result
         };
     }
            ));
 }
예제 #4
0
        public override ActionStatus SaveItem(GoalscorerModel model, DBActionType action)
        {
            //add bet only if round is not expired
            if (new MatchRepository().CurrentRoundIsExpired())
            {
                return(new ActionStatus());
            }

            return(this.CallStoredProcedure
                   (
                       action,
                       () => { return new SPResult {
                                   Result = this.Context.SaveGoalscorerForRoundBet(this.UserID, model.ID)
                               }; }
                   ));
        }
예제 #5
0
        public override ActionStatus SaveItem(TeamModel model, DBActionType action)
        {
            //add bet only if global bets are not expired
            if (new MatchRepository().GlobalBetsExpired())
            {
                return(new ActionStatus
                {
                    Success = false,
                    Message = "Bets on the winner team are not allowed anymore"
                });
            }

            return(this.CallStoredProcedure
                   (
                       action,
                       () => { return new SPResult {
                                   Result = this.Context.SaveWinnerBet(this.UserID, model.ID)
                               }; }
                   ));
        }
예제 #6
0
 /// <summary>
 /// Function to be called after the item has been saved (edited or newly added)
 /// </summary>
 /// <param name="actionType"></param>
 /// <param name="model"></param>
 protected virtual Dictionary <string, object> AfterSave(DBActionType actionType, M model)
 {
     return(null);
 }
예제 #7
0
 public override ActionStatus SaveItem(GoalscorerModel model, DBActionType action)
 {
     return(new GoalscorerRepository(this.Context, this.UserID).SaveItem(model, action));
 }
예제 #8
0
 public virtual ActionStatus SaveItem(M model, DBActionType action)
 {
     throw new NotImplementedException();
 }
예제 #9
0
        protected ActionStatus GetDBResult(StoredProcResult spResult, string spError, DBActionType DBActionType)
        {
            var    dbResult = new ActionStatus();
            string action   = DBActionType.ToString().ToLower();

            switch (spResult)
            {
            case StoredProcResult.ErrChanged:   dbResult.Message = "Failed to " + action + " because records have been changed since last view. Please reload the page."; break;

            case StoredProcResult.ErrNoRecord:   dbResult.Message = "Failed to " + action + " because record doesn't exist."; break;

            case StoredProcResult.ErrRecExist:   dbResult.Message = "Failed to " + action + " because the record already exists."; break;

            case StoredProcResult.ErrRecHasLink:   dbResult.Message = "Failed to " + action + " because there are records that depend on this one."; break;

            case StoredProcResult.ErrUnknown:       dbResult.Message = "Failed to " + action + "."; break;

            case StoredProcResult.InvalidPassword:       dbResult.Message = "Failed to " + action + " because the password is invalid!"; break;

            case StoredProcResult.Success:       dbResult.Message = "The data was saved successfully!"; dbResult.Success = true; break;

            default:   dbResult.Message = "Failed to " + action + "."; break;
            }

            if (!String.IsNullOrWhiteSpace(spError))
            {
                dbResult.Message += " " + spError;
            }

            return(dbResult);
        }
예제 #10
0
        private static Int64? DBExecuteNonQuery(IDBClass _IDBClass, DBActionType _DBActionType)
        {
            if (_DBActionType != DBActionType.Insert)
                throw new BPAExtensionException("DBActionType Not Found!");

            DbCommand DbComm = GETDBCommand(DBCore.SQLDBType);

            String PrimaryKey = GETDBPrimaryKey(_IDBClass);
            String Table = GETDBName(_IDBClass);

            Int64? ID = null;

            if (!DBCore.UseAutoIncrement)
                ID = GETDBLastID(PrimaryKey, Table);

            StringBuilder sbName = new StringBuilder();
            StringBuilder sbValue = new StringBuilder();

            Parameters ps = new Parameters();

            var props = (from t in _IDBClass.GetType().GetProperties()
                         where t.GetValue(_IDBClass, null) != null
                         && t.Name != "PrimaryKey"
                         && t.Name != "SQLSelect"
                         && t.Name != "SQLWhere"
                         && t.Name != "SQLOrderBy"
                         select new
                         {
                             Name = t.Name,
                             Value = t.GetValue(_IDBClass, null),
                             DbType = GETDBType((t.GetValue(_IDBClass, null)).GetType().Name)
                         });

            foreach (var prop in props)
            {
                sbName.Append(String.Format("{0}, ", prop.Name));
                sbValue.Append(String.Format("#{0}, ", prop.Name));

                ps.Add(new Parameter(String.Format("#{0}", prop.Name), prop.Value, prop.DbType));
            }

            if (!DBCore.UseAutoIncrement)
            {
                sbName.Append(String.Format("{0}, ", PrimaryKey));
                sbValue.Append(String.Format("#{0}, ", PrimaryKey));

                ps.Add(new Parameter(String.Format("#{0}", PrimaryKey), ID, DbType.Int32));
            }

            DbComm.CommandText = String.Format(
                "INSERT INTO {0} ({1}) VALUES ({2})",
                Table,
                new StringBuilder(sbName.Remove(sbName.ToString().LastIndexOf(','), 2).ToString()),
                new StringBuilder(sbValue.Remove(sbValue.ToString().LastIndexOf(','), 2).ToString())
            );

            GETDBParams(DbComm, ps);

            Int64? _ID = DBSync(DbComm, DBActionType.Insert);

            if (DBCore.UseAutoIncrement)
                ID = _ID;

            return ID;
        }
예제 #11
0
        private static void ExecuteNonQuery(IDBClass _IDBClass, DBActionType _DBActionType)
        {
            DbCommand DbComm = GETDBCommand(DBCore.SQLDBType);

            String SQLQuery = String.Empty;
            String Table = GETDBName(_IDBClass);
            String PrimaryKey = GETDBPrimaryKey(_IDBClass);
            String PrimaryKeyValue = String.Empty;
            DbType PrimaryKeyType = DbType.Object;

            Parameters ps = new Parameters();

            switch (_DBActionType)
            {
                case DBActionType.Update:
                    StringBuilder sb = new StringBuilder();

                    var propsUpdate = (from t in _IDBClass.GetType().GetProperties()
                                       where t.GetValue(_IDBClass, null) != null
                                       && t.Name != "PrimaryKey"
                                       && t.Name != "SQLSelect"
                                       && t.Name != "SQLWhere"
                                       && t.Name != "SQLOrderBy"
                                       select new
                                       {
                                           Name = t.Name,
                                           Value = t.GetValue(_IDBClass, null),
                                           DbType = GETDBType((t.GetValue(_IDBClass, null)).GetType().Name)
                                       });

                    foreach (var prop in propsUpdate)
                    {
                        if (prop.Name != PrimaryKey)
                        {
                            sb.Append(String.Format("{0} = #{1}, ", prop.Name, prop.Name));

                            ps.Add(new Parameter(String.Format("#{0}", prop.Name), prop.Value, prop.DbType));
                        }
                        else
                        {
                            PrimaryKeyType = prop.DbType;
                            PrimaryKeyValue = prop.Value.ToString();
                        }
                    }

                    sb = new StringBuilder(sb.Remove(sb.ToString().LastIndexOf(','), 2).ToString());

                    SQLQuery = String.Format("UPDATE {0} SET {1} WHERE {2} = #{3}", Table, sb, PrimaryKey, PrimaryKey);

                    ps.Add(new Parameter(String.Format("#{0}", PrimaryKey), PrimaryKeyValue, DbType.Int32));
                    break;

                case DBActionType.Delete:
                    var propsDelete = (from t in _IDBClass.GetType().GetProperties()
                                       where t.Name == GETDBPrimaryKey(_IDBClass)
                                       select new
                                       {
                                           Name = t.Name,
                                           Value = t.GetValue(_IDBClass, null),
                                           DbType = GETDBType((t.GetValue(_IDBClass, null)).GetType().Name)
                                       });

                    foreach (var prop in propsDelete)
                    {
                        if (prop.Name == PrimaryKey)
                        {
                            PrimaryKeyType = prop.DbType;
                            PrimaryKeyValue = prop.Value.ToString();
                        }
                    }

                    SQLQuery = String.Format("DELETE FROM {0} WHERE {1} = #{2}", Table, PrimaryKey, PrimaryKey);

                    ps.Add(new Parameter(String.Format("#{0}", PrimaryKey), PrimaryKeyValue, DbType.Int32));
                    break;

                case DBActionType.Select:
                case DBActionType.Insert:
                default:
                    throw new BPAExtensionException("DBActionType Not Found!");
            }

            DbComm.CommandText = SQLQuery.ToString();

            GETDBParams(DbComm, ps);

            DBSync(DbComm, null);
        }
예제 #12
0
        private static Int64? DBSync(DbCommand DbComm, DBActionType? _DBActionType)
        {
            Int64? ID = null;

            DbTransaction DbTrans = null;

            try
            {
                DbComm.Connection.Open();

                DbTrans = DbComm.Connection.BeginTransaction();

                DbComm.Transaction = DbTrans;
                DbComm.ExecuteNonQuery();

                if (_DBActionType == DBActionType.Insert && DBCore.UseAutoIncrement)
                {
                    DbComm.CommandText = String.Format(
                        "SELECT MAX(ID_{0}) MAXID FROM {0}",
                        DbComm.CommandText.Substring(0, DbComm.CommandText.IndexOf('(')).ToUpper().Replace("INSERT INTO", "").Trim()
                    ).ToLower();

                    using (DbDataReader dr = DbComm.ExecuteReader())
                        while (dr.Read())
                            ID = dr[0].ToInt64();
                }

                DbTrans.Commit();
            }
            catch (Exception)
            {
                if (DbComm.Connection.State == ConnectionState.Open)
                    DbTrans.Rollback();

                throw;
            }
            finally
            {
                if (DbComm.Connection.State == ConnectionState.Open)
                    DbComm.Connection.Close();
            }

            return ID;
        }
예제 #13
0
        private static void DBSync(DbCommand DbComm, String SQLQuery, DBActionType? _DBActionType)
        {
            DbComm.CommandText = SQLQuery;

            DBSync(DbComm, _DBActionType);
        }
예제 #14
0
 protected override Dictionary <string, object> AfterSave(DBActionType actionType, MatchForRoundModel model)
 {
     return(this.GetUserData());
 }