@@ -26,110 +26,32 @@ IAccountService accountService
2626 /// </summary>
2727 public IEnumerable < MarketplaceListingViewModel > GetAllListings ( )
2828 {
29- var items = context . PostedItem
30- . Include ( x => x . Category )
31- . Include ( x => x . Condition )
32- . Include ( x => x . Status )
33- . Include ( x => x . PostImage )
34- . Where ( item => item . StatusId != 3 )
35- . OrderByDescending ( item => item . PostedAt )
36- . ToList ( ) ; // Materialize to memory
37-
38- var accountDict = context . ACCOUNT
39- . Where ( a => items . Select ( i => i . PostedById . ToString ( ) ) . Distinct ( ) . Contains ( a . gordon_id ) )
40- . ToDictionary ( a => a . gordon_id , a => a . AD_Username ) ;
41-
42- return items . Select ( item => new MarketplaceListingViewModel
43- {
44- Id = item . Id ,
45- PostedAt = item . PostedAt ,
46- Name = item . Name ,
47- Price = item . Price ,
48- CategoryId = item . CategoryId ,
49- CategoryName = item . Category ? . CategoryName ,
50- Detail = item . Detail ,
51- ConditionId = item . ConditionId ,
52- ConditionName = item . Condition ? . ConditionName ,
53- StatusId = item . StatusId ,
54- StatusName = item . Status ? . StatusName ,
55- ImagePaths = item . PostImage ? . Select ( img => img . ImagePath ) . ToList ( ) ?? new List < string > ( ) ,
56- PosterUsername = accountDict . TryGetValue ( item . PostedById . ToString ( ) , out var username ) ? username : null
57- } ) . ToList ( ) ;
29+ return context . Post
30+ . Where ( post => post . StatusId != 3 )
31+ . OrderByDescending ( post => post . PostedAt )
32+ . AsEnumerable ( )
33+ . Select ( post => MarketplaceListingViewModel . From ( post ) ) ;
5834 }
5935
6036 public IEnumerable < MarketplaceListingViewModel > GetUserListings ( string username )
6137 {
62- var account = accountService . GetAccountByUsername ( username ) ;
63- if ( account == null ) return new List < MarketplaceListingViewModel > ( ) ;
64-
65- int userId = int . Parse ( account . GordonID ) ;
66-
67- var items = context . PostedItem
68- . Include ( x => x . Category )
69- . Include ( x => x . Condition )
70- . Include ( x => x . Status )
71- . Include ( x => x . PostImage )
72- . Where ( item => item . PostedById == userId && item . StatusId != 3 )
38+ return context . Post
39+ . Where ( item => item . PostedByUsername == username && item . StatusId != 3 )
7340 . OrderByDescending ( item => item . PostedAt )
74- . ToList ( ) ;
41+ . Select ( post => MarketplaceListingViewModel . From ( post ) ) ;
7542
76- var accountDict = context . ACCOUNT
77- . Where ( a => items . Select ( i => i . PostedById . ToString ( ) ) . Distinct ( ) . Contains ( a . gordon_id ) )
78- . ToDictionary ( a => a . gordon_id , a => a . AD_Username ) ;
79-
80- return items . Select ( item => new MarketplaceListingViewModel
81- {
82- Id = item . Id ,
83- PostedAt = item . PostedAt ,
84- Name = item . Name ,
85- Price = item . Price ,
86- CategoryId = item . CategoryId ,
87- CategoryName = item . Category ? . CategoryName ,
88- Detail = item . Detail ,
89- ConditionId = item . ConditionId ,
90- ConditionName = item . Condition ? . ConditionName ,
91- StatusId = item . StatusId ,
92- StatusName = item . Status ? . StatusName ,
93- ImagePaths = item . PostImage ? . Select ( img => img . ImagePath ) . ToList ( ) ?? new List < string > ( ) ,
94- PosterUsername = accountDict . TryGetValue ( item . PostedById . ToString ( ) , out var username ) ? username : null
95- } ) . ToList ( ) ;
9643 }
9744
9845 /// <summary>
9946 /// Get a specific marketplace listing by ID.
10047 /// </summary>
10148 public MarketplaceListingViewModel GetListingById ( int listingId )
10249 {
103- var listing = context . PostedItem
104- . Include ( x => x . Category )
105- . Include ( x => x . Condition )
106- . Include ( x => x . Status )
107- . Include ( x => x . PostImage )
108- . FirstOrDefault ( x => x . Id == listingId ) ;
109-
110- if ( listing == null )
111- {
112- throw new ResourceNotFoundException { ExceptionMessage = "Listing not found." } ;
113- }
114-
115- var account = context . ACCOUNT . FirstOrDefault ( a => a . gordon_id == listing . PostedById . ToString ( ) ) ;
50+ var listing = context . Post . FirstOrDefault ( post => post . Id == listingId ) ;
11651
117- return new MarketplaceListingViewModel
118- {
119- Id = listing . Id ,
120- PostedAt = listing . PostedAt ,
121- Name = listing . Name ,
122- Price = listing . Price ,
123- CategoryId = listing . CategoryId ,
124- CategoryName = listing . Category ? . CategoryName ,
125- Detail = listing . Detail ,
126- ConditionId = listing . ConditionId ,
127- ConditionName = listing . Condition ? . ConditionName ,
128- StatusId = listing . StatusId ,
129- StatusName = listing . Status ? . StatusName ,
130- ImagePaths = listing . PostImage ? . Select ( img => img . ImagePath ) . ToList ( ) ?? new List < string > ( ) ,
131- PosterUsername = account ? . AD_Username
132- } ;
52+ return listing == null
53+ ? throw new ResourceNotFoundException { ExceptionMessage = "Listing not found." }
54+ : MarketplaceListingViewModel . From ( listing ) ;
13355 }
13456
13557 /// <summary>
@@ -296,11 +218,7 @@ public IEnumerable<MarketplaceListingViewModel> GetFilteredListings(
296218 string ? search , string ? sortBy , bool desc = false ,
297219 int page = 1 , int pageSize = 20 )
298220 {
299- var query = context . PostedItem
300- . Include ( x => x . Category )
301- . Include ( x => x . Condition )
302- . Include ( x => x . Status )
303- . Include ( x => x . PostImage )
221+ var query = context . Post
304222 . Where ( x => x . StatusId != 3 ) ;
305223
306224 if ( categoryId . HasValue )
@@ -338,28 +256,7 @@ public IEnumerable<MarketplaceListingViewModel> GetFilteredListings(
338256 // Pagination
339257 query = query . Skip ( ( page - 1 ) * pageSize ) . Take ( pageSize ) ;
340258
341- var items = query . ToList ( ) ;
342-
343- var accountDict = context . ACCOUNT
344- . Where ( a => items . Select ( i => i . PostedById . ToString ( ) ) . Distinct ( ) . Contains ( a . gordon_id ) )
345- . ToDictionary ( a => a . gordon_id , a => a . AD_Username ) ;
346-
347- return items . Select ( item => new MarketplaceListingViewModel
348- {
349- Id = item . Id ,
350- PostedAt = item . PostedAt ,
351- Name = item . Name ,
352- Price = item . Price ,
353- CategoryId = item . CategoryId ,
354- CategoryName = item . Category ? . CategoryName ,
355- Detail = item . Detail ,
356- ConditionId = item . ConditionId ,
357- ConditionName = item . Condition ? . ConditionName ,
358- StatusId = item . StatusId ,
359- StatusName = item . Status ? . StatusName ,
360- ImagePaths = item . PostImage ? . Select ( img => img . ImagePath ) . ToList ( ) ?? new List < string > ( ) ,
361- PosterUsername = accountDict . TryGetValue ( item . PostedById . ToString ( ) , out var username ) ? username : null
362- } ) . ToList ( ) ;
259+ return query . Select ( post => MarketplaceListingViewModel . From ( post ) ) ;
363260 }
364261
365262
0 commit comments