33
44using System ;
55using System . Collections . Generic ;
6+ using System . Data . Common ;
67using System . Data . Entity ;
78using System . IO ;
89using System . Linq ;
@@ -24,19 +25,22 @@ public class PackageService : CorePackageService, IPackageService
2425 private readonly IAuditingService _auditingService ;
2526 private readonly ITelemetryService _telemetryService ;
2627 private readonly ISecurityPolicyService _securityPolicyService ;
28+ private readonly IEntitiesContext _entitiesContext ;
2729
2830 public PackageService (
2931 IEntityRepository < PackageRegistration > packageRegistrationRepository ,
3032 IEntityRepository < Package > packageRepository ,
3133 IEntityRepository < Certificate > certificateRepository ,
3234 IAuditingService auditingService ,
3335 ITelemetryService telemetryService ,
34- ISecurityPolicyService securityPolicyService )
36+ ISecurityPolicyService securityPolicyService ,
37+ IEntitiesContext entitiesContext )
3538 : base ( packageRepository , packageRegistrationRepository , certificateRepository )
3639 {
3740 _auditingService = auditingService ?? throw new ArgumentNullException ( nameof ( auditingService ) ) ;
3841 _telemetryService = telemetryService ?? throw new ArgumentNullException ( nameof ( telemetryService ) ) ;
3942 _securityPolicyService = securityPolicyService ?? throw new ArgumentNullException ( nameof ( securityPolicyService ) ) ;
43+ _entitiesContext = entitiesContext ?? throw new ArgumentNullException ( nameof ( entitiesContext ) ) ;
4044 }
4145
4246 /// <summary>
@@ -136,12 +140,60 @@ public override PackageRegistration FindPackageRegistrationById(string packageId
136140 }
137141
138142 public virtual IReadOnlyCollection < Package > FindPackagesById (
139- string id ,
143+ string id ,
140144 PackageDeprecationFieldsToInclude deprecationFields = PackageDeprecationFieldsToInclude . None )
141145 {
142146 return GetPackagesByIdQueryable ( id , deprecationFields ) . ToList ( ) ;
143147 }
144148
149+ public PackageDependents GetPackageDependents ( string id )
150+ {
151+ if ( string . IsNullOrWhiteSpace ( id ) )
152+ {
153+ throw new ArgumentNullException ( nameof ( id ) ) ;
154+ }
155+
156+ PackageDependents result = new PackageDependents ( ) ;
157+ result . TopPackages = GetListOfDependents ( id ) ;
158+ result . TotalPackageCount = GetDependentCount ( id ) ;
159+ return result ;
160+ }
161+
162+ private IReadOnlyCollection < PackageDependent > GetListOfDependents ( string id )
163+ {
164+ var packageDependentsList = new List < PackageDependent > ( ) ;
165+ var listPackages = ( from pd in _entitiesContext . PackageDependencies
166+ join p in _entitiesContext . Packages on pd . PackageKey equals p . Key
167+ join pr in _entitiesContext . PackageRegistrations on p . PackageRegistrationKey equals pr . Key
168+ where p . IsLatestSemVer2 && pd . Id == id
169+ group 1 by new { pr . Id , pr . DownloadCount , p . Description } into ng
170+ orderby ng . Key . DownloadCount descending
171+ select new { ng . Key . Id , ng . Key . DownloadCount , ng . Key . Description }
172+ ) . Take ( 5 ) . ToList ( ) ;
173+
174+ foreach ( var pd in listPackages )
175+ {
176+ var packageDependent = new PackageDependent ( ) ;
177+ packageDependent . Description = pd . Description ;
178+ packageDependent . DownloadCount = pd . DownloadCount ;
179+ packageDependent . Id = pd . Id ;
180+ packageDependentsList . Add ( packageDependent ) ;
181+ }
182+
183+ return packageDependentsList ;
184+ }
185+
186+ private int GetDependentCount ( string id )
187+ {
188+ var totalCount = ( from pd in _entitiesContext . PackageDependencies
189+ join p in _entitiesContext . Packages on pd . PackageKey equals p . Key
190+ where pd . Id == id && p . IsLatestSemVer2
191+ group 1 by p . PackageRegistrationKey
192+ ) . Count ( ) ;
193+
194+ return totalCount ;
195+ }
196+
145197 public virtual IReadOnlyCollection < Package > FindPackagesById (
146198 string id ,
147199 bool includePackageRegistration )
0 commit comments