Compare commits
4 Commits
4634c4ae98
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 762680537b | |||
| dbcd9f4872 | |||
| 18289006d1 | |||
| d90a6f970f |
2
Einheit_4/2025_5BHITN_Konstantin_Hintermayer_smarterrent/.gitignore
vendored
Normal file
2
Einheit_4/2025_5BHITN_Konstantin_Hintermayer_smarterrent/.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
bin/
|
||||
obj/
|
||||
@@ -1,4 +1,9 @@
|
||||
@inherits LayoutComponentBase
|
||||
@using Services
|
||||
@inherits LayoutComponentBase
|
||||
@implements IDisposable
|
||||
@rendermode RenderMode.InteractiveServer
|
||||
@inject IErrorService ErrorService
|
||||
|
||||
|
||||
<div class="page">
|
||||
<div class="sidebar">
|
||||
@@ -7,6 +12,13 @@
|
||||
|
||||
<main>
|
||||
<article class="content px-4">
|
||||
@if(ErrorService.ex != null) {
|
||||
<div class="alert alert-danger m-3" role="alert">
|
||||
@ErrorService.ex.Message
|
||||
<button type="button" class="btn-close float-end" aria-label="Close" @onclick="DismissError"></button>
|
||||
</div>
|
||||
}
|
||||
|
||||
@Body
|
||||
</article>
|
||||
</main>
|
||||
@@ -17,3 +29,20 @@
|
||||
<a href="." class="reload">Reload</a>
|
||||
<span class="dismiss">🗙</span>
|
||||
</div>
|
||||
|
||||
@code {
|
||||
protected override void OnInitialized()
|
||||
{
|
||||
ErrorService.OnChange += StateHasChanged;
|
||||
}
|
||||
|
||||
private void DismissError()
|
||||
{
|
||||
ErrorService.ex = null;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
ErrorService.OnChange -= StateHasChanged;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
@page "/ErrorTest"
|
||||
@using Microsoft.Data.SqlClient
|
||||
@using Services
|
||||
@inject IErrorService ErrorService
|
||||
|
||||
<PageTitle>Error Test</PageTitle>
|
||||
|
||||
<h1>Error Page</h1>
|
||||
|
||||
<button @onclick="SetError" class="btn btn-primary">Set Error</button>
|
||||
|
||||
@code {
|
||||
|
||||
private void SetError() {
|
||||
ErrorService.ex = new Exception("Hello World");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,7 +1,9 @@
|
||||
@page "/"
|
||||
@using Models
|
||||
@using Services
|
||||
@using System.Runtime.InteropServices
|
||||
@inject IRentService RentService
|
||||
@inject IErrorService ErrorService
|
||||
|
||||
<PageTitle>Home</PageTitle>
|
||||
|
||||
@@ -45,9 +47,11 @@
|
||||
|
||||
protected override void OnInitialized()
|
||||
{
|
||||
try {
|
||||
_vehicles = RentService.GetAvailableCars();
|
||||
Console.WriteLine("VC" + _vehicles.Count);
|
||||
@* return Task.CompletedTask; *@
|
||||
} catch (Exception e) {
|
||||
ErrorService.ex = e;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,33 +1,48 @@
|
||||
@page "/rent"
|
||||
@using Models
|
||||
@using Services
|
||||
@using System.Runtime.InteropServices
|
||||
@inject IRentService RentService
|
||||
@inject NavigationManager NavigationManager
|
||||
@inject IErrorService ErrorService
|
||||
|
||||
<PageTitle>Rent</PageTitle>
|
||||
|
||||
<h1>Rent</h1>
|
||||
|
||||
<form @onsubmit="RentCar">
|
||||
<InputSelect @bind-Value="_vehicleId">
|
||||
|
||||
<div class="form-group mb-3">
|
||||
<label for="vehicle">Vehicle: </label>
|
||||
<InputSelect @bind-Value="_vehicleId" id="vehicle" class="form-control" placeholder="Fahrzeug">
|
||||
@foreach (Vehicle v in _vehicles)
|
||||
{
|
||||
<option value=@v.Id>@v.Id - @v.Brand - @v.Model</option>
|
||||
}
|
||||
</InputSelect>
|
||||
</div>
|
||||
|
||||
<InputSelect @bind-Value="_customerId">
|
||||
<div class="form-group mb-3">
|
||||
<label for="customer">Kunde: </label>
|
||||
<InputSelect @bind-Value="_customerId" id="customer" class="form-control" placeholder="Kunde">
|
||||
@foreach (Customer c in _customers)
|
||||
{
|
||||
<option value=@c.Id>@c.Name</option>
|
||||
}
|
||||
</InputSelect>
|
||||
</div>
|
||||
|
||||
<InputDate @bind-Value="_from"/>
|
||||
<div class="form-group mb-3">
|
||||
<label for="from">Von: </label>
|
||||
<InputDate @bind-Value="_from" class="form-control" id="from" />
|
||||
</div>
|
||||
|
||||
<InputDate @bind-Value="_thru"/>
|
||||
<div class="form-group mb-3">
|
||||
<label for="thru">Bis: </label>
|
||||
<InputDate @bind-Value="_thru" class="form-control" id="thru" />
|
||||
</div>
|
||||
|
||||
<button type="submit">Submit</button>
|
||||
<button type="submit" class="btn btn-primary">Mieten!</button>
|
||||
</form>
|
||||
|
||||
@code {
|
||||
@@ -41,15 +56,22 @@
|
||||
|
||||
protected override void OnInitialized()
|
||||
{
|
||||
try {
|
||||
_vehicles = RentService.GetAvailableCars();
|
||||
_customers = RentService.GetCustomers();
|
||||
} catch (Exception e) {
|
||||
ErrorService.ex = e;
|
||||
}
|
||||
}
|
||||
|
||||
private Task RentCar()
|
||||
{
|
||||
try {
|
||||
RentService.RentCar(_customerId, _vehicleId, _from, _thru);
|
||||
Console.WriteLine("Rented Car");
|
||||
NavigationManager.NavigateTo("/");
|
||||
} catch (Exception e) {
|
||||
ErrorService.ex = e;
|
||||
}
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
@using Models
|
||||
@using Services
|
||||
@inject IRollbackService RollbackService
|
||||
@inject IErrorService ErrorService
|
||||
|
||||
<PageTitle>Rollback</PageTitle>
|
||||
|
||||
@@ -12,7 +13,11 @@
|
||||
|
||||
@code {
|
||||
private Task PerformRollback() {
|
||||
try {
|
||||
RollbackService.ResetToDefaults();
|
||||
} catch (Exception e) {
|
||||
ErrorService.ex = e;
|
||||
}
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@ public static class ServiceCollectionExtension
|
||||
|
||||
services.Services.AddScoped<IRentService, RentService>();
|
||||
services.Services.AddScoped<IRollbackService, RollbackService>();
|
||||
services.Services.AddScoped<IErrorService, ErrorService>();
|
||||
return services;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
using System.Data;
|
||||
using Configuration;
|
||||
using Microsoft.Data.SqlClient;
|
||||
using Models;
|
||||
|
||||
namespace Services
|
||||
{
|
||||
public interface IErrorService
|
||||
{
|
||||
Exception? ex { get; set; }
|
||||
event Action? OnChange;
|
||||
}
|
||||
|
||||
public class ErrorService : IErrorService
|
||||
{
|
||||
private Exception? _ex;
|
||||
public Exception? ex
|
||||
{
|
||||
get => _ex;
|
||||
set
|
||||
{
|
||||
_ex = value;
|
||||
NotifyStateChanged();
|
||||
}
|
||||
}
|
||||
|
||||
public event Action? OnChange;
|
||||
private void NotifyStateChanged() => OnChange?.Invoke();
|
||||
}
|
||||
}
|
||||
@@ -17,8 +17,7 @@ namespace Services
|
||||
public List<Vehicle> GetAvailableCars()
|
||||
{
|
||||
List<Vehicle> vehicles = new List<Vehicle>();
|
||||
try
|
||||
{
|
||||
|
||||
using SqlConnection conn = sqlConnectionFactory.CreateSqlConnection();
|
||||
string sql = "SELECT * FROM v_VerfuegbareFahrzeuge;";
|
||||
SqlCommand cmd = new SqlCommand(sql, conn);
|
||||
@@ -29,11 +28,6 @@ namespace Services
|
||||
{
|
||||
vehicles.Add(Vehicle.FromReader(reader));
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine(e);
|
||||
}
|
||||
|
||||
return vehicles;
|
||||
}
|
||||
@@ -41,8 +35,7 @@ namespace Services
|
||||
public List<Customer> GetCustomers()
|
||||
{
|
||||
List<Customer> customers = new List<Customer>();
|
||||
try
|
||||
{
|
||||
|
||||
using SqlConnection conn = sqlConnectionFactory.CreateSqlConnection();
|
||||
string sql = "SELECT * FROM Customer;";
|
||||
SqlCommand cmd = new SqlCommand(sql, conn);
|
||||
@@ -53,19 +46,13 @@ namespace Services
|
||||
{
|
||||
customers.Add(Customer.FromReader(reader));
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine(e);
|
||||
}
|
||||
|
||||
return customers;
|
||||
}
|
||||
|
||||
public void RentCar(long customerId, long vehicleId, DateTime start, DateTime end)
|
||||
{
|
||||
try
|
||||
{
|
||||
|
||||
using SqlConnection conn = sqlConnectionFactory.CreateSqlConnection();
|
||||
using SqlCommand cmd = new SqlCommand("sp_MieteFahrzeug", conn);
|
||||
cmd.CommandType = CommandType.StoredProcedure;
|
||||
@@ -76,10 +63,6 @@ namespace Services
|
||||
cmd.Parameters.AddWithValue("@Ende", end);
|
||||
|
||||
cmd.ExecuteNonQuery();
|
||||
}catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine(ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -13,8 +13,6 @@ namespace Services
|
||||
public class RollbackService(SqlConnectionFactory sqlConnectionFactory) : IRollbackService
|
||||
{
|
||||
public void ResetToDefaults()
|
||||
{
|
||||
try
|
||||
{
|
||||
using SqlConnection conn = sqlConnectionFactory.CreateSqlConnection();
|
||||
|
||||
@@ -28,10 +26,5 @@ namespace Services
|
||||
|
||||
cmd.ExecuteNonQuery();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine(ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user