A couple of new logics have been implemented. Need to create views for complicated queries. :|

This commit is contained in:
Yuee Shiness 2023-05-05 00:54:07 +04:00
parent 9a35b1224b
commit 1ca752ecd9
9 changed files with 259 additions and 9 deletions

View File

@ -0,0 +1,88 @@
using Npgsql;
using RentalBusiness.Models.ViewModels;
namespace RentalBusiness.AuxilaryElements.BusinessLogic
{
public class BrandLogic
{
public List<BrandViewModel> _brand;
public BrandLogic()
{
_brand = new();
_brand = ReadBrands().Result;
}
public async Task<List<BrandViewModel>> ReadBrands()
{
NpgsqlConnection connection = new NpgsqlConnection("Host=localhost;Database=local;Username=postgres;Password=postgres");
List<BrandViewModel> brands = new List<BrandViewModel>();
connection.Open();
await using (NpgsqlCommand getRecordStorage = new NpgsqlCommand("SELECT brand_id, brand_name, brand_ratio FROM brand", connection))
{
await using (NpgsqlDataReader reader = await getRecordStorage.ExecuteReaderAsync())
{
while (await reader.ReadAsync())
{
BrandViewModel viewModel = ReadModelView(reader);
brands.Add(viewModel);
}
}
}
connection.Close();
return brands;
}
public BrandViewModel ReadModelView(NpgsqlDataReader reader)
{
int? id = reader["brand_id"] as int?;
string brandName = reader["brand_name"] as string;
int? ratio = reader["brand_ratio"] as int?;
BrandViewModel view = new BrandViewModel
{
ID = id.Value,
BrandName = new string(brandName)
};
return view;
}
public void PostBrand(BrandViewModel view)
{
NpgsqlConnection connection = new NpgsqlConnection("Host=localhost;Database=local;Username=postgres;Password=postgres");
connection.Open();
using var checkDuplicate = new NpgsqlCommand("SELECT brand_id FROM brand WHERE brand_name LIKE @bn")
{
Parameters =
{
new("bn", view.BrandName)
}
};
int? buff = Convert.ToInt32(checkDuplicate.ExecuteScalar());
if(buff == null)
{
return;
}
using var postBrand = new NpgsqlCommand("INSERT INTO brand (brand_name,brand_ratio) VALUES(@bs,@br)",connection)
{
Parameters =
{
new("bs",view.BrandName),
new("br",view.BrandRatio)
}
};
postBrand.ExecuteNonQueryAsync();
connection.Close();
}
}
}

View File

@ -0,0 +1,74 @@
using Npgsql;
using RentalBusiness.Enums;
using RentalBusiness.Models.ViewModels;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory.Database;
namespace RentalBusiness.AuxilaryElements.BusinessLogic
{
public class ModelLogic
{
public List<ModelViewModel> _model;
public ModelLogic()
{
_model = new();
_model = ReadModels().Result;
}
public async Task<List<ModelViewModel>> ReadModels()
{
NpgsqlConnection connection = new NpgsqlConnection("Host=localhost;Database=local;Username=postgres;Password=postgres");
List<ModelViewModel> models = new List<ModelViewModel>();
connection.Open();
await using (NpgsqlCommand getRecordStorage = new NpgsqlCommand("SELECT model_id, model_spec, model_ratio FROM model", connection))
{
await using (NpgsqlDataReader reader = await getRecordStorage.ExecuteReaderAsync())
{
while (await reader.ReadAsync())
{
ModelViewModel viewModel = ReadModelView(reader);
models.Add(viewModel);
}
}
}
connection.Close();
return models;
}
public ModelViewModel ReadModelView(NpgsqlDataReader reader)
{
int? id = reader["model_id"] as int?;
string modelSpec = reader["model_spec"] as string;
int? ratio = reader["model_ratio"] as int?;
ModelViewModel view = new ModelViewModel
{
ID = id.Value,
ModelSpec = new string(modelSpec),
ModelRatio = ratio.Value,
};
return view;
}
public void PostModel(ModelViewModel view)
{
NpgsqlConnection connection = new NpgsqlConnection("Host=localhost;Database=local;Username=postgres;Password=postgres");
connection.Open();
using var postModel = new NpgsqlCommand("INSERT INTO model (model_spec,model_ratio) VALUES(@ms,@mr)")
{
Parameters =
{
new("ms",view.ModelSpec),
new("mr",view.ModelRatio)
}
};
postModel.ExecuteNonQueryAsync();
connection.Close();
}
}
}

View File

@ -1,4 +1,5 @@
using Npgsql; using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Npgsql;
using RentalBusiness.Enums; using RentalBusiness.Enums;
using RentalBusiness.Models.ViewModels; using RentalBusiness.Models.ViewModels;
@ -8,7 +9,6 @@ namespace RentalBusiness.AuxilaryElements.BusinessLogic
{ {
public List<StorageViewModel> _storageFull; public List<StorageViewModel> _storageFull;
public List<StorageViewModel> _storageFree; public List<StorageViewModel> _storageFree;
public StorageLogic() public StorageLogic()
{ {
_storageFree = new List<StorageViewModel>(); _storageFree = new List<StorageViewModel>();
@ -77,5 +77,26 @@ namespace RentalBusiness.AuxilaryElements.BusinessLogic
}; };
return view; return view;
} }
public void PostCar(StorageViewModel model)
{
NpgsqlConnection connection = new NpgsqlConnection("Host=localhost;Database=local;Username=postgres;Password=postgres");
connection.Open();
using var postCar = new NpgsqlCommand("INSERT INTO storage(brand_id,model_id,car_ratio_per_hour,car_status) VALUES(@bid,@mid,@ratio,@cs)", connection)
{
Parameters =
{
new("bid",model.Brand_ID),
new("mid",model.Model_ID),
new("ratio",model.CarRatio),
new("cs",model.CarStatus),
}
};
postCar.ExecuteNonQueryAsync();
connection.Close();
}
} }
} }

View File

@ -12,12 +12,16 @@ namespace RentalBusiness.Controllers
private readonly StorageLogic _storageLogic; private readonly StorageLogic _storageLogic;
private readonly ClientLogic _clientLogic; private readonly ClientLogic _clientLogic;
private readonly ContractLogic _contractLogic; private readonly ContractLogic _contractLogic;
private readonly ModelLogic _modelLogic;
private readonly BrandLogic _brandLogic;
public HomeController(ILogger<HomeController> logger) public HomeController(ILogger<HomeController> logger)
{ {
_logger = logger; _logger = logger;
_storageLogic = new StorageLogic(); _storageLogic = new StorageLogic();
_clientLogic = new ClientLogic(); _clientLogic = new ClientLogic();
_contractLogic = new ContractLogic(); _contractLogic = new ContractLogic();
_modelLogic = new ModelLogic();
_brandLogic = new BrandLogic();
} }
[HttpGet] [HttpGet]
@ -77,10 +81,51 @@ namespace RentalBusiness.Controllers
[HttpGet] [HttpGet]
public IActionResult Car() public IActionResult Car()
{ {
ViewBag.Cars = _storageLogic._storageFull; ViewBag.Models = _modelLogic._model;
ViewBag.Brands = _brandLogic._brand;
return View(); return View();
} }
[HttpPost]
public void Car(string? brandname, int? brandratio, string? modelname, int? modelratio, int? brand, int? model )
{
if(!string.IsNullOrEmpty(brandname) && !string.IsNullOrEmpty(brandratio.ToString()))
{
BrandViewModel brandModel = new()
{
BrandName = brandname,
BrandRatio = brandratio
};
_brandLogic.PostBrand(brandModel);
}
if (!string.IsNullOrEmpty(modelname) && !string.IsNullOrEmpty(modelratio.ToString()))
{
ModelViewModel modelModel = new()
{
ModelSpec = modelname,
ModelRatio = brandratio
};
_modelLogic.PostModel(modelModel);
return;
}
if(brand != null && model != null)
{
StorageViewModel storageModel = new()
{
Brand_ID = brand,
Model_ID = model,
CarRatio = _modelLogic._model.First(x => x.ID == model).ModelRatio + _brandLogic._brand.First(x => x.ID == brand).BrandRatio
};
_storageLogic.PostCar(storageModel);
}
}
[HttpGet] [HttpGet]
public IActionResult Storage() public IActionResult Storage()
{ {

View File

@ -0,0 +1,9 @@
namespace RentalBusiness.Models.ViewModels
{
public class BrandViewModel
{
public int? ID { get; set; }
public string BrandName { get; set; } = string.Empty;
public int? BrandRatio { get; set; }
}
}

View File

@ -0,0 +1,10 @@
namespace RentalBusiness.Models.ViewModels
{
public class ModelViewModel
{
public int? ID { get; set; }
public string ModelSpec { get; set; } = string.Empty;
public int? ModelRatio { get; set; }
}
}

View File

@ -7,8 +7,11 @@ namespace RentalBusiness.Models.ViewModels
public int ID { get; set; } public int ID { get; set; }
public string CarInfo { get; set; } = string.Empty; public string CarInfo { get; set; } = string.Empty;
public int CarRatio { get; set; } public int? CarRatio { get; set; }
public CarStatus CarStatus { get; set; } = CarStatus.available; public CarStatus CarStatus { get; set; } = CarStatus.available;
public int? Brand_ID { get; set; }
public int? Model_ID { get; set; }
} }
} }

View File

@ -20,15 +20,15 @@
<div class="modeltitle" style="text-align: center; font-size: 25px;">ADD A MODEL</div> <div class="modeltitle" style="text-align: center; font-size: 25px;">ADD A MODEL</div>
<div class="modelname" style="text-align: left; font-size: 15px;">Model name:</div> <div class="modelname" style="text-align: left; font-size: 15px;">Model name:</div>
<input type="text" id="modelname" name="modelname" /> <input type="text" id="modelname" name="modelname" />
<div class="modeltratio" style="text-align: left; font-size: 15px;">Model ratio:</div> <div class="modelratio" style="text-align: left; font-size: 15px;">Model ratio:</div>
<input type="text" id="modeltratio" name="modeltratio" /> <input type="text" id="modelratio" name="modelratio" />
<input type="submit" id="modelbtn" value="Model" class="btn btn-primary" /> <input type="submit" id="modelbtn" value="Model" class="btn btn-primary" />
</div> </div>
<aside style="flex: 1"></aside> <aside style="flex: 1"></aside>
<div class="flex-containerB3" style="flex-direction: column; background-color:yellow;flex: 4; gap: 20px;display: flex; align-items: center; justify-content: center;"> <div class="flex-containerB3" style="flex-direction: column; background-color:yellow;flex: 4; gap: 20px;display: flex; align-items: center; justify-content: center;">
<div class="brandtitle" style="text-align: center; font-size: 25px;">ADD A CAR</div> <div class="brandtitle" style="text-align: center; font-size: 25px;">ADD A CAR</div>
<select></select> <select id="brand" name="brand" class="form-control" asp-items="@(new SelectList(@ViewBag.Brands,"ID","BrandName"))"></select>
<select></select> <select id="model" name="model" class="form-control" asp-items="@(new SelectList(@ViewBag.Models,"ID","ModelSpec"))"></select>
<input type="submit" id="carbtn" value="Car" class="btn btn-primary" /> <input type="submit" id="carbtn" value="Car" class="btn btn-primary" />
</div> </div>
<aside style="flex: 1"></aside> <aside style="flex: 1"></aside>

View File

@ -29,7 +29,7 @@
<a class="nav-link text-dark" asparea="" asp-controller="Home" asp-action="Index">ADD CONTRACT</a> <a class="nav-link text-dark" asparea="" asp-controller="Home" asp-action="Index">ADD CONTRACT</a>
</li> </li>
<li class="nav-item"> <li class="nav-item">
<a class="nav-link text-dark" asparea="" asp-controller="Home" asp-action="Car">ADD CAR</a> <a class="nav-link text-dark" asparea="" asp-controller="Home" asp-action="Car">ADD BRAND/MODEL/CAR</a>
</li> </li>
<li class="nav-item"> <li class="nav-item">
<a class="nav-link text-dark" asparea="" asp-controller="Home" asp-action="Customer">ADD CUSTOMER</a> <a class="nav-link text-dark" asparea="" asp-controller="Home" asp-action="Customer">ADD CUSTOMER</a>