<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using Dapper;
using Dragon.Data.Interfaces;
using Dragon.Data.Repositories;
using Dragon.SecurityServer.AccountSTS.Models;
using Dragon.SecurityServer.Identity.Models;

namespace Dragon.SecurityServer.AccountSTS.Services
{
    public class AppService : IAppService
    {
        private readonly IRepository<AppInfo> _consumerInfoRepository;
        private readonly IRepository<IdentityUserApp> _consumerUserRepository;

        public AppService(IRepository<AppInfo> consumerInfoRepository, IRepository<IdentityUserApp> consumerUserRepository)
        {
            _consumerInfoRepository = consumerInfoRepository;
            _consumerUserRepository = consumerUserRepository;
        }

        public IList<AppInfo> GetOtherRegisteredAppsInSameGroup(Guid userId, Guid appId)
        {
            return GetRegisteredAppsInSameGroup(userId, appId, true);
        }

        public IList<AppInfo> GetRegisteredAppsInSameGroup(Guid userId, Guid appId)
        {
            return GetRegisteredAppsInSameGroup(userId, appId, false);
        }

        private IList<AppInfo> GetRegisteredAppsInSameGroup(Guid userId, Guid appId, bool othersOnly)
        {
            var appInfo = _consumerInfoRepository.Get(appId.ToString());
            var appsInUse = _consumerUserRepository.GetByWhere(new Dictionary<string, object> {{"UserId", userId}}).Select(x => x.AppId).ToList();
            IList<AppInfo> consumerInfos = null;
            using (var c = ConnectionHelper.Open())
            {
                var query = "SELECT * FROM ConsumerInfo where AppId IN @Ids AND GroupId = @GroupId" + (othersOnly ? " AND AppId != @AppId" : "");
                consumerInfos = c.Query<AppInfo>(query, new {Ids = appsInUse, GroupId = appInfo.GroupId, AppId = appId}).ToList();
            }
            return consumerInfos;
        }
    }
}<commit_msg>Fixed improper handling of apps with no entry in the ConsumerInfo table<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using Dapper;
using Dragon.Data.Interfaces;
using Dragon.Data.Repositories;
using Dragon.SecurityServer.AccountSTS.Models;
using Dragon.SecurityServer.Identity.Models;

namespace Dragon.SecurityServer.AccountSTS.Services
{
    public class AppService : IAppService
    {
        private readonly IRepository<AppInfo> _consumerInfoRepository;
        private readonly IRepository<IdentityUserApp> _consumerUserRepository;

        public AppService(IRepository<AppInfo> consumerInfoRepository, IRepository<IdentityUserApp> consumerUserRepository)
        {
            _consumerInfoRepository = consumerInfoRepository;
            _consumerUserRepository = consumerUserRepository;
        }

        public IList<AppInfo> GetOtherRegisteredAppsInSameGroup(Guid userId, Guid appId)
        {
            return GetRegisteredAppsInSameGroup(userId, appId, true);
        }

        public IList<AppInfo> GetRegisteredAppsInSameGroup(Guid userId, Guid appId)
        {
            return GetRegisteredAppsInSameGroup(userId, appId, false);
        }

        private IList<AppInfo> GetRegisteredAppsInSameGroup(Guid userId, Guid appId, bool othersOnly)
        {
            var appInfo = _consumerInfoRepository.Get(appId.ToString());
            if (appInfo == null) return new List<AppInfo>();
            var appsInUse = _consumerUserRepository.GetByWhere(new Dictionary<string, object> {{"UserId", userId}}).Select(x => x.AppId).ToList();
            if (!appsInUse.Any()) return new List<AppInfo>();
            IList<AppInfo> consumerInfos = null;
            using (var c = ConnectionHelper.Open())
            {
                var query = "SELECT * FROM ConsumerInfo where AppId IN @Ids AND GroupId = @GroupId" + (othersOnly ? " AND AppId != @AppId" : "");
                consumerInfos = c.Query<AppInfo>(query, new {Ids = appsInUse, GroupId = appInfo.GroupId, AppId = appId}).ToList();
            }
            return consumerInfos;
        }
    }
}
{'fraction_non_alphanumeric': 0.08805342567400445, 'fraction_numerical': 0.0, 'mean_word_length': 3.1307456588355467, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6199987', 'n_tokens_mistral': 1104, 'n_tokens_neox': 1080, 'n_words': 246}