Skip to content

Commit 35b51f0

Browse files
committed
Use Marketplace.Post view for more efficient querying
1 parent 19bb4f2 commit 35b51f0

File tree

5 files changed

+108
-120
lines changed

5 files changed

+108
-120
lines changed

Gordon360/Models/CCT/Context/CCTContext.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,8 @@ public CCTContext(DbContextOptions<CCTContext> options)
146146

147147
public virtual DbSet<ParticipantView> ParticipantView { get; set; }
148148

149+
public virtual DbSet<Post> Post { get; set; }
150+
149151
public virtual DbSet<PostImage> PostImage { get; set; }
150152

151153
public virtual DbSet<PostedItem> PostedItem { get; set; }
@@ -670,6 +672,11 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
670672
entity.Property(e => e.SpecifiedGender).IsFixedLength();
671673
});
672674

675+
modelBuilder.Entity<Post>(entity =>
676+
{
677+
entity.ToView("Post", "Marketplace");
678+
});
679+
673680
modelBuilder.Entity<PostImage>(entity =>
674681
{
675682
entity.HasOne(d => d.PostedItem).WithMany(p => p.PostImage)

Gordon360/Models/CCT/Context/efpt.CCT.config.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,10 @@
418418
"Name": "[LostAndFound].[MissingItemData]",
419419
"ObjectType": 3
420420
},
421+
{
422+
"Name": "[Marketplace].[Post]",
423+
"ObjectType": 3
424+
},
421425
{
422426
"Name": "[RecIM].[ParticipantView]",
423427
"ObjectType": 3
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// <auto-generated> This file has been auto generated by EF Core Power Tools. </auto-generated>
2+
#nullable disable
3+
using System;
4+
using System.Collections.Generic;
5+
using System.ComponentModel.DataAnnotations;
6+
using System.ComponentModel.DataAnnotations.Schema;
7+
using Microsoft.EntityFrameworkCore;
8+
9+
namespace Gordon360.Models.CCT;
10+
11+
[Keyless]
12+
public partial class Post
13+
{
14+
public int Id { get; set; }
15+
16+
public int CategoryId { get; set; }
17+
18+
[Required]
19+
[StringLength(50)]
20+
public string CategoryName { get; set; }
21+
22+
public int ConditionId { get; set; }
23+
24+
[Required]
25+
[StringLength(50)]
26+
public string ConditionName { get; set; }
27+
28+
public DateTime? DeletedAt { get; set; }
29+
30+
[Required]
31+
[StringLength(1000)]
32+
public string Detail { get; set; }
33+
34+
[Required]
35+
[StringLength(50)]
36+
public string Name { get; set; }
37+
38+
public int? OriginalPostId { get; set; }
39+
40+
public DateTime PostedAt { get; set; }
41+
42+
public int PostedById { get; set; }
43+
44+
[StringLength(50)]
45+
[Unicode(false)]
46+
public string PostedByUsername { get; set; }
47+
48+
[Column(TypeName = "money")]
49+
public decimal Price { get; set; }
50+
51+
public int StatusId { get; set; }
52+
53+
[Required]
54+
[StringLength(50)]
55+
public string StatusName { get; set; }
56+
57+
[StringLength(8000)]
58+
[Unicode(false)]
59+
public string ImagePaths { get; set; }
60+
}

Gordon360/Models/ViewModels/MarketplaceListingViewModel.cs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1+
using Gordon360.Models.CCT;
12
using System;
23
using System.Collections.Generic;
34
using System.Linq;
4-
using Gordon360.Models.CCT;
5-
using Gordon360.Models.CCT.Context;
65

76

87
namespace Gordon360.Models.ViewModels
@@ -21,7 +20,7 @@ public class MarketplaceListingViewModel
2120
public int StatusId { get; set; }
2221
public string StatusName { get; set; }
2322
public List<string> ImagePaths { get; set; }
24-
public string PosterUsername { get; set; }
23+
public string PosterUsername { get; set; }
2524

2625
public static implicit operator MarketplaceListingViewModel(PostedItem item)
2726
{
@@ -41,5 +40,26 @@ public static implicit operator MarketplaceListingViewModel(PostedItem item)
4140
ImagePaths = item.PostImage?.Select(img => img.ImagePath).ToList() ?? new List<string>(),
4241
};
4342
}
43+
44+
45+
public static MarketplaceListingViewModel From(Post post)
46+
{
47+
return new MarketplaceListingViewModel
48+
{
49+
Id = post.Id,
50+
PostedAt = post.PostedAt,
51+
Name = post.Name,
52+
Price = post.Price,
53+
CategoryId = post.CategoryId,
54+
CategoryName = post.CategoryName,
55+
Detail = post.Detail,
56+
ConditionId = post.ConditionId,
57+
ConditionName = post.ConditionName,
58+
StatusId = post.StatusId,
59+
StatusName = post.StatusName,
60+
ImagePaths = post.ImagePaths?.Split(";")?.ToList() ?? [],
61+
PosterUsername = post.PostedByUsername
62+
};
63+
}
4464
}
4565
}

Gordon360/Services/MarketplaceService.cs

Lines changed: 14 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)