Exemple #1
0
        public async Task <BaseResponse> GetUserSession(int PartnerId, int CpartyId, int TerminalId)
        {
            AvihangUserSessions avihangUserSessions = new AvihangUserSessions();
            AvihangUserInfo     avihangUserInfo     = new AvihangUserInfo();
            BaseResponse        baseResponse        = new BaseResponse();

            avihangUserSessions = _avihangUserSessionsRepository.GetByPartnerIdAndCpartyId(PartnerId, CpartyId).Result;
            if (getSessionState(avihangUserSessions) != Framework.Utility.SessionState.Ok)
            {
                var    providers    = _providersRepository.GetById((int)ProviderType.Avihang).Result;
                var    providerApis = _providerApisRepository.GetByProviderId((int)ProviderType.Avihang).Result;
                string ApiUrl       = providerApis.Where(a => a.Id == (int)AvihangApis.InternalUserSession).FirstOrDefault().Url;
                string Uri          = $"{(providers.BaseUrl.EndsWith("/") ? providers.BaseUrl : providers.BaseUrl + "/")}{(ApiUrl.StartsWith("/") ? ApiUrl.Remove(0, 1) : ApiUrl)}";
                Header header       = new Header(Uri, 1, "application/json");
                header.token           = _avihangTokensService.GetToken(TerminalId).Result.Token;
                header.clientAgentInfo = "172.16.2.9";
                header.clientIPAddress = "172.16.2.9";
                header.terminalId      = TerminalId;
                UserSessionRequest userSessionRequest = new UserSessionRequest(PartnerId, CpartyId);
                var preResponse = _factory.GetChannel().CallWebApi <BaseApiHeader, UserSessionRequest>(header, userSessionRequest);
                avihangUserSessions         = new AvihangUserSessions();
                avihangUserSessions.ResDate = DateTime.Now;
                baseResponse = Utilities.JsonTextToModel <BaseResponse>(preResponse.Content);
                baseResponse.CopyPropertiesTo(avihangUserSessions);
                baseResponse.info.CopyPropertiesTo(avihangUserSessions);
                baseResponse.info.CopyPropertiesTo(avihangUserInfo);
                await _avihangUserInfoRepository.Insert(avihangUserInfo);

                avihangUserSessions.AvihangUserInfoId = avihangUserInfo.Id;
                await _avihangUserSessionsRepository.Add(avihangUserSessions);

                await _avihangUserSessionsRepository.CommitAsync();
            }
            return(baseResponse);
        }
        public async Task <AvihangTokens> GetToken(int terminalId)
        {
            AuthResponse  authResponse  = new AuthResponse();
            AvihangTokens avihangTokens = new AvihangTokens();

            try
            {
                avihangTokens = _avihangTokensRepository.GetByTerminalId(terminalId.ToString()).Result;
                if (TokenState(avihangTokens) != Framework.Utility.TokenState.Ok)
                {
                    var           providers    = _providersRepository.GetById((int)ProviderType.Avihang).Result;
                    var           providerApis = _providerApisRepository.GetByProviderId((int)ProviderType.Avihang).Result;
                    string        ApiUrl       = providerApis.Where(a => a.Id == (int)AvihangApis.Token).FirstOrDefault().Url;
                    string        Uri          = $"{(providers.BaseUrl.EndsWith("/") ? providers.BaseUrl : providers.BaseUrl + "/")}{(ApiUrl.StartsWith("/") ? ApiUrl.Remove(0, 1) : ApiUrl)}";
                    BaseApiHeader header       = new BaseApiHeader(Uri, 1, "application/json");
                    AuthRequest   authRequest  = new AuthRequest(providers.UserName, providers.Password, terminalId);
                    var           preResponse  = _factory.GetChannel().CallWebApi <BaseApiHeader, AuthRequest>(header, authRequest);
                    avihangTokens.ResDate = DateTime.Now;
                    authResponse          = Utilities.JsonTextToModel <AuthResponse>(preResponse.Content);
                    authResponse.CopyPropertiesTo(avihangTokens);
                    authResponse.info.CopyPropertiesTo(avihangTokens);
                    avihangTokens.TerminalId     = terminalId.ToString();
                    authResponse.info.terminalId = terminalId;
                    await _avihangTokensRepository.Add(avihangTokens);

                    await _avihangTokensRepository.CommitAsync();
                }
            }
            catch (Exception ex)
            {
                _logService.LogText(ex.Message);
                throw;
            }


            return(avihangTokens);
        }