Working version: Rents done
This commit is contained in:
@@ -8,6 +8,7 @@ using Microsoft.EntityFrameworkCore;
|
||||
using Data;
|
||||
using Data.Entities;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using System.Text;
|
||||
|
||||
namespace WebApp.Controllers
|
||||
{
|
||||
@@ -61,15 +62,27 @@ namespace WebApp.Controllers
|
||||
[Authorize]
|
||||
public async Task<IActionResult> Create(Rents rents)
|
||||
{
|
||||
|
||||
if (ModelState.IsValid)
|
||||
{
|
||||
var car = _context.Cars.FirstOrDefault(car => car.Id == 1);
|
||||
try
|
||||
{
|
||||
if (CarIsAvailable(rents.CarId, rents.StartDate, rents.EndDate))
|
||||
{
|
||||
var car = _context.Cars.FirstOrDefault(car => car.Id == rents.CarId);
|
||||
rents.Car = car;
|
||||
var user = _context.Users.FirstOrDefault(user => user.UserName == User.Identity.Name);
|
||||
rents.User = user;
|
||||
_context.Add(rents);
|
||||
}
|
||||
else throw new ArgumentOutOfRangeException("Selected car is unavailable at the chosen times.");
|
||||
await _context.SaveChangesAsync();
|
||||
}
|
||||
catch (ArgumentOutOfRangeException e)
|
||||
{
|
||||
byte[] data = Encoding.UTF8.GetBytes(e.ParamName + " <a href=\"javascript:history.back()\">Back</a>");
|
||||
Response.ContentType = "text/html";
|
||||
await Response.Body.WriteAsync(data, 0, data.Length);
|
||||
}
|
||||
return RedirectToAction(nameof(Index));
|
||||
}
|
||||
return View(rents);
|
||||
@@ -103,10 +116,14 @@ namespace WebApp.Controllers
|
||||
if (ModelState.IsValid)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (CarIsAvailable(rents.CarId, rents.StartDate, rents.EndDate))
|
||||
{
|
||||
_context.Update(rents);
|
||||
await _context.SaveChangesAsync();
|
||||
}
|
||||
else throw new ArgumentOutOfRangeException("Selected car is unavailable at the chosen times.");
|
||||
}
|
||||
catch (DbUpdateConcurrencyException)
|
||||
{
|
||||
if (!RentsExists(rents.Id))
|
||||
@@ -118,6 +135,12 @@ namespace WebApp.Controllers
|
||||
throw;
|
||||
}
|
||||
}
|
||||
catch (ArgumentOutOfRangeException ex)
|
||||
{
|
||||
byte[] data = Encoding.UTF8.GetBytes(ex.ParamName + " <a href=\"javascript:history.back()\">Back</a>");
|
||||
Response.ContentType = "text/html";
|
||||
await Response.Body.WriteAsync(data, 0, data.Length);
|
||||
}
|
||||
return RedirectToAction(nameof(Index));
|
||||
}
|
||||
return View(rents);
|
||||
@@ -160,16 +183,22 @@ namespace WebApp.Controllers
|
||||
}
|
||||
public bool CarIsAvailable(int carId, DateTime startDate, DateTime endDate)
|
||||
{
|
||||
var rents = _context.Rents.Where(x => x.Car.Id == carId);
|
||||
if (rents != null)
|
||||
{
|
||||
var rents = _context.Rents.Where(x => x.CarId == carId);
|
||||
foreach (var item in rents)
|
||||
{
|
||||
return !(item.StartDate < endDate && startDate < item.EndDate);
|
||||
}
|
||||
if (item.StartDate > item.EndDate)
|
||||
throw new ArgumentException("A start can not be after its end.");
|
||||
|
||||
if (startDate > endDate)
|
||||
throw new ArgumentException("B start can not be after its end.");
|
||||
|
||||
if (!((item.EndDate < startDate && item.StartDate < startDate) ||
|
||||
(endDate < item.StartDate && startDate < item.StartDate)))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else return true;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,9 +10,9 @@
|
||||
<div class="col-md-4">
|
||||
<form asp-action="Create">
|
||||
<div class="form-group">
|
||||
<label asp-for="Car.Id" class="control-label"></label>
|
||||
<input asp-for="Car.Id" class="form-control" />
|
||||
<span asp-validation-for="Car.Id" class="text-danger"></span>
|
||||
<label asp-for="CarId" class="control-label"></label>
|
||||
<input asp-for="CarId" class="form-control" />
|
||||
<span asp-validation-for="CarId" class="text-danger"></span>
|
||||
</div>
|
||||
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
|
||||
<div class="form-group">
|
||||
|
||||
@@ -12,9 +12,9 @@
|
||||
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
|
||||
<input type="hidden" asp-for="Id" />
|
||||
<div class="form-group">
|
||||
<label asp-for="Car.Id" class="control-label"></label>
|
||||
<input asp-for="Car.Id" class="form-control" />
|
||||
<span asp-validation-for="Car.Id" class="text-danger"></span>
|
||||
<label asp-for="CarId" class="control-label"></label>
|
||||
<input asp-for="CarId" class="form-control" />
|
||||
<span asp-validation-for="CarId" class="text-danger"></span>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label asp-for="StartDate" class="control-label"></label>
|
||||
|
||||
Reference in New Issue
Block a user