Skip to content

Commit

Permalink
Added Delete BlogPost
Browse files Browse the repository at this point in the history
  • Loading branch information
linkdotnet committed Jul 7, 2021
1 parent fa9feba commit 315d179
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 6 deletions.
12 changes: 8 additions & 4 deletions LinkDotNet.Blog.Web/Shared/Admin/BlogPostAdminActions.razor
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
@inject NavigationManager _navigationManager
@using LinkDotNet.Infrastructure.Persistence
@inject NavigationManager _navigationManager
@inject IToastService _toastService
@inject IRepository _repository

<AuthorizeView>
<div class="blogpost-admin">
Expand All @@ -8,7 +10,7 @@
<button type="button" class="btn btn-danger" @onclick="ShowConfirmDialog" aria-label="delete"><i class="fas fa-trash"></i> Delete
Blogpost</button>
</div>
<ConfirmDialog @ref="ConfirmDialog" Title="Delete Blog Post" Content="Do you want to delete the Blog Post?" OnYesPressed="@ShowMessage">
<ConfirmDialog @ref="ConfirmDialog" Title="Delete Blog Post" Content="Do you want to delete the Blog Post?" OnYesPressed="@DeleteBlogPostAsync">
</ConfirmDialog>
</AuthorizeView>

Expand All @@ -18,9 +20,11 @@

private ConfirmDialog ConfirmDialog { get; set; }

private void ShowMessage()
private async Task DeleteBlogPostAsync()
{
_toastService.ShowInfo("Hey");
await _repository.DeleteAsync(BlogPostId);
_toastService.ShowSuccess("The Blog Post was successfully deleted");
_navigationManager.NavigateTo("/");
}

private void ShowConfirmDialog()
Expand Down
2 changes: 1 addition & 1 deletion LinkDotNet.Blog.Web/Shared/ConfirmDialog.razor
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<h3>@Content</h3>
<div class="actions">
<button @onclick="OnYesButtonPressed" type="button" class="btn btn-primary">Ok</button>
<button @onclick="OnNoButtonPressed" type="button" class="btn btn">Cancel</button>
<button @onclick="OnNoButtonPressed" type="button" class="btn btn-secondary">Cancel</button>
</div>
</ModalDialog>

Expand Down
4 changes: 3 additions & 1 deletion LinkDotNet.Blog.Web/Shared/ConfirmDialog.razor.css
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@
}

.actions * {
padding-left: 5px;
margin-left: 5px;
margin-top: 25px;
width: 125px;
}
2 changes: 2 additions & 0 deletions LinkDotNet.Infrastructure/Persistence/IRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,7 @@ public interface IRepository
Task<IEnumerable<BlogPost>> GetAllAsync(Expression<Func<BlogPost, bool>> filter = null, Expression<Func<BlogPost, object>> orderBy = null, bool descending = true);

Task StoreAsync(BlogPost blogPost);

Task DeleteAsync(string blogPostId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,16 @@ public Task StoreAsync(BlogPost blogPost)
blogPosts.Add(blogPost);
return Task.CompletedTask;
}

public Task DeleteAsync(string blogPostId)
{
var blogPostToDelete = blogPosts.SingleOrDefault(b => b.Id == blogPostId);
if (blogPostToDelete != null)
{
blogPosts.Remove(blogPostToDelete);
}

return Task.CompletedTask;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,12 @@ public async Task StoreAsync(BlogPost blogPost)
await session.StoreAsync(blogPost);
await session.SaveChangesAsync();
}

public async Task DeleteAsync(string blogPostId)
{
using var session = documentStore.OpenAsyncSession();
session.Delete(blogPostId);
await session.SaveChangesAsync();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,12 @@ public async Task StoreAsync(BlogPost blogPost)

await blogPostContext.SaveChangesAsync();
}

public async Task DeleteAsync(string blogPostId)
{
var blogPostToDelete = await GetByIdAsync(blogPostId);
blogPostContext.Remove(blogPostToDelete);
await blogPostContext.SaveChangesAsync();
}
}
}

0 comments on commit 315d179

Please sign in to comment.