public int CreateHero(Hero hero, IEnumerable<IPostedFile> photos)
 {
     var heroId = 0;
     using (var tran = new DbTransactionManager())
     {
         tran.BeginTransaction();
         using (var db = new DataAccess(tran))
         {
             db.CreateStoredProcCommand("dbo.CreateHero");
             db.AddInputParameter("@FirstName", DbType.String, hero.FirstName);
             db.AddInputParameter("@LastName", DbType.String, hero.LastName);
             db.AddInputParameter("@MiddleName", DbType.String, hero.MiddleName);
             db.AddInputParameter("@Dob", DbType.DateTime, hero.Dob);
             db.AddInputParameter("@Died", DbType.DateTime, hero.Died);
             db.AddInputParameter("@MilitaryGroupId", DbType.Int32, hero.MilitaryGroupId);
             db.AddInputParameter("@MilitaryRankId", DbType.Int32, hero.MilitaryRankId);
             db.AddInputParameter("@MilitaryAwardId", DbType.Int32, hero.MilitaryAwardId);
             db.AddInputParameter("@Biography", DbType.String, hero.Biography);
             db.AddInputParameter("@LanguageId", DbType.Int32, hero.LanguageId);
             db.AddInputParameter("@UpdatedBy", DbType.Int32, hero.UpdatedBy);
             db.AddInputParameter("@Photos", DbType.Xml, photos.ToDbXml());
             db.AddOutputParameter("@HeroId", DbType.Int32);
             try
             {
                 db.ExecuteNonQuery();
                 heroId = db.GetParameterValue<int>("@HeroId");
                 SaveBlobs(heroId, photos);
                 tran.Commit();
             }
             catch (SqlException ex)
             {
                 tran.Rollback();
                 Logger.Error("Error while creating hero", ex);
                 throw new RepositoryException("Oops! Something went wrong... :(", ex);
             }
             catch (Exception ex)
             {
                 tran.Rollback();
                 Logger.Error("Error while creating hero", ex);
                 throw new RepositoryException("Oops! Something went wrong... :(", ex);
             }
             return heroId;
         }
     }
 }
        public User CreateUser(User user)
        {
            using (var tran = new DbTransactionManager())
            {
                tran.BeginTransaction();
                using (var db = new DataAccess(tran))
                {
                    try
                    {
                        db.CreateStoredProcCommand("dbo.CreateUser");
                        db.AddInputParameter("@Email", DbType.String, user.Email);
                        db.AddInputParameter("@DisplayName", DbType.String, user.DisplayName);
                        db.AddOutputParameter("@UserId", DbType.Int32);
                        db.ExecuteNonQuery();
                        user.UserId = db.GetParameterValue<int>("@UserId");

                        user.OAuth.UserId = user.UserId;
                        user.OAuth = CreateOAuthUser(user.OAuth, tran);

                        db.CreateStoredProcCommand("dbo.CreateUserProfile");
                        db.AddInputParameter("@UserId", DbType.String, user.UserId);
                        db.AddInputParameter("@LanguageId", DbType.String, user.Profile.Lang.LanguageId);
                        db.AddInputParameter("@Dob", DbType.String, user.Profile.Dob);
                        db.AddInputParameter("@Nationality", DbType.String, user.Profile.Nationality);
                        db.AddOutputParameter("@UserProfileId", DbType.Int32);
                        db.ExecuteNonQuery();
                        user.Profile.UserProfileId = db.GetParameterValue<int>("@UserProfileId");

                        user = GetUser(user.UserId);
                        tran.Commit();
                    }
                    catch (Exception ex)
                    {
                        tran.Rollback();
                        Logger.Error("Error while creating user.", ex);
                        throw;
                    }
                    return user;
                }
            }
        }
        public void UpdateHero(Hero hero, IEnumerable<IPostedFile> photos)
        {
            using (var tran = new DbTransactionManager())
            {
                try
                {
                    tran.BeginTransaction();
                    using (var db = new DataAccess(tran))
                    {
                        db.CreateStoredProcCommand("dbo.UpdateHero");
                        db.AddInputParameter("@HeroId", DbType.Int32, hero.HeroId);
                        db.AddInputParameter("@FirstName", DbType.String, hero.FirstName);
                        db.AddInputParameter("@LastName", DbType.String, hero.LastName);
                        db.AddInputParameter("@MiddleName", DbType.String, hero.MiddleName);
                        db.AddInputParameter("@Dob", DbType.DateTime, hero.Dob);
                        db.AddInputParameter("@Died", DbType.DateTime, hero.Died);
                        db.AddInputParameter("@MilitaryGroupId", DbType.Int32, hero.MilitaryGroupId);
                        db.AddInputParameter("@MilitaryRankId", DbType.Int32, hero.MilitaryRankId);
                        db.AddInputParameter("@MilitaryAwardId", DbType.Int32, hero.MilitaryAwardId);
                        db.AddInputParameter("@Biography", DbType.String, hero.Biography);
                        db.AddInputParameter("@LanguageId", DbType.Int32, hero.LanguageId);
                        db.AddInputParameter("@UpdatedBy", DbType.Int32, hero.UpdatedBy);
                        db.AddInputParameter("@Photos", DbType.Xml, photos.ToDbXml());

                        db.ExecuteNonQuery();
                        SaveBlobs(hero.HeroId.Value, photos);
                        tran.Commit();
                    }
                }
                catch (Exception ex)
                {                    
                    tran.Rollback();
                    Logger.Error("Error while updating hero", ex);
                    throw;
                }
            }
        }
 public void RevokeUserFromRoles(int userId, IEnumerable<int> roles)
 {
     using (var tran = new DbTransactionManager())
     {
         tran.BeginTransaction();
         using (var db = new DataAccess(tran))
         {
             try
             {
                 db.CreateStoredProcCommand("dbo.RevokeUserFromRole");
                 foreach (var id in roles)
                 {
                     db.AddInputParameter("@UserId", DbType.Int32, userId);
                     db.AddInputParameter("@RoleId", DbType.Int32, id);
                     db.ExecuteNonQuery();
                     db.ResetCommand(false);
                 }
                 tran.Commit();
             }
             catch (Exception ex)
             {
                 tran.Rollback();
                 Logger.Error("Error while revoking roles.", ex);
                 throw;
             }
         }
     }
 }
        public void UpdateUser(User user)
        {
            using (var tran = new DbTransactionManager())
            {
                tran.BeginTransaction();
                using (var db = new DataAccess(tran))
                {
                    db.CreateStoredProcCommand("dbo.UpdateUser");
                    db.AddInputParameter("@UserId", DbType.Int32, user.UserId);
                    db.AddInputParameter("@Email", DbType.String, user.Email);
                    db.AddInputParameter("@DisplayName", DbType.String, user.DisplayName);
                    try
                    {
                        db.ExecuteNonQuery();

                        if (user.Profile != null)
                        {
                            UpdateUserProfile(user.Profile);
                        }
                        tran.Commit();
                    }
                    catch (Exception ex)
                    {
                        tran.Rollback();
                        Logger.Error("Error while updating user.", ex);
                        throw;
                    }
                }
            }
        }
 public OAuthUser CreateOAuthUser(OAuthUser user)
 {
     using (var tran = new DbTransactionManager())
     {
         try
         {
             tran.BeginTransaction();
             user = CreateOAuthUser(user, tran);
             tran.Commit();
         }
         catch (Exception ex)
         {
             tran.Rollback();
             Logger.Error("Error while creating OAuth user.", ex);
             throw;
         }
     }
     return user;
 }