Skip to content

Commit b24fca0

Browse files
committed
Fixed upsert bug (closes #1)
1 parent 6a26dab commit b24fca0

File tree

4 files changed

+70
-1
lines changed

4 files changed

+70
-1
lines changed

LinkDotNet.Blog.IntegrationTests/Infrastructure/Persistence/Sql/SqlRepositoryTests.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.Linq;
22
using System.Threading.Tasks;
33
using FluentAssertions;
4+
using LinkDotNet.Blog.TestUtilities;
45
using LinkDotNet.Domain;
56
using Microsoft.EntityFrameworkCore;
67
using Xunit;
@@ -66,5 +67,21 @@ public async Task ShouldGetAllBlogPosts()
6667
var tagContent = blogPostFromRepo.Tags.Select(t => t.Content).ToList();
6768
tagContent.Should().Contain(new[] { "Tag 1", "Tag 2" });
6869
}
70+
71+
[Fact]
72+
public async Task ShouldBeUpdateable()
73+
{
74+
var blogPost = new BlogPostBuilder().Build();
75+
await DbContext.BlogPosts.AddAsync(blogPost);
76+
await DbContext.SaveChangesAsync();
77+
var blogPostFromDb = await BlogPostRepository.GetByIdAsync(blogPost.Id);
78+
var updater = new BlogPostBuilder().WithTitle("New Title").Build();
79+
blogPostFromDb.Update(updater);
80+
81+
await BlogPostRepository.StoreAsync(blogPostFromDb);
82+
83+
var blogPostAfterSave = await DbContext.BlogPosts.AsNoTracking().SingleAsync(b => b.Id == blogPostFromDb.Id);
84+
blogPostAfterSave.Title.Should().Be("New Title");
85+
}
6986
}
7087
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
@page "/update/{blogPostId}"
2+
@attribute [Authorize]
3+
@using LinkDotNet.Infrastructure.Persistence
4+
@using LinkDotNet.Blog.Web.Shared.Admin
5+
@using LinkDotNet.Domain
6+
@inherits MarkdownComponentBase
7+
@inject IRepository _repository
8+
@inject IToastService _toastService
9+
10+
@if (blogPostFromDb != null)
11+
{
12+
<CreateNewBlogPost
13+
Title="Update BlogPost"
14+
BlogPost="@blogPostFromDb"
15+
OnBlogPostCreated="StoreBlogPostAsync"></CreateNewBlogPost>
16+
}
17+
else
18+
{
19+
<p>Loading...</p>
20+
}
21+
22+
@code {
23+
[Parameter]
24+
public string BlogPostId { get; set; }
25+
26+
private BlogPost blogPostFromDb;
27+
28+
protected override async Task OnParametersSetAsync()
29+
{
30+
if (string.IsNullOrEmpty(BlogPostId))
31+
{
32+
throw new ArgumentNullException(nameof(BlogPostId), "You have to provide an id");
33+
}
34+
35+
blogPostFromDb = await _repository.GetByIdAsync(BlogPostId);
36+
}
37+
38+
private async Task StoreBlogPostAsync(BlogPost blogPost)
39+
{
40+
blogPostFromDb.Update(blogPost);
41+
await _repository.StoreAsync(blogPostFromDb);
42+
_toastService.ShowInfo($"Updated BlogPost {blogPost.Title}");
43+
}
44+
}

LinkDotNet.Infrastructure/Persistence/Sql/BlogPostRepository.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,15 @@ public async Task<IEnumerable<BlogPost>> GetAllAsync(Expression<Func<BlogPost, b
4646

4747
public async Task StoreAsync(BlogPost blogPost)
4848
{
49-
await blogPostContext.BlogPosts.AddAsync(blogPost);
49+
if (string.IsNullOrEmpty(blogPost.Id))
50+
{
51+
await blogPostContext.BlogPosts.AddAsync(blogPost);
52+
}
53+
else
54+
{
55+
blogPostContext.Entry(blogPost).State = EntityState.Modified;
56+
}
57+
5058
await blogPostContext.SaveChangesAsync();
5159
}
5260
}

0 commit comments

Comments
 (0)