From c04ffc502012f138a09abb7ddd8eb1251a56b9bf Mon Sep 17 00:00:00 2001 From: Dimitar Byalkov Date: Sat, 9 Apr 2022 00:16:58 +0300 Subject: [PATCH] Mostly working create rent. Awaiting migration --- RentACar/Data/Models/Car.cs | 10 ----- .../WebApp/Controllers/RentsController.cs | 11 ++--- RentACar/WebApp/Views/Rents/Index.cshtml | 40 +++++++++++-------- 3 files changed, 27 insertions(+), 34 deletions(-) diff --git a/RentACar/Data/Models/Car.cs b/RentACar/Data/Models/Car.cs index ae49492..c2be692 100644 --- a/RentACar/Data/Models/Car.cs +++ b/RentACar/Data/Models/Car.cs @@ -12,22 +12,12 @@ namespace Data.Entities [Required] [Key] public int Id { get; set; } - - [Required] public string Brand { get; set; } - - [Required] public string Model { get; set; } - - [Required] public int Year { get; set; } - - [Required] public int CountPassengerSeats { get; set; } public string Description { get; set; } - - [Required] public decimal PriceForDay { get; set; } } } diff --git a/RentACar/WebApp/Controllers/RentsController.cs b/RentACar/WebApp/Controllers/RentsController.cs index c1f9cc9..8e6fa7e 100644 --- a/RentACar/WebApp/Controllers/RentsController.cs +++ b/RentACar/WebApp/Controllers/RentsController.cs @@ -59,12 +59,14 @@ namespace WebApp.Controllers [HttpPost] [ValidateAntiForgeryToken] [Authorize] - public async Task Create([Bind("CarId,StartDate,EndDate")] Rents rents) + public async Task Create(Rents rents) { if (ModelState.IsValid) { var car = _context.Cars.FirstOrDefault(car => car.Id == 1); rents.Car = car; + var user = _context.Users.FirstOrDefault(user => user.UserName == User.Identity.Name); + rents.User = user; _context.Add(rents); await _context.SaveChangesAsync(); return RedirectToAction(nameof(Index)); @@ -95,13 +97,8 @@ namespace WebApp.Controllers [HttpPost] [ValidateAntiForgeryToken] [Authorize(Roles = "Admin")] - public async Task Edit(int id, [Bind("CarId,StartDate,EndDate")] Rents rents) + public async Task Edit(Rents rents) { - if (id != rents.Id) - { - return NotFound(); - } - if (ModelState.IsValid) { try diff --git a/RentACar/WebApp/Views/Rents/Index.cshtml b/RentACar/WebApp/Views/Rents/Index.cshtml index ca7e01d..ed6e53f 100644 --- a/RentACar/WebApp/Views/Rents/Index.cshtml +++ b/RentACar/WebApp/Views/Rents/Index.cshtml @@ -23,6 +23,9 @@ End Date + + User + @if (this.User.IsInRole("Admin")) { @@ -34,25 +37,28 @@ @foreach (var item in Model) { - + + + @Html.DisplayFor(modelItem => item.Car.Id) + + + @Html.DisplayFor(modelItem => item.StartDate) + + + @Html.DisplayFor(modelItem => item.EndDate) + + + @Html.DisplayFor(modelItem => item.User.UserName) + + @if (this.User.IsInRole("Admin")) + { - @Html.DisplayFor(modelItem => item.Car.Id) + Edit | + Details | + Delete - - @Html.DisplayFor(modelItem => item.StartDate) - - - @Html.DisplayFor(modelItem => item.EndDate) - - @if (this.User.IsInRole("Admin")) - { - - Edit | - Details | - Delete - - } - + } + }