Skip to content

Commit a952671

Browse files
danielmpetrovcsharpfritz
authored andcommitted
Default article rename bugfix (#338)
* Check for id, not slug constant * Add IsHomePage property to Domain Article * Forbid access to <default_article>/delete * Update DetailsModel * Use automapper in SearchModel
1 parent 77e9591 commit a952671

File tree

15 files changed

+31
-43
lines changed

15 files changed

+31
-43
lines changed

CoreWiki.Application/Articles/Managing/Dto/ArticleManageDto.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ namespace CoreWiki.Application.Articles.Managing.Dto
55
{
66
public class ArticleManageDto
77
{
8+
public bool IsHomePage { get; set; }
89
public string Content { get; set; }
910
public int Id { get; set; }
1011
public string Slug { get; set; }

CoreWiki.Application/Articles/Reading/Dto/ArticleReadingDto.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ namespace CoreWiki.Application.Articles.Reading.Dto
55
{
66
public class ArticleReadingDto
77
{
8+
public bool IsHomePage { get; set; }
89
public string Content { get; set; }
910
public int Id { get; set; }
1011
public string Slug { get; set; }

CoreWiki.Application/Articles/Search/Dto/ArticleSearchDto.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ namespace CoreWiki.Application.Articles.Search.Dto
55
{
66
public class ArticleSearchDto
77
{
8+
public bool IsHomePage { get; set; }
89
public string Content { get; set; }
910
public int Id { get; set; }
1011
public string Slug { get; set; }

CoreWiki.Core/Domain/Article.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ public string Topic { get
3737

3838
public string Content { get; set; }
3939

40+
public bool IsHomePage => Id == 1;
41+
4042
public virtual ICollection<Comment> Comments { get; set; }
4143

4244
public virtual ICollection<ArticleHistory> History { get; set; }

CoreWiki.Data/ApplicationDbContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
2323
Id = 1,
2424
Topic = "Home Page",
2525
Slug = "home-page",
26-
Content = "This is the default home page. Please change me!",
26+
Content = "This is the default home page. Please change me!",
2727
Published = Instant.FromDateTimeUtc(new DateTime(2018, 6, 19, 14, 31, 2, 265, DateTimeKind.Utc)),
2828
AuthorId = Guid.Empty
2929
};

CoreWiki.FirstStart/StartupExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public static IApplicationBuilder UseFirstStartConfiguration(this IApplicationBu
3636

3737
private static bool IsFirstStartIncomplete(HttpContext context)
3838
{
39-
return true;
39+
return false;
4040
}
4141
}
4242

CoreWiki.Test/Pages/SearchTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using CoreWiki.Application.Articles.Search.Dto;
22
using CoreWiki.Application.Articles.Search.Queries;
3+
using CoreWiki.Configuration.Startup;
34
using CoreWiki.Pages;
45
using MediatR;
56
using Moq;
@@ -30,7 +31,7 @@ public async Task OnGetAsync_WithPageNumberEqualsTo2And12Posts_ShouldReturnCurre
3031
}));
3132

3233
// Act
33-
var searchModel = new SearchModel(mediator.Object);
34+
var searchModel = new SearchModel(mediator.Object, ConfigureAutomapperServices.ConfigureAutomapper(null));
3435

3536
var result = await searchModel.OnGetAsync(query: "test", pageNumber: 2);
3637

CoreWiki/Configuration/Startup/CoreWikiWebsiteProfile.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Security.Claims;
34
using AutoMapper;
45
using CoreWiki.Application.Articles.Managing.Commands;
56
using CoreWiki.Application.Articles.Reading.Commands;
67
using CoreWiki.Application.Articles.Reading.Dto;
8+
using CoreWiki.Application.Articles.Search.Dto;
79
using CoreWiki.Application.Common;
810
using CoreWiki.ViewModels;
911

@@ -37,6 +39,10 @@ public CoreWikiWebsiteProfile()
3739
CreateMap<ClaimsPrincipal, EditArticleCommand>(MemberList.None)
3840
.ForMember(d => d.AuthorId, m => m.MapFrom(s => Guid.Parse(s.FindFirstValue(ClaimTypes.NameIdentifier))))
3941
.ForMember(d => d.AuthorName, m => m.MapFrom(s => s.Identity.Name));
42+
43+
CreateMap<IList<ArticleReadingDto>, SearchResultDto<ArticleSummary>>(MemberList.None)
44+
.ForMember(d => d.Results, m => m.MapFrom(s => s))
45+
.ForMember(d => d.TotalResults, m => m.MapFrom(s => s.Count));
4046
}
4147
}
4248
}

CoreWiki/Pages/Delete.cshtml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public async Task<IActionResult> OnGetAsync(string slug)
4444
return NotFound();
4545
}
4646

47-
if (article.Slug == UrlHelpers.HomePageSlug)
47+
if (article.IsHomePage)
4848
{
4949
await _mediator.Publish(new DeleteHomePageAttemptNotification());
5050
return Forbid();

CoreWiki/Pages/Details.cshtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
<div>
4343
<a asp-page="/Edit" asp-route-slug="@Model.Article.Slug" asp-policy="@PolicyConstants.CanEditArticles" class="btn btn-outline-info">@Localizer["Edit"]</a>
4444

45-
@if (Model.Article.Slug != UrlHelpers.HomePageSlug)
45+
@if (!Model.Article.IsHomePage)
4646
{
4747
<a href="~/" class="btn btn-secondary">@Localizer["GoHome"]</a>
4848
}

CoreWiki/Pages/Details.cshtml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public DetailsModel(IMediator mediator, IMapper mapper)
2626

2727
public ArticleDetails Article { get; set; }
2828

29-
[ViewDataAttribute]
29+
[ViewData]
3030
public string Slug { get; set; }
3131

3232
public async Task<IActionResult> OnGetAsync(string slug)

CoreWiki/Pages/Search.cshtml.cs

Lines changed: 10 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,26 @@
11
using CoreWiki.ViewModels;
22
using Microsoft.AspNetCore.Mvc;
33
using Microsoft.AspNetCore.Mvc.RazorPages;
4-
using System.Linq;
54
using System.Threading.Tasks;
65
using CoreWiki.Application.Articles.Reading.Queries;
76
using CoreWiki.Application.Articles.Search.Dto;
87
using CoreWiki.Application.Articles.Search.Queries;
98
using MediatR;
9+
using AutoMapper;
1010

1111
namespace CoreWiki.Pages
1212
{
1313
public class SearchModel : PageModel
1414
{
1515
private readonly IMediator _mediator;
16+
private readonly IMapper _mapper;
1617
public SearchResultDto<ArticleSummary> SearchResult;
1718
private const int ResultsPerPage = 10;
1819

19-
public SearchModel(IMediator mediator)
20+
public SearchModel(IMediator mediator, IMapper mapper)
2021
{
2122
_mediator = mediator;
23+
_mapper = mapper;
2224
}
2325

2426
public string RequestedPage => Request.Path.Value.ToLowerInvariant().Substring(1);
@@ -29,28 +31,10 @@ public async Task<IActionResult> OnGetAsync([FromQuery(Name = "Query")]string qu
2931
{
3032
return Page();
3133
}
32-
var qry = new SearchArticlesQuery(query,
33-
pageNumber,
34-
ResultsPerPage);
34+
var qry = new SearchArticlesQuery(query, pageNumber, ResultsPerPage);
3535
var result = await _mediator.Send(qry);
3636

37-
//todo: use automapper
38-
SearchResult = new SearchResultDto<ArticleSummary>
39-
{
40-
Query = result.Query,
41-
TotalResults = result.TotalResults,
42-
ResultsPerPage = result.ResultsPerPage,
43-
CurrentPage = result.CurrentPage,
44-
Results = (from article in result.Results
45-
select new ArticleSummary
46-
{
47-
Slug = article.Slug,
48-
Topic = article.Topic,
49-
Published = article.Published,
50-
ViewCount = article.ViewCount
51-
}).ToList()
52-
};
53-
//SearchResult.CurrentPage = 1;
37+
SearchResult = _mapper.Map<SearchResultDto<ArticleSummary>>(result);
5438

5539
return Page();
5640
}
@@ -60,20 +44,10 @@ public async Task<IActionResult> OnGetLatestChangesAsync()
6044
var qry = new GetLatestArticlesQuery(10);
6145
var results = await _mediator.Send(qry);
6246

63-
SearchResult = new SearchResultDto<ArticleSummary>
64-
{
65-
Results = (from article in results
66-
select new ArticleSummary
67-
{
68-
Slug = article.Slug,
69-
Topic = article.Topic,
70-
Published = article.Published,
71-
ViewCount = article.ViewCount
72-
}).ToList(),
73-
ResultsPerPage = 11,
74-
CurrentPage = 1
75-
};
76-
SearchResult.TotalResults = SearchResult.Results.Count;
47+
SearchResult = _mapper.Map<SearchResultDto<ArticleSummary>>(results);
48+
SearchResult.ResultsPerPage = 11;
49+
SearchResult.CurrentPage = 1;
50+
7751
return Page();
7852
}
7953
}

CoreWiki/Pages/_ArticleRow.cshtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
<h6 class="card-subtitle mb-2 text-muted">@Localizer["ViewCount"]: <span> @Model.ViewCount</span></h6>
1717

1818
<a class="card-link btn btn-outline-info btn-sm" asp-page="/Edit" asp-route-slug="@Model.Slug">@Localizer["Edit"]</a>
19-
@if (Model.Slug != UrlHelpers.HomePageSlug)
19+
@if (!Model.IsHomePage)
2020
{
2121
<a class="card-link btn btn-outline-danger btn-sm" asp-policy="CanDeleteArticles" asp-policy-message="You do not have permission to delete this article." asp-page="/Delete" asp-route-slug="@Model.Slug">@Localizer["Delete"]</a>
2222
}

CoreWiki/ViewModels/ArticleDetails.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ namespace CoreWiki.ViewModels
66
{
77
public class ArticleDetails
88
{
9+
public bool IsHomePage { get; set; }
910
public int Id { get; set; }
1011
public Guid AuthorId { get; set; }
1112
public string Slug { get; set; }

CoreWiki/ViewModels/ArticleSummary.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ namespace CoreWiki.ViewModels
44
{
55
public class ArticleSummary
66
{
7+
public bool IsHomePage { get; set; }
78
public string Slug { get; set; }
89
public string Topic { get; set; }
910
public Instant Published { get; set; }

0 commit comments

Comments
 (0)