Mostly working create rent. Awaiting migration

This commit is contained in:
Dimitar Byalkov
2022-04-09 00:16:58 +03:00
parent 91e835f63d
commit c04ffc5020
3 changed files with 27 additions and 34 deletions

View File

@@ -12,22 +12,12 @@ namespace Data.Entities
[Required] [Required]
[Key] [Key]
public int Id { get; set; } public int Id { get; set; }
[Required]
public string Brand { get; set; } public string Brand { get; set; }
[Required]
public string Model { get; set; } public string Model { get; set; }
[Required]
public int Year { get; set; } public int Year { get; set; }
[Required]
public int CountPassengerSeats { get; set; } public int CountPassengerSeats { get; set; }
public string Description { get; set; } public string Description { get; set; }
[Required]
public decimal PriceForDay { get; set; } public decimal PriceForDay { get; set; }
} }
} }

View File

@@ -59,12 +59,14 @@ namespace WebApp.Controllers
[HttpPost] [HttpPost]
[ValidateAntiForgeryToken] [ValidateAntiForgeryToken]
[Authorize] [Authorize]
public async Task<IActionResult> Create([Bind("CarId,StartDate,EndDate")] Rents rents) public async Task<IActionResult> Create(Rents rents)
{ {
if (ModelState.IsValid) if (ModelState.IsValid)
{ {
var car = _context.Cars.FirstOrDefault(car => car.Id == 1); var car = _context.Cars.FirstOrDefault(car => car.Id == 1);
rents.Car = car; rents.Car = car;
var user = _context.Users.FirstOrDefault(user => user.UserName == User.Identity.Name);
rents.User = user;
_context.Add(rents); _context.Add(rents);
await _context.SaveChangesAsync(); await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index)); return RedirectToAction(nameof(Index));
@@ -95,13 +97,8 @@ namespace WebApp.Controllers
[HttpPost] [HttpPost]
[ValidateAntiForgeryToken] [ValidateAntiForgeryToken]
[Authorize(Roles = "Admin")] [Authorize(Roles = "Admin")]
public async Task<IActionResult> Edit(int id, [Bind("CarId,StartDate,EndDate")] Rents rents) public async Task<IActionResult> Edit(Rents rents)
{ {
if (id != rents.Id)
{
return NotFound();
}
if (ModelState.IsValid) if (ModelState.IsValid)
{ {
try try

View File

@@ -23,6 +23,9 @@
<th> <th>
End Date End Date
</th> </th>
<th>
User
</th>
@if (this.User.IsInRole("Admin")) @if (this.User.IsInRole("Admin"))
{ {
<th> <th>
@@ -44,6 +47,9 @@
<td> <td>
@Html.DisplayFor(modelItem => item.EndDate) @Html.DisplayFor(modelItem => item.EndDate)
</td> </td>
<td>
@Html.DisplayFor(modelItem => item.User.UserName)
</td>
@if (this.User.IsInRole("Admin")) @if (this.User.IsInRole("Admin"))
{ {
<td> <td>