Example #1
0
        public async Task <DownloadResult> DownloadAllrpgProfile(int userId)
        {
            var user = await UserRepository.WithProfile(userId);

            if (user.Allrpg?.Sid != null)
            {
                return(DownloadResult.AlreadyDownloaded);
            }

            var reply = await _api.GetProfile(user.Email);

            switch (reply.Status)
            {
            case AllrpgApi.Status.Success:
                break;

            case AllrpgApi.Status.NetworkError:
                return(DownloadResult.NetworkError);

            case AllrpgApi.Status.ParseError:
                return(DownloadResult.ParseError);

            case AllrpgApi.Status.NoSuchUser:
                user.Allrpg     = user.Allrpg ?? new AllrpgUserDetails();
                user.Allrpg.Sid = 0;
                user.Allrpg.PreventAllrpgPassword = true;
                await UnitOfWork.SaveChangesAsync();

                return(DownloadResult.Success);

            case AllrpgApi.Status.WrongKey:
                return(DownloadResult.WrongKey);

            default:
                throw new ArgumentOutOfRangeException();
            }

            var result = reply.Result;

            user.Allrpg = user.Allrpg ?? new AllrpgUserDetails()
            {
                PreventAllrpgPassword = user.PasswordHash != null
            };
            user.Allrpg.JsonProfile = reply.RawResult; //Save raw value, we m.b. like to parse it later

            AllrpgImportUtilities.ImportUserFromResult(user, result);

            await UnitOfWork.SaveChangesAsync();

            return(DownloadResult.Success);
        }
        private async Task ImportUser(ProfileReply allrpgUser)
        {
            var usersRepository = UnitOfWork.GetUsersRepository();
            var user            = await usersRepository.GetByAllRpgId(allrpgUser.sid);

            if (user != null)
            {
                _operationLog.Info($"USER.FOUND: Id = {user.Id}, Email = {user.Email}, Allrpg = {user.Allrpg.Sid}");
                Users.Add(allrpgUser.sid, user);
                return;
            }

            var email  = allrpgUser.em.ToLowerInvariant();
            var email2 = allrpgUser.em2.ToLowerInvariant();

            user = await usersRepository.GetByEmail(email) ?? await usersRepository.GetByEmail(email2);

            string action;

            if (user == null)
            {
                var username = new[] { email, email2 }.WhereNotNullOrWhiteSpace().First();
                user = new User
                {
                    Email    = username,
                    UserName = username
                };
                UnitOfWork.GetDbSet <User>().Add(user);
                action = "CREATE";
            }
            else
            {
                action = "UPDATE";
            }
            Users.Add(allrpgUser.sid, user);
            user.Allrpg     = user.Allrpg ?? new AllrpgUserDetails();
            user.Allrpg.Sid = allrpgUser.sid;
            AllrpgImportUtilities.ImportUserFromResult(user, allrpgUser);

            _operationLog.Info($"USER.{action} Id = {user.Id}, Email = {user.Email}, Allrpg = {user.Allrpg.Sid}");
        }