Frontend + backend fixes.
This commit is contained in:
parent
c23db1b70f
commit
5e11905073
@ -8,19 +8,38 @@ namespace RentalBusiness.Controllers
|
||||
{
|
||||
private readonly ILogger<HomeController> _logger;
|
||||
|
||||
private readonly List<StorageViewModel> _storages;
|
||||
private readonly List<ClientViewModel> _clients;
|
||||
public HomeController(ILogger<HomeController> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
_storages = new List<StorageViewModel>();
|
||||
_clients = new List<ClientViewModel>();
|
||||
_clients = SeedData.ReadClients().Result;
|
||||
_storages = SeedData.ReadStorage().Result;
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public IActionResult Index()
|
||||
{
|
||||
ViewBag.Cars = _storages;
|
||||
ViewBag.Clients = _clients;
|
||||
return View();
|
||||
}
|
||||
|
||||
public IActionResult Privacy()
|
||||
|
||||
[HttpPost]
|
||||
public void Index(int carID,int customerID,DateTime returnDate)
|
||||
{
|
||||
return View();
|
||||
ContractViewModel contract = new()
|
||||
{
|
||||
CarID = carID,
|
||||
CustomerID = customerID,
|
||||
ReturnDate = returnDate,
|
||||
Price = Calc(carID, returnDate)
|
||||
};
|
||||
|
||||
SeedData.PostContract(contract) ;
|
||||
}
|
||||
|
||||
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
|
||||
@ -28,5 +47,11 @@ namespace RentalBusiness.Controllers
|
||||
{
|
||||
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public double Calc(int id, DateTime returnDate)
|
||||
{
|
||||
return _storages.ElementAt(id - 1).CarRatio * ((returnDate - DateTime.Now).TotalHours * 4);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,8 +1,9 @@
|
||||
namespace RentalBusiness.Enums
|
||||
|
||||
namespace RentalBusiness.Enums
|
||||
{
|
||||
public enum CarStatus
|
||||
{
|
||||
Available = 0,
|
||||
IsTaken = 1
|
||||
Available,
|
||||
IsTaken
|
||||
}
|
||||
}
|
||||
|
@ -2,8 +2,8 @@
|
||||
{
|
||||
public enum ContractStatus
|
||||
{
|
||||
InProcess = 0,
|
||||
Finished = 1,
|
||||
Expired = 2
|
||||
InProcess,
|
||||
Finished,
|
||||
Expired
|
||||
}
|
||||
}
|
||||
|
9
RentalBusiness/Models/ClientViewModel.cs
Normal file
9
RentalBusiness/Models/ClientViewModel.cs
Normal file
@ -0,0 +1,9 @@
|
||||
namespace RentalBusiness.Models
|
||||
{
|
||||
public class ClientViewModel
|
||||
{
|
||||
public int ID { get; set; }
|
||||
public string FullName { get; set; } = string.Empty;
|
||||
|
||||
}
|
||||
}
|
14
RentalBusiness/Models/ContractViewModel.cs
Normal file
14
RentalBusiness/Models/ContractViewModel.cs
Normal file
@ -0,0 +1,14 @@
|
||||
using RentalBusiness.Enums;
|
||||
|
||||
namespace RentalBusiness.Models
|
||||
{
|
||||
public class ContractViewModel
|
||||
{
|
||||
public int CarID { get; set; }
|
||||
public int CustomerID { get; set; }
|
||||
public DateTime CurDate { get; set; } = DateTime.Now;
|
||||
public DateTime ReturnDate { get; set; }
|
||||
public double Price { get; set; }
|
||||
public ContractStatus Status { get; set; } = ContractStatus.InProcess;
|
||||
}
|
||||
}
|
@ -29,8 +29,7 @@ public partial class RentalbusinessContext : DbContext
|
||||
|
||||
static RentalbusinessContext()
|
||||
{
|
||||
NpgsqlConnection.GlobalTypeMapper.MapEnum<CarStatus>();
|
||||
NpgsqlConnection.GlobalTypeMapper.MapEnum<ContractStatus>();
|
||||
|
||||
}
|
||||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) => optionsBuilder.UseNpgsql("Host=localhost;Port=5432;Database=local;Username=postgres;Password=postgres");
|
||||
|
||||
|
@ -3,13 +3,15 @@ using Npgsql;
|
||||
using RentalBusiness.AuxilaryElements;
|
||||
using RentalBusiness.AuxilaryElements.Implements;
|
||||
using RentalBusiness.AuxilaryElements.ImplementsCars;
|
||||
using RentalBusiness.Enums;
|
||||
using System.Security.Cryptography.X509Certificates;
|
||||
using static Microsoft.EntityFrameworkCore.DbLoggerCategory.Database;
|
||||
|
||||
namespace RentalBusiness.Models
|
||||
{
|
||||
public static class SeedData
|
||||
{
|
||||
private static ICarBrand car;
|
||||
private static ICarBrand car;
|
||||
static readonly List<ICarBrand> Brands = new()
|
||||
{
|
||||
{ car = new Porsche()},
|
||||
@ -23,7 +25,7 @@ namespace RentalBusiness.Models
|
||||
|
||||
public static async Task InitializeBrands(NpgsqlConnection connection)
|
||||
{
|
||||
for (int i = 0; i < 7; i++)
|
||||
for (int i = 0; i < Brands.Count; i++)
|
||||
{
|
||||
car = Brands.ElementAt(i);
|
||||
await using var command = new NpgsqlCommand("INSERT INTO brand (brand_name,brand_ratio) VALUES(@name,@ratio)", connection)
|
||||
@ -84,16 +86,47 @@ namespace RentalBusiness.Models
|
||||
|
||||
public static async Task InitializeCars(NpgsqlConnection connection)
|
||||
{
|
||||
int brandID = 0;
|
||||
int modelID = 0;
|
||||
foreach (var brand in Brands)
|
||||
{
|
||||
await using (var brandIDcommand = new NpgsqlCommand("SELECT brand_id FROM brand WHERE brand.brand_name = @bbn", connection))
|
||||
{
|
||||
brandIDcommand.Parameters.AddWithValue("bbn", brand.Name);
|
||||
|
||||
await using (NpgsqlDataReader reader = await brandIDcommand.ExecuteReaderAsync())
|
||||
{
|
||||
while (reader.Read())
|
||||
{
|
||||
brandID = Convert.ToInt32(reader[0].ToString());
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
foreach (var model in brand.Models)
|
||||
{
|
||||
await using (var modelIDcommand = new NpgsqlCommand("SELECT model_id FROM model WHERE model.model_spec = @mmn", connection))
|
||||
{
|
||||
modelIDcommand.Parameters.AddWithValue("mmn", model.Name);
|
||||
|
||||
await using (NpgsqlDataReader reader = await modelIDcommand.ExecuteReaderAsync())
|
||||
{
|
||||
while (reader.Read())
|
||||
{
|
||||
modelID = Convert.ToInt32(reader[0].ToString());
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
await using var command = new NpgsqlCommand("INSERT INTO storage (brand_id,model_id,car_ratio_per_hour,car_status) VALUES(@bid,@mid,@ratio,@status)", connection)
|
||||
{
|
||||
Parameters =
|
||||
{
|
||||
new("spec",model.Name),
|
||||
new("ratio",model.Ratio)
|
||||
new("bid", brandID),
|
||||
new("mid", modelID),
|
||||
new("ratio",model.Ratio += brand.Ratio),
|
||||
new("status",CarStatus.Available)
|
||||
}
|
||||
|
||||
};
|
||||
@ -103,9 +136,108 @@ namespace RentalBusiness.Models
|
||||
}
|
||||
}
|
||||
|
||||
public static async Task InitializeContracts(NpgsqlConnection connection)
|
||||
public static void PostContract(ContractViewModel contract)
|
||||
{
|
||||
NpgsqlConnection connection = new NpgsqlConnection("Host=localhost;Database=local;Username=postgres;Password=postgres");
|
||||
connection.Open();
|
||||
|
||||
using var command = new NpgsqlCommand("INSERT INTO contract (car_id,customer_id,rental_date,return_date,price,contract_status) VALUES(@caid,@cuid,@rend,@retd,@price,@cs)", connection)
|
||||
{
|
||||
Parameters =
|
||||
{
|
||||
new("caid",contract.CarID),
|
||||
new("cuid",contract.CustomerID),
|
||||
new("rend",contract.CurDate),
|
||||
new("retd",contract.ReturnDate),
|
||||
new("price",contract.Price),
|
||||
new("cs",contract.Status)
|
||||
}
|
||||
|
||||
};
|
||||
command.ExecuteNonQueryAsync();
|
||||
|
||||
using (var carcommand = new NpgsqlCommand("UPDATE storage SET car_status = @cs WHERE car_id = @caid", connection))
|
||||
{
|
||||
carcommand.Parameters.AddWithValue("caid", contract.CarID);
|
||||
};
|
||||
command.ExecuteNonQueryAsync();
|
||||
|
||||
connection.Close();
|
||||
}
|
||||
|
||||
public static async Task<List<StorageViewModel>> ReadStorage()
|
||||
{
|
||||
NpgsqlConnection connection = new NpgsqlConnection("Host=localhost;Database=local;Username=postgres;Password=postgres");
|
||||
List<StorageViewModel> cars = new List<StorageViewModel>();
|
||||
connection.Open();
|
||||
await using (NpgsqlCommand getRecordStorage = new NpgsqlCommand("SELECT S.car_id, B.brand_name, M.model_spec, S.car_ratio_per_hour " +
|
||||
"FROM storage S, brand B, model M " +
|
||||
"WHERE S.car_status = 'available' AND S.model_id = M.model_id " +
|
||||
"AND S.brand_id = B.brand_id",connection))
|
||||
{
|
||||
await using (NpgsqlDataReader reader = await getRecordStorage.ExecuteReaderAsync())
|
||||
{
|
||||
while(await reader.ReadAsync())
|
||||
{
|
||||
StorageViewModel viewModel = ReadStorageView(reader);
|
||||
cars.Add(viewModel);
|
||||
}
|
||||
}
|
||||
}
|
||||
connection.Close();
|
||||
return cars;
|
||||
}
|
||||
|
||||
public static StorageViewModel ReadStorageView(NpgsqlDataReader reader)
|
||||
{
|
||||
int? id = reader["car_id"] as int?;
|
||||
string brandName = reader["brand_name"] as string;
|
||||
string modelName = reader["model_spec"] as string;
|
||||
int? ratio = reader["car_ratio_per_hour"] as int?;
|
||||
|
||||
StorageViewModel view = new StorageViewModel
|
||||
{
|
||||
ID = id.Value,
|
||||
CarInfo = new string(brandName + " " + modelName),
|
||||
CarRatio = ratio.Value
|
||||
};
|
||||
return view;
|
||||
}
|
||||
|
||||
public static async Task<List<ClientViewModel>> ReadClients()
|
||||
{
|
||||
NpgsqlConnection connection = new NpgsqlConnection("Host=localhost;Database=local;Username=postgres;Password=postgres");
|
||||
List<ClientViewModel> clients = new List<ClientViewModel>();
|
||||
connection.Open();
|
||||
await using (NpgsqlCommand getRecordStorage = new NpgsqlCommand("SELECT C.customer_id, C.last_name, C.first_name, C.middle_name " +
|
||||
"FROM customer C " , connection))
|
||||
{
|
||||
await using (NpgsqlDataReader reader = await getRecordStorage.ExecuteReaderAsync())
|
||||
{
|
||||
while (await reader.ReadAsync())
|
||||
{
|
||||
ClientViewModel viewModel = ReadClientView(reader);
|
||||
clients.Add(viewModel);
|
||||
}
|
||||
}
|
||||
}
|
||||
connection.Close();
|
||||
return clients;
|
||||
}
|
||||
|
||||
public static ClientViewModel ReadClientView(NpgsqlDataReader reader)
|
||||
{
|
||||
int? id = reader["customer_id"] as int?;
|
||||
string lastName = reader["last_name"] as string;
|
||||
string firstName = reader["first_name"] as string;
|
||||
string middleName = reader["middle_name"] as string;
|
||||
|
||||
ClientViewModel view = new ClientViewModel
|
||||
{
|
||||
ID = id.Value,
|
||||
FullName = new string(lastName + " " + firstName + " " + middleName)
|
||||
};
|
||||
return view;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
10
RentalBusiness/Models/StorageViewModel.cs
Normal file
10
RentalBusiness/Models/StorageViewModel.cs
Normal file
@ -0,0 +1,10 @@
|
||||
namespace RentalBusiness.Models
|
||||
{
|
||||
public class StorageViewModel
|
||||
{
|
||||
public int ID { get; set; }
|
||||
public string CarInfo { get; set; } = string.Empty;
|
||||
|
||||
public int CarRatio { get; set; }
|
||||
}
|
||||
}
|
@ -5,6 +5,8 @@ using RentalBusiness.Models;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
var dataSourceBuilder = new NpgsqlDataSourceBuilder("Host=localhost;Database=local;Username=postgres;Password=postgres");
|
||||
dataSourceBuilder.MapEnum<CarStatus>();
|
||||
dataSourceBuilder.MapEnum<ContractStatus>();
|
||||
await using var dataSource = dataSourceBuilder.Build();
|
||||
await using var connection = await dataSource.OpenConnectionAsync();
|
||||
// Add services to the container.
|
||||
@ -18,13 +20,12 @@ using (var scope = app.Services.CreateScope())
|
||||
var services = scope.ServiceProvider;
|
||||
using (var context = new RentalbusinessContext(services.GetRequiredService<DbContextOptions<RentalbusinessContext>>()))
|
||||
{
|
||||
if(!context.Models.Any() && !context.Brands.Any() && !context.Customers.Any() && !context.Contracts.Any() && !context.Storages.Any())
|
||||
if(!context.Models.Any() && !context.Brands.Any() && !context.Customers.Any() && !context.Storages.Any())
|
||||
{
|
||||
await SeedData.InitializeBrands(connection);
|
||||
await SeedData.InitializeCustomers(connection);
|
||||
await SeedData.InitializeCars(connection);
|
||||
await SeedData.InitializeContracts(connection);
|
||||
await SeedData.InitializeModels(connection);
|
||||
await SeedData.InitializeCustomers(connection);
|
||||
await SeedData.InitializeCars(connection);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,8 +1,62 @@
|
||||
@{
|
||||
ViewData["Title"] = "Home Page";
|
||||
ViewData["Title"] = "Rent";
|
||||
}
|
||||
|
||||
<div class="text-center">
|
||||
<h1 class="display-4">Welcome</h1>
|
||||
<p>Learn about <a href="https://docs.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p>
|
||||
<h2 class="display-4">Rent page</h2>
|
||||
</div>
|
||||
<form method="post">
|
||||
<div class="row">
|
||||
<div class="col-4">Car:</div>
|
||||
<div class="col-8">
|
||||
<select id="car" name="car" class="form-control" asp-items="@(new SelectList(@ViewBag.Cars,"ID","CarInfo"))"></select>
|
||||
</div>
|
||||
</div>
|
||||
<div class="row">
|
||||
<div class="col-4">Customer:</div>
|
||||
<div class="col-8">
|
||||
<select id="customer" name="ccustomerar" class="form-control" asp-items="@(new SelectList(@ViewBag.Clients,"ID","FullName"))"></select>
|
||||
</div>
|
||||
</div>
|
||||
<div class="row">
|
||||
<div class="col-4">ReturnDate:</div>
|
||||
<div class="col-8">
|
||||
<input type="date" id="returndate" name="returndate">
|
||||
</div>
|
||||
</div>
|
||||
<div class="row">
|
||||
<div class="col-4">Total:</div>
|
||||
<div class="col-8"><input type="text" id="sum" name="sum" readonly /></div>
|
||||
</div>
|
||||
<div class="row">
|
||||
<div class="col-8"></div>
|
||||
<div class="col-4"><input type="submit" value="Create" class="btn btn-primary" /></div>
|
||||
</div>
|
||||
</form>
|
||||
<script>
|
||||
$('#car').on('change', function ()
|
||||
{
|
||||
check();
|
||||
});
|
||||
$('#returndate').on('change', function () {
|
||||
check();
|
||||
});
|
||||
|
||||
function check()
|
||||
{
|
||||
console.info("I'M HERE");
|
||||
var car = $('#car').val();
|
||||
var date = $('#returndate').val();
|
||||
if (date && dress)
|
||||
{
|
||||
$.ajax({
|
||||
method: "POST",
|
||||
url: "/Home/Calc",
|
||||
data: { car: car, date: date },
|
||||
success: function (result)
|
||||
{
|
||||
$("#sum").val(result);
|
||||
}
|
||||
});
|
||||
};
|
||||
}
|
||||
</script>
|
Loading…
Reference in New Issue
Block a user