Skip to content

Commit

Permalink
Edit BlogPost
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmedsalehalzabr committed Jul 29, 2024
1 parent 0d3a16d commit 32ffd6c
Show file tree
Hide file tree
Showing 7 changed files with 207 additions and 18 deletions.
77 changes: 77 additions & 0 deletions Bloggle/Controllers/AdminBlogPostController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,82 @@ public async Task<IActionResult> List()

return View(blogPosts);
}

[HttpGet]
public async Task<IActionResult> Edit(Guid id)
{
var blgPosts = await blogPostRepository.GetByIdAsync(id);
var tags = await tagRepository.GetAllAsync();

if (blgPosts != null)
{
var model = new EditBlogPostRequest
{
Id = blgPosts.Id,
Heading = blgPosts.Heading,
ShortDescription = blgPosts.ShortDescription,
Author = blgPosts.Author,
PublishedDate = blgPosts.PublishedDate,
PageTitle = blgPosts.PageTitle,
Content = blgPosts.Content,
FeaturedImageUrl = blgPosts.FeaturedImageUrl,
UrlHandle = blgPosts.UrlHandle,
Visible = blgPosts.Visible,
Tags = tags.Select(x => new SelectListItem
{
Text = x.Name,
Value = x.Id.ToString(),
}),
SelectedTag = blgPosts.Tags.Select(x => x.Id.ToString()).ToArray()
};
return View(model);
}

return View(null);
}

[HttpPost]
public async Task<IActionResult> Edit(EditBlogPostRequest editBlogPostRequest)
{
var blogPostDomanModel = new BlogPost
{
Id = editBlogPostRequest.Id,
Heading = editBlogPostRequest.Heading,
ShortDescription = editBlogPostRequest.ShortDescription,
Author = editBlogPostRequest.Author,
PageTitle = editBlogPostRequest.PageTitle,
Content = editBlogPostRequest.Content,
PublishedDate = editBlogPostRequest.PublishedDate,
FeaturedImageUrl = editBlogPostRequest.FeaturedImageUrl,
UrlHandle = editBlogPostRequest.UrlHandle,
Visible = editBlogPostRequest.Visible,

};

// Map tags into domain model
var seTags = new List<Tag>();
foreach (var selectedTag in editBlogPostRequest.SelectedTag)
{
if (Guid.TryParse(selectedTag, out var tag))
{
var foundTag = await tagRepository.GetAsync(tag);
if (foundTag != null)
{
seTags.Add(foundTag);
}
}
}

blogPostDomanModel.Tags = seTags;

//Submit information to repository to update
var updateBlog = await blogPostRepository.UpdateAsync(blogPostDomanModel);
if (updateBlog != null)
{
return RedirectToAction("List");
}

return RedirectToAction("Edit");
}
}
}
12 changes: 0 additions & 12 deletions Bloggle/Controllers/BlogPostController.cs

This file was deleted.

24 changes: 24 additions & 0 deletions Bloggle/Models/ViewModels/EditBlogPostRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Microsoft.AspNetCore.Mvc.Rendering;

namespace Bloggle.Models.ViewModels
{
public class EditBlogPostRequest
{
public Guid Id { get; set; }
public string Heading { get; set; }
public string PageTitle { get; set; }
public string Content { get; set; }
public string ShortDescription { get; set; }
public string FeaturedImageUrl { get; set; }
public string UrlHandle { get; set; }
public DateTime PublishedDate { get; set; }
public string Author { get; set; }
public bool Visible { get; set; }

// Display tags
public IEnumerable<SelectListItem> Tags { get; set; }

// Collect Tag
public string[] SelectedTag { get; set; } = Array.Empty<string>();
}
}
27 changes: 23 additions & 4 deletions Bloggle/Repository/BlogPostRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,33 @@ public async Task<IEnumerable<BlogPost>> GetAllPostsAsync()
return await appDbContext.BlogPosts.Include(x => x.Tags).ToListAsync();
}

public Task<BlogPost> GetByIdAsync(Guid id)
public async Task<BlogPost> GetByIdAsync(Guid id)
{
throw new NotImplementedException();
return await appDbContext.BlogPosts.Include(x => x.Tags).FirstOrDefaultAsync(x => x.Id == id);
}

public Task<BlogPost> UpdateAsync(BlogPost blogPost)
public async Task<BlogPost> UpdateAsync(BlogPost blogPost)
{
throw new NotImplementedException();
var existingBlog = await appDbContext.BlogPosts.Include(x => x.Tags)
.FirstOrDefaultAsync(x => x.Id == blogPost.Id);
if (existingBlog != null)
{
existingBlog.Id = blogPost.Id;
existingBlog.Heading = blogPost.Heading;
existingBlog.Author = blogPost.Author;
existingBlog.ShortDescription = blogPost.ShortDescription;
existingBlog.Content = blogPost.Content;
existingBlog.PublishedDate = blogPost.PublishedDate;
existingBlog.Visible = blogPost.Visible;
existingBlog.PageTitle = blogPost.PageTitle;
existingBlog.FeaturedImageUrl = blogPost.FeaturedImageUrl;
existingBlog.UrlHandle = blogPost.UrlHandle;
existingBlog.Tags = blogPost.Tags;

await appDbContext.SaveChangesAsync();
return existingBlog;
}
return null;
}
}
}
4 changes: 2 additions & 2 deletions Bloggle/Repository/IBlogPostRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public interface IBlogPostRepository
Task<IEnumerable<BlogPost>> GetAllPostsAsync();
Task<BlogPost> GetByIdAsync(Guid id);
Task<BlogPost> AddAsync(BlogPost blogPost);
Task<BlogPost> UpdateAsync(BlogPost blogPost);
Task<BlogPost> DeleteAsync(Guid id);
Task<BlogPost?> UpdateAsync(BlogPost blogPost);
Task<BlogPost?> DeleteAsync(Guid id);
}
}
74 changes: 74 additions & 0 deletions Bloggle/Views/AdminBlogPost/Edit.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
@model Bloggle.Models.ViewModels.EditBlogPostRequest
@{
}

<div class="bg-secondary bg-opacity-10 py-2">
<div class="container">
<h1>Edit BlogPost - Admin Functionality</h1>
</div>
</div>

<div class="container py-5">
@if (Model != null)
{
<form method="post">
<div class="mb-3">
<label class="form-label">Id</label>
<input type="text" class="form-control" id="id" asp-for="Id" readonly />
</div>
<div class="mb-3">
<label class="form-label">Heading</label>
<input type="text" class="form-control" id="Heading" asp-for="Heading" />
</div>
<div class="mb-3">
<label class="form-label">Page Title</label>
<input type="text" class="form-control" id="PageTitle" asp-for="PageTitle" />
</div>
<div class="mb-3">
<label class="form-label">Content</label>
<textarea class="form-control" id="Content" asp-for="Content"> </textarea>
</div>
<div class="mb-3">
<label class="form-label">Short Description</label>
<input type="text" class="form-control" id="ShortDescription" asp-for="ShortDescription" />
</div>
<div class="mb-3">
<label class="form-label">FeaturedImageUrl</label>
<input type="text" class="form-control" id="FeaturedImageUrl" asp-for="FeaturedImageUrl" />
</div>
<div class="mb-3">
<label class="form-label">UrlHandle</label>
<input type="text" class="form-control" id="UrlHandle" asp-for="UrlHandle" />
</div>
<div class="mb-3">
<label class="form-label">PublishedDate</label>
<input type="date" class="form-control" id="PublishedDate" asp-for="PublishedDate" />
</div>
<div class="mb-3">
<label class="form-label">Author</label>
<input type="text" class="form-control" id="Author" asp-for="Author" />
</div>

<div class="form-check mb-3">
<input class="form-check-input" type="checkbox" id="Visible" asp-for="Visible">
<label class="form-check-label">
Is Visible?
</label>
</div>
<div class="mb-3">
<label class="form-label">Tags</label>
<select class="form-select"
asp-items="@Model.Tags"
asp-for="SelectedTag"></select>
</div>
<div class="mb-3">
<button type="submit" class="btn btn-dark">Update</button>
</div>
</form>
}
else
{
<p>No blog post found!</p>
}

</div>
7 changes: 7 additions & 0 deletions Bloggle/Views/AdminBlogPost/List.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<td>Id</td>
<td>Heading</td>
<td>Tags</td>
<td></td>
</tr>
</thead>
<tbody>
Expand All @@ -33,6 +34,12 @@
}
</div>
</td>
<td>
<a asp-area=""
asp-action="Edit"
asp-controller="AdminBlogPost"
asp-route-id="@blogPost.Id">Edit</a>
</td>
</tr>
}
</tbody>
Expand Down

0 comments on commit 32ffd6c

Please sign in to comment.