Example #1
0
        /// <summary>
        /// Clear and refill account friendships with actual data.
        /// </summary>
        /// <param name="account"></param>
        public void UpdateFriendships(ComplexUserAccount account)
        {
            IEnumerable <FriendshipModel> allAccountFriendships = FindAllFriendships(account.Id);

            account.Friendships.Clear();
            account.Friendships.AddRange(allAccountFriendships);
        }
        public RegistrationStatus RegisterAccount(RegistrationRequest request, out ComplexUserAccount registeredAcc)
        {
            UserAccountModel existingAccount = FindByName(request.UserName);

            if (existingAccount != null)
            {
                registeredAcc = null;
                return(RegistrationStatus.AlreadyExists);
            }

            var newAccount = new ComplexUserAccount
            {
                Name = request.UserName
            };

            Password passwordData = passwordService.CreatePasswordData(request.Password);

            passwordData.ParentId = newAccount.Id;

            newAccount.PasswordDatas.Add(passwordData);

            RegistrationStatus result = RegisterAccount(newAccount, out newAccount);

            registeredAcc = newAccount;
            registeredAcc.PasswordData = registeredAcc.PasswordDatas.First();

            return(result);
        }
        private RegistrationStatus RegisterAccount(ComplexUserAccount newAccount, out ComplexUserAccount registeredAcc)
        {
            lock (Locker)
            {
                registeredAcc = null;

                try
                {
                    UserAccountModel   trimmedAccount    = new AccountValidator(newAccount).Trim();
                    ComplexUserAccount trimmedComplexAcc = trimmedAccount.ToComplex();
                    trimmedComplexAcc.PasswordDatas.AddRange(newAccount.PasswordDatas);

                    trimmedComplexAcc.RegistrationTime = DateTime.Now;

                    AccountDbInteracter.Instance.AddOrUpdate(trimmedComplexAcc);

                    stateService.RegisterNewUserState(trimmedComplexAcc);
                    passwordService.SaveOrUpdatePassword(newAccount.PasswordDatas[0], false);

                    registeredAcc = trimmedComplexAcc;

                    return(RegistrationStatus.Registered);
                }
                catch (Exception ex)
                {
                    string message = $"Failed to register account '{newAccount.Name}': {ex.Message}";
                    logger.Error(ex, "Failed to register account");
                    message.WriteToConsole(ConsoleColor.Red);

                    return(RegistrationStatus.Error);
                }
            }
        }
Example #4
0
        /// <summary>
        /// Save a friend request to database.
        /// </summary>
        /// <param name="request">Incoming friend request model.</param>
        /// <returns>Saved complex request model.</returns>
        public ComplexFriendRequest Save(FriendRequest request)
        {
            RegistrationService service   = RegistrationService.Instance;
            ComplexUserAccount  requester = service.FindById(request.RequesterAccount.Id);
            ComplexUserAccount  target    = service.FindById(request.TargetAccount.Id);

            using (var context = new ComplexFriendRequestContext())
            {
                ComplexFriendRequest savedRequest = context.Objects.FirstOrDefault(req => req.Id == request.Id);

                if (savedRequest == null)
                {
                    var complexRequest = new ComplexFriendRequest
                    {
                        Id           = request.Id,
                        ParentId     = requester.Id,
                        TargetId     = target.Id,
                        CreationDate = request.CreationDate,
                        RequestState = FriendRequestState.AwaitingUserAnswer
                    };

                    context.Objects.Add(complexRequest);

                    context.SaveChanges();

                    return(complexRequest);
                }

                return(savedRequest);
            }
        }
Example #5
0
        /// <summary>
        /// Add new or update already registered account in database.
        /// </summary>
        /// <param name="account">Account to add or update.</param>
        public void AddOrUpdate(ComplexUserAccount account)
        {
            lock (Locker)
            {
                try
                {
                    using (var dbContext = new ComplexUserContext())
                    {
                        var storedAcc = dbContext.Objects.FirstOrDefault(acc => acc.Id == account.Id);

                        if (storedAcc == null)
                        {
                            dbContext.Objects.Add(account);
                            $"Added new account '{ account.Name }'".WriteToConsole();
                        }
                        else
                        {
                            TransferAllProperties(account, storedAcc);
                            storedAcc.PrepareMappedProps();

                            $"Updated account '{ account.Name }'".WriteToConsole();
                        }

                        dbContext.SaveChanges();
                        "Saved account changes".WriteToConsole();
                    }
                }
                catch (Exception ex)
                {
                    ex.Message.WriteToConsole(ConsoleColor.Red);
                    logger.Error(ex, $"Failed to add or update account '{ account.Name }'");
                }
            }
        }
Example #6
0
        private static ComplexUserAccount CreateFrom(UserAccountModel otherAccount)
        {
            var complex = new ComplexUserAccount
            {
                Id = otherAccount.Id,
                //UniqueId = otherAccount.UniqueId,
                Name             = otherAccount.Name,
                LastLogInTime    = otherAccount.LastLogInTime,
                Privilege        = otherAccount.Privilege,
                Expirience       = otherAccount.Expirience,
                RegistrationTime = otherAccount.RegistrationTime,
            };

            return(complex);
        }
Example #7
0
        public static UserAccountModel ToSimpleModel(this ComplexUserAccount complexAccount)
        {
            var simpleModel = new UserAccountModel
            {
                LastLogInTime    = complexAccount.LastLogInTime,
                Id               = complexAccount.Id,
                Name             = complexAccount.Name,
                RegistrationTime = complexAccount.RegistrationTime,
                Expirience       = complexAccount.Expirience,
                Privilege        = complexAccount.Privilege
            };

            simpleModel.Albums.AddRange(complexAccount.Albums);

            return(simpleModel);
        }
Example #8
0
        public static void TransferAllProperties(ComplexUserAccount from, ComplexUserAccount to)
        {
            if (to.Id != from.Id)
            {
                throw new InvalidOperationException("Id mismatch");
            }
            //if(to.UniqueId != from.UniqueId) throw new InvalidOperationException("Unique id mismatch");

            TransferSimpleProperties(from, to);

            //to.Friendships = from.Friendships;
            to.Friendships.Clear();
            from.Friendships.ForEach(to.Friendships.Add);

            //to.FriendRequests= from.FriendRequests;
            to.FriendRequests.Clear();
            from.FriendRequests.ForEach(to.FriendRequests.Add);
        }
Example #9
0
        private dynamic ProcessSearchFriends(SearchRequest searchRequest, SerializeHelper helper)
        {
            var registrationService = RegistrationService.Instance;
            var friendService       = FriendshipService.Instance;

            ComplexUserAccount senderAcc = registrationService.FindById(searchRequest.SenderId);

            if (senderAcc == null)
            {
                return($"Account id {searchRequest.SenderId} is not registered");
            }

            friendService.UpdateFriendships(senderAcc);

            var stateService = AccountStateService.Instance;

            AccountStateModel AccountStateSelector(FriendshipModel friendship)
            {
                Guid friendId =
                    friendship.AcceptorAccountId == searchRequest.SenderId ?
                    friendship.ParentId :
                    friendship.AcceptorAccountId;

                ComplexAccountState complexAccountState = stateService.GetStatusOf(friendId);

                if (complexAccountState == null)
                {
                    throw new InvalidOperationException($"Found null state pair for account id { friendId }");
                }

                return(complexAccountState.ToSimple());
            }

            IEnumerable <AccountStateModel> friendStateModels = senderAcc.Friendships.Select(AccountStateSelector);

            var serialized = helper.Serialize(friendStateModels);

            return(serialized);
        }
Example #10
0
        public static ComplexUserAccount Copy(this ComplexUserAccount otherAccount)
        {
            ComplexUserAccount complex = CreateFrom((UserAccountModel)otherAccount);

            return(complex);
        }
 internal void RegisterNewUserState(ComplexUserAccount newUserAccount)
 {
     CacheNewState(newUserAccount, string.Empty);
 }