Page CURD comleted
This commit is contained in:
parent
57b89d6456
commit
366f0c7bef
9 changed files with 491 additions and 0 deletions
|
|
@ -52,6 +52,11 @@ namespace Services.Implemnetation
|
||||||
return await _context.Banners.AsNoTracking().ToListAsync();
|
return await _context.Banners.AsNoTracking().ToListAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<Banner> GetBannersForPage()
|
||||||
|
{
|
||||||
|
return _context.Banners.AsNoTracking().ToList();
|
||||||
|
}
|
||||||
|
|
||||||
public void Update(Banner banner)
|
public void Update(Banner banner)
|
||||||
{
|
{
|
||||||
_context.Banners.Update(banner);
|
_context.Banners.Update(banner);
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,8 @@ namespace Services.Interaces
|
||||||
public interface IBannerRepository
|
public interface IBannerRepository
|
||||||
{
|
{
|
||||||
Task<IEnumerable<Banner>> GetBanners();
|
Task<IEnumerable<Banner>> GetBanners();
|
||||||
|
|
||||||
|
List<Banner> GetBannersForPage();
|
||||||
List<Banner> GetAllBanners();
|
List<Banner> GetAllBanners();
|
||||||
|
|
||||||
Banner GetBannerById(int id);
|
Banner GetBannerById(int id);
|
||||||
|
|
|
||||||
207
Web/Areas/Admin/Controllers/PageController.cs
Normal file
207
Web/Areas/Admin/Controllers/PageController.cs
Normal file
|
|
@ -0,0 +1,207 @@
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Microsoft.AspNetCore.Mvc.Rendering;
|
||||||
|
using Model;
|
||||||
|
using Services.Interaces;
|
||||||
|
using Services.SlugServices;
|
||||||
|
using Web.ViewModel.PageVM;
|
||||||
|
|
||||||
|
namespace Web.Areas.Admin.Controllers
|
||||||
|
{
|
||||||
|
public class PageController : Controller
|
||||||
|
{
|
||||||
|
private readonly IPageRepository _pageRepository;
|
||||||
|
private readonly IBannerRepository _bannerRepository;
|
||||||
|
|
||||||
|
public PageController(IPageRepository pageRepository,IBannerRepository bannerRepository)
|
||||||
|
{
|
||||||
|
_pageRepository = pageRepository;
|
||||||
|
_bannerRepository = bannerRepository;
|
||||||
|
}
|
||||||
|
public IActionResult Index()
|
||||||
|
{
|
||||||
|
var pages = _pageRepository.GetPageWithBanner();
|
||||||
|
|
||||||
|
List<PageViewModel> result = new List<PageViewModel>();
|
||||||
|
|
||||||
|
foreach (var page in pages)
|
||||||
|
{
|
||||||
|
result.Add(new PageViewModel { Id = page.Id, Title = page.Title, Slug = page.Slug, banner = page.banner });
|
||||||
|
}
|
||||||
|
|
||||||
|
return View(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpGet]
|
||||||
|
public IActionResult Create()
|
||||||
|
{
|
||||||
|
ViewBag.DropDownData=GetSidebarsForDropDownList();
|
||||||
|
|
||||||
|
return View();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
[HttpPost]
|
||||||
|
public async Task<IActionResult> Create(PageViewModel viewmodel)
|
||||||
|
{
|
||||||
|
if(!ModelState.IsValid)
|
||||||
|
{
|
||||||
|
ViewBag.DropDownData = GetSidebarsForDropDownList();
|
||||||
|
return View(viewmodel);
|
||||||
|
}
|
||||||
|
|
||||||
|
string slug;
|
||||||
|
|
||||||
|
if (string.IsNullOrEmpty(viewmodel.Slug))
|
||||||
|
slug = SlugService.Create(true, viewmodel.Title);
|
||||||
|
else
|
||||||
|
slug = SlugService.Create(true, viewmodel.Slug);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if(_pageRepository.SlugExists(slug))
|
||||||
|
{
|
||||||
|
ModelState.AddModelError("", "Title or slug exists");
|
||||||
|
ViewBag.DropDownData = GetSidebarsForDropDownList();
|
||||||
|
return View(viewmodel);
|
||||||
|
}
|
||||||
|
|
||||||
|
Page page = new Page();
|
||||||
|
|
||||||
|
page.Title = viewmodel.Title;
|
||||||
|
page.Slug = slug;
|
||||||
|
page.Content = viewmodel.Content;
|
||||||
|
page.banner = viewmodel.banner;
|
||||||
|
page.BannerId = viewmodel.BannerId;
|
||||||
|
|
||||||
|
|
||||||
|
_pageRepository.Add(page);
|
||||||
|
await _pageRepository.commitAsync();
|
||||||
|
TempData["Success"] = "page created successfully";
|
||||||
|
|
||||||
|
return RedirectToAction(nameof(Index));
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpGet]
|
||||||
|
public IActionResult Edit(int id)
|
||||||
|
{
|
||||||
|
var pageFromdb=_pageRepository.GetPageById(id);
|
||||||
|
|
||||||
|
var viewmodel = new PageViewModel
|
||||||
|
{
|
||||||
|
Id = pageFromdb.Id,
|
||||||
|
Title = pageFromdb.Title,
|
||||||
|
Slug = pageFromdb.Slug,
|
||||||
|
Content=pageFromdb.Content,
|
||||||
|
banner=pageFromdb.banner,
|
||||||
|
BannerId=pageFromdb.BannerId,
|
||||||
|
|
||||||
|
|
||||||
|
};
|
||||||
|
ViewBag.DropDownData = GetSidebarsForDropDownList();
|
||||||
|
|
||||||
|
|
||||||
|
return View(viewmodel);
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpPost]
|
||||||
|
public async Task<IActionResult> Edit(PageViewModel viewmodel)
|
||||||
|
{
|
||||||
|
if(!ModelState.IsValid)
|
||||||
|
{
|
||||||
|
|
||||||
|
ViewBag.DropDownData = GetSidebarsForDropDownList();
|
||||||
|
return View(viewmodel);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
string slug;
|
||||||
|
|
||||||
|
if (string.IsNullOrEmpty(viewmodel.Slug))
|
||||||
|
slug = SlugService.Create(true, viewmodel.Title);
|
||||||
|
else
|
||||||
|
slug = SlugService.Create(true, viewmodel.Slug);
|
||||||
|
|
||||||
|
if(_pageRepository.SlugExists(slug,viewmodel.Id))
|
||||||
|
{
|
||||||
|
|
||||||
|
ModelState.AddModelError("", "Title or slug exists");
|
||||||
|
ViewBag.DropDownData = GetSidebarsForDropDownList();
|
||||||
|
return View(viewmodel);
|
||||||
|
}
|
||||||
|
|
||||||
|
Page page = _pageRepository.GetPageById(viewmodel.Id);
|
||||||
|
|
||||||
|
page.Title = viewmodel.Title;
|
||||||
|
page.Slug = slug;
|
||||||
|
page.Content = viewmodel.Content;
|
||||||
|
page.banner = viewmodel.banner;
|
||||||
|
page.BannerId = viewmodel.BannerId;
|
||||||
|
|
||||||
|
|
||||||
|
_pageRepository.Update(page);
|
||||||
|
|
||||||
|
await _pageRepository.commitAsync();
|
||||||
|
|
||||||
|
TempData["Success"] = "page updated successfully";
|
||||||
|
|
||||||
|
|
||||||
|
return RedirectToAction(nameof(Index));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
[HttpGet]
|
||||||
|
public IActionResult Delete(int id)
|
||||||
|
{
|
||||||
|
var pageFromdb = _pageRepository.GetPageById(id);
|
||||||
|
|
||||||
|
var viewmodel = new PageViewModel
|
||||||
|
{
|
||||||
|
Id = pageFromdb.Id,
|
||||||
|
Title = pageFromdb.Title,
|
||||||
|
Slug = pageFromdb.Slug,
|
||||||
|
Content = pageFromdb.Content,
|
||||||
|
banner = pageFromdb.banner,
|
||||||
|
BannerId = pageFromdb.BannerId,
|
||||||
|
|
||||||
|
|
||||||
|
};
|
||||||
|
ViewBag.DropDownData = GetSidebarsForDropDownList();
|
||||||
|
|
||||||
|
|
||||||
|
return View(viewmodel);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
[HttpPost]
|
||||||
|
[ActionName("Delete")]
|
||||||
|
public async Task<IActionResult> DeleteConfirm(int id)
|
||||||
|
{
|
||||||
|
|
||||||
|
|
||||||
|
_pageRepository.Delete(id);
|
||||||
|
await _pageRepository.commitAsync();
|
||||||
|
TempData["Success"] = "page Deleted successfully";
|
||||||
|
|
||||||
|
return RedirectToAction(nameof(Index));
|
||||||
|
|
||||||
|
}
|
||||||
|
private List<SelectListItem> GetSidebarsForDropDownList()
|
||||||
|
{
|
||||||
|
var banners = _bannerRepository.GetBannersForPage();
|
||||||
|
|
||||||
|
List<SelectListItem> dropDown = new List<SelectListItem>();
|
||||||
|
|
||||||
|
foreach (var item in banners)
|
||||||
|
{
|
||||||
|
dropDown.Add(new SelectListItem { Text = item.Title, Value = item.Id.ToString() });
|
||||||
|
}
|
||||||
|
|
||||||
|
return dropDown;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
67
Web/Areas/Admin/Views/Page/Create.cshtml
Normal file
67
Web/Areas/Admin/Views/Page/Create.cshtml
Normal file
|
|
@ -0,0 +1,67 @@
|
||||||
|
@model PageViewModel
|
||||||
|
|
||||||
|
@{
|
||||||
|
ViewData["Title"] = "Create";
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
<div class="container mt-4">
|
||||||
|
<div class="card justify-content-center">
|
||||||
|
<div class="card-body">
|
||||||
|
<h5 class="card-title">Create page</h5>
|
||||||
|
|
||||||
|
<div class="row ">
|
||||||
|
<!-- 12 columns for textboxes -->
|
||||||
|
|
||||||
|
<form asp-action="Create" asp-controller="Page">
|
||||||
|
<div asp-validation-summary="All" class="text-danger"></div>
|
||||||
|
|
||||||
|
<div class="mb-3 col-12">
|
||||||
|
<label asp-for="Title" class="control-label"></label>
|
||||||
|
<input asp-for="Title" class="form-control" />
|
||||||
|
<span asp-validation-for="Title" class="text-danger"></span>
|
||||||
|
</div>
|
||||||
|
<div class="mb-3 col-12">
|
||||||
|
<label asp-for="Slug" class="control-label"></label>
|
||||||
|
<input asp-for="Slug" class="form-control" />
|
||||||
|
|
||||||
|
</div>
|
||||||
|
<div class="mb-3 col-12">
|
||||||
|
<label asp-for="Content" class="control-label"></label>
|
||||||
|
<textarea asp-for="Content" class="form-control"></textarea>
|
||||||
|
<span asp-validation-for="Content" class="text-danger"></span>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="mb-3 col-12">
|
||||||
|
<label asp-for="BannerId" class="control-label"></label>
|
||||||
|
<select asp-for="BannerId" asp-items="ViewBag.DropDownData" class="form-control"></select>
|
||||||
|
<span asp-validation-for="BannerId" class="text-danger"></span>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="mb-3">
|
||||||
|
<input type="submit" value="Create" class="btn btn-outline-primary" /> | <a asp-action="Index" class="btn btn-primary">Back to list</a>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
|
||||||
|
@section Scripts {
|
||||||
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/ckeditor/4.11.4/ckeditor.js"></script>
|
||||||
|
<script>
|
||||||
|
CKEDITOR.replace("Content");
|
||||||
|
</script>
|
||||||
|
|
||||||
|
@{
|
||||||
|
<partial name="_ValidationScriptsPartial" />
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
60
Web/Areas/Admin/Views/Page/Delete.cshtml
Normal file
60
Web/Areas/Admin/Views/Page/Delete.cshtml
Normal file
|
|
@ -0,0 +1,60 @@
|
||||||
|
@model PageViewModel
|
||||||
|
|
||||||
|
@{
|
||||||
|
ViewData["Title"] = "Delete";
|
||||||
|
}
|
||||||
|
|
||||||
|
<div class="container mt-4">
|
||||||
|
<div class="card justify-content-center">
|
||||||
|
<div class="card-body">
|
||||||
|
<h5 class="card-title">Delete page</h5>
|
||||||
|
<h6 class="text-danger">Are you sure you want to delete the <span class="badge bg-danger">@Model.Title</span></h6>
|
||||||
|
|
||||||
|
<div class="row ">
|
||||||
|
<!-- 12 columns for textboxes -->
|
||||||
|
|
||||||
|
<form asp-action="Delete">
|
||||||
|
<div asp-validation-summary="All" class="text-danger"></div>
|
||||||
|
|
||||||
|
<div class="mb-3 col-12">
|
||||||
|
<label asp-for="Title" class="control-label"></label>
|
||||||
|
<input asp-for="Title" class="form-control" disabled />
|
||||||
|
<span asp-validation-for="Title" class="text-danger"></span>
|
||||||
|
</div>
|
||||||
|
<div class="mb-3 col-12">
|
||||||
|
<label asp-for="Slug" class="control-label"></label>
|
||||||
|
<input asp-for="Slug" class="form-control" disabled />
|
||||||
|
|
||||||
|
<div class="mb-3 col-12">
|
||||||
|
<label asp-for="Content" class="control-label"></label>
|
||||||
|
<textarea asp-for="Content" disabled></textarea>
|
||||||
|
<span asp-validation-for="Content" class="text-danger"></span>
|
||||||
|
</div>
|
||||||
|
<div class="mb-3 col-12">
|
||||||
|
<label asp-for="BannerId" class="control-label"></label>
|
||||||
|
<select asp-for="BannerId" asp-items="ViewBag.DropDownData" class="form-control" disabled></select>
|
||||||
|
<span asp-validation-for="BannerId" class="text-danger"></span>
|
||||||
|
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="mb-3">
|
||||||
|
<input type="submit" value="Delete" class="btn btn-outline-primary" /> | <a asp-action="Index" class="btn btn-primary">Back to list</a>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
|
||||||
|
@section Scripts {
|
||||||
|
|
||||||
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/ckeditor/4.11.4/ckeditor.js"></script>
|
||||||
|
<script>
|
||||||
|
CKEDITOR.replace("Content");
|
||||||
|
</script>
|
||||||
|
@{
|
||||||
|
<partial name="_ValidationScriptsPartial" />
|
||||||
|
}
|
||||||
|
}
|
||||||
68
Web/Areas/Admin/Views/Page/Edit.cshtml
Normal file
68
Web/Areas/Admin/Views/Page/Edit.cshtml
Normal file
|
|
@ -0,0 +1,68 @@
|
||||||
|
@model Web.ViewModel.PageVM.PageViewModel
|
||||||
|
|
||||||
|
@{
|
||||||
|
ViewData["Title"] = "Edit";
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
<div class="container mt-4">
|
||||||
|
<div class="card justify-content-center">
|
||||||
|
<div class="card-body">
|
||||||
|
<h5 class="card-title">Create page</h5>
|
||||||
|
|
||||||
|
<div class="row ">
|
||||||
|
<!-- 12 columns for textboxes -->
|
||||||
|
|
||||||
|
<form asp-action="Edit" asp-controller="Page">
|
||||||
|
<div asp-validation-summary="All" class="text-danger"></div>
|
||||||
|
|
||||||
|
<div class="mb-3 col-12">
|
||||||
|
<label asp-for="Title" class="control-label"></label>
|
||||||
|
<input asp-for="Title" class="form-control" />
|
||||||
|
<span asp-validation-for="Title" class="text-danger"></span>
|
||||||
|
</div>
|
||||||
|
<div class="mb-3 col-12">
|
||||||
|
<label asp-for="Slug" class="control-label"></label>
|
||||||
|
<input asp-for="Slug" class="form-control" />
|
||||||
|
|
||||||
|
</div>
|
||||||
|
<div class="mb-3 col-12">
|
||||||
|
<label asp-for="Content" class="control-label"></label>
|
||||||
|
<textarea asp-for="Content" class="form-control"></textarea>
|
||||||
|
<span asp-validation-for="Content" class="text-danger"></span>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="mb-3 col-12">
|
||||||
|
<label asp-for="BannerId" class="control-label"></label>
|
||||||
|
<select asp-for="BannerId" asp-items="ViewBag.DropDownData" class="form-control"></select>
|
||||||
|
<span asp-validation-for="BannerId" class="text-danger"></span>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="mb-3">
|
||||||
|
<input type="submit" value="Update" class="btn btn-outline-primary" /> | <a asp-action="Index" class="btn btn-primary">Back to list</a>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
|
||||||
|
@section Scripts {
|
||||||
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/ckeditor/4.11.4/ckeditor.js"></script>
|
||||||
|
<script>
|
||||||
|
CKEDITOR.replace("Content");
|
||||||
|
</script>
|
||||||
|
|
||||||
|
@{
|
||||||
|
<partial name="_ValidationScriptsPartial" />
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
57
Web/Areas/Admin/Views/Page/Index.cshtml
Normal file
57
Web/Areas/Admin/Views/Page/Index.cshtml
Normal file
|
|
@ -0,0 +1,57 @@
|
||||||
|
@model IEnumerable<PageViewModel>
|
||||||
|
|
||||||
|
@{
|
||||||
|
ViewData["Title"] = "Index";
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
<div class="container mt-5">
|
||||||
|
|
||||||
|
<partial name="_Notification" />
|
||||||
|
|
||||||
|
<div class="card bg-default mb-3 ">
|
||||||
|
<div class="card-header">Pages</div>
|
||||||
|
<div class="card-body">
|
||||||
|
<h4 class="card-title">Page list</h4>
|
||||||
|
<p>
|
||||||
|
<a asp-action="Create" class="btn btn-primary">Create New page</a>
|
||||||
|
</p>
|
||||||
|
|
||||||
|
<table class="table table-responsive w-100 d-block d-md-table">
|
||||||
|
<thead class="w-auto">
|
||||||
|
<tr>
|
||||||
|
|
||||||
|
<th scope="col">Id</th>
|
||||||
|
<th scope="col">Title</th>
|
||||||
|
<th scope="col">Slug</th>
|
||||||
|
<th scope="col">Banner</th>
|
||||||
|
<th scope="col" class="d-flex justify-content-end">Action</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody class="w-auto">
|
||||||
|
@foreach (var item in Model)
|
||||||
|
{
|
||||||
|
<tr class="table-secondary">
|
||||||
|
|
||||||
|
<td>@item.Id</td>
|
||||||
|
<td> <span class="badge bg-primary">@item.Title</span></td>
|
||||||
|
<td>@item.Slug</td>
|
||||||
|
<td>@item.banner?.Title</td>
|
||||||
|
<td class="d-flex justify-content-end">
|
||||||
|
<a asp-action="Delete" asp-route-id="@item.Id" class="btn btn-danger btn-s"><i class="bi bi-trash"></i> Delete</a> |
|
||||||
|
<a asp-action="Edit" asp-route-id="@item.Id" class="btn btn-warning btn-s"><i class="bi bi-pencil-square"></i> Edit</a>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -7,4 +7,5 @@
|
||||||
@using Web.ViewModel.FooterVm
|
@using Web.ViewModel.FooterVm
|
||||||
@using Web.ViewModel.SocialMediaVM
|
@using Web.ViewModel.SocialMediaVM
|
||||||
@using Web.ViewModel.QuestionnaireVM
|
@using Web.ViewModel.QuestionnaireVM
|
||||||
|
@using Web.ViewModel.PageVM
|
||||||
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
|
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
|
||||||
|
|
|
||||||
24
Web/ViewModel/PageVM/PageViewModel.cs
Normal file
24
Web/ViewModel/PageVM/PageViewModel.cs
Normal file
|
|
@ -0,0 +1,24 @@
|
||||||
|
using Model;
|
||||||
|
using System.ComponentModel;
|
||||||
|
using System.ComponentModel.DataAnnotations;
|
||||||
|
using System.ComponentModel.DataAnnotations.Schema;
|
||||||
|
|
||||||
|
namespace Web.ViewModel.PageVM
|
||||||
|
{
|
||||||
|
public class PageViewModel
|
||||||
|
{
|
||||||
|
public int Id { get; set; }
|
||||||
|
[Required]
|
||||||
|
public string? Title { get; set; }
|
||||||
|
public string? Slug { get; set; }
|
||||||
|
[Required]
|
||||||
|
public string? Content { get; set; }
|
||||||
|
|
||||||
|
|
||||||
|
[DisplayName("Banner")]
|
||||||
|
public int BannerId { get; set; }
|
||||||
|
|
||||||
|
[ForeignKey("BannerId")]
|
||||||
|
public Banner? banner { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Reference in a new issue