/// <summary>
        /// Updates the user. Passing a new user with FacebookUserId and SteamUserId will insert a new User.
        /// </summary>
        /// <param name="user">The user.</param>
        public void UpdateUser(User user)
        {
            if (user == null)
            {
                throw new ArgumentNullException("user");
            }

            if (String.IsNullOrEmpty(user.SteamUserId))
            {
                throw new ArgumentException("user.SteamUserId cannot be null or empty", "user");
            }

            if (user.FacebookUserId == 0)
            {
                throw new ArgumentException("user.FacebookUserId cannot be 0", "user");
            }

            bool exists = Exists(user.FacebookUserId);

            if (!exists)
            {
                // the user does not exist, create a new one.
                User newUser = new User
                {
                    FacebookUserId     = user.FacebookUserId,
                    SteamUserId        = user.SteamUserId,
                    AccessToken        = user.AccessToken,
                    AutoUpdate         = user.AutoUpdate,
                    PublishDescription = user.PublishDescription
                };

                _repository.InsertOnSubmit(newUser);
                _repository.SubmitChanges();

                return;
            }

            // update
            User existingUser = _repository.Users.Where(u => u.FacebookUserId == user.FacebookUserId).Single();

            // if the user changed steam IDs, remove their achievements
            if (existingUser.SteamUserId != user.SteamUserId)
            {
                var existingAchievements = _repository.UserAchievements
                                           .Where(ua => ua.FacebookUserId == user.FacebookUserId)
                                           .ToArray();
                if (existingAchievements.Length > 0)
                {
                    _repository.DeleteAllOnSubmit(existingAchievements);
                }
            }

            existingUser.AccessToken        = user.AccessToken;
            existingUser.AutoUpdate         = user.AutoUpdate;
            existingUser.SteamUserId        = user.SteamUserId;
            existingUser.PublishDescription = user.PublishDescription;

            _repository.SubmitChanges();
        }
        /// <summary>
        /// Updates the user. Passing a new user with FacebookUserId and SteamUserId will insert a new User.
        /// </summary>
        /// <param name="user">The user.</param>
        public void UpdateUser(User user)
        {
            if (user == null)
            {
                throw new ArgumentNullException("user");
            }

            if (String.IsNullOrEmpty(user.SteamUserId))
            {
                throw new ArgumentException("user.SteamUserId cannot be null or empty", "user");
            }

            if (user.FacebookUserId == 0)
            {
                throw new ArgumentException("user.FacebookUserId cannot be 0", "user");
            }

            bool exists = Exists(user.FacebookUserId);

            if (!exists)
            {
                // the user does not exist, create a new one.
                User newUser = new User
                {
                    FacebookUserId     = user.FacebookUserId,
                    SteamUserId        = user.SteamUserId,
                    AccessToken        = user.AccessToken,
                    AutoUpdate         = user.AutoUpdate,
                    PublishDescription = user.PublishDescription
                };

                _repository.InsertOnSubmit(newUser);
                _repository.SubmitChanges();

                return;
            }

            // update
            User existingUser = _repository.Users.Where(u => u.FacebookUserId == user.FacebookUserId).Single();

            existingUser.AccessToken        = user.AccessToken;
            existingUser.AutoUpdate         = user.AutoUpdate;
            existingUser.SteamUserId        = user.SteamUserId;
            existingUser.PublishDescription = user.PublishDescription;

            _repository.SubmitChanges();
        }
Esempio n. 3
0
        /// <summary>
        /// Updates the steam user id.
        /// </summary>
        /// <param name="facebookUserId">The facebook user id.</param>
        /// <param name="steamUserId">The steam user id.</param>
        public void UpdateSteamUserId(long facebookUserId, string steamUserId)
        {
            if (steamUserId == null)
            {
                throw new ArgumentNullException("steamUserId");
            }

            IQueryable <User> query = from u in _repository.Users
                                      where u.FacebookUserId == facebookUserId
                                      select u;

            User user = query.SingleOrDefault();

            if (user == null)
            {
                // the user does not exist, create a new one.
                user = new User {
                    FacebookUserId = facebookUserId, SteamUserId = steamUserId
                };

                _repository.InsertOnSubmit(user);
                _repository.SubmitChanges();
            }
            else
            {
                // nothing to change
                if (user.SteamUserId == steamUserId)
                {
                    return;
                }

                // delete all achievements associated with the old id
                IQueryable <UserAchievement> userAchievements = from u in _repository.UserAchievements
                                                                where u.SteamUserId == user.SteamUserId
                                                                select u;

                _repository.DeleteAllOnSubmit(userAchievements.ToArray());
                _repository.SubmitChanges();

                // update steam id
                user.SteamUserId = steamUserId;
                _repository.SubmitChanges();
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Updates the user. Passing a new user with FacebookUserId and SteamUserId will insert a new User.
        /// </summary>
        /// <param name="user">The user.</param>
        public void UpdateUser(User user)
        {
            if (user == null)
            {
                throw new ArgumentNullException("user");
            }

            if (String.IsNullOrEmpty(user.SteamUserId))
            {
                throw new ArgumentException("user.SteamUserId cannot be null or empty", "user");
            }

            if (user.FacebookUserId == 0)
            {
                throw new ArgumentException("user.FacebookUserId cannot be 0", "user");
            }

            bool exists = _repository.Users.Where(u => u.FacebookUserId == user.FacebookUserId).Any();

            if (!exists)
            {
                // the user does not exist, create a new one.
                User newUser = new User
                {
                    FacebookUserId = user.FacebookUserId,
                    SteamUserId    = user.SteamUserId,
                    AccessToken    = user.AccessToken,
                    AutoUpdate     = user.AutoUpdate
                };

                _repository.InsertOnSubmit(newUser);
                _repository.SubmitChanges();

                return;
            }

            User existingUser = _repository.Users.Where(u => u.FacebookUserId == user.FacebookUserId).Single();

            string existingSteamUserId = existingUser.SteamUserId;
            bool   steamIdChanged      = existingSteamUserId.ToUpper() != user.SteamUserId.ToUpper();
            bool   hasAchievements     = false;
            List <UserAchievement> userAchievements = null;

            if (steamIdChanged)
            {
                // delete the user's acheivements
                userAchievements = (from u in _repository.UserAchievements
                                    where u.SteamUserId.ToUpper() == existingSteamUserId.ToUpper()
                                    select u).ToList();

                hasAchievements = userAchievements.Any();
                if (hasAchievements)
                {
                    _repository.DeleteAllOnSubmit(userAchievements);
                    _repository.SubmitChanges();
                }
            }

            // update
            existingUser             = _repository.Users.Where(u => u.FacebookUserId == user.FacebookUserId).Single();
            existingUser.AccessToken = user.AccessToken;
            existingUser.AutoUpdate  = user.AutoUpdate;
            existingUser.SteamUserId = user.SteamUserId;

            _repository.SubmitChanges();

            if (steamIdChanged && hasAchievements)
            {
                // insert the user's updated achievements
                List <UserAchievement> updatedUserAchievements = new List <UserAchievement>();

                foreach (UserAchievement userAchievement in userAchievements)
                {
                    UserAchievement updated =
                        new UserAchievement
                    {
                        AchievementId = userAchievement.AchievementId,
                        Date          = userAchievement.Date,
                        Id            = userAchievement.Id,
                        Published     = userAchievement.Published,
                        SteamUserId   = user.SteamUserId
                    };

                    updatedUserAchievements.Add(updated);
                }

                _repository.InsertAllOnSubmit(updatedUserAchievements);
                _repository.SubmitChanges();
            }
        }