Banner with the CURD operation

This commit is contained in:
Qais Yousuf 2024-02-21 18:35:32 +01:00
parent 86d6e8c49d
commit 4a65418344
13 changed files with 11496 additions and 8473 deletions

View file

@ -37,12 +37,17 @@ namespace Services.Implemnetation
}
public List<Banner> GetAllBanners()
{
return _context.Banners.AsNoTracking().ToList();
}
public Banner GetBannerById(int id)
{
return _context.Banners.AsNoTracking().Where(x => x.Id == id).FirstOrDefault();
}
public async Task<List<Banner>> GetBanners()
public async Task<IEnumerable<Banner>> GetBanners()
{
return await _context.Banners.AsNoTracking().ToListAsync();
}

View file

@ -9,7 +9,8 @@ namespace Services.Interaces
{
public interface IBannerRepository
{
Task<List<Banner>> GetBanners();
Task<IEnumerable<Banner>> GetBanners();
List<Banner> GetAllBanners();
Banner GetBannerById(int id);

View file

@ -1,5 +1,8 @@
using Microsoft.AspNetCore.Mvc;
using Model;
using Services.Interaces;
using System.Collections.Immutable;
using Web.ViewModel;
namespace Web.Areas.Admin.Controllers
{
@ -13,9 +16,55 @@ namespace Web.Areas.Admin.Controllers
}
public IActionResult Index()
{
var bannerFromdb = _banner.GetAllBanners();
var viewmodel = new List<BannerViewModel>();
var baner = _banner.GetBanners();
return View(baner);
foreach(var Banner in bannerFromdb)
{
viewmodel.Add(new BannerViewModel
{
Id = Banner.Id,
Title = Banner.Title,
Content=Banner.Content,
Description=Banner.Description,
ImageUrl=Banner.ImageUrl,
LinkUrl=Banner.LinkUrl,
});
}
return View(viewmodel);
}
[HttpGet]
public IActionResult Create()
{
return View();
}
[HttpPost]
public async Task<IActionResult> Create(BannerViewModel viewmodel)
{
if(ModelState.IsValid)
{
var banner = new Banner
{
Title = viewmodel.Title,
Content = viewmodel.Content,
Description = viewmodel.Description,
LinkUrl = viewmodel.LinkUrl,
ImageUrl = viewmodel.ImageUrl,
};
await _banner.Add(banner);
await _banner.commitAsync();
return RedirectToAction(nameof(Index));
}
return View(viewmodel);
}
}
}

View file

@ -0,0 +1,68 @@
@model BannerViewModel
@{
ViewData["Title"] = "Create";
}
<div class="container mt-4">
<div class="card justify-content-center">
<div class="card-body">
<h5 class="card-title">Create banner</h5>
<div class="row ">
<!-- 12 columns for textboxes -->
<form asp-action="Create">
<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="Description" class="control-label"></label>
<input asp-for="Description" class="form-control" />
<span asp-validation-for="Description" class="text-danger"></span>
</div>
<div class="mb-3 col-12">
<label asp-for="Content" class="control-label"></label>
<textarea asp-for="Content"></textarea>
<span asp-validation-for="Content" class="text-danger"></span>
</div>
<div class="mb-3 col-12">
<label asp-for="LinkUrl" class="control-label"></label>
<input asp-for="LinkUrl" class="form-control" />
<span asp-validation-for="LinkUrl" class="text-danger"></span>
</div>
<div class="mb-3 col-12">
<label asp-for="ImageUrl" class="control-label"></label>
<input asp-for="ImageUrl" class="form-control" />
<span asp-validation-for="ImageUrl" 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"/>
}
}

View file

@ -1,4 +1,4 @@
@model IEnumerable<Model.Banner>
@model IEnumerable<BannerViewModel>
@{
ViewData["Title"] = "Index";
@ -7,7 +7,7 @@
<h1>Index</h1>
<p>
<a asp-action="Create">Create New</a>
<a asp-action="Create" class="btn btn-primary">Create New</a>
</p>
<table class="table">
<thead>
@ -30,7 +30,9 @@
<th>
@Html.DisplayNameFor(model => model.ImageUrl)
</th>
<th></th>
<th>
Action
</th>
</tr>
</thead>
<tbody>

View file

@ -4,15 +4,16 @@
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>@ViewData["Title"] - Web</title>
<link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.min.css" />
<link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.css" />
<link rel="stylesheet" href="~/css/site.css" asp-append-version="true" />
<link rel="stylesheet" href="~/Web.styles.css" asp-append-version="true" />
</head>
<body>
<header>
<nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-light bg-white border-bottom box-shadow mb-3">
<div class="container-fluid">
<a class="navbar-brand" asp-area="" asp-controller="Home" asp-action="Index">Web</a>
<a class="navbar-brand" asp-area="admin" asp-controller="Home" asp-action="Index">Web</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target=".navbar-collapse" aria-controls="navbarSupportedContent"
aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
@ -20,10 +21,10 @@
<div class="navbar-collapse collapse d-sm-inline-flex justify-content-between">
<ul class="navbar-nav flex-grow-1">
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Index">Home</a>
<a class="nav-link text-dark" asp-area="admin" asp-controller="Home" asp-action="Index">Home</a>
</li>
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Privacy">Privacy</a>
<a class="nav-link text-dark" asp-area="admin" asp-controller="Home" asp-action="Privacy">Privacy</a>
</li>
</ul>
</div>
@ -38,7 +39,7 @@
<footer class="border-top footer text-muted">
<div class="container">
&copy; 2024 - Web - <a asp-area="" asp-controller="Home" asp-action="Privacy">Privacy</a>
&copy; 2024 - Web - <a asp-area="admin" asp-controller="Home" asp-action="Privacy">Privacy</a>
</div>
</footer>
<script src="~/lib/jquery/dist/jquery.min.js"></script>

View file

@ -1,3 +1,4 @@
@using Web
@using Web.Models
@using Web.ViewModel
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

View file

@ -1,3 +1,3 @@
@{
Layout = "_Layout";
Layout = "_AdminLayout";
}

View file

@ -1,3 +1,5 @@
using Microsoft.AspNetCore.Mvc.Razor;
using Microsoft.Extensions.DependencyInjection;
using Services.Implemnetation;
using Services.Interaces;
using Web.Extesions;
@ -7,10 +9,12 @@ var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllersWithViews();
builder.Services.ConfigureSQLConnection(builder.Configuration);
builder.Services.AddScoped<IPageRepository,PageRepository>();
builder.Services.AddScoped<IBannerRepository,BannerRepository>();
builder.Services.ConfigurePageServices();
builder.Services.ConfigureBannerServices();
var app = builder.Build();
@ -29,12 +33,15 @@ app.UseRouting();
app.UseAuthorization();
app.MapControllerRoute(
name: "SurveyVista",
pattern:"Admin/{controller=Home}/{action=Index}/{id?}");
app.MapAreaControllerRoute(
name: "MyAdminArea",
areaName:"admin",
pattern: "admin/{controller=Home}/{action=Index}/{id?}");
app.MapControllerRoute(
name: "default",
pattern:"{controller=Home}/{action=Index}/{id?}");
app.Run();

View file

@ -0,0 +1,24 @@
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
namespace Web.ViewModel
{
public class BannerViewModel
{
public int Id { get; set; }
[Required]
public string? Title { get; set; }
[Required]
public string? Description { get; set; }
[Required]
public string? Content { get; set; }
[Required]
[DisplayName("Link Url")]
public string? LinkUrl { get; set; }
[Required]
[DisplayName("Image Url")]
public string? ImageUrl { get; set; }
}
}

View file

@ -4,7 +4,7 @@
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>@ViewData["Title"] - Web</title>
<link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.min.css" />
<link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.css" />
<link rel="stylesheet" href="~/css/site.css" asp-append-version="true" />
<link rel="stylesheet" href="~/Web.styles.css" asp-append-version="true" />
</head>

File diff suppressed because it is too large Load diff