Skip to content

Commit 315d179

Browse files
committed
Added Delete BlogPost
1 parent fa9feba commit 315d179

File tree

7 files changed

+39
-6
lines changed

7 files changed

+39
-6
lines changed

LinkDotNet.Blog.Web/Shared/Admin/BlogPostAdminActions.razor

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
@inject NavigationManager _navigationManager
1+
@using LinkDotNet.Infrastructure.Persistence
2+
@inject NavigationManager _navigationManager
23
@inject IToastService _toastService
4+
@inject IRepository _repository
35

46
<AuthorizeView>
57
<div class="blogpost-admin">
@@ -8,7 +10,7 @@
810
<button type="button" class="btn btn-danger" @onclick="ShowConfirmDialog" aria-label="delete"><i class="fas fa-trash"></i> Delete
911
Blogpost</button>
1012
</div>
11-
<ConfirmDialog @ref="ConfirmDialog" Title="Delete Blog Post" Content="Do you want to delete the Blog Post?" OnYesPressed="@ShowMessage">
13+
<ConfirmDialog @ref="ConfirmDialog" Title="Delete Blog Post" Content="Do you want to delete the Blog Post?" OnYesPressed="@DeleteBlogPostAsync">
1214
</ConfirmDialog>
1315
</AuthorizeView>
1416

@@ -18,9 +20,11 @@
1820

1921
private ConfirmDialog ConfirmDialog { get; set; }
2022

21-
private void ShowMessage()
23+
private async Task DeleteBlogPostAsync()
2224
{
23-
_toastService.ShowInfo("Hey");
25+
await _repository.DeleteAsync(BlogPostId);
26+
_toastService.ShowSuccess("The Blog Post was successfully deleted");
27+
_navigationManager.NavigateTo("/");
2428
}
2529

2630
private void ShowConfirmDialog()

LinkDotNet.Blog.Web/Shared/ConfirmDialog.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<h3>@Content</h3>
33
<div class="actions">
44
<button @onclick="OnYesButtonPressed" type="button" class="btn btn-primary">Ok</button>
5-
<button @onclick="OnNoButtonPressed" type="button" class="btn btn">Cancel</button>
5+
<button @onclick="OnNoButtonPressed" type="button" class="btn btn-secondary">Cancel</button>
66
</div>
77
</ModalDialog>
88

LinkDotNet.Blog.Web/Shared/ConfirmDialog.razor.css

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,7 @@
33
}
44

55
.actions * {
6-
padding-left: 5px;
6+
margin-left: 5px;
7+
margin-top: 25px;
8+
width: 125px;
79
}

LinkDotNet.Infrastructure/Persistence/IRepository.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,7 @@ public interface IRepository
1313
Task<IEnumerable<BlogPost>> GetAllAsync(Expression<Func<BlogPost, bool>> filter = null, Expression<Func<BlogPost, object>> orderBy = null, bool descending = true);
1414

1515
Task StoreAsync(BlogPost blogPost);
16+
17+
Task DeleteAsync(string blogPostId);
1618
}
1719
}

LinkDotNet.Infrastructure/Persistence/InMemory/InMemoryRepository.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,16 @@ public Task StoreAsync(BlogPost blogPost)
5454
blogPosts.Add(blogPost);
5555
return Task.CompletedTask;
5656
}
57+
58+
public Task DeleteAsync(string blogPostId)
59+
{
60+
var blogPostToDelete = blogPosts.SingleOrDefault(b => b.Id == blogPostId);
61+
if (blogPostToDelete != null)
62+
{
63+
blogPosts.Remove(blogPostToDelete);
64+
}
65+
66+
return Task.CompletedTask;
67+
}
5768
}
5869
}

LinkDotNet.Infrastructure/Persistence/RavenDb/BlogPostRepository.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,5 +52,12 @@ public async Task StoreAsync(BlogPost blogPost)
5252
await session.StoreAsync(blogPost);
5353
await session.SaveChangesAsync();
5454
}
55+
56+
public async Task DeleteAsync(string blogPostId)
57+
{
58+
using var session = documentStore.OpenAsyncSession();
59+
session.Delete(blogPostId);
60+
await session.SaveChangesAsync();
61+
}
5562
}
5663
}

LinkDotNet.Infrastructure/Persistence/Sql/BlogPostRepository.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,12 @@ public async Task StoreAsync(BlogPost blogPost)
5757

5858
await blogPostContext.SaveChangesAsync();
5959
}
60+
61+
public async Task DeleteAsync(string blogPostId)
62+
{
63+
var blogPostToDelete = await GetByIdAsync(blogPostId);
64+
blogPostContext.Remove(blogPostToDelete);
65+
await blogPostContext.SaveChangesAsync();
66+
}
6067
}
6168
}

0 commit comments

Comments
 (0)