@@ -18,7 +18,7 @@ public class MarketplaceService(
1818 CCTContext context ,
1919 IWebHostEnvironment webHostEnvironment ,
2020 ServerUtils serverUtils ,
21- IAccountService accountService // Inject this
21+ IAccountService accountService
2222 ) : IMarketplaceService
2323 {
2424 /// <summary>
@@ -31,28 +31,32 @@ public IEnumerable<MarketplaceListingViewModel> GetAllListings()
3131 . Include ( x => x . Condition )
3232 . Include ( x => x . Status )
3333 . Include ( x => x . PostImage )
34- . Where ( item => item . StatusId != 3 ) // Exclude statusid 3
34+ . Where ( item => item . StatusId != 3 )
3535 . OrderByDescending ( item => item . PostedAt )
36- . Select ( item => new MarketplaceListingViewModel
36+ . Join (
37+ context . ACCOUNT ,
38+ item => item . PostedById . ToString ( ) ,
39+ acc => acc . gordon_id ,
40+ ( item , acc ) => new { item , acc }
41+ )
42+ . Select ( x => new MarketplaceListingViewModel
3743 {
38- Id = item . Id ,
39- PostedAt = item . PostedAt ,
40- Name = item . Name ,
41- Price = item . Price ,
42- CategoryId = item . CategoryId ,
43- CategoryName = item . Category . CategoryName ,
44- Detail = item . Detail ,
45- ConditionId = item . ConditionId ,
46- ConditionName = item . Condition . ConditionName ,
47- StatusId = item . StatusId ,
48- StatusName = item . Status . StatusName ,
49- ImagePaths = item . PostImage . Select ( img => img . ImagePath ) . ToList ( ) ,
50- PosterUsername = context . ACCOUNT
51- . Where ( a => a . gordon_id == item . PostedById . ToString ( ) )
52- . Select ( a => a . AD_Username )
53- . FirstOrDefault ( )
44+ Id = x . item . Id ,
45+ PostedAt = x . item . PostedAt ,
46+ Name = x . item . Name ,
47+ Price = x . item . Price ,
48+ CategoryId = x . item . CategoryId ,
49+ CategoryName = x . item . Category . CategoryName ,
50+ Detail = x . item . Detail ,
51+ ConditionId = x . item . ConditionId ,
52+ ConditionName = x . item . Condition . ConditionName ,
53+ StatusId = x . item . StatusId ,
54+ StatusName = x . item . Status . StatusName ,
55+ ImagePaths = x . item . PostImage . Select ( img => img . ImagePath ) . ToList ( ) ,
56+ PosterUsername = x . acc != null ? x . acc . AD_Username : null
5457 } )
5558 . ToList ( ) ;
59+
5660 return listings ;
5761 }
5862
@@ -68,27 +72,29 @@ public IEnumerable<MarketplaceListingViewModel> GetUserListings(string username)
6872 . Include ( x => x . Condition )
6973 . Include ( x => x . Status )
7074 . Include ( x => x . PostImage )
71- . Where ( item => item . PostedById == userId )
72- . Where ( item => item . StatusId != 3 ) // Exclude statusid 3
75+ . Where ( item => item . PostedById == userId && item . StatusId != 3 )
7376 . OrderByDescending ( item => item . PostedAt )
74- . Select ( item => new MarketplaceListingViewModel
77+ . Join (
78+ context . ACCOUNT ,
79+ item => item . PostedById . ToString ( ) ,
80+ acc => acc . gordon_id ,
81+ ( item , acc ) => new { item , acc }
82+ )
83+ . Select ( x => new MarketplaceListingViewModel
7584 {
76- Id = item . Id ,
77- PostedAt = item . PostedAt ,
78- Name = item . Name ,
79- Price = item . Price ,
80- CategoryId = item . CategoryId ,
81- CategoryName = item . Category . CategoryName ,
82- Detail = item . Detail ,
83- ConditionId = item . ConditionId ,
84- ConditionName = item . Condition . ConditionName ,
85- StatusId = item . StatusId ,
86- StatusName = item . Status . StatusName ,
87- ImagePaths = item . PostImage . Select ( img => img . ImagePath ) . ToList ( ) ,
88- PosterUsername = context . ACCOUNT
89- . Where ( a => a . gordon_id == item . PostedById . ToString ( ) )
90- . Select ( a => a . AD_Username )
91- . FirstOrDefault ( )
85+ Id = x . item . Id ,
86+ PostedAt = x . item . PostedAt ,
87+ Name = x . item . Name ,
88+ Price = x . item . Price ,
89+ CategoryId = x . item . CategoryId ,
90+ CategoryName = x . item . Category . CategoryName ,
91+ Detail = x . item . Detail ,
92+ ConditionId = x . item . ConditionId ,
93+ ConditionName = x . item . Condition . ConditionName ,
94+ StatusId = x . item . StatusId ,
95+ StatusName = x . item . Status . StatusName ,
96+ ImagePaths = x . item . PostImage . Select ( img => img . ImagePath ) . ToList ( ) ,
97+ PosterUsername = x . acc != null ? x . acc . AD_Username : null
9298 } )
9399 . ToList ( ) ;
94100
@@ -105,7 +111,7 @@ public MarketplaceListingViewModel GetListingById(int listingId)
105111 . Include ( x => x . Condition )
106112 . Include ( x => x . Status )
107113 . Include ( x => x . PostImage )
108- . FirstOrDefault ( x => x . Id == listingId ) ;
114+ . FirstOrDefault ( x => x . Id == listingId ) ;
109115
110116 if ( listing == null )
111117 {
@@ -301,8 +307,7 @@ public IEnumerable<MarketplaceListingViewModel> GetFilteredListings(
301307 . Include ( x => x . Condition )
302308 . Include ( x => x . Status )
303309 . Include ( x => x . PostImage )
304- . Where ( x => x . StatusId != 3 )
305- . AsQueryable ( ) ;
310+ . Where ( x => x . StatusId != 3 ) ;
306311
307312 if ( categoryId . HasValue )
308313 query = query . Where ( x => x . CategoryId == categoryId . Value ) ;
@@ -339,25 +344,32 @@ public IEnumerable<MarketplaceListingViewModel> GetFilteredListings(
339344 // Pagination
340345 query = query . Skip ( ( page - 1 ) * pageSize ) . Take ( pageSize ) ;
341346
342- return query . Select ( item => new MarketplaceListingViewModel
343- {
344- Id = item . Id ,
345- PostedAt = item . PostedAt ,
346- Name = item . Name ,
347- Price = item . Price ,
348- CategoryId = item . CategoryId ,
349- CategoryName = item . Category . CategoryName ,
350- Detail = item . Detail ,
351- ConditionId = item . ConditionId ,
352- ConditionName = item . Condition . ConditionName ,
353- StatusId = item . StatusId ,
354- StatusName = item . Status . StatusName ,
355- ImagePaths = item . PostImage . Select ( img => img . ImagePath ) . ToList ( ) ,
356- PosterUsername = context . ACCOUNT
357- . Where ( a => a . gordon_id == item . PostedById . ToString ( ) )
358- . Select ( a => a . AD_Username )
359- . FirstOrDefault ( )
360- } ) . ToList ( ) ;
347+ var listings = query
348+ . Join (
349+ context . ACCOUNT ,
350+ item => item . PostedById . ToString ( ) ,
351+ acc => acc . gordon_id ,
352+ ( item , acc ) => new { item , acc }
353+ )
354+ . Select ( x => new MarketplaceListingViewModel
355+ {
356+ Id = x . item . Id ,
357+ PostedAt = x . item . PostedAt ,
358+ Name = x . item . Name ,
359+ Price = x . item . Price ,
360+ CategoryId = x . item . CategoryId ,
361+ CategoryName = x . item . Category . CategoryName ,
362+ Detail = x . item . Detail ,
363+ ConditionId = x . item . ConditionId ,
364+ ConditionName = x . item . Condition . ConditionName ,
365+ StatusId = x . item . StatusId ,
366+ StatusName = x . item . Status . StatusName ,
367+ ImagePaths = x . item . PostImage . Select ( img => img . ImagePath ) . ToList ( ) ,
368+ PosterUsername = x . acc != null ? x . acc . AD_Username : null
369+ } )
370+ . ToList ( ) ;
371+
372+ return listings ;
361373 }
362374
363375
@@ -391,7 +403,7 @@ public int GetFilteredListingsCount(
391403 /// Get all marketplace threads for admin view (one row per thread, including deleted/expired).
392404 /// </summary>
393405 public IEnumerable < MarketplaceAdminViewModel > GetAdminThreads (
394- int ? id , int ? categoryId , int ? statusId , decimal ? minPrice ,
406+ int ? id , int ? categoryId , int ? statusId , decimal ? minPrice ,
395407 decimal ? maxPrice , string ? search , string ? sortBy , bool desc = false ,
396408 int page = 1 , int pageSize = 20 )
397409 {
@@ -405,7 +417,7 @@ public IEnumerable<MarketplaceAdminViewModel> GetAdminThreads(
405417 // Filtering
406418 if ( id . HasValue )
407419 query = query . Where ( x => x . Id == id . Value || x . OriginalPostId == id . Value ) ;
408-
420+
409421 if ( categoryId . HasValue )
410422 query = query . Where ( x => x . CategoryId == categoryId . Value ) ;
411423
@@ -423,7 +435,7 @@ public IEnumerable<MarketplaceAdminViewModel> GetAdminThreads(
423435
424436 // Group by thread (OriginalPostId or Id if null), select latest version per thread
425437 var threads = query
426- . AsEnumerable ( ) // Grouping with navigation properties must be done in memory
438+ . AsEnumerable ( )
427439 . GroupBy ( x => x . OriginalPostId ?? x . Id )
428440 . Select ( g => g . OrderByDescending ( x => x . Id ) . First ( ) ) ;
429441
@@ -447,6 +459,11 @@ public IEnumerable<MarketplaceAdminViewModel> GetAdminThreads(
447459 // Pagination
448460 threads = threads . Skip ( ( page - 1 ) * pageSize ) . Take ( pageSize ) ;
449461
462+ var userIds = threads . Select ( x => x . PostedById . ToString ( ) ) . Distinct ( ) . ToList ( ) ;
463+ var accountDict = context . ACCOUNT
464+ . Where ( a => userIds . Contains ( a . gordon_id ) )
465+ . ToDictionary ( a => a . gordon_id , a => a . AD_Username ) ;
466+
450467 return threads . Select ( item => new MarketplaceAdminViewModel
451468 {
452469 Id = item . Id ,
@@ -461,12 +478,8 @@ public IEnumerable<MarketplaceAdminViewModel> GetAdminThreads(
461478 StatusId = item . StatusId ,
462479 StatusName = item . Status ? . StatusName ,
463480 ImagePaths = item . PostImage ? . Select ( img => img . ImagePath ) . ToList ( ) ?? new List < string > ( ) ,
464- PosterUsername = context . ACCOUNT
465- . Where ( a => a . gordon_id == item . PostedById . ToString ( ) )
466- . Select ( a => a . AD_Username )
467- . FirstOrDefault ( ) ,
481+ PosterUsername = accountDict . TryGetValue ( item . PostedById . ToString ( ) , out var username ) ? username : null ,
468482 ThreadId = item . OriginalPostId ?? item . Id
469-
470483 } ) . ToList ( ) ;
471484 }
472485
@@ -476,32 +489,35 @@ public IEnumerable<MarketplaceAdminViewModel> GetAdminThreads(
476489 public IEnumerable < MarketplaceListingViewModel > GetThreadEditHistory ( int threadId )
477490 {
478491 var history = context . PostedItem
479- . Include ( x => x . Category )
480- . Include ( x => x . Condition )
481- . Include ( x => x . Status )
482- . Include ( x => x . PostImage )
483- . Where ( x => x . OriginalPostId == threadId || x . Id == threadId )
484- . OrderBy ( x => x . Id )
485- . Select ( item => new MarketplaceListingViewModel
486- {
487- Id = item . Id ,
488- PostedAt = item . PostedAt ,
489- Name = item . Name ,
490- Price = item . Price ,
491- CategoryId = item . CategoryId ,
492- CategoryName = item . Category . CategoryName ,
493- Detail = item . Detail ,
494- ConditionId = item . ConditionId ,
495- ConditionName = item . Condition . ConditionName ,
496- StatusId = item . StatusId ,
497- StatusName = item . Status . StatusName ,
498- ImagePaths = item . PostImage . Select ( img => img . ImagePath ) . ToList ( ) ,
499- PosterUsername = context . ACCOUNT
500- . Where ( a => a . gordon_id == item . PostedById . ToString ( ) )
501- . Select ( a => a . AD_Username )
502- . FirstOrDefault ( )
503- } )
504- . ToList ( ) ;
492+ . Include ( x => x . Category )
493+ . Include ( x => x . Condition )
494+ . Include ( x => x . Status )
495+ . Include ( x => x . PostImage )
496+ . Where ( x => x . OriginalPostId == threadId || x . Id == threadId )
497+ . OrderByDescending ( item => item . Id )
498+ . Join (
499+ context . ACCOUNT ,
500+ item => item . PostedById . ToString ( ) ,
501+ acc => acc . gordon_id ,
502+ ( item , acc ) => new { item , acc }
503+ )
504+ . Select ( x => new MarketplaceListingViewModel
505+ {
506+ Id = x . item . Id ,
507+ PostedAt = x . item . PostedAt ,
508+ Name = x . item . Name ,
509+ Price = x . item . Price ,
510+ CategoryId = x . item . CategoryId ,
511+ CategoryName = x . item . Category . CategoryName ,
512+ Detail = x . item . Detail ,
513+ ConditionId = x . item . ConditionId ,
514+ ConditionName = x . item . Condition . ConditionName ,
515+ StatusId = x . item . StatusId ,
516+ StatusName = x . item . Status . StatusName ,
517+ ImagePaths = x . item . PostImage . Select ( img => img . ImagePath ) . ToList ( ) ,
518+ PosterUsername = x . acc != null ? x . acc . AD_Username : null
519+ } )
520+ . ToList ( ) ;
505521
506522 return history ;
507523 }
0 commit comments