Compare commits

..

4 Commits

Author SHA1 Message Date
762680537b New: Readme
All checks were successful
Build and Push Docker Image / build (push) Successful in 4m7s
2026-03-18 11:09:30 +01:00
dbcd9f4872 Update: Rental Form Styles
All checks were successful
Build and Push Docker Image / build (push) Successful in 2m10s
2026-03-18 10:49:04 +01:00
18289006d1 New: better Exception Handling
All checks were successful
Build and Push Docker Image / build (push) Successful in 7m0s
2026-03-18 09:47:58 +01:00
d90a6f970f New: gitignore 2026-03-18 09:46:56 +01:00
11 changed files with 173 additions and 83 deletions

View File

@@ -0,0 +1,2 @@
bin/
obj/

View File

@@ -1,4 +1,9 @@
@inherits LayoutComponentBase @using Services
@inherits LayoutComponentBase
@implements IDisposable
@rendermode RenderMode.InteractiveServer
@inject IErrorService ErrorService
<div class="page"> <div class="page">
<div class="sidebar"> <div class="sidebar">
@@ -7,6 +12,13 @@
<main> <main>
<article class="content px-4"> <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 @Body
</article> </article>
</main> </main>
@@ -17,3 +29,20 @@
<a href="." class="reload">Reload</a> <a href="." class="reload">Reload</a>
<span class="dismiss">🗙</span> <span class="dismiss">🗙</span>
</div> </div>
@code {
protected override void OnInitialized()
{
ErrorService.OnChange += StateHasChanged;
}
private void DismissError()
{
ErrorService.ex = null;
}
public void Dispose()
{
ErrorService.OnChange -= StateHasChanged;
}
}

View File

@@ -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");
}
}

View File

@@ -1,7 +1,9 @@
@page "/" @page "/"
@using Models @using Models
@using Services @using Services
@using System.Runtime.InteropServices
@inject IRentService RentService @inject IRentService RentService
@inject IErrorService ErrorService
<PageTitle>Home</PageTitle> <PageTitle>Home</PageTitle>
@@ -45,9 +47,11 @@
protected override void OnInitialized() protected override void OnInitialized()
{ {
try {
_vehicles = RentService.GetAvailableCars(); _vehicles = RentService.GetAvailableCars();
Console.WriteLine("VC" + _vehicles.Count); } catch (Exception e) {
@* return Task.CompletedTask; *@ ErrorService.ex = e;
}
} }
} }

View File

@@ -1,33 +1,48 @@
@page "/rent" @page "/rent"
@using Models @using Models
@using Services @using Services
@using System.Runtime.InteropServices
@inject IRentService RentService @inject IRentService RentService
@inject NavigationManager NavigationManager @inject NavigationManager NavigationManager
@inject IErrorService ErrorService
<PageTitle>Rent</PageTitle> <PageTitle>Rent</PageTitle>
<h1>Rent</h1> <h1>Rent</h1>
<form @onsubmit="RentCar"> <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) @foreach (Vehicle v in _vehicles)
{ {
<option value=@v.Id>@v.Id - @v.Brand - @v.Model</option> <option value=@v.Id>@v.Id - @v.Brand - @v.Model</option>
} }
</InputSelect> </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) @foreach (Customer c in _customers)
{ {
<option value=@c.Id>@c.Name</option> <option value=@c.Id>@c.Name</option>
} }
</InputSelect> </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> </form>
@code { @code {
@@ -41,15 +56,22 @@
protected override void OnInitialized() protected override void OnInitialized()
{ {
try {
_vehicles = RentService.GetAvailableCars(); _vehicles = RentService.GetAvailableCars();
_customers = RentService.GetCustomers(); _customers = RentService.GetCustomers();
} catch (Exception e) {
ErrorService.ex = e;
}
} }
private Task RentCar() private Task RentCar()
{ {
try {
RentService.RentCar(_customerId, _vehicleId, _from, _thru); RentService.RentCar(_customerId, _vehicleId, _from, _thru);
Console.WriteLine("Rented Car");
NavigationManager.NavigateTo("/"); NavigationManager.NavigateTo("/");
} catch (Exception e) {
ErrorService.ex = e;
}
return Task.CompletedTask; return Task.CompletedTask;
} }
} }

View File

@@ -2,6 +2,7 @@
@using Models @using Models
@using Services @using Services
@inject IRollbackService RollbackService @inject IRollbackService RollbackService
@inject IErrorService ErrorService
<PageTitle>Rollback</PageTitle> <PageTitle>Rollback</PageTitle>
@@ -12,7 +13,11 @@
@code { @code {
private Task PerformRollback() { private Task PerformRollback() {
try {
RollbackService.ResetToDefaults(); RollbackService.ResetToDefaults();
} catch (Exception e) {
ErrorService.ex = e;
}
return Task.CompletedTask; return Task.CompletedTask;
} }
} }

View File

@@ -17,6 +17,7 @@ public static class ServiceCollectionExtension
services.Services.AddScoped<IRentService, RentService>(); services.Services.AddScoped<IRentService, RentService>();
services.Services.AddScoped<IRollbackService, RollbackService>(); services.Services.AddScoped<IRollbackService, RollbackService>();
services.Services.AddScoped<IErrorService, ErrorService>();
return services; return services;
} }
} }

View File

@@ -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();
}
}

View File

@@ -17,8 +17,7 @@ namespace Services
public List<Vehicle> GetAvailableCars() public List<Vehicle> GetAvailableCars()
{ {
List<Vehicle> vehicles = new List<Vehicle>(); List<Vehicle> vehicles = new List<Vehicle>();
try
{
using SqlConnection conn = sqlConnectionFactory.CreateSqlConnection(); using SqlConnection conn = sqlConnectionFactory.CreateSqlConnection();
string sql = "SELECT * FROM v_VerfuegbareFahrzeuge;"; string sql = "SELECT * FROM v_VerfuegbareFahrzeuge;";
SqlCommand cmd = new SqlCommand(sql, conn); SqlCommand cmd = new SqlCommand(sql, conn);
@@ -29,11 +28,6 @@ namespace Services
{ {
vehicles.Add(Vehicle.FromReader(reader)); vehicles.Add(Vehicle.FromReader(reader));
} }
}
catch (Exception e)
{
Console.WriteLine(e);
}
return vehicles; return vehicles;
} }
@@ -41,8 +35,7 @@ namespace Services
public List<Customer> GetCustomers() public List<Customer> GetCustomers()
{ {
List<Customer> customers = new List<Customer>(); List<Customer> customers = new List<Customer>();
try
{
using SqlConnection conn = sqlConnectionFactory.CreateSqlConnection(); using SqlConnection conn = sqlConnectionFactory.CreateSqlConnection();
string sql = "SELECT * FROM Customer;"; string sql = "SELECT * FROM Customer;";
SqlCommand cmd = new SqlCommand(sql, conn); SqlCommand cmd = new SqlCommand(sql, conn);
@@ -53,19 +46,13 @@ namespace Services
{ {
customers.Add(Customer.FromReader(reader)); customers.Add(Customer.FromReader(reader));
} }
}
catch (Exception e)
{
Console.WriteLine(e);
}
return customers; return customers;
} }
public void RentCar(long customerId, long vehicleId, DateTime start, DateTime end) public void RentCar(long customerId, long vehicleId, DateTime start, DateTime end)
{ {
try
{
using SqlConnection conn = sqlConnectionFactory.CreateSqlConnection(); using SqlConnection conn = sqlConnectionFactory.CreateSqlConnection();
using SqlCommand cmd = new SqlCommand("sp_MieteFahrzeug", conn); using SqlCommand cmd = new SqlCommand("sp_MieteFahrzeug", conn);
cmd.CommandType = CommandType.StoredProcedure; cmd.CommandType = CommandType.StoredProcedure;
@@ -76,10 +63,6 @@ namespace Services
cmd.Parameters.AddWithValue("@Ende", end); cmd.Parameters.AddWithValue("@Ende", end);
cmd.ExecuteNonQuery(); cmd.ExecuteNonQuery();
}catch (Exception ex)
{
Console.WriteLine(ex);
}
} }
} }
} }

View File

@@ -13,8 +13,6 @@ namespace Services
public class RollbackService(SqlConnectionFactory sqlConnectionFactory) : IRollbackService public class RollbackService(SqlConnectionFactory sqlConnectionFactory) : IRollbackService
{ {
public void ResetToDefaults() public void ResetToDefaults()
{
try
{ {
using SqlConnection conn = sqlConnectionFactory.CreateSqlConnection(); using SqlConnection conn = sqlConnectionFactory.CreateSqlConnection();
@@ -28,10 +26,5 @@ namespace Services
cmd.ExecuteNonQuery(); cmd.ExecuteNonQuery();
} }
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
} }
} }

3
README.md Normal file
View File

@@ -0,0 +1,3 @@
# Smartrent
Deployed unter: https://sr.kocoder.xyz