diff --git a/WebStoreCoreApplication.sln b/WebStoreCoreApplication.sln index d68fbcf..03f594e 100644 --- a/WebStoreCoreApplication.sln +++ b/WebStoreCoreApplication.sln @@ -18,9 +18,7 @@ Global {581700BA-91A1-4244-A194-0EF45BB2C613}.Release|Any CPU.ActiveCfg = Release|Any CPU {581700BA-91A1-4244-A194-0EF45BB2C613}.Release|Any CPU.Build.0 = Release|Any CPU {A927D081-0956-4C16-8F5A-10F933B3A19B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {A927D081-0956-4C16-8F5A-10F933B3A19B}.Debug|Any CPU.Build.0 = Debug|Any CPU {A927D081-0956-4C16-8F5A-10F933B3A19B}.Release|Any CPU.ActiveCfg = Release|Any CPU - {A927D081-0956-4C16-8F5A-10F933B3A19B}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/WebStoreCoreApplication/Controllers/BaseController.cs b/WebStoreCoreApplication/Controllers/BaseController.cs index 8d294da..6d7a82f 100644 --- a/WebStoreCoreApplication/Controllers/BaseController.cs +++ b/WebStoreCoreApplication/Controllers/BaseController.cs @@ -3,17 +3,19 @@ using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; +using WebStoreCoreApplication.Controllers.Infrastructure; namespace WebStoreCoreApplication.Controllers { public class BaseController : Controller { + [SimpleActionFilter] public IActionResult Index() { return View(); } - public IActionResult _404() + public IActionResult NotPage() { return View(); } diff --git a/WebStoreCoreApplication/Controllers/EmployeeController.cs b/WebStoreCoreApplication/Controllers/EmployeeController.cs index 55c3ce3..9fdd08f 100644 --- a/WebStoreCoreApplication/Controllers/EmployeeController.cs +++ b/WebStoreCoreApplication/Controllers/EmployeeController.cs @@ -3,48 +3,96 @@ using System.Linq; using System.Net.Cache; using System.Threading.Tasks; +using AspNetCore; using Microsoft.AspNetCore.Mvc; +using WebStoreCoreApplication.Controllers.Infrastructure.Interfaces; using WebStoreCoreApplication.ViewModels; +using WebStoreCoreApplication.Controllers; namespace WebStoreCoreApplication.Controllers { + [Route("peoples")] public class EmployeeController : Controller { - private readonly List _employees = new List + + private readonly IEmployeeService _employeeService; + + public EmployeeController(IEmployeeService employeeService) { - new EmployeeViewModel - { - Id = 1, - Name = "Петр", - SName = "Петров", - FName = "Петрович", - Age = 33, - Position ="BOSS" - }, - new EmployeeViewModel - { - Id = 2, - Name = "Фёдр", - SName = "Фёдоров", - FName = "Фёдорович", - Age = 25, - Position ="Программист"} - }; + _employeeService = employeeService; + } + + [Route("idx")] public IActionResult Index() { return View(); } + [Route("all")] public IActionResult Employees() { - return View(_employees); + return View(_employeeService.GetAll()); } - + + [Route("{id}")] public IActionResult EmployeeDetails(int id) { - var employeeviewmodel = _employees.FirstOrDefault(x => x.Id == id); - if (employeeviewmodel == null) return NotFound(); + var employeeviewmodel = _employeeService.GetByID(id); + + if (employeeviewmodel == null) + return NotFound(); + return View(employeeviewmodel); } + + [HttpPost] + [Route("edit/{id?}")] + public IActionResult Edit (EmployeeViewModel employeemodel) //int? id) + { + if (employeemodel.Id > 0) + { + var dbitem = _employeeService.GetByID(employeemodel.Id); + + if (ReferenceEquals(dbitem, null)) + return NotFound(); + dbitem.IName = employeemodel.IName; + dbitem.FName = employeemodel.FName; + dbitem.OName = employeemodel.OName; + dbitem.Age = employeemodel.Age; + dbitem.Position = employeemodel.Position; + } + else + { + _employeeService.AddNew(employeemodel); + } + _employeeService.Commit(); + return RedirectToAction(nameof(Employees)); + /* + if (!id.HasValue) + return View(new EmployeeViewModel()); + + var model = _employeeService.GetByID(id.Value); + + if (model == null) + return NotFound(); + + return View(model); + */ + } + + [Route("delete/{id?}")] + public IActionResult Delete (int id) + { + var deluser = _employeeService.GetByID(id); + if (deluser.Id > 0 & (!ReferenceEquals(deluser, null))) + { + _employeeService.Delete(id); + } + return RedirectToAction(nameof(Employees)); + } + + + + } } \ No newline at end of file diff --git a/WebStoreCoreApplication/Controllers/Infrastructure/EmployeeWork.cs b/WebStoreCoreApplication/Controllers/Infrastructure/EmployeeWork.cs new file mode 100644 index 0000000..c171f7f --- /dev/null +++ b/WebStoreCoreApplication/Controllers/Infrastructure/EmployeeWork.cs @@ -0,0 +1,39 @@ +using Microsoft.AspNetCore.Http; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace WebStoreCoreApplication.Controllers.Infrastructure +{ + public class EmployeeWork + { + private const string correct = "qqq1234"; + public RequestDelegate Next { get; } + + public EmployeeWork(RequestDelegate next) + { + Next = next; + } + + public async Task InvokeAsync(HttpContext context) + { + var token = context.Request.Query["usertoken"]; + + if (string.IsNullOrEmpty(token)) + { + await Next.Invoke(context); + return; + } + if (token == correct) + { + await Next.Invoke(context); + } + else + { + await context.Response.WriteAsync("BAAAADDDD!!!!"); + } + } + + } +} diff --git a/WebStoreCoreApplication/Controllers/Infrastructure/Interfaces/IEmployeeService.cs b/WebStoreCoreApplication/Controllers/Infrastructure/Interfaces/IEmployeeService.cs new file mode 100644 index 0000000..1c63759 --- /dev/null +++ b/WebStoreCoreApplication/Controllers/Infrastructure/Interfaces/IEmployeeService.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using WebStoreCoreApplication.ViewModels; + +namespace WebStoreCoreApplication.Controllers.Infrastructure.Interfaces +{ + public interface IEmployeeService + { + IEnumerable GetAll(); + + EmployeeViewModel GetByID(int id); + + void Commit(); + + void AddNew(EmployeeViewModel newmodel); + + void Delete(int id); + } +} diff --git a/WebStoreCoreApplication/Controllers/Infrastructure/Services/InMemoryEmployeeServices.cs b/WebStoreCoreApplication/Controllers/Infrastructure/Services/InMemoryEmployeeServices.cs new file mode 100644 index 0000000..4cac53d --- /dev/null +++ b/WebStoreCoreApplication/Controllers/Infrastructure/Services/InMemoryEmployeeServices.cs @@ -0,0 +1,63 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using WebStoreCoreApplication.Controllers.Infrastructure.Interfaces; +using WebStoreCoreApplication.ViewModels; + +namespace WebStoreCoreApplication.Controllers.Infrastructure.Services +{ + public class InMemoryEmployeeServices : IEmployeeService + { + private readonly List _employees = new List + { + new EmployeeViewModel + { + Id = 1, + IName = "Петр", + FName = "Петров", + OName = "Петрович", + Age = 33, + Position ="BOSS" + }, + new EmployeeViewModel + { + Id = 2, + IName = "Фёдр", + FName = "Фёдоров", + OName = "Фёдорович", + Age = 25, + Position ="Программист"} + }; + + public void AddNew(EmployeeViewModel newmodel) + { + newmodel.Id = _employees.Max(e => e.Id) + 1; + _employees.Add(newmodel); + } + + public void Commit() + { + // + } + + public void Delete(int id) + { + var employee = GetByID(id); + if (employee is null) + return; + + _employees.Remove(employee); + } + + public IEnumerable GetAll() + { + return _employees; + } + + public EmployeeViewModel GetByID(int id) + { + return _employees.FirstOrDefault(e => e.Id.Equals(id)); + } + } +} diff --git a/WebStoreCoreApplication/Controllers/Infrastructure/SimpleActionFilter.cs b/WebStoreCoreApplication/Controllers/Infrastructure/SimpleActionFilter.cs new file mode 100644 index 0000000..cd62e90 --- /dev/null +++ b/WebStoreCoreApplication/Controllers/Infrastructure/SimpleActionFilter.cs @@ -0,0 +1,24 @@ +using Microsoft.AspNetCore.Mvc.Filters; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace WebStoreCoreApplication.Controllers.Infrastructure +{ + public class SimpleActionFilter : Attribute, IActionFilter + { + public void OnActionExecuting(ActionExecutingContext context) + { + // throw new NotImplementedException(); + return; + } + + public void OnActionExecuted(ActionExecutedContext context) + { + // throw new NotImplementedException(); + return; + } + + } +} diff --git a/WebStoreCoreApplication/Startup.cs b/WebStoreCoreApplication/Startup.cs index e13d0da..a29af5d 100644 --- a/WebStoreCoreApplication/Startup.cs +++ b/WebStoreCoreApplication/Startup.cs @@ -5,9 +5,13 @@ using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; +using Microsoft.CodeAnalysis.Options; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; +using WebStoreCoreApplication.Controllers.Infrastructure; +using WebStoreCoreApplication.Controllers.Infrastructure.Interfaces; +using WebStoreCoreApplication.Controllers.Infrastructure.Services; namespace WebStoreCoreApplication { @@ -23,6 +27,15 @@ public Startup(IConfiguration configuration) public void ConfigureServices(IServiceCollection services) { services.AddMvc(); + + /* + services.AddMvc(options => + { + options.Filters.Add(typeof(SimpleActionFilter)); + }); + */ + + services.AddSingleton(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. @@ -37,6 +50,9 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env) var hello = _configuration["CustomeHelloWorld"]; + //app.Map("/Index", CustomIndexHandler); + app.UseMiddleware(); + app.UseRouting(); app.UseEndpoints(endpoints => @@ -52,6 +68,31 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env) */ }); } + /* + private void CustomIndexHandler(IApplicationBuilder app) + { + app.Run(async context => + { + await context.Response.WriteAsync("Index Custom!!!"); + }); + } + + private void UseMiddlewareSample(IApplicationBuilder app) + { + app.Use(async (context, next) => + { + bool isError = false; + if (isError) + { + await context.Response.WriteAsync("Errrrrrrrooooooorrrrr!!!!"); + } + else + { + await next.Invoke(); + } + }); + } + */ } } diff --git a/WebStoreCoreApplication/ViewModels/EmployeeViewModel.cs b/WebStoreCoreApplication/ViewModels/EmployeeViewModel.cs index 2b563d1..440bc3d 100644 --- a/WebStoreCoreApplication/ViewModels/EmployeeViewModel.cs +++ b/WebStoreCoreApplication/ViewModels/EmployeeViewModel.cs @@ -8,9 +8,9 @@ namespace WebStoreCoreApplication.ViewModels public class EmployeeViewModel { public int Id { get; set; } - public string Name { get; set;} - public string SName { get; set;} + public string IName { get; set;} public string FName { get; set;} + public string OName { get; set;} public int Age { get; set;} public string Position { get; set;} } diff --git a/WebStoreCoreApplication/Views/Base/Index.cshtml b/WebStoreCoreApplication/Views/Base/Index.cshtml index cfb3d41..26dabfc 100644 --- a/WebStoreCoreApplication/Views/Base/Index.cshtml +++ b/WebStoreCoreApplication/Views/Base/Index.cshtml @@ -70,7 +70,7 @@
- @Html.Partial("Partial/LeftBar") + @await Html.PartialAsync("Partial/LeftBar")
diff --git a/WebStoreCoreApplication/Views/Base/_404.cshtml b/WebStoreCoreApplication/Views/Base/NotPage.cshtml similarity index 100% rename from WebStoreCoreApplication/Views/Base/_404.cshtml rename to WebStoreCoreApplication/Views/Base/NotPage.cshtml diff --git a/WebStoreCoreApplication/Views/Base/Partial/LeftBar.cshtml b/WebStoreCoreApplication/Views/Base/Partial/LeftBar.cshtml index d8aa391..49bd609 100644 --- a/WebStoreCoreApplication/Views/Base/Partial/LeftBar.cshtml +++ b/WebStoreCoreApplication/Views/Base/Partial/LeftBar.cshtml @@ -1,12 +1,12 @@ 
\ No newline at end of file diff --git a/WebStoreCoreApplication/Views/Employee/Delete.cshtml b/WebStoreCoreApplication/Views/Employee/Delete.cshtml new file mode 100644 index 0000000..1cf9a4b --- /dev/null +++ b/WebStoreCoreApplication/Views/Employee/Delete.cshtml @@ -0,0 +1,31 @@ +@model WebStoreCoreApplication.ViewModels.EmployeeViewModel +@{ + ViewData["Title"] = "Delete"; + Layout = "~/Views/Shared/_Layout.cshtml"; +} + +

Delete

+ + +

Id

+
+
+
+
+
+ +
+ + + +
+ +
+ +
+
+
+
+ diff --git a/WebStoreCoreApplication/Views/Employee/Edit.cshtml b/WebStoreCoreApplication/Views/Employee/Edit.cshtml new file mode 100644 index 0000000..fff7923 --- /dev/null +++ b/WebStoreCoreApplication/Views/Employee/Edit.cshtml @@ -0,0 +1,53 @@ +@model WebStoreCoreApplication.ViewModels.EmployeeViewModel + +@{ + ViewData["Title"] = "Edit"; + Layout = "~/Views/Shared/_Layout.cshtml"; +} + Redactor +

Redactor

+

EmployViewModel


+
+
+
+
+ +
+ + + +
+
+ + + +
+
+ + + +
+
+ + + +
+
+ + + +
+
+ + + +
+
+ +
+
+
+
+ diff --git a/WebStoreCoreApplication/Views/Employee/EmployeeDetails.cshtml b/WebStoreCoreApplication/Views/Employee/EmployeeDetails.cshtml index 32c8621..ebee50d 100644 --- a/WebStoreCoreApplication/Views/Employee/EmployeeDetails.cshtml +++ b/WebStoreCoreApplication/Views/Employee/EmployeeDetails.cshtml @@ -15,15 +15,15 @@ Имя - @Model.Name + @Model.IName Фамилия - @Model.SName + @Model.FName Отчество - @Model.FName + @Model.OName Возраст diff --git a/WebStoreCoreApplication/Views/Employee/Employees.cshtml b/WebStoreCoreApplication/Views/Employee/Employees.cshtml index 420f2f4..58186c3 100644 --- a/WebStoreCoreApplication/Views/Employee/Employees.cshtml +++ b/WebStoreCoreApplication/Views/Employee/Employees.cshtml @@ -8,16 +8,19 @@ Employees + Новый сотрудник @foreach (var item in Model) { + - - + + + }
@item.Name@item.SName@item.IName @item.FName@item.OName @Html.ActionLink("Детали", "EmployeeDetails", new { id=item.Id})
diff --git a/WebStoreCoreApplication/Views/Shared/Header/HeadBot.cshtml b/WebStoreCoreApplication/Views/Shared/Header/HeadBot.cshtml index dd17490..c084f55 100644 --- a/WebStoreCoreApplication/Views/Shared/Header/HeadBot.cshtml +++ b/WebStoreCoreApplication/Views/Shared/Header/HeadBot.cshtml @@ -33,7 +33,7 @@
  • Blog Single
  • -
  • 404
  • +
  • 404
  • Contact
  • Employee
  • diff --git a/WebStoreCoreApplication/Views/Shared/_Layout.cshtml b/WebStoreCoreApplication/Views/Shared/_Layout.cshtml index ebfc52d..e9f5bb1 100644 --- a/WebStoreCoreApplication/Views/Shared/_Layout.cshtml +++ b/WebStoreCoreApplication/Views/Shared/_Layout.cshtml @@ -3,8 +3,10 @@ @Html.Partial("Head") - - @RenderBody() + +
    + @RenderBody() +
    @Html.Partial("Foot")