From 4e25e34b998a9fdf0eb977582618fb8171e0e071 Mon Sep 17 00:00:00 2001 From: "ani_konarcheva@abv.bg" Date: Thu, 7 Apr 2022 23:52:54 +0300 Subject: [PATCH] UsersController --- RentACar/Data/Data/RentACarDbContext.cs | 2 +- RentACar/Data/Models/Car.cs | 1 + RentACar/Data/Models/Rents.cs | 4 + RentACar/RentACar.sln | 10 +- .../Identity/Pages/Account/Register.cshtml.cs | 6 +- .../WebApp/Controllers/UsersController.cs | 153 ++++++++++++++++++ RentACar/WebApp/Views/Rents/Index.cshtml | 18 ++- RentACar/WebApp/Views/Users/Create.cshtml | 118 ++++++++++++++ RentACar/WebApp/Views/Users/Delete.cshtml | 123 ++++++++++++++ RentACar/WebApp/Views/Users/Details.cshtml | 120 ++++++++++++++ RentACar/WebApp/Views/Users/Edit.cshtml | 114 +++++++++++++ RentACar/WebApp/Views/Users/Index.cshtml | 131 +++++++++++++++ RentACar/WebApp/appsettings.json | 2 +- .../WebApp/bin/Debug/net5.0/appsettings.json | 2 +- 14 files changed, 784 insertions(+), 20 deletions(-) create mode 100644 RentACar/WebApp/Controllers/UsersController.cs create mode 100644 RentACar/WebApp/Views/Users/Create.cshtml create mode 100644 RentACar/WebApp/Views/Users/Delete.cshtml create mode 100644 RentACar/WebApp/Views/Users/Details.cshtml create mode 100644 RentACar/WebApp/Views/Users/Edit.cshtml create mode 100644 RentACar/WebApp/Views/Users/Index.cshtml diff --git a/RentACar/Data/Data/RentACarDbContext.cs b/RentACar/Data/Data/RentACarDbContext.cs index 80fe50b..5e28229 100644 --- a/RentACar/Data/Data/RentACarDbContext.cs +++ b/RentACar/Data/Data/RentACarDbContext.cs @@ -20,7 +20,7 @@ namespace Data { if (!optionsBuilder.IsConfigured) { - optionsBuilder.UseSqlServer("Server=.;Database=RentACar;Integrated Security=true;"); + optionsBuilder.UseSqlServer("Server=.\\SQLEXPRESS;Database=RentACar;Integrated Security=true;"); } } diff --git a/RentACar/Data/Models/Car.cs b/RentACar/Data/Models/Car.cs index 2ecb9cc..8416f84 100644 --- a/RentACar/Data/Models/Car.cs +++ b/RentACar/Data/Models/Car.cs @@ -9,6 +9,7 @@ namespace Data.Entities { public class Car { + [Required] public int Id { get; set; } [Required] diff --git a/RentACar/Data/Models/Rents.cs b/RentACar/Data/Models/Rents.cs index 77a541a..52349d1 100644 --- a/RentACar/Data/Models/Rents.cs +++ b/RentACar/Data/Models/Rents.cs @@ -1,6 +1,7 @@ using Microsoft.AspNetCore.Identity; using System; using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; using System.Linq; using System.Text; @@ -11,10 +12,13 @@ namespace Data.Entities public class Rents { public int Id { get; set; } + [Required] [ForeignKey("Car")] public int CarId { get; set; } public virtual Car Car { get; set; } + [Required] public DateTime StartDate { get; set; } + [Required] public DateTime EndDate { get; set; } [ForeignKey("User")] public string UserId { get; set; } diff --git a/RentACar/RentACar.sln b/RentACar/RentACar.sln index 1858c0b..ed6961a 100644 --- a/RentACar/RentACar.sln +++ b/RentACar/RentACar.sln @@ -1,14 +1,12 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Version 17 -VisualStudioVersion = 17.1.32228.430 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.32106.194 MinimumVisualStudioVersion = 10.0.40219.1 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WebApp", "WebApp\WebApp.csproj", "{FD105E4B-8B22-41FA-A754-BF199782A366}" EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Data", "Data\Data.csproj", "{CAF59A92-38E8-4115-9761-7F4A6E90848F}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Unit Tests", "Unit Tests\Unit Tests.csproj", "{14BE8EAC-8625-43C9-99AA-46442CED0808}" -EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -23,10 +21,6 @@ Global {CAF59A92-38E8-4115-9761-7F4A6E90848F}.Debug|Any CPU.Build.0 = Debug|Any CPU {CAF59A92-38E8-4115-9761-7F4A6E90848F}.Release|Any CPU.ActiveCfg = Release|Any CPU {CAF59A92-38E8-4115-9761-7F4A6E90848F}.Release|Any CPU.Build.0 = Release|Any CPU - {14BE8EAC-8625-43C9-99AA-46442CED0808}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {14BE8EAC-8625-43C9-99AA-46442CED0808}.Debug|Any CPU.Build.0 = Debug|Any CPU - {14BE8EAC-8625-43C9-99AA-46442CED0808}.Release|Any CPU.ActiveCfg = Release|Any CPU - {14BE8EAC-8625-43C9-99AA-46442CED0808}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/RentACar/WebApp/Areas/Identity/Pages/Account/Register.cshtml.cs b/RentACar/WebApp/Areas/Identity/Pages/Account/Register.cshtml.cs index b451f3f..f6f01b4 100644 --- a/RentACar/WebApp/Areas/Identity/Pages/Account/Register.cshtml.cs +++ b/RentACar/WebApp/Areas/Identity/Pages/Account/Register.cshtml.cs @@ -14,6 +14,7 @@ using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; using Microsoft.AspNetCore.WebUtilities; using Microsoft.Extensions.Logging; +using Microsoft.EntityFrameworkCore; namespace WebApp.Areas.Identity.Pages.Account { @@ -56,12 +57,13 @@ namespace WebApp.Areas.Identity.Pages.Account [Display(Name = "Email")] public string Email { get; set; } - [Display(Name = "First Name")] + [Display(Name = "First name")] public string FirstName { get; set; } - [Display(Name = "Last Name")] + [Display(Name = "Last name")] public string LastName { get; set; } + [Phone] [Display(Name = "Phone")] public string PhoneNumber { get; set; } diff --git a/RentACar/WebApp/Controllers/UsersController.cs b/RentACar/WebApp/Controllers/UsersController.cs new file mode 100644 index 0000000..f652472 --- /dev/null +++ b/RentACar/WebApp/Controllers/UsersController.cs @@ -0,0 +1,153 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.Rendering; +using Microsoft.EntityFrameworkCore; +using Data; +using Data.Entities; + +namespace WebApp.Controllers +{ + public class UsersController : Controller + { + private readonly RentACarDbContext _context; + + public UsersController(RentACarDbContext context) + { + _context = context; + } + + // GET: Users + public async Task Index() + { + return View(await _context.Users.ToListAsync()); + } + + // GET: Users/Details/5 + public async Task Details(string id) + { + if (id == null) + { + return NotFound(); + } + + var user = await _context.Users + .FirstOrDefaultAsync(m => m.Id == id); + if (user == null) + { + return NotFound(); + } + + return View(user); + } + + // GET: Users/Create + public IActionResult Create() + { + return View(); + } + + // POST: Users/Create + // To protect from overposting attacks, enable the specific properties you want to bind to. + // For more details, see http://go.microsoft.com/fwlink/?LinkId=317598. + [HttpPost] + [ValidateAntiForgeryToken] + public async Task Create([Bind("FirstName,LastName,PersonalNumber,Id,UserName,NormalizedUserName,Email,NormalizedEmail,EmailConfirmed,PasswordHash,SecurityStamp,ConcurrencyStamp,PhoneNumber,PhoneNumberConfirmed,TwoFactorEnabled,LockoutEnd,LockoutEnabled,AccessFailedCount")] User user) + { + if (ModelState.IsValid) + { + _context.Add(user); + await _context.SaveChangesAsync(); + return RedirectToAction(nameof(Index)); + } + return View(user); + } + + // GET: Users/Edit/5 + public async Task Edit(string id) + { + if (id == null) + { + return NotFound(); + } + + var user = await _context.Users.FindAsync(id); + if (user == null) + { + return NotFound(); + } + return View(user); + } + + // POST: Users/Edit/5 + // To protect from overposting attacks, enable the specific properties you want to bind to. + // For more details, see http://go.microsoft.com/fwlink/?LinkId=317598. + [HttpPost] + [ValidateAntiForgeryToken] + public async Task Edit(string id, [Bind("FirstName,LastName,PersonalNumber,Id,UserName,NormalizedUserName,Email,NormalizedEmail,EmailConfirmed,PasswordHash,SecurityStamp,ConcurrencyStamp,PhoneNumber,PhoneNumberConfirmed,TwoFactorEnabled,LockoutEnd,LockoutEnabled,AccessFailedCount")] User user) + { + if (id != user.Id) + { + return NotFound(); + } + + if (ModelState.IsValid) + { + try + { + _context.Update(user); + await _context.SaveChangesAsync(); + } + catch (DbUpdateConcurrencyException) + { + if (!UserExists(user.Id)) + { + return NotFound(); + } + else + { + throw; + } + } + return RedirectToAction(nameof(Index)); + } + return View(user); + } + + // GET: Users/Delete/5 + public async Task Delete(string id) + { + if (id == null) + { + return NotFound(); + } + + var user = await _context.Users + .FirstOrDefaultAsync(m => m.Id == id); + if (user == null) + { + return NotFound(); + } + + return View(user); + } + + // POST: Users/Delete/5 + [HttpPost, ActionName("Delete")] + [ValidateAntiForgeryToken] + public async Task DeleteConfirmed(string id) + { + var user = await _context.Users.FindAsync(id); + _context.Users.Remove(user); + await _context.SaveChangesAsync(); + return RedirectToAction(nameof(Index)); + } + + private bool UserExists(string id) + { + return _context.Users.Any(e => e.Id == id); + } + } +} diff --git a/RentACar/WebApp/Views/Rents/Index.cshtml b/RentACar/WebApp/Views/Rents/Index.cshtml index 80c82a5..c342a8a 100644 --- a/RentACar/WebApp/Views/Rents/Index.cshtml +++ b/RentACar/WebApp/Views/Rents/Index.cshtml @@ -4,7 +4,8 @@ ViewData["Title"] = "Index"; } -

Index

+

Rents

+

Available cars

Create New @@ -30,12 +31,15 @@ @Html.DisplayFor(modelItem => item.EndDate) - - Edit | - Details | - Delete - - + @if (this.User.IsInRole("Admin")) + { + + Edit | + Details | + Delete + + } + } diff --git a/RentACar/WebApp/Views/Users/Create.cshtml b/RentACar/WebApp/Views/Users/Create.cshtml new file mode 100644 index 0000000..5c34fe4 --- /dev/null +++ b/RentACar/WebApp/Views/Users/Create.cshtml @@ -0,0 +1,118 @@ +@model Data.Entities.User + +@{ + ViewData["Title"] = "Create"; +} + +

Create

+ +

User

+
+
+
+
+
+
+ + + +
+
+ + + +
+
+ + + +
+
+ + + +
+
+ + + +
+
+ + + +
+
+ + + +
+
+ + + +
+
+ +
+
+ + + +
+
+ + + +
+
+ + + +
+
+ + + +
+
+ +
+
+ +
+
+ + + +
+
+ +
+
+ + + +
+
+ +
+
+
+
+ + + +@section Scripts { + @{await Html.RenderPartialAsync("_ValidationScriptsPartial");} +} diff --git a/RentACar/WebApp/Views/Users/Delete.cshtml b/RentACar/WebApp/Views/Users/Delete.cshtml new file mode 100644 index 0000000..990ae5d --- /dev/null +++ b/RentACar/WebApp/Views/Users/Delete.cshtml @@ -0,0 +1,123 @@ +@model Data.Entities.User + +@{ + ViewData["Title"] = "Delete"; +} + +

Delete

+ +

Are you sure you want to delete this?

+
+

User

+
+
+
+ @Html.DisplayNameFor(model => model.FirstName) +
+
+ @Html.DisplayFor(model => model.FirstName) +
+
+ @Html.DisplayNameFor(model => model.LastName) +
+
+ @Html.DisplayFor(model => model.LastName) +
+
+ @Html.DisplayNameFor(model => model.PersonalNumber) +
+
+ @Html.DisplayFor(model => model.PersonalNumber) +
+
+ @Html.DisplayNameFor(model => model.UserName) +
+
+ @Html.DisplayFor(model => model.UserName) +
+
+ @Html.DisplayNameFor(model => model.NormalizedUserName) +
+
+ @Html.DisplayFor(model => model.NormalizedUserName) +
+
+ @Html.DisplayNameFor(model => model.Email) +
+
+ @Html.DisplayFor(model => model.Email) +
+
+ @Html.DisplayNameFor(model => model.NormalizedEmail) +
+
+ @Html.DisplayFor(model => model.NormalizedEmail) +
+
+ @Html.DisplayNameFor(model => model.EmailConfirmed) +
+
+ @Html.DisplayFor(model => model.EmailConfirmed) +
+
+ @Html.DisplayNameFor(model => model.PasswordHash) +
+
+ @Html.DisplayFor(model => model.PasswordHash) +
+
+ @Html.DisplayNameFor(model => model.SecurityStamp) +
+
+ @Html.DisplayFor(model => model.SecurityStamp) +
+
+ @Html.DisplayNameFor(model => model.ConcurrencyStamp) +
+
+ @Html.DisplayFor(model => model.ConcurrencyStamp) +
+
+ @Html.DisplayNameFor(model => model.PhoneNumber) +
+
+ @Html.DisplayFor(model => model.PhoneNumber) +
+
+ @Html.DisplayNameFor(model => model.PhoneNumberConfirmed) +
+
+ @Html.DisplayFor(model => model.PhoneNumberConfirmed) +
+
+ @Html.DisplayNameFor(model => model.TwoFactorEnabled) +
+
+ @Html.DisplayFor(model => model.TwoFactorEnabled) +
+
+ @Html.DisplayNameFor(model => model.LockoutEnd) +
+
+ @Html.DisplayFor(model => model.LockoutEnd) +
+
+ @Html.DisplayNameFor(model => model.LockoutEnabled) +
+
+ @Html.DisplayFor(model => model.LockoutEnabled) +
+
+ @Html.DisplayNameFor(model => model.AccessFailedCount) +
+
+ @Html.DisplayFor(model => model.AccessFailedCount) +
+
+ +
+ + | + Back to List +
+
diff --git a/RentACar/WebApp/Views/Users/Details.cshtml b/RentACar/WebApp/Views/Users/Details.cshtml new file mode 100644 index 0000000..00a56fb --- /dev/null +++ b/RentACar/WebApp/Views/Users/Details.cshtml @@ -0,0 +1,120 @@ +@model Data.Entities.User + +@{ + ViewData["Title"] = "Details"; +} + +

Details

+ +
+

User

+
+
+
+ @Html.DisplayNameFor(model => model.FirstName) +
+
+ @Html.DisplayFor(model => model.FirstName) +
+
+ @Html.DisplayNameFor(model => model.LastName) +
+
+ @Html.DisplayFor(model => model.LastName) +
+
+ @Html.DisplayNameFor(model => model.PersonalNumber) +
+
+ @Html.DisplayFor(model => model.PersonalNumber) +
+
+ @Html.DisplayNameFor(model => model.UserName) +
+
+ @Html.DisplayFor(model => model.UserName) +
+
+ @Html.DisplayNameFor(model => model.NormalizedUserName) +
+
+ @Html.DisplayFor(model => model.NormalizedUserName) +
+
+ @Html.DisplayNameFor(model => model.Email) +
+
+ @Html.DisplayFor(model => model.Email) +
+
+ @Html.DisplayNameFor(model => model.NormalizedEmail) +
+
+ @Html.DisplayFor(model => model.NormalizedEmail) +
+
+ @Html.DisplayNameFor(model => model.EmailConfirmed) +
+
+ @Html.DisplayFor(model => model.EmailConfirmed) +
+
+ @Html.DisplayNameFor(model => model.PasswordHash) +
+
+ @Html.DisplayFor(model => model.PasswordHash) +
+
+ @Html.DisplayNameFor(model => model.SecurityStamp) +
+
+ @Html.DisplayFor(model => model.SecurityStamp) +
+
+ @Html.DisplayNameFor(model => model.ConcurrencyStamp) +
+
+ @Html.DisplayFor(model => model.ConcurrencyStamp) +
+
+ @Html.DisplayNameFor(model => model.PhoneNumber) +
+
+ @Html.DisplayFor(model => model.PhoneNumber) +
+
+ @Html.DisplayNameFor(model => model.PhoneNumberConfirmed) +
+
+ @Html.DisplayFor(model => model.PhoneNumberConfirmed) +
+
+ @Html.DisplayNameFor(model => model.TwoFactorEnabled) +
+
+ @Html.DisplayFor(model => model.TwoFactorEnabled) +
+
+ @Html.DisplayNameFor(model => model.LockoutEnd) +
+
+ @Html.DisplayFor(model => model.LockoutEnd) +
+
+ @Html.DisplayNameFor(model => model.LockoutEnabled) +
+
+ @Html.DisplayFor(model => model.LockoutEnabled) +
+
+ @Html.DisplayNameFor(model => model.AccessFailedCount) +
+
+ @Html.DisplayFor(model => model.AccessFailedCount) +
+
+
+ diff --git a/RentACar/WebApp/Views/Users/Edit.cshtml b/RentACar/WebApp/Views/Users/Edit.cshtml new file mode 100644 index 0000000..5646ff1 --- /dev/null +++ b/RentACar/WebApp/Views/Users/Edit.cshtml @@ -0,0 +1,114 @@ +@model Data.Entities.User + +@{ + ViewData["Title"] = "Edit"; +} + +

Edit

+ +

User

+
+
+
+
+
+
+ + + +
+
+ + + +
+
+ + + +
+ +
+ + + +
+
+ + + +
+
+ + + +
+
+ + + +
+
+ +
+
+ + + +
+
+ + + +
+
+ + + +
+
+ + + +
+
+ +
+
+ +
+
+ + + +
+
+ +
+
+ + + +
+
+ +
+
+
+
+ + + +@section Scripts { + @{await Html.RenderPartialAsync("_ValidationScriptsPartial");} +} diff --git a/RentACar/WebApp/Views/Users/Index.cshtml b/RentACar/WebApp/Views/Users/Index.cshtml new file mode 100644 index 0000000..72e545d --- /dev/null +++ b/RentACar/WebApp/Views/Users/Index.cshtml @@ -0,0 +1,131 @@ +@model IEnumerable + +@{ + ViewData["Title"] = "Index"; +} + +

Index

+ +

+ Create New +

+ + + + + + + + + + + + + + + + + + + + + + + + +@foreach (var item in Model) { + + + + + + + + + + + + + + + + + + + + +} + +
+ @Html.DisplayNameFor(model => model.FirstName) + + @Html.DisplayNameFor(model => model.LastName) + + @Html.DisplayNameFor(model => model.PersonalNumber) + + @Html.DisplayNameFor(model => model.UserName) + + @Html.DisplayNameFor(model => model.NormalizedUserName) + + @Html.DisplayNameFor(model => model.Email) + + @Html.DisplayNameFor(model => model.NormalizedEmail) + + @Html.DisplayNameFor(model => model.EmailConfirmed) + + @Html.DisplayNameFor(model => model.PasswordHash) + + @Html.DisplayNameFor(model => model.SecurityStamp) + + @Html.DisplayNameFor(model => model.ConcurrencyStamp) + + @Html.DisplayNameFor(model => model.PhoneNumber) + + @Html.DisplayNameFor(model => model.PhoneNumberConfirmed) + + @Html.DisplayNameFor(model => model.TwoFactorEnabled) + + @Html.DisplayNameFor(model => model.LockoutEnd) + + @Html.DisplayNameFor(model => model.LockoutEnabled) + + @Html.DisplayNameFor(model => model.AccessFailedCount) +
+ @Html.DisplayFor(modelItem => item.FirstName) + + @Html.DisplayFor(modelItem => item.LastName) + + @Html.DisplayFor(modelItem => item.PersonalNumber) + + @Html.DisplayFor(modelItem => item.UserName) + + @Html.DisplayFor(modelItem => item.NormalizedUserName) + + @Html.DisplayFor(modelItem => item.Email) + + @Html.DisplayFor(modelItem => item.NormalizedEmail) + + @Html.DisplayFor(modelItem => item.EmailConfirmed) + + @Html.DisplayFor(modelItem => item.PasswordHash) + + @Html.DisplayFor(modelItem => item.SecurityStamp) + + @Html.DisplayFor(modelItem => item.ConcurrencyStamp) + + @Html.DisplayFor(modelItem => item.PhoneNumber) + + @Html.DisplayFor(modelItem => item.PhoneNumberConfirmed) + + @Html.DisplayFor(modelItem => item.TwoFactorEnabled) + + @Html.DisplayFor(modelItem => item.LockoutEnd) + + @Html.DisplayFor(modelItem => item.LockoutEnabled) + + @Html.DisplayFor(modelItem => item.AccessFailedCount) + + Edit | + Details | + Delete +
diff --git a/RentACar/WebApp/appsettings.json b/RentACar/WebApp/appsettings.json index 5bd170a..e899df0 100644 --- a/RentACar/WebApp/appsettings.json +++ b/RentACar/WebApp/appsettings.json @@ -1,6 +1,6 @@ { "ConnectionStrings": { - "DefaultConnection": "Server=.;Database=RentACar;Integrated Security=true;" + "DefaultConnection": "Server=.\\SQLEXPRESS;Database=RentACar;Integrated Security=true;" }, "Logging": { "LogLevel": { diff --git a/RentACar/WebApp/bin/Debug/net5.0/appsettings.json b/RentACar/WebApp/bin/Debug/net5.0/appsettings.json index 5bd170a..e899df0 100644 --- a/RentACar/WebApp/bin/Debug/net5.0/appsettings.json +++ b/RentACar/WebApp/bin/Debug/net5.0/appsettings.json @@ -1,6 +1,6 @@ { "ConnectionStrings": { - "DefaultConnection": "Server=.;Database=RentACar;Integrated Security=true;" + "DefaultConnection": "Server=.\\SQLEXPRESS;Database=RentACar;Integrated Security=true;" }, "Logging": { "LogLevel": {