Skip to content

Commit 554c952

Browse files
committed
Fixed tip search
1 parent e43c8c7 commit 554c952

7 files changed

Lines changed: 76 additions & 24 deletions

File tree

Shared/TipModel.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,18 @@ public class TipSearchRequest
7070
public int PageSize { get; set; } = 10;
7171
}
7272

73+
/// <summary>
74+
/// Result model for tip search operations containing both results and total count
75+
/// </summary>
76+
public class TipSearchResult
77+
{
78+
public List<TipModel> Tips { get; set; } = new();
79+
public int TotalCount { get; set; }
80+
public int Page { get; set; } = 1;
81+
public int PageSize { get; set; } = 10;
82+
public int TotalPages => (int)Math.Ceiling((double)TotalCount / PageSize);
83+
}
84+
7385
/// <summary>
7486
/// DTO for API responses
7587
/// </summary>

Web/Pages/Tips/Category.cshtml.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,18 +66,17 @@ public async Task<IActionResult> OnGetAsync()
6666
PageSize = 12
6767
};
6868

69-
var tips = await _contentService.SearchTipsAsync(request);
70-
var totalCount = tips.Count;
69+
var searchResult = await _contentService.SearchTipsAsync(request);
7170

7271
ViewModel = new TipListViewModel
7372
{
74-
Tips = tips,
73+
Tips = searchResult.Tips,
7574
Categories = categories,
7675
Tags = tags,
7776
SelectedCategory = Category,
7877
Page = PageNumber,
79-
PageSize = request.PageSize,
80-
TotalCount = totalCount
78+
PageSize = searchResult.PageSize,
79+
TotalCount = searchResult.TotalCount
8180
};
8281

8382
ViewData["Title"] = $"{Category} Tips & Tricks";

Web/Pages/Tips/Index.cshtml

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,14 @@
4444
<option value="">All Categories</option>
4545
@foreach (var cat in Model.ViewModel.Categories)
4646
{
47-
<option value="@cat" selected="@(cat == Model.Category)">@cat</option>
47+
if (cat == Model.Category)
48+
{
49+
<option value="@cat" selected>@cat</option>
50+
}
51+
else
52+
{
53+
<option value="@cat">@cat</option>
54+
}
4855
}
4956
</select>
5057
</div>
@@ -56,7 +63,14 @@
5663
<option value="">All Tags</option>
5764
@foreach (var tagOption in Model.ViewModel.Tags)
5865
{
59-
<option value="@tagOption" selected="@(tagOption == Model.Tag)">@tagOption</option>
66+
if (tagOption == Model.Tag)
67+
{
68+
<option value="@tagOption" selected>@tagOption</option>
69+
}
70+
else
71+
{
72+
<option value="@tagOption">@tagOption</option>
73+
}
6074
}
6175
</select>
6276
</div>
@@ -66,9 +80,30 @@
6680
<label for="difficulty" class="form-label">Difficulty</label>
6781
<select class="form-select" id="difficulty" name="difficulty">
6882
<option value="">All Levels</option>
69-
<option value="Beginner" selected="@(Model.Difficulty == "Beginner")">Beginner</option>
70-
<option value="Intermediate" selected="@(Model.Difficulty == "Intermediate")">Intermediate</option>
71-
<option value="Advanced" selected="@(Model.Difficulty == "Advanced")">Advanced</option>
83+
@if (Model.Difficulty == "Beginner")
84+
{
85+
<option value="Beginner" selected>Beginner</option>
86+
}
87+
else
88+
{
89+
<option value="Beginner">Beginner</option>
90+
}
91+
@if (Model.Difficulty == "Intermediate")
92+
{
93+
<option value="Intermediate" selected>Intermediate</option>
94+
}
95+
else
96+
{
97+
<option value="Intermediate">Intermediate</option>
98+
}
99+
@if (Model.Difficulty == "Advanced")
100+
{
101+
<option value="Advanced" selected>Advanced</option>
102+
}
103+
else
104+
{
105+
<option value="Advanced">Advanced</option>
106+
}
72107
</select>
73108
</div>
74109

Web/Pages/Tips/Index.cshtml.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,24 +48,23 @@ public async Task<IActionResult> OnGetAsync()
4848
PageSize = 12
4949
};
5050

51-
var tips = await _contentService.SearchTipsAsync(request);
52-
var totalCount = tips.Count;
51+
var searchResult = await _contentService.SearchTipsAsync(request);
5352

5453
// Get filter options
5554
var categories = await _contentService.GetCategoriesAsync();
5655
var tags = await _contentService.GetTagsAsync();
5756

5857
ViewModel = new TipListViewModel
5958
{
60-
Tips = tips,
59+
Tips = searchResult.Tips,
6160
Categories = categories,
6261
Tags = tags,
6362
SelectedCategory = Category,
6463
SelectedTag = Tag,
6564
SearchTerm = Search,
6665
Page = PageNumber,
67-
PageSize = request.PageSize,
68-
TotalCount = totalCount
66+
PageSize = searchResult.PageSize,
67+
TotalCount = searchResult.TotalCount
6968
};
7069

7170
ViewData["Title"] = "AI Tips & Tricks";

Web/Pages/Tips/Tag.cshtml.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,22 +49,21 @@ public async Task<IActionResult> OnGetAsync()
4949
PageSize = 12
5050
};
5151

52-
var tips = await _contentService.SearchTipsAsync(request);
53-
var totalCount = tips.Count;
52+
var searchResult = await _contentService.SearchTipsAsync(request);
5453

5554
// Get filter options
5655
var categories = await _contentService.GetCategoriesAsync();
5756
var tags = await _contentService.GetTagsAsync();
5857

5958
ViewModel = new TipListViewModel
6059
{
61-
Tips = tips,
60+
Tips = searchResult.Tips,
6261
Categories = categories,
6362
Tags = tags,
6463
SelectedTag = Tag,
6564
Page = PageNumber,
66-
PageSize = request.PageSize,
67-
TotalCount = totalCount
65+
PageSize = searchResult.PageSize,
66+
TotalCount = searchResult.TotalCount
6867
};
6968

7069
ViewData["Title"] = $"#{Tag} Tips & Tricks";

Web/Services/ContentService.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public async Task<List<TipModel>> GetAllTipsAsync()
8787
t.UrlSlug.Equals(slug, StringComparison.OrdinalIgnoreCase));
8888
}
8989

90-
public async Task<List<TipModel>> SearchTipsAsync(TipSearchRequest request)
90+
public async Task<TipSearchResult> SearchTipsAsync(TipSearchRequest request)
9191
{
9292
var tips = await GetTipsFromCacheAsync();
9393
var query = tips.AsQueryable();
@@ -124,14 +124,22 @@ public async Task<List<TipModel>> SearchTipsAsync(TipSearchRequest request)
124124
// Order by published date (newest first)
125125
query = query.OrderByDescending(t => t.PublishedDate);
126126

127-
// Apply pagination
127+
// Get total count before pagination
128128
var totalCount = query.Count();
129+
130+
// Apply pagination
129131
var results = query
130132
.Skip((request.Page - 1) * request.PageSize)
131133
.Take(request.PageSize)
132134
.ToList();
133135

134-
return results;
136+
return new TipSearchResult
137+
{
138+
Tips = results,
139+
TotalCount = totalCount,
140+
Page = request.Page,
141+
PageSize = request.PageSize
142+
};
135143
}
136144

137145
public async Task<List<string>> GetCategoriesAsync()

Web/Services/IContentService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public interface IContentService
99
{
1010
Task<List<TipModel>> GetAllTipsAsync();
1111
Task<TipModel?> GetTipBySlugAsync(string slug);
12-
Task<List<TipModel>> SearchTipsAsync(TipSearchRequest request);
12+
Task<TipSearchResult> SearchTipsAsync(TipSearchRequest request);
1313
Task<List<string>> GetCategoriesAsync();
1414
Task<List<string>> GetTagsAsync();
1515
Task<List<TipModel>> GetRelatedTipsAsync(TipModel tip, int count = 3);

0 commit comments

Comments
 (0)