Skip to content

Commit cbc3de5

Browse files
committed
Added initial work for Fallback image
1 parent d9f1cef commit cbc3de5

File tree

10 files changed

+64
-8
lines changed

10 files changed

+64
-8
lines changed

src/LinkDotNet.Blog.Domain/BlogPost.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ private BlogPost()
1818

1919
public string PreviewImageUrl { get; private set; }
2020

21+
public string PreviewImageUrlFallback { get; private set; }
22+
2123
public DateTime UpdatedDate { get; private set; }
2224

2325
public virtual ICollection<Tag> Tags { get; private set; }
@@ -33,7 +35,8 @@ public static BlogPost Create(
3335
string previewImageUrl,
3436
bool isPublished,
3537
DateTime? updatedDate = null,
36-
IEnumerable<string> tags = null)
38+
IEnumerable<string> tags = null,
39+
string previewImageUrlFallback = null)
3740
{
3841
var blogPost = new BlogPost
3942
{
@@ -42,6 +45,7 @@ public static BlogPost Create(
4245
Content = content,
4346
UpdatedDate = updatedDate ?? DateTime.Now,
4447
PreviewImageUrl = previewImageUrl,
48+
PreviewImageUrlFallback = previewImageUrlFallback,
4549
IsPublished = isPublished,
4650
Tags = tags?.Select(Tag.Create).ToList(),
4751
};
@@ -61,6 +65,7 @@ public void Update(BlogPost from)
6165
Content = from.Content;
6266
UpdatedDate = from.UpdatedDate;
6367
PreviewImageUrl = from.PreviewImageUrl;
68+
PreviewImageUrlFallback = from.PreviewImageUrlFallback;
6469
IsPublished = from.IsPublished;
6570
ReplaceTags(from.Tags);
6671
}

src/LinkDotNet.Blog.Infrastructure/Persistence/Sql/Mapping/BlogPostConfiguration.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public void Configure(EntityTypeBuilder<BlogPost> builder)
1717
builder.Navigation(x => x.Tags).AutoInclude();
1818
builder.Property(x => x.Title).HasMaxLength(256).IsRequired();
1919
builder.Property(x => x.PreviewImageUrl).HasMaxLength(1024).IsRequired();
20+
builder.Property(x => x.PreviewImageUrlFallback).HasMaxLength(1024);
2021
builder.Property(x => x.Content).IsRequired();
2122
builder.Property(x => x.ShortDescription).IsRequired();
2223
builder.Property(x => x.Likes).IsRequired();

src/LinkDotNet.Blog.Web/Features/Admin/BlogPostEditor/Components/CreateNewBlogPost.razor

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@
3131
<label for="preview">Preview-Url</label>
3232
<InputText class="form-control" id="preview" @bind-Value="model.PreviewImageUrl"/>
3333
</div>
34+
<div class="mb-3">
35+
<label for="preview">Fallback preview-Url</label>
36+
<InputText class="form-control" id="fallback-preview" @bind-Value="model.PreviewImageUrlFallback"/>
37+
<small for="allback-preview" class="form-text text-muted">Optional: Used as a fallback if the preview image can't be used by the browser.</small>
38+
</div>
3439
<div class="form-check">
3540
<InputCheckbox class="form-check-input" id="published" @bind-Value="model.IsPublished" />
3641
<label class="form-check-label" for="published">Publish</label><br/>

src/LinkDotNet.Blog.Web/Features/Admin/BlogPostEditor/Components/CreateNewModel.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ public class CreateNewModel
3131

3232
public DateTime OriginalUpdatedDate { get; set; }
3333

34+
public string PreviewImageUrlFallback { get; set; }
35+
3436
public static CreateNewModel FromBlogPost(BlogPost blogPost)
3537
{
3638
return new CreateNewModel
@@ -43,6 +45,7 @@ public static CreateNewModel FromBlogPost(BlogPost blogPost)
4345
IsPublished = blogPost.IsPublished,
4446
PreviewImageUrl = blogPost.PreviewImageUrl,
4547
OriginalUpdatedDate = blogPost.UpdatedDate,
48+
PreviewImageUrlFallback = blogPost.PreviewImageUrlFallback,
4649
};
4750
}
4851

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
@if (string.IsNullOrEmpty(PreviewImageUrlFallback))
2+
{
3+
<img src="@PreviewImageUrl" alt="Preview image blogpost" loading="@LazyLoadTag"/>
4+
}
5+
else
6+
{
7+
<picture>
8+
<source srcset="@PreviewImageUrl" />
9+
<img src="@PreviewImageUrlFallback" alt="Preview image blogpost" loading="@LazyLoadTag"/>
10+
</picture>
11+
}
12+
13+
@code {
14+
[Parameter]
15+
public string PreviewImageUrl { get; set; }
16+
17+
[Parameter]
18+
public string PreviewImageUrlFallback { get; set; }
19+
20+
[Parameter]
21+
public bool LazyLoadImage { get; set; }
22+
23+
private string LazyLoadTag => LazyLoadImage ? "lazy" : "eager";
24+
}

src/LinkDotNet.Blog.Web/Features/Components/ShortBlogPost.razor

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@
55
<article>
66
<div class="blog-card @AltCssClass">
77
<div class="meta">
8-
<div class="photo" style="background-image: url(@BlogPost.PreviewImageUrl)"></div>
8+
<div>
9+
<PreviewImage PreviewImageUrl="@BlogPost.PreviewImageUrl"
10+
PreviewImageUrlFallback="@BlogPost.PreviewImageUrlFallback"
11+
LazyLoadImage="@LazyLoadPreviewImage"></PreviewImage>
12+
</div>
13+
@* <div class="photo" style="background-image: url(@BlogPost.PreviewImageUrl)"></div> *@
914
<ul class="details">
1015
<li class="date">@BlogPost.UpdatedDate.ToString("dd/MM/yyyy")</li>
1116
@if (BlogPost.Tags != null)
@@ -39,6 +44,9 @@
3944

4045
[Parameter]
4146
public bool UseAlternativeStyle { get; set; }
47+
48+
[Parameter]
49+
public bool LazyLoadPreviewImage { get; set; }
4250

4351
private string AltCssClass => UseAlternativeStyle ? "alt" : string.Empty;
4452

src/LinkDotNet.Blog.Web/Pages/_Layout.cshtml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@
4141
An unhandled exception has occurred. See browser dev tools for details.
4242
</environment>
4343
<a href="" class="reload">Reload</a>
44-
<a class="dismiss">🗙</a>
44+
<a class="dismiss">x</a>
4545
</div>
4646
<script src="_framework/blazor.server.js"></script>
4747
<script async src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.5.1/highlight.min.js" integrity="sha512-yUUc0qWm2rhM7X0EFe82LNnv2moqArj5nro/w1bi05A09hRVeIZbN6jlMoyu0+4I/Bu4Ck/85JQIU82T82M28w==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
48-
<script async src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.5.1/languages/csharp.min.js" integrity="sha512-wOhBmvmYJyIcnoY/XHP/c7SyyK05H0NleQgId+c6taC3OWIJNj0DbG0J8dAuxpWNjsk84y4yHQraUtJnWHnpNA==" crossorigin="anonymous" referrerpolicy="no-referrer"></script></script>
48+
<script async src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.5.1/languages/csharp.min.js" integrity="sha512-wOhBmvmYJyIcnoY/XHP/c7SyyK05H0NleQgId+c6taC3OWIJNj0DbG0J8dAuxpWNjsk84y4yHQraUtJnWHnpNA==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
4949
<script async src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js" integrity="sha256-cMPWkL3FzjuaFSfEYESYmjF25hCIL6mfRSPnW8OVvM4=" crossorigin="anonymous"></script>
5050
<script async src="components/selection.js" ></script>
5151
<script async src="components/slideshow.js" ></script>

tests/LinkDotNet.Blog.TestUtilities/BlogPostBuilder.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ public class BlogPostBuilder
88
private string title = "BlogPost";
99
private string shortDescription = "Some Text";
1010
private string content = "Some Content";
11-
private string url = "localhost";
11+
private string previewImageUrl = "localhost";
12+
private string previewImageUrlFallback = null;
1213
private bool isPublished = true;
1314
private string[] tags;
1415
private int likes;
@@ -34,7 +35,13 @@ public BlogPostBuilder WithContent(string content)
3435

3536
public BlogPostBuilder WithPreviewImageUrl(string url)
3637
{
37-
this.url = url;
38+
previewImageUrl = url;
39+
return this;
40+
}
41+
42+
public BlogPostBuilder WithPreviewImageUrlFallback(string url)
43+
{
44+
previewImageUrlFallback = url;
3845
return this;
3946
}
4047

@@ -64,7 +71,7 @@ public BlogPostBuilder WithUpdatedDate(DateTime updateDate)
6471

6572
public BlogPost Build()
6673
{
67-
var blogPost = BlogPost.Create(title, shortDescription, content, url, isPublished, updateDate, tags);
74+
var blogPost = BlogPost.Create(title, shortDescription, content, previewImageUrl, isPublished, updateDate, tags, previewImageUrlFallback);
6875
blogPost.Likes = likes;
6976
return blogPost;
7077
}

tests/LinkDotNet.Blog.UnitTests/Domain/BlogPostTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public void ShouldUpdateBlogPost()
1212
{
1313
var blogPostToUpdate = new BlogPostBuilder().Build();
1414
blogPostToUpdate.Id = "random-id";
15-
var blogPost = BlogPost.Create("Title", "Desc", "Content", "Url", true);
15+
var blogPost = BlogPost.Create("Title", "Desc", "Content", "Url", true, previewImageUrlFallback: "Url2");
1616
blogPost.Id = "something else";
1717

1818
blogPostToUpdate.Update(blogPost);
@@ -21,6 +21,7 @@ public void ShouldUpdateBlogPost()
2121
blogPostToUpdate.ShortDescription.Should().Be("Desc");
2222
blogPostToUpdate.Content.Should().Be("Content");
2323
blogPostToUpdate.PreviewImageUrl.Should().Be("Url");
24+
blogPostToUpdate.PreviewImageUrlFallback.Should().Be("Url2");
2425
blogPostToUpdate.IsPublished.Should().BeTrue();
2526
blogPostToUpdate.Tags.Should().BeNullOrEmpty();
2627
}

tests/LinkDotNet.Blog.UnitTests/Web/Features/Admin/BlogPostEditor/Components/CreateNewBlogPostTests.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public void ShouldCreateNewBlogPostWhenValidDataGiven()
2424
cut.Find("#short").Input("My short Description");
2525
cut.Find("#content").Input("My content");
2626
cut.Find("#preview").Change("My preview url");
27+
cut.Find("#fallback-preview").Change("My fallback preview url");
2728
cut.Find("#published").Change(false);
2829
cut.Find("#tags").Change("Tag1,Tag2,Tag3");
2930

@@ -35,6 +36,7 @@ public void ShouldCreateNewBlogPostWhenValidDataGiven()
3536
blogPost.ShortDescription.Should().Be("My short Description");
3637
blogPost.Content.Should().Be("My content");
3738
blogPost.PreviewImageUrl.Should().Be("My preview url");
39+
blogPost.PreviewImageUrlFallback.Should().Be("My fallback preview url");
3840
blogPost.IsPublished.Should().BeFalse();
3941
blogPost.UpdatedDate.Should().NotBe(default);
4042
blogPost.Tags.Should().HaveCount(3);

0 commit comments

Comments
 (0)